diff --git a/drivers/sqlite/sqlite_util.php b/drivers/sqlite/sqlite_util.php index 8765794..f471cfe 100644 --- a/drivers/sqlite/sqlite_util.php +++ b/drivers/sqlite/sqlite_util.php @@ -137,9 +137,12 @@ class SQLite_Util extends DB_Util { $obj_res = $res->fetchAll(PDO::FETCH_ASSOC); unset($res); + + // If the row is empty, continue; + if (empty($obj_res)) continue; // Nab the column names by getting the keys of the first row - $columns = array_keys($obj_res[0]); + $columns = array_keys(current($obj_res)); $insert_rows = array(); @@ -190,9 +193,9 @@ class SQLite_Util extends DB_Util { $sql_array[] = $r['sql']; } - $sql_structure = implode("\n\n", $sql_array); + $sql_structure = implode(";\n", $sql_array) . ";"; return $sql_structure; } } -// End of sqlite_util.php +// End of sqlite_util.php \ No newline at end of file diff --git a/tests/databases/sqlite/sqlite.php b/tests/databases/sqlite/sqlite.php index 360d63a..d6b53bc 100644 --- a/tests/databases/sqlite/sqlite.php +++ b/tests/databases/sqlite/sqlite.php @@ -39,6 +39,109 @@ class SQLiteTest extends UnitTestCase { unset($this->db); } + // -------------------------------------------------------------------------- + // ! Util Method tests + // -------------------------------------------------------------------------- + + public function TestCreateTable() + { + //Attempt to create the table + $sql = $this->db->util->create_table('create_test', + array( + 'id' => 'INTEGER', + 'key' => 'TEXT', + 'val' => 'TEXT', + ), + array( + 'id' => 'PRIMARY KEY' + ) + ); + $this->db->query($sql); + + //Attempt to create the table + $sql = $this->db->util->create_table('create_join', + array( + 'id' => 'INTEGER', + 'key' => 'TEXT', + 'val' => 'TEXT', + ), + array( + 'id' => 'PRIMARY KEY' + ) + ); + $this->db->query($sql); + + // A table to delete + $sql = $this->db->util->create_table('create_delete', + array( + 'id' => 'INTEGER', + 'key' => 'TEXT', + 'val' => 'TEXT', + ), + array( + 'id' => 'PRIMARY KEY' + ) + ); + $this->db->query($sql); + + //Check + $dbs = $this->db->get_tables(); + + $this->assertTrue(in_array('create_test', $dbs)); + } + + // -------------------------------------------------------------------------- + + public function TestBackupData() + { + $sql = mb_trim($this->db->util->backup_data()); + + $sql_array = explode("\n", $sql); + + $expected = <<assertEqual($expected_array, $sql_array); + } + + // -------------------------------------------------------------------------- + + public function TestBackupStructure() + { + $sql = mb_trim($this->db->util->backup_structure()); + + $expected = <<assertEqual($expected_array, $result_array); + } + + // -------------------------------------------------------------------------- + + public function TestDeleteTable() + { + $sql = $this->db->util->delete_table('create_delete'); + + $this->db->query($sql); + + //Check + $dbs = $this->db->get_tables(); + $this->assertFalse(in_array('create_delete', $dbs)); + } + + // -------------------------------------------------------------------------- + // ! General Tests // -------------------------------------------------------------------------- public function TestConnection() @@ -73,42 +176,6 @@ class SQLiteTest extends UnitTestCase { // -------------------------------------------------------------------------- - public function TestCreateTable() - { - //Attempt to create the table - $sql = $this->db->util->create_table('create_test', - array( - 'id' => 'INTEGER', - 'key' => 'TEXT', - 'val' => 'TEXT', - ), - array( - 'id' => 'PRIMARY KEY' - ) - ); - $this->db->query($sql); - - //Attempt to create the table - $sql = $this->db->util->create_table('create_join', - array( - 'id' => 'INTEGER', - 'key' => 'TEXT', - 'val' => 'TEXT', - ), - array( - 'id' => 'PRIMARY KEY' - ) - ); - $this->db->query($sql); - - //Check - $dbs = $this->db->get_tables(); - - $this->assertTrue(in_array('create_test', $dbs)); - } - - // -------------------------------------------------------------------------- - public function TestTruncate() { $this->db->truncate('create_test'); @@ -171,24 +238,6 @@ SQL; // -------------------------------------------------------------------------- - // This is really time intensive ! Run only when needed - /*public function TestDeleteTable() - { - //Make sure the table exists to delete - $dbs = $this->db->get_tables(); - $this->assertTrue(isset($dbs['create_test'])); - - //Attempt to delete the table - $sql = $this->db->sql->delete_table('create_test'); - $this->db->query($sql); - - //Check - $dbs = $this->db->get_tables(); - $this->assertFalse(in_array('create_test', $dbs)); - }*/ - - // -------------------------------------------------------------------------- - public function TestGetDBs() { $this->assertFalse($this->db->get_dbs()); @@ -201,6 +250,8 @@ SQL; $this->assertFalse($this->db->get_schemas()); } + // -------------------------------------------------------------------------- + // ! SQL Tests // -------------------------------------------------------------------------- public function TestNullMethods()