A few more query builder tests

This commit is contained in:
Timothy Warren 2012-03-14 15:46:54 -04:00
parent 09caf1d256
commit 5e73938733
6 changed files with 47 additions and 12 deletions

View File

@ -579,15 +579,14 @@ class Query_Builder {
*/ */
public function join($table, $condition, $type='') public function join($table, $condition, $type='')
{ {
$table = $this->db->quote_ident($table); // Paste it back together
$table = implode(" ", array_map(array($this->db, 'quote_ident'), explode(' ', trim($table))));
//$condition = preg_replace('`(\W)`', " $1 ", $condition);
$cond_array = explode(' ', trim($condition));
$cond_array = array_map('trim', $cond_array);
$matches = array(); $condition = $table . ' ON ' . $this->db->quote_ident($cond_array[0]) . $cond_array[1] .
' ' . $this->db->quote_ident($cond_array[2]);
if (preg_match('/([\[\w\.]+)([\W\s]+)(.+)/', $condition, $matches))
{
$condition = $this->db->quote_ident($matches[0]) . ' ' . $matches[1] .
' ' . $this->db->quote_ident($matches[2]);
}
$this->query_map[] = array( $this->query_map[] = array(
'type' => 'join', 'type' => 'join',
@ -947,6 +946,9 @@ class Query_Builder {
{ {
unset($this->$name); unset($this->$name);
} }
// Set values as an empty array
$this->values = array();
} }
} }
@ -959,7 +961,7 @@ class Query_Builder {
* @param string $table * @param string $table
* @return $string * @return $string
*/ */
private function _compile($type, $table="") private function _compile($type='', $table="")
{ {
$sql = ''; $sql = '';
@ -1039,7 +1041,7 @@ class Query_Builder {
break; break;
} }
// echo $sql.'<br />'; echo $sql.'<br />';
return $sql; return $sql;
} }

View File

@ -178,6 +178,15 @@ class FirebirdQBTest extends UnitTestCase {
$this->assertTrue(is_resource($query)); $this->assertTrue(is_resource($query));
} }
function TestJoin()
{
$query = $this->db->from('create_test')
->join('create_join cj', 'cj.id = create_test.id')
->get();
$this->assertTrue(is_resource($query));
}
function TestInsert() function TestInsert()
{ {
$query = $this->db->set('id', 4) $query = $this->db->set('id', 4)

View File

@ -83,7 +83,7 @@ class FirebirdTest extends UnitTestCase {
/*function TestCreateTable() /*function TestCreateTable()
{ {
//Attempt to create the table //Attempt to create the table
$sql = $this->db->sql->create_table('create_test', array( $sql = $this->db->sql->create_table('create_join', array(
'id' => 'SMALLINT', 'id' => 'SMALLINT',
'key' => 'VARCHAR(64)', 'key' => 'VARCHAR(64)',
'val' => 'BLOB SUB_TYPE TEXT' 'val' => 'BLOB SUB_TYPE TEXT'
@ -103,6 +103,8 @@ class FirebirdTest extends UnitTestCase {
$this->assertTrue($table_exists); $this->assertTrue($table_exists);
}*/ }*/
function TestTruncate() function TestTruncate()
{ {
$this->db->truncate('create_test'); $this->db->truncate('create_test');

View File

@ -164,6 +164,15 @@
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
function TestJoin()
{
$query = $this->db->from('create_test')
->join('create_join cj', 'cj.id = create_test.id')
->get();
$this->assertIsA($query, 'PDOStatement');
}
function TestInsert() function TestInsert()
{ {
$query = $this->db->set('id', 4) $query = $this->db->set('id', 4)

View File

@ -74,6 +74,19 @@ class SQLiteTest extends UnitTestCase {
); );
$this->db->query($sql); $this->db->query($sql);
//Attempt to create the table
$sql = $this->db->sql->create_table('create_join',
array(
'id' => 'INTEGER',
'key' => 'TEXT',
'val' => 'TEXT',
),
array(
'id' => 'PRIMARY KEY'
)
);
$this->db->query($sql);
//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->assertEqual($dbs['create_test'], 'CREATE TABLE "create_test" (id INTEGER PRIMARY KEY, key TEXT , val TEXT )');

Binary file not shown.