diff --git a/sys/db/query_builder.php b/sys/db/query_builder.php index f8435f6..ad45499 100644 --- a/sys/db/query_builder.php +++ b/sys/db/query_builder.php @@ -99,16 +99,23 @@ class Query_Builder { $sql = $this->sql->limit($sql, $limit, $offset); } - echo $sql."
"; + // echo $sql."
"; // Do prepared statements for anything involving a "where" clause if ( ! empty($this->where_string)) { - return $this->db->prepare_execute($sql, array_values($this->where_array)); + $result = $this->db->prepare_execute($sql, array_values($this->where_array)); + } + else + { + // Otherwise, a simple query will do. + $result = $this->db->query($sql); } - // Otherwise, a simple query will do. - return $this->db->query($sql); + // Reset for next query + $this->_reset(); + + return $result; } // -------------------------------------------------------------------------- @@ -248,6 +255,19 @@ class Query_Builder { // -------------------------------------------------------------------------- + /** + * Clear out the class variables, so the next query can be run + */ + private function _reset() + { + unset($this->table); + unset($this->where_array); + unset($this->where_string); + unset($this->select_string); + } + + // -------------------------------------------------------------------------- + /** * String together the sql statements for sending to the db * diff --git a/tests/databases/firebird.php b/tests/databases/firebird.php index 1fcfe4f..090a0c8 100644 --- a/tests/databases/firebird.php +++ b/tests/databases/firebird.php @@ -70,7 +70,9 @@ class FirebirdTest extends UnitTestCase { { $only_system = TRUE; - foreach($this->tables as $t) + $tables = $this->db->get_system_tables(); + + foreach($tables as $t) { if(stripos($t, 'rdb$') !== 0 && stripos($t, 'mon$') !== 0) { @@ -88,7 +90,7 @@ class FirebirdTest extends UnitTestCase { $this->assertTrue($res); } - function TestCreateTable() + /*function TestCreateTable() { //Attempt to create the table $sql = $this->db->sql->create_table('create_test', array( @@ -104,16 +106,16 @@ class FirebirdTest extends UnitTestCase { $this->setUp(); //Check - /*$table_exists = (bool)in_array('create_test', $this->tables); + $table_exists = (bool)in_array('create_test', $this->tables); echo "create_test exists :".(int)$table_exists.'
'; - $this->assertTrue($table_exists);*/ - } + $this->assertTrue($table_exists); + }*/ function TestCommitTransaction() { - $this->TestCreateTransaction(); + $res = $this->db->beginTransaction(); $sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (10, 12, 14)'; $this->db->query($sql); @@ -124,7 +126,7 @@ class FirebirdTest extends UnitTestCase { function TestRollbackTransaction() { - $this->TestCreateTransaction(); + $res = $this->db->beginTransaction(); $sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (182, 96, 43)'; $this->db->query($sql); @@ -133,6 +135,81 @@ class FirebirdTest extends UnitTestCase { $this->assertTrue($res); } + + + function TestPreparedStatements() + { + $sql = <<db->prepare($sql); + $this->db->execute(array(1,"booger's", "Gross")); + + } + + function TestPrepareExecute() + { + $sql = <<db->prepare_execute($sql, array( + 2, "works", 'also?' + )); + + } + + function TestPrepareQuery() + { + $this->assertFalse($this->db->prepare_query('', array())); + } + + /*function TestDeleteTable() + { + //Attempt to delete the table + $sql = $this->db->sql->delete_table('create_test'); + $this->db->query($sql); + + //Reset + $this->tearDown(); + $this->setUp(); + + //Check + $table_exists = in_array('create_test', $this->tables); + $this->assertFalse($table_exists); + }*/ +} + +/** + * Firebird Query Builder Tests + */ +class FirebirdQBTest extends UnitTestCase { + + function __construct() + { + parent::__construct(); + } + + function setUp() + { + $dbpath = TEST_DIR.DS.'test_dbs'.DS.'FB_TEST_DB.FDB'; + + // Test the query builder + $params = new Stdclass(); + $params->type = 'firebird'; + $params->file = $dbpath; + $params->host = 'localhost'; + $params->user = 'sysdba'; + $params->pass = 'masterkey'; + $this->qb = new Query_Builder($params); + } + + function tearDown() + { + unset($this->qb); + } + function TestQBGet() { $query = $this->qb->get('create_test'); @@ -176,47 +253,4 @@ class FirebirdTest extends UnitTestCase { $this->assertTrue(is_resource($query)); } - - function TestPreparedStatements() - { - $sql = <<db->prepare($sql); - $this->db->execute(array(1,"booger's", "Gross")); - - } - - function TestPrepareExecute() - { - $sql = <<db->prepare_execute($sql, array( - 2, "works", 'also?' - )); - - } - - function TestPrepareQuery() - { - $this->assertFalse($this->db->prepare_query('', array())); - } - - function TestDeleteTable() - { - //Attempt to delete the table - $sql = $this->db->sql->delete_table('create_test'); - $this->db->query($sql); - - //Reset - $this->tearDown(); - $this->setUp(); - - //Check - $table_exists = in_array('create_test', $this->tables); - $this->assertFalse($table_exists); - } } \ No newline at end of file diff --git a/tests/databases/sqlite.php b/tests/databases/sqlite.php index 73c743a..b49edb3 100644 --- a/tests/databases/sqlite.php +++ b/tests/databases/sqlite.php @@ -130,50 +130,7 @@ SQL; $res = $this->db->rollback(); $this->assertTrue($res); } - - function TestQBGet() - { - $query = $this->qb->get('create_test'); - $this->assertIsA($query, 'PDOStatement'); - } - - function TestQBGetLimit() - { - $query = $this->qb->get('create_test', 2); - - $this->assertIsA($query, 'PDOStatement'); - } - - function TestQBGetLimitSkip() - { - $query = $this->qb->get('create_test', 2, 1); - - $this->assertIsA($query, 'PDOStatement'); - } - - function TestQBSelectWhereGet() - { - $query = $this->qb->select('id, key as k, val')->where('id >', 1)->get('create_test', 2, 1); - - $this->assertIsA($query, 'PDOStatement'); - } - - function TestQBSelectWhereGet2() - { - $query = $this->qb->select('id, key as k, val')->where('id', 1)->get('create_test', 2, 1); - - $this->assertIsA($query, 'PDOStatement'); - } - - function TestQBSelectGet() - { - $query = $this->qb->select('id, key as k, val')->get('create_test', 2, 1); - - $this->assertIsA($query, 'PDOStatement'); - - } - // This is really time intensive ! Run only when needed /*function TestDeleteTable() { @@ -190,4 +147,69 @@ SQL; $this->assertFalse(in_array('create_test', $dbs)); }*/ +} + +/** + * Class for testing Query Builder with SQLite + */ + class SQLiteQBTest extends UnitTestCase { + + function setUp() + { + $path = dirname(__FILE__)."/../test_dbs/test_sqlite.db"; + $this->db = new SQLite($path); + + $params = new Stdclass(); + $params->type = 'sqlite'; + $params->file = $path; + $params->host = 'localhost'; + $this->qb = new Query_Builder($params); + } + + function tearDown() + { + unset($this->db); + } + + function TestGet() + { + $query = $this->qb->get('create_test'); + + $this->assertIsA($query, 'PDOStatement'); + } + + function TestGetLimit() + { + $query = $this->qb->get('create_test', 2); + + $this->assertIsA($query, 'PDOStatement'); + } + + function TestGetLimitSkip() + { + $query = $this->qb->get('create_test', 2, 1); + + $this->assertIsA($query, 'PDOStatement'); + } + + function TestSelectWhereGet() + { + $query = $this->qb->select('id, key as k, val')->where('id >', 1)->get('create_test', 2, 1); + + $this->assertIsA($query, 'PDOStatement'); + } + + function TestSelectWhereGet2() + { + $query = $this->qb->select('id, key as k, val')->where('id', 1)->get('create_test', 2, 1); + + $this->assertIsA($query, 'PDOStatement'); + } + + function TestSelectGet() + { + $query = $this->qb->select('id, key as k, val')->get('create_test', 2, 1); + + $this->assertIsA($query, 'PDOStatement'); + } } \ No newline at end of file diff --git a/tests/test_dbs/FB_TEST_DB.FDB b/tests/test_dbs/FB_TEST_DB.FDB index 9499e06..d960203 100755 Binary files a/tests/test_dbs/FB_TEST_DB.FDB and b/tests/test_dbs/FB_TEST_DB.FDB differ