Creating/Deleting SQLite tables works
This commit is contained in:
parent
c12f73afc8
commit
5fbcd5fe04
@ -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())
|
||||
{
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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}\"";
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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']));
|
||||
}
|
||||
}
|
Binary file not shown.
Reference in New Issue
Block a user