diff --git a/classes/query_builder.php b/classes/query_builder.php index d80f8e7..7f98743 100644 --- a/classes/query_builder.php +++ b/classes/query_builder.php @@ -1266,6 +1266,8 @@ class Query_Builder implements iQuery_Builder { { $vals = array_merge($this->values, (array) $this->where_values); } + + $evals = (is_array($vals)) ? $vals : array(); $start_time = microtime(TRUE); @@ -1283,12 +1285,18 @@ class Query_Builder implements iQuery_Builder { $total_time = number_format($end_time - $start_time, 5); // Add the interpreted query to the list of executed queries + foreach($evals as $k => &$v) + { + $v = ( ! is_numeric($v)) ? htmlentities($this->db->quote($v), ENT_HTML401 | ENT_NOQUOTES, 'utf-8', FALSE) : $v; + } $esql = str_replace('?', "%s", $sql); array_unshift($vals, $esql); + array_unshift($evals, $esql); + $this->queries[] = array( 'time' => $total_time, - 'sql' => call_user_func_array('sprintf', $vals), + 'sql' => call_user_func_array('sprintf', $evals), ); $this->queries['total_time'] += $total_time; @@ -1357,11 +1365,11 @@ class Query_Builder implements iQuery_Builder { $params = array_fill(0, $param_count, '?'); $sql = "INSERT INTO {$table} (" . implode(',', $this->set_array_keys) . - ') VALUES ('.implode(',', $params).')'; + ")\nVALUES (".implode(',', $params).')'; break; case "update": - $sql = "UPDATE {$table} SET {$this->set_string}"; + $sql = "UPDATE {$table}\nSET {$this->set_string}"; break; case "delete": diff --git a/drivers/sqlite/sqlite_util.php b/drivers/sqlite/sqlite_util.php index 869e828..aab5a3b 100644 --- a/drivers/sqlite/sqlite_util.php +++ b/drivers/sqlite/sqlite_util.php @@ -119,7 +119,7 @@ class SQLite_Util extends DB_Util { if( ! empty($excluded)) { - $sql .= ' WHERE NOT IN("'.implode('","', $excluded).'")'; + $sql .= " WHERE \"name\" NOT IN('".implode("','", $excluded)."')"; } $res = $this->query($sql); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 32a766c..d727a07 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -17,10 +17,10 @@ * Base class for TestCases */ class Query_TestCase extends PHPUnit_Framework_TestCase { - + /** * Wrapper for Simpletest's assertEqual - * + * * @param mixed $expected * @param mixed $actual * @param string $message @@ -29,7 +29,7 @@ class Query_TestCase extends PHPUnit_Framework_TestCase { { $this->assertEquals($expected, $actual, $message); } - + /** * Wrapper for SimpleTest's assertIsA * @@ -41,17 +41,17 @@ class Query_TestCase extends PHPUnit_Framework_TestCase { { $this->assertTrue(is_a($object, $type), $message); } - + /** * Implementation of SimpleTest's assertReference - * + * * @param mixed $first * @param mixed $second * @param string $message */ public function assertReference($first, $second, $message='') { - if (is_object($first)) + if (is_object($first)) { $res = ($first === $second); } @@ -61,7 +61,7 @@ class Query_TestCase extends PHPUnit_Framework_TestCase { $first = uniqid("test"); $is_ref = ($first === $second); $first = $temp; - $res = $is_ref; + $res = $is_ref; } $this->assertTrue($res, $message); } @@ -85,6 +85,24 @@ require_once(QTEST_DIR . '/core/db_test.php'); require_once(QTEST_DIR . '/core/db_qp_test.php'); require_once(QTEST_DIR . '/core/db_qb_test.php'); +// Preset SQLite connection, so there aren't locking issues +if (extension_loaded('pdo_sqlite')) +{ + $path = QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db'; + $params = (object) array( + 'type' => 'sqlite', + 'file' => $path, + 'host' => 'localhost', + 'prefix' => 'create_', + 'alias' => 'test_sqlite', + 'options' => array( + PDO::ATTR_PERSISTENT => TRUE + ) + ); + + Query($params); +} + // If Firebird (interbase) extension does not exist, // create a fake class to suppress errors from skipped tests if ( ! function_exists('fbird_connect')) diff --git a/tests/core/db_test.php b/tests/core/db_test.php index c8f7951..d600f1a 100644 --- a/tests/core/db_test.php +++ b/tests/core/db_test.php @@ -56,7 +56,7 @@ abstract class DBTest extends Query_TestCase { public function testBackupData() { - $this->assertTrue(is_string($this->db->util->backup_data())); + $this->assertTrue(is_string($this->db->util->backup_data(array('create_delete')))); } // -------------------------------------------------------------------------- diff --git a/tests/databases/sqlite/SqliteQBTest.php b/tests/databases/sqlite/SqliteQBTest.php index a90853a..c9334e1 100644 --- a/tests/databases/sqlite/SqliteQBTest.php +++ b/tests/databases/sqlite/SqliteQBTest.php @@ -14,38 +14,31 @@ /** * Class for testing Query Builder with SQLite - * + * * @requires extension pdo_sqlite */ class SQLiteQBTest extends QBTest { public function setUp() { - $path = QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db'; - $params = (object) array( - 'type' => 'sqlite', - 'file' => $path, - 'host' => 'localhost', - 'prefix' => 'create_', - 'options' => array( - PDO::ATTR_PERSISTENT => TRUE - ) - ); - $this->db = Query($params); + // Set up in the bootstrap to mitigate + // connection locking issues + $this->db = Query('test_sqlite'); // echo '
SQLite Queries
'; } - - public function testInsert() { $this->markTestSkipped();} - public function testInsertArray() { $this->markTestSkipped();} - public function testUpdate() { $this->markTestSkipped();} - public function testSetArrayUpdate() { $this->markTestSkipped();} - public function testWhereSetUpdate() { $this->markTestSkipped();} - public function testDelete() { $this->markTestSkipped();} - public function testBadNumRows() { $this->markTestSkipped();} - + // -------------------------------------------------------------------------- - + + public function testQueryFunctionAlias() + { + $db = Query('test_sqlite'); + + $this->assertTrue($this->db === $db); + } + + // -------------------------------------------------------------------------- + public function testInsertBatch() { $insert_array = array( @@ -70,9 +63,9 @@ $this->assertNull($query); } - + // -------------------------------------------------------------------------- - + public function testQueryExplain() { $query = $this->db->select('id, key as k, val') @@ -80,11 +73,11 @@ ->where('id >', 1) ->where('id <', 900) ->get('create_test', 2, 1); - + $res = $query->fetchAll(PDO::FETCH_ASSOC); - + $expected_possibilities = array(); - + $expected_possibilities[] = array( array( 'order' => '0', @@ -92,7 +85,7 @@ 'detail' => 'TABLE create_test USING PRIMARY KEY', ) ); - + $expected_possibilities[] = array ( array ( 'selectid' => '0', @@ -101,7 +94,7 @@ 'detail' => 'SEARCH TABLE create_test USING INTEGER PRIMARY KEY (rowid>? AND rowid '0', @@ -110,7 +103,7 @@ 'detail' => 'SEARCH TABLE create_test USING INTEGER PRIMARY KEY (rowid>? AND rowid '0', @@ -119,9 +112,9 @@ 'detail' => 'SEARCH TABLE create_test USING INTEGER PRIMARY KEY (rowid>? AND rowiddb = new SQLite($path); + // Set up in the bootstrap to mitigate + // connection locking issues + $this->db = Query('test_sqlite'); } - - // -------------------------------------------------------------------------- - public function tearDown() - { - unset($this->db); - } - // -------------------------------------------------------------------------- // ! Util Method tests // -------------------------------------------------------------------------- - + public function testCreateTable() - { + { //Attempt to create the table $sql = $this->db->util->create_table('create_test', array( @@ -64,7 +58,7 @@ class SQLiteTest extends DBTest { ) ); $this->db->query($sql); - + // A table to delete $sql = $this->db->util->create_table('create_delete', array( @@ -83,32 +77,32 @@ class SQLiteTest extends DBTest { $this->assertTrue(in_array('create_test', $dbs)); } - + // -------------------------------------------------------------------------- - + public function testBackupData() { - $sql = mb_trim($this->db->util->backup_data()); - + $sql = mb_trim($this->db->util->backup_data(array('create_join', 'create_test'))); + $sql_array = explode("\n", $sql); - + $expected = <<assertEqual($expected_array, $sql_array); } - + // -------------------------------------------------------------------------- - - public function testBackupStructure() + + public function testBackupStructure() { $sql = mb_trim($this->db->util->backup_structure()); - + $expected = <<assertEqual($expected_array, $result_array); } - + // -------------------------------------------------------------------------- public function testDeleteTable() { $sql = $this->db->util->delete_table('create_delete'); - + $this->db->query($sql); - + //Check $dbs = $this->db->get_tables(); $this->assertFalse(in_array('create_delete', $dbs)); } - + // -------------------------------------------------------------------------- // ! General tests // -------------------------------------------------------------------------- public function testConnection() { - $this->assertIsA($this->db, 'SQLite'); + $db = new SQLite(QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db'); + + $this->assertIsA($db, 'SQLite'); + $this->assertIsA($this->db->db, 'SQLite'); + + unset($db); } - + // -------------------------------------------------------------------------- public function testGetTables() @@ -150,7 +149,7 @@ SQL; $tables = $this->db->get_tables(); $this->assertTrue(is_array($tables)); } - + // -------------------------------------------------------------------------- public function testGetSystemTables() @@ -159,22 +158,14 @@ SQL; $this->assertTrue(is_array($tables)); } - - // -------------------------------------------------------------------------- - public function testCreateTransaction() - { - $res = $this->db->beginTransaction(); - $this->assertTrue($res); - } - // -------------------------------------------------------------------------- public function testTruncate() - { + { $this->db->truncate('create_test'); } - + // -------------------------------------------------------------------------- public function testPreparedStatements() @@ -188,7 +179,7 @@ SQL; $statement->execute(); } - + // -------------------------------------------------------------------------- public function testPrepareExecute() @@ -202,7 +193,7 @@ SQL; )); } - + // -------------------------------------------------------------------------- public function testCommitTransaction() @@ -215,11 +206,11 @@ SQL; $res = $this->db->commit(); $this->assertTrue($res); } - + // -------------------------------------------------------------------------- public function testRollbackTransaction() - { + { $res = $this->db->beginTransaction(); $sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (182, 96, 43)'; @@ -228,43 +219,43 @@ SQL; $res = $this->db->rollback(); $this->assertTrue($res); } - + // -------------------------------------------------------------------------- public function testGetDBs() { $this->assertNull($this->db->get_dbs()); } - + // -------------------------------------------------------------------------- public function testGetSchemas() { $this->assertNull($this->db->get_schemas()); } - + // -------------------------------------------------------------------------- // ! SQL tests // -------------------------------------------------------------------------- - + public function testNullMethods() { $sql = $this->db->sql->system_table_list(); $this->assertEqual(NULL, $sql); - + $sql = $this->db->sql->trigger_list(); $this->assertEqual(NULL, $sql); - + $sql = $this->db->sql->function_list(); $this->assertEqual(NULL, $sql); - + $sql = $this->db->sql->procedure_list(); $this->assertEqual(NULL, $sql); - + $sql = $this->db->sql->sequence_list(); $this->assertEqual(NULL, $sql); } - - // @TODO Fix this + + // @TODO Fix this public function testGetTypes() {} } \ No newline at end of file