From 5e73938733a5e284af98cc41e97d8db9cedb3d2a Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 14 Mar 2012 15:46:54 -0400 Subject: [PATCH] A few more query builder tests --- sys/db/query_builder.php | 24 +++++++++++++----------- tests/databases/firebird-qb.php | 9 +++++++++ tests/databases/firebird.php | 4 +++- tests/databases/sqlite-qb.php | 9 +++++++++ tests/databases/sqlite.php | 13 +++++++++++++ tests/test_dbs/FB_TEST_DB.FDB | Bin 802816 -> 802816 bytes 6 files changed, 47 insertions(+), 12 deletions(-) diff --git a/sys/db/query_builder.php b/sys/db/query_builder.php index 470a856..1d78881 100644 --- a/sys/db/query_builder.php +++ b/sys/db/query_builder.php @@ -578,16 +578,15 @@ class Query_Builder { * @return $this */ 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(); - - if (preg_match('/([\[\w\.]+)([\W\s]+)(.+)/', $condition, $matches)) - { - $condition = $this->db->quote_ident($matches[0]) . ' ' . $matches[1] . - ' ' . $this->db->quote_ident($matches[2]); - } + $condition = $table . ' ON ' . $this->db->quote_ident($cond_array[0]) . $cond_array[1] . + ' ' . $this->db->quote_ident($cond_array[2]); $this->query_map[] = array( 'type' => 'join', @@ -947,6 +946,9 @@ class Query_Builder { { unset($this->$name); } + + // Set values as an empty array + $this->values = array(); } } @@ -959,7 +961,7 @@ class Query_Builder { * @param string $table * @return $string */ - private function _compile($type, $table="") + private function _compile($type='', $table="") { $sql = ''; @@ -1039,7 +1041,7 @@ class Query_Builder { break; } - // echo $sql.'
'; + echo $sql.'
'; return $sql; } diff --git a/tests/databases/firebird-qb.php b/tests/databases/firebird-qb.php index 28aa318..bce231a 100644 --- a/tests/databases/firebird-qb.php +++ b/tests/databases/firebird-qb.php @@ -178,6 +178,15 @@ class FirebirdQBTest extends UnitTestCase { $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() { $query = $this->db->set('id', 4) diff --git a/tests/databases/firebird.php b/tests/databases/firebird.php index b715ac0..80aa806 100644 --- a/tests/databases/firebird.php +++ b/tests/databases/firebird.php @@ -83,7 +83,7 @@ class FirebirdTest extends UnitTestCase { /*function TestCreateTable() { //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', 'key' => 'VARCHAR(64)', 'val' => 'BLOB SUB_TYPE TEXT' @@ -103,6 +103,8 @@ class FirebirdTest extends UnitTestCase { $this->assertTrue($table_exists); }*/ + + function TestTruncate() { $this->db->truncate('create_test'); diff --git a/tests/databases/sqlite-qb.php b/tests/databases/sqlite-qb.php index 4f148c6..5798244 100644 --- a/tests/databases/sqlite-qb.php +++ b/tests/databases/sqlite-qb.php @@ -164,6 +164,15 @@ $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() { $query = $this->db->set('id', 4) diff --git a/tests/databases/sqlite.php b/tests/databases/sqlite.php index 6ce21ad..6f090d6 100644 --- a/tests/databases/sqlite.php +++ b/tests/databases/sqlite.php @@ -73,6 +73,19 @@ class SQLiteTest extends UnitTestCase { ) ); $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 $dbs = $this->db->get_tables(); diff --git a/tests/test_dbs/FB_TEST_DB.FDB b/tests/test_dbs/FB_TEST_DB.FDB index 58ec9fab6c1913f0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