Fix mysql create_table method

This commit is contained in:
Timothy Warren 2012-03-15 16:12:20 -04:00
parent 0567c3443f
commit 1178b5704d
5 changed files with 73 additions and 7 deletions

View File

@ -57,7 +57,7 @@ class MySQL extends DB_PDO {
public function get_dbs() public function get_dbs()
{ {
$res = $this->query("SHOW DATABASES"); $res = $this->query("SHOW DATABASES");
return $this->fetchAll(PDO::FETCH_ASSOC); return array_values($this->fetchAll(PDO::FETCH_ASSOC));
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -70,7 +70,16 @@ class MySQL extends DB_PDO {
public function get_tables() public function get_tables()
{ {
$res = $this->query("SHOW TABLES"); $res = $this->query("SHOW TABLES");
return $res->fetchAll(PDO::FETCH_ASSOC);
$tables = array();
$rows = $res->fetchAll(PDO::FETCH_NUM);
foreach($rows as $r)
{
$tables[] = $r[0];
}
return $tables;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -29,7 +29,60 @@
*/ */
public function create_table($name, $columns, array $constraints=array(), array $indexes=array()) public function create_table($name, $columns, array $constraints=array(), array $indexes=array())
{ {
//TODO: implement $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;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -21,7 +21,7 @@ class MySQLTest extends UnitTestCase {
function __construct() function __construct()
{ {
parent::__construct();
} }
function setUp() function setUp()
@ -58,6 +58,7 @@ class MySQLTest extends UnitTestCase {
if (empty($this->db)) return; if (empty($this->db)) return;
$tables = $this->db->get_tables(); $tables = $this->db->get_tables();
$this->assertTrue(is_array($tables)); $this->assertTrue(is_array($tables));
} }
@ -85,7 +86,7 @@ class MySQLTest extends UnitTestCase {
//Attempt to create the table //Attempt to create the table
$sql = $this->db->sql->create_table('create_test', $sql = $this->db->sql->create_table('create_test',
array( array(
'id' => 'INTEGER', 'id' => 'int(10)',
'key' => 'TEXT', 'key' => 'TEXT',
'val' => 'TEXT', 'val' => 'TEXT',
), ),
@ -93,12 +94,13 @@ class MySQLTest extends UnitTestCase {
'id' => 'PRIMARY KEY' 'id' => 'PRIMARY KEY'
) )
); );
$this->db->query($sql); $this->db->query($sql);
//Attempt to create the table //Attempt to create the table
$sql = $this->db->sql->create_table('create_join', $sql = $this->db->sql->create_table('create_join',
array( array(
'id' => 'INTEGER', 'id' => 'int(10)',
'key' => 'TEXT', 'key' => 'TEXT',
'val' => 'TEXT', 'val' => 'TEXT',
), ),
@ -110,7 +112,9 @@ class MySQLTest extends UnitTestCase {
//Check //Check
$dbs = $this->db->get_tables(); $dbs = $this->db->get_tables();
//$this->assertEqual($dbs['create_test'], 'CREATE TABLE "create_test" (id INTEGER PRIMARY KEY, key TEXT , val TEXT )');
$this->assertTrue(in_array('create_test', $dbs));
} }
/*function TestTruncate() /*function TestTruncate()

Binary file not shown.

Binary file not shown.