From 1178b5704ddb9ae4a1a52f14d344a0c6c0ec1cca Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 15 Mar 2012 16:12:20 -0400 Subject: [PATCH] Fix mysql create_table method --- drivers/mysql.php | 13 ++++++-- drivers/mysql_sql.php | 55 +++++++++++++++++++++++++++++++++- tests/databases/mysql.php | 12 +++++--- tests/test_dbs/FB_TEST_DB.FDB | Bin 802816 -> 802816 bytes tests/test_dbs/test_sqlite.db | Bin 3072 -> 3072 bytes 5 files changed, 73 insertions(+), 7 deletions(-) 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 01e68d6494b00a8b1b957a1b027e6e320a36a6f8..b532551301483beac7883bf502e7c4fd54b0d5a5 100755 GIT binary patch delta 2148 zcmb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x|)CzTrjXF!wnqHX7{}<-FchK@2*&TEyi>wC?U)({9NQ3p#&w4RF z1uVu#p9C2t$S8R$`(2fbyy%_&Hv!ZC+AG-wfQ%Akl)UgJd9IU`1Cq@9B*-X1M#&?c zLgDZEV;kn?NOd1s~+Xo wjqLUat+MvYlIX8cvi8b)s4J8#z_KX16^dBm{Tl1qp(=^f2X49I>~D7V4=tZregFUf diff --git a/tests/test_dbs/test_sqlite.db b/tests/test_dbs/test_sqlite.db index 3490d13eb312e8d722dd73a5cc51e3ec6e81b08d..ebbe0151818aa621ed0837746782a5701169c7ef 100644 GIT binary patch delta 19 ZcmZpWXpop7#l*-nQN{^KZcJFf4FE0v1f&1} delta 19 ZcmZpWXpop7#l%oFQN{^KZcJFf4FEJu1#SQU