From 5fbcd5fe0401e15112469437ad330a38679f8406 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 8 Feb 2012 09:01:51 -0500 Subject: [PATCH] Creating/Deleting SQLite tables works --- src/common/db_pdo.php | 4 +++- src/databases/sqlite.php | 15 +++++++++++++-- src/databases/sqlite_manip.php | 6 +++--- tests/databases/sqlite.php | 28 +++++++++++++++++++++++++++- tests/test_dbs/test_sqlite.db | Bin 2048 -> 4096 bytes 5 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/common/db_pdo.php b/src/common/db_pdo.php index bfc9423..2240a70 100644 --- a/src/common/db_pdo.php +++ b/src/common/db_pdo.php @@ -19,7 +19,9 @@ */ abstract class DB_PDO extends PDO { - protected $statement, $manip; + public $manip; + + protected $statement; function __construct($dsn, $username=NULL, $password=NULL, $driver_options=array()) { diff --git a/src/databases/sqlite.php b/src/databases/sqlite.php index 20daaf6..381b970 100644 --- a/src/databases/sqlite.php +++ b/src/databases/sqlite.php @@ -28,6 +28,9 @@ class SQLite extends DB_PDO { { // DSN is simply `sqlite:/path/to/db` parent::__construct("sqlite:{$dsn}"); + + $class = __CLASS__."_manip"; + $this->manip = new $class; } /** @@ -50,8 +53,16 @@ class SQLite extends DB_PDO { */ function get_tables() { - $res = $this->query("SELECT name FROM sqlite_master WHERE type='table'"); - return $res->fetchAll(PDO::FETCH_ASSOC); + $tables = array(); + $res = $this->query("SELECT \"name\", \"sql\" FROM sqlite_master WHERE type='table'"); + $result = $res->fetchAll(PDO::FETCH_ASSOC); + + foreach($result as $r) + { + $tables[$r['name']] = $r['sql']; + } + + return $tables; } /** diff --git a/src/databases/sqlite_manip.php b/src/databases/sqlite_manip.php index c45e471..3c0d198 100644 --- a/src/databases/sqlite_manip.php +++ b/src/databases/sqlite_manip.php @@ -57,9 +57,9 @@ class SQLite_manip extends db_manip { // Join column definitons together $columns = array(); - foreach($coumn_array as $name => $props) + foreach($column_array as $n => $props) { - $str = "{$name} "; + $str = "{$n} "; $str .= (isset($props['type'])) ? "{$props['type']}" : ""; $str .= (isset($props['constraint'])) ? "{$props['constraint']} " : ""; @@ -82,7 +82,7 @@ class SQLite_manip extends db_manip { */ function delete_table($name) { - return "DROP TABLE IF EXISTS {$table}"; + return "DROP TABLE IF EXISTS \"{$name}\""; } /** diff --git a/tests/databases/sqlite.php b/tests/databases/sqlite.php index 68c8f7c..cd2ee7b 100644 --- a/tests/databases/sqlite.php +++ b/tests/databases/sqlite.php @@ -40,6 +40,32 @@ class SQLiteTest extends UnitTestCase { function TestGetTables() { $tables = $this->db->get_tables(); - $this->assertEqual($tables[0]['name'], 'test'); + $this->assertTrue(isset($tables['test'])); + } + + function TestCreateTable() + { + //Attempt to create the table + $sql = $this->db->manip->create_table('create_test', array('id' => 'INTEGER PRIMARY KEY')); + $this->db->query($sql); + + //Check + $dbs = $this->db->get_tables(); + $this->assertEqual($dbs['create_test'], 'CREATE TABLE create_test (id INTEGER PRIMARY KEY)'); + } + + 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->manip->delete_table('create_test'); + $this->db->query($sql); + + //Check + $dbs = $this->db->get_tables(); + $this->assertTrue(empty($dbs['create_test'])); } } \ No newline at end of file diff --git a/tests/test_dbs/test_sqlite.db b/tests/test_dbs/test_sqlite.db index 1822bdd6bd7b682a5fdd1b28d016c9afe2f605f9..a8e52bdb18efb15a4d1e67d81f668b4438b3997d 100644 GIT binary patch delta 88 zcmZn=Xi%6SEy&Bjz`z2;%s|Wp#Jm%AjD>j_bRQ{!WEh!OGBB@X4q;Z?Sh$^uv3X*l lE@SA#!obZdnZsEo2C%a-0u`}r7G!zPyjg+e0rSKLRRE`162t%i delta 51 tcmZorXb_knEy&1#3K%Eq7*7mf<>CbjGcvDaU|zXdkj0vL^GfD$767jD2lW5|