diff --git a/drivers/mysql.php b/drivers/mysql.php index b35c5ef..16e03f1 100644 --- a/drivers/mysql.php +++ b/drivers/mysql.php @@ -57,7 +57,7 @@ class MySQL extends DB_PDO { public function get_dbs() { $res = $this->query("SHOW DATABASES"); - return $this->fetchAll(PDO::FETCH_ASSOC); + return array_values($this->fetchAll(PDO::FETCH_ASSOC)); } // -------------------------------------------------------------------------- @@ -70,7 +70,16 @@ class MySQL extends DB_PDO { public function get_tables() { $res = $this->query("SHOW TABLES"); - return $res->fetchAll(PDO::FETCH_ASSOC); + + $tables = array(); + $rows = $res->fetchAll(PDO::FETCH_NUM); + + foreach($rows as $r) + { + $tables[] = $r[0]; + } + + return $tables; } // -------------------------------------------------------------------------- diff --git a/drivers/mysql_sql.php b/drivers/mysql_sql.php index 1eeec9c..caa03e4 100644 --- a/drivers/mysql_sql.php +++ b/drivers/mysql_sql.php @@ -29,7 +29,60 @@ */ public function create_table($name, $columns, array $constraints=array(), array $indexes=array()) { - //TODO: implement + $column_array = array(); + + // Reorganize into an array indexed with column information + // Eg $column_array[$colname] = array( + // 'type' => ..., + // 'constraint' => ..., + // 'index' => ..., + // ) + foreach($columns as $colname => $type) + { + if(is_numeric($colname)) + { + $colname = $type; + } + + $column_array[$colname] = array(); + $column_array[$colname]['type'] = ($type !== $colname) ? $type : ''; + } + + if( ! empty($constraints)) + { + foreach($constraints as $col => $const) + { + $column_array[$col]['constraint'] = "{$const} ({$col})"; + } + } + + // Join column definitons together + $columns = array(); + foreach($column_array as $n => $props) + { + $n = trim($n, '`'); + + $str = "`{$n}` "; + $str .= (isset($props['type'])) ? "{$props['type']} " : ""; + + $columns[] = $str; + } + + // Add constraints + foreach($column_array as $n => $props) + { + if (isset($props['constraint'])) + { + $columns[] = $props['constraint']; + } + } + + // Generate the sql for the creation of the table + $sql = "CREATE TABLE IF NOT EXISTS `{$name}` ("; + $sql .= implode(", ", $columns); + $sql .= ")"; + + return $sql; } // -------------------------------------------------------------------------- diff --git a/tests/databases/mysql.php b/tests/databases/mysql.php index e1f1611..01d7643 100644 --- a/tests/databases/mysql.php +++ b/tests/databases/mysql.php @@ -21,7 +21,7 @@ class MySQLTest extends UnitTestCase { function __construct() { - + parent::__construct(); } function setUp() @@ -58,6 +58,7 @@ class MySQLTest extends UnitTestCase { if (empty($this->db)) return; $tables = $this->db->get_tables(); + $this->assertTrue(is_array($tables)); } @@ -85,7 +86,7 @@ class MySQLTest extends UnitTestCase { //Attempt to create the table $sql = $this->db->sql->create_table('create_test', array( - 'id' => 'INTEGER', + 'id' => 'int(10)', 'key' => 'TEXT', 'val' => 'TEXT', ), @@ -93,12 +94,13 @@ class MySQLTest extends UnitTestCase { 'id' => 'PRIMARY KEY' ) ); + $this->db->query($sql); //Attempt to create the table $sql = $this->db->sql->create_table('create_join', array( - 'id' => 'INTEGER', + 'id' => 'int(10)', 'key' => 'TEXT', 'val' => 'TEXT', ), @@ -110,7 +112,9 @@ class MySQLTest extends UnitTestCase { //Check $dbs = $this->db->get_tables(); - //$this->assertEqual($dbs['create_test'], 'CREATE TABLE "create_test" (id INTEGER PRIMARY KEY, key TEXT , val TEXT )'); + + $this->assertTrue(in_array('create_test', $dbs)); + } /*function TestTruncate() diff --git a/tests/test_dbs/FB_TEST_DB.FDB b/tests/test_dbs/FB_TEST_DB.FDB index 01e68d6..b532551 100755 Binary files a/tests/test_dbs/FB_TEST_DB.FDB and b/tests/test_dbs/FB_TEST_DB.FDB differ diff --git a/tests/test_dbs/test_sqlite.db b/tests/test_dbs/test_sqlite.db index 3490d13..ebbe015 100644 Binary files a/tests/test_dbs/test_sqlite.db and b/tests/test_dbs/test_sqlite.db differ