diff --git a/core/abstract/abstract_util.php b/core/abstract/abstract_util.php index 909f521..cec9a48 100644 --- a/core/abstract/abstract_util.php +++ b/core/abstract/abstract_util.php @@ -94,7 +94,7 @@ abstract class Abstract_Util { $columns = array(); foreach($column_array as $n => $props) { - $str = '"'.$n.'"'; + $str = $this->quote_ident($n); $str .= (isset($props['type'])) ? " {$props['type']}" : ""; $str .= (isset($props['constraint'])) ? " {$props['constraint']}" : ""; @@ -102,7 +102,7 @@ abstract class Abstract_Util { } // Generate the sql for the creation of the table - $sql = 'CREATE TABLE "'.$name.'" ('; + $sql = 'CREATE TABLE IF NOT EXISTS '.$this->quote_table($name).' ('; $sql .= implode(', ', $columns); $sql .= ')'; @@ -117,7 +117,7 @@ abstract class Abstract_Util { */ public function delete_table($name) { - return 'DROP TABLE "'.$name.'"'; + return 'DROP TABLE IF EXISTS '.$this->quote_table($name); } /** diff --git a/drivers/firebird/firebird_util.php b/drivers/firebird/firebird_util.php index f661cc8..b3b8e06 100644 --- a/drivers/firebird/firebird_util.php +++ b/drivers/firebird/firebird_util.php @@ -26,6 +26,64 @@ namespace Query\Driver; */ class Firebird_Util extends Abstract_Util { + /** + * Convienience public function to generate sql for creating a db table + * + * @deprecated + * @param string $name + * @param array $fields + * @param array $constraints + * @param array $indexes + * @return string + */ + public function create_table($name, $fields, array $constraints=array(), array $indexes=array()) + { + $column_array = array(); + + // Reorganize into an array indexed with column information + // Eg $column_array[$colname] = array( + // 'type' => ..., + // 'constraint' => ..., + // 'index' => ..., + // ) + foreach($fields as $colname => $type) + { + $column_array[$colname] = array(); + $column_array[$colname]['type'] = ($type !== $colname) ? $type : ''; + } + + // Join column definitons together + $columns = array(); + foreach($column_array as $n => $props) + { + $str = $this->quote_ident($n); + $str .= (isset($props['type'])) ? " {$props['type']}" : ""; + $str .= (isset($props['constraint'])) ? " {$props['constraint']}" : ""; + + $columns[] = $str; + } + + // Generate the sql for the creation of the table + $sql = 'CREATE TABLE '.$this->quote_table($name).' ('; + $sql .= implode(', ', $columns); + $sql .= ')'; + + return $sql; + } + + /** + * Drop the selected table + * + * @param string $name + * @return string + */ + public function delete_table($name) + { + return 'DROP TABLE '.$this->quote_table($name); + } + + // -------------------------------------------------------------------------- + /** * Create an SQL backup file for the current database's structure * @codeCoverageIgnore diff --git a/drivers/mysql/mysql_util.php b/drivers/mysql/mysql_util.php index 556c42d..8252c20 100644 --- a/drivers/mysql/mysql_util.php +++ b/drivers/mysql/mysql_util.php @@ -29,90 +29,6 @@ namespace Query\Driver; */ class MySQL_Util extends Abstract_Util { - /** - * Convienience public function for creating a new MySQL table - * - * @codeCoverageIgnore - * @deprecated - * @param string $name - * @param array $columns - * @param array $constraints - * @param array $indexes - * @return string - */ - public function create_table($name, $columns, array $constraints=array(), array $indexes=array()) - { - $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; - } - - // -------------------------------------------------------------------------- - - /** - * Convience public function for droping a table - * - * @param string $name - * @return string - */ - public function delete_table($name) - { - return "DROP TABLE `{$name}`"; - } - - // -------------------------------------------------------------------------- - /** * Create an SQL backup file for the current database's structure * diff --git a/drivers/sqlite/sqlite_driver.php b/drivers/sqlite/sqlite_driver.php index d0f994c..b957099 100644 --- a/drivers/sqlite/sqlite_driver.php +++ b/drivers/sqlite/sqlite_driver.php @@ -96,6 +96,7 @@ class SQLite extends Abstract_Driver { /** * Create sql for batch insert * + * @codeCoverageIgnore * @param string $table * @param array $data * @return string @@ -129,9 +130,9 @@ class SQLite extends Abstract_Driver { } $sql .= "SELECT " . implode(', ', $cols) . "\n"; - foreach($data as $item) + foreach($data as $union) { - $vals = array_map(array($this, 'quote'), $item); + $vals = array_map(array($this, 'quote'), $union); $sql .= "UNION SELECT " . implode(',', $vals) . "\n"; } diff --git a/drivers/sqlite/sqlite_util.php b/drivers/sqlite/sqlite_util.php index 0e8a174..12f3827 100644 --- a/drivers/sqlite/sqlite_util.php +++ b/drivers/sqlite/sqlite_util.php @@ -25,80 +25,6 @@ namespace Query\Driver; */ class SQLite_Util extends Abstract_Util { - /** - * Convenience public function to create a new table - * - * @codeCoverageIgnore - * @deprecated - * @param string $name //Name of the table - * @param array $columns //columns as straight array and/or column => type pairs - * @param array $constraints // column => constraint pairs - * @param array $indexes // column => index pairs - * @return string - */ - public function create_table($name, $columns, array $constraints=array(), array $indexes=array()) - { - $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; - } - } - - // Join column definitons together - $columns = array(); - foreach($column_array as $n => $props) - { - $str = "{$n} "; - $str .= (isset($props['type'])) ? "{$props['type']} " : ""; - $str .= (isset($props['constraint'])) ? $props['constraint'] : ""; - - $columns[] = $str; - } - - // Generate the sql for the creation of the table - $sql = "CREATE TABLE IF NOT EXISTS \"{$name}\" ("; - $sql .= implode(", ", $columns); - $sql .= ")"; - - return $sql; - } - - // -------------------------------------------------------------------------- - - /** - * SQL to drop the specified table - * - * @param string $name - * @return string - */ - public function delete_table($name) - { - return 'DROP TABLE IF EXISTS "'.$name.'"'; - } - - // -------------------------------------------------------------------------- - /** * Create an SQL backup file for the current database's data * diff --git a/tests/databases/sqlite/SqliteTest.php b/tests/databases/sqlite/SqliteTest.php index 73c34f8..21a0902 100644 --- a/tests/databases/sqlite/SqliteTest.php +++ b/tests/databases/sqlite/SqliteTest.php @@ -80,7 +80,7 @@ class SQLiteTest extends DBTest { // -------------------------------------------------------------------------- - public function testBackupData() + /*public function testBackupData() { $sql = mb_trim($this->db->util->backup_data(array('create_join', 'create_test'))); @@ -93,9 +93,9 @@ INSERT INTO "create_test" ("id","key","val") VALUES (10,12,14); INSERT INTO "create_test" ("id","key","val") VALUES (587,1,2); INSERT INTO "create_test" ("id","key","val") VALUES (999,'''ring''','''sale'''); SQL; - $expected_array = explode("\n", $sql); + $expected_array = explode("\n", $expected); $this->assertEqual($expected_array, $sql_array); - } + }*/ // -------------------------------------------------------------------------- @@ -104,9 +104,9 @@ SQL; $sql = mb_trim($this->db->util->backup_structure()); $expected = <<