Creating/Deleting SQLite tables works

This commit is contained in:
Timothy Warren 2012-02-08 09:01:51 -05:00
parent c12f73afc8
commit 5fbcd5fe04
5 changed files with 46 additions and 7 deletions

View File

@ -19,7 +19,9 @@
*/ */
abstract class DB_PDO extends PDO { abstract class DB_PDO extends PDO {
protected $statement, $manip; public $manip;
protected $statement;
function __construct($dsn, $username=NULL, $password=NULL, $driver_options=array()) function __construct($dsn, $username=NULL, $password=NULL, $driver_options=array())
{ {

View File

@ -28,6 +28,9 @@ class SQLite extends DB_PDO {
{ {
// DSN is simply `sqlite:/path/to/db` // DSN is simply `sqlite:/path/to/db`
parent::__construct("sqlite:{$dsn}"); parent::__construct("sqlite:{$dsn}");
$class = __CLASS__."_manip";
$this->manip = new $class;
} }
/** /**
@ -50,8 +53,16 @@ class SQLite extends DB_PDO {
*/ */
function get_tables() function get_tables()
{ {
$res = $this->query("SELECT name FROM sqlite_master WHERE type='table'"); $tables = array();
return $res->fetchAll(PDO::FETCH_ASSOC); $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;
} }
/** /**

View File

@ -57,9 +57,9 @@ class SQLite_manip extends db_manip {
// Join column definitons together // Join column definitons together
$columns = array(); $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['type'])) ? "{$props['type']}" : "";
$str .= (isset($props['constraint'])) ? "{$props['constraint']} " : ""; $str .= (isset($props['constraint'])) ? "{$props['constraint']} " : "";
@ -82,7 +82,7 @@ class SQLite_manip extends db_manip {
*/ */
function delete_table($name) function delete_table($name)
{ {
return "DROP TABLE IF EXISTS {$table}"; return "DROP TABLE IF EXISTS \"{$name}\"";
} }
/** /**

View File

@ -40,6 +40,32 @@ class SQLiteTest extends UnitTestCase {
function TestGetTables() function TestGetTables()
{ {
$tables = $this->db->get_tables(); $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']));
} }
} }

Binary file not shown.