diff --git a/autoload.php b/autoload.php index ef351eb..e690a86 100644 --- a/autoload.php +++ b/autoload.php @@ -38,48 +38,39 @@ require(QBASE_PATH.'common.php'); * Load query classes * * @subpackage Core + * @codeCoverageIgnore * @param string $class */ function query_autoload($class) { $class_segments = explode('\\', $class); $class = strtolower(array_pop($class_segments)); - + // Load DB Driver classes - if ($class_segments == array('Query', 'Driver')) + $driver_path = QDRIVER_PATH . "{$class}"; + if ($class_segments == array('Query', 'Driver') && is_dir($driver_path)) { - $driver_path = QDRIVER_PATH . "{$class}"; - // @codeCoverageIgnoreStart - if (is_dir($driver_path)) + + // Firebird is a special case, since it's not a PDO driver + if ( + in_array($class, PDO::getAvailableDrivers()) + || function_exists('fbird_connect') && $class === 'firebird' + ) { - // Firebird is a special case, since it's not a PDO driver - if ( - in_array($class, PDO::getAvailableDrivers()) - || function_exists('fbird_connect') && $class === 'firebird' - ) - { - - array_map('do_include', glob("{$driver_path}/*.php")); - return; - } - + array_map('do_include', glob("{$driver_path}/*.php")); } - // @codeCoverageIgnoreEnd } - + // Load other classes foreach(array( - QBASE_PATH . "classes/interfaces/{$class}.php", - QBASE_PATH . "classes/abstract/{$class}.php", - QBASE_PATH . "classes/{$class}.php" + QBASE_PATH . "core/interfaces/{$class}.php", + QBASE_PATH . "core/abstract/{$class}.php", + QBASE_PATH . "core/{$class}.php" ) as $path) { if (file_exists($path)) { - // @codeCoverageIgnoreStart require_once($path); - return; - // @codeCoverageIgnoreEnd } } } diff --git a/common.php b/common.php index 34eb4cb..62b8388 100644 --- a/common.php +++ b/common.php @@ -92,7 +92,7 @@ function Query($params = '') elseif ( ! is_scalar($params) && ! is_null($params)) { $params = new ArrayObject($params, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS); - + // Otherwise, return a new connection return $cmanager->connect($params); } diff --git a/classes/abstract/abstract_driver.php b/core/abstract/abstract_driver.php similarity index 92% rename from classes/abstract/abstract_driver.php rename to core/abstract/abstract_driver.php index 38d1e4f..508cf67 100644 --- a/classes/abstract/abstract_driver.php +++ b/core/abstract/abstract_driver.php @@ -291,7 +291,7 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface { */ public function get_tables() { - return $this->driver_query($this->sql->table_list()); + return $this->driver_query('table_list'); } // ------------------------------------------------------------------------- @@ -303,7 +303,7 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface { */ public function get_dbs() { - return $this->driver_query($this->sql->db_list()); + return $this->driver_query('db_list'); } // ------------------------------------------------------------------------- @@ -315,7 +315,7 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface { */ public function get_views() { - return $this->driver_query($this->sql->view_list()); + return $this->driver_query('view_list'); } // ------------------------------------------------------------------------- @@ -327,7 +327,7 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface { */ public function get_sequences() { - return $this->driver_query($this->sql->sequence_list()); + return $this->driver_query('sequence_list'); } // ------------------------------------------------------------------------- @@ -339,7 +339,7 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface { */ public function get_functions() { - return $this->driver_query($this->sql->function_list(), FALSE); + return $this->driver_query('function_list', FALSE); } // ------------------------------------------------------------------------- @@ -351,7 +351,7 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface { */ public function get_procedures() { - return $this->driver_query($this->sql->procedure_list(), FALSE); + return $this->driver_query('procedure_list', FALSE); } // ------------------------------------------------------------------------- @@ -363,7 +363,7 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface { */ public function get_triggers() { - return $this->driver_query($this->sql->trigger_list(), FALSE); + return $this->driver_query('trigger_list', FALSE); } // ------------------------------------------------------------------------- @@ -376,7 +376,7 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface { */ public function get_system_tables() { - return $this->driver_query($this->sql->system_table_list()); + return $this->driver_query('system_table_list'); } // -------------------------------------------------------------------------- @@ -401,7 +401,7 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface { */ public function get_types() { - return $this->driver_query($this->sql->type_list(), FALSE); + return $this->driver_query('type_list', FALSE); } // ------------------------------------------------------------------------- @@ -409,20 +409,24 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface { /** * Method to simplify retreiving db results for meta-data queries * - * @param string|array|null $sql + * @param string|array|null $query * @param bool $filtered_index * @return array */ - public function driver_query($sql, $filtered_index=TRUE) + public function driver_query($query, $filtered_index=TRUE) { - // Return if the values are returned instead of a query, - // or if the query doesn't apply to the driver - if (is_array($sql) || is_null($sql)) + // Call the appropriate method, if it exists + if (method_exists($this->sql, $query)) { - return $sql; + $query = $this->sql->$query(); } - $res = $this->query($sql); + // Return if the values are returned instead of a query, + // or if the query doesn't apply to the driver + if ( ! is_string($query)) return $query; + + // Run the query! + $res = $this->query($query); $flag = ($filtered_index) ? \PDO::FETCH_NUM : \PDO::FETCH_ASSOC; $all = $res->fetchAll($flag); @@ -471,7 +475,7 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface { * * @param string $table * @param array $data - * @return string + * @return array */ public function insert_batch($table, $data=array()) { @@ -485,12 +489,12 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface { . implode(',', $this->quote_ident($fields)) . ") VALUES "; - // Create the placholder groups + // Create the placeholder groups $params = array_fill(0, count($fields), '?'); $param_string = "(" . implode(',', $params) . ")"; $param_list = array_fill(0, count($data), $param_string); - // Add another grouping for each + // For each grouping, add the values foreach($data as $group) { $vals = array_merge($vals, array_values($group)); diff --git a/classes/abstract/abstract_sql.php b/core/abstract/abstract_sql.php similarity index 100% rename from classes/abstract/abstract_sql.php rename to core/abstract/abstract_sql.php diff --git a/classes/connection_manager.php b/core/connection_manager.php similarity index 99% rename from classes/connection_manager.php rename to core/connection_manager.php index 8cad702..39d4590 100644 --- a/classes/connection_manager.php +++ b/core/connection_manager.php @@ -15,8 +15,6 @@ namespace Query; -use \Query\Driver; - /** * Generic exception for bad drivers * @@ -174,7 +172,7 @@ final class Connection_Manager { * @throws BadDBDriverException */ private function parse_params(\ArrayObject $params) - { + { $params->type = strtolower($params->type); $dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql'; diff --git a/classes/db_util.php b/core/db_util.php similarity index 100% rename from classes/db_util.php rename to core/db_util.php diff --git a/classes/interfaces/driver_interface.php b/core/interfaces/driver_interface.php similarity index 100% rename from classes/interfaces/driver_interface.php rename to core/interfaces/driver_interface.php diff --git a/classes/interfaces/query_builder_interface.php b/core/interfaces/query_builder_interface.php similarity index 100% rename from classes/interfaces/query_builder_interface.php rename to core/interfaces/query_builder_interface.php diff --git a/classes/interfaces/sql_interface.php b/core/interfaces/sql_interface.php similarity index 100% rename from classes/interfaces/sql_interface.php rename to core/interfaces/sql_interface.php diff --git a/classes/query_builder.php b/core/query_builder.php similarity index 99% rename from classes/query_builder.php rename to core/query_builder.php index 5668a28..e67a9e3 100644 --- a/classes/query_builder.php +++ b/core/query_builder.php @@ -198,6 +198,7 @@ class Query_Builder implements Query_Builder_Interface { /** * Destructor + * @codeCoverageIgnore */ public function __destruct() { @@ -1085,12 +1086,9 @@ class Query_Builder implements Query_Builder_Interface { // Get the generated values and sql string list($sql, $data) = $this->db->insert_batch($table, $data); - if ( ! is_null($sql)) - { - return $this->_run('', $table, $sql, $data); - } - - return NULL; + return ( ! is_null($sql)) + ? $this->_run('', $table, $sql, $data) + : NULL; } // -------------------------------------------------------------------------- diff --git a/classes/query_parser.php b/core/query_parser.php similarity index 100% rename from classes/query_parser.php rename to core/query_parser.php diff --git a/drivers/firebird/firebird_driver.php b/drivers/firebird/firebird_driver.php index 56e42f4..ac9733d 100644 --- a/drivers/firebird/firebird_driver.php +++ b/drivers/firebird/firebird_driver.php @@ -343,12 +343,40 @@ class Firebird extends Abstract_Driver { * * @param string $table * @param array $data - * @return string + * @return array */ public function insert_batch($table, $data=array()) { - // This is not very applicable to the firebird database - return NULL; + // Each member of the data array needs to be an array + if ( ! is_array(current($data))) return NULL; + + // Start the block of sql statements + $sql = "EXECUTE BLOCK AS BEGIN\n"; + + $vals = array(); // Values for insertion + $table = $this->quote_table($table); + $fields = array_keys(current($data)); + + $insert_template = "INSERT INTO {$table} (" + . implode(',', $this->quote_ident($fields)) + . ") VALUES ("; + + foreach($data as $item) + { + // Quote string values + $vals = array_map(array($this, 'quote'), $item); + + // Add the values in the sql + $sql .= $insert_template . implode(', ', $vals) . ");\n"; + } + + // End the block of SQL statements + $sql .= "END"; + + // Ruturn a null array value so the query is run as it is, + // not as a prepared statement, because a prepared statement + // doesn't work for this type of query in Firebird. + return array($sql, NULL); } } // End of firebird_driver.php \ No newline at end of file diff --git a/drivers/sqlite/sqlite_driver.php b/drivers/sqlite/sqlite_driver.php index 3148e50..80196dd 100644 --- a/drivers/sqlite/sqlite_driver.php +++ b/drivers/sqlite/sqlite_driver.php @@ -100,10 +100,10 @@ class SQLite extends Abstract_Driver { * @param array $data * @return string */ - public function insert_batch($table, $data=array()) + /*public function insert_batch($table, $data=array()) { // This is not very applicable to the firebird database return NULL; - } + }*/ } //End of sqlite_driver.php \ No newline at end of file diff --git a/tests/core/db_qb_test.php b/tests/core/db_qb_test.php index 2911716..2aa6a71 100644 --- a/tests/core/db_qb_test.php +++ b/tests/core/db_qb_test.php @@ -590,7 +590,7 @@ abstract class QBTest extends Query_TestCase { array( 'id' => 89, 'key' => 34, - 'val' => 57, + 'val' => "10 o'clock", ), array( 'id' => 48, diff --git a/tests/databases/firebird/FirebirdQBTest.php b/tests/databases/firebird/FirebirdQBTest.php index 93a1f5c..aae1d9b 100644 --- a/tests/databases/firebird/FirebirdQBTest.php +++ b/tests/databases/firebird/FirebirdQBTest.php @@ -80,33 +80,6 @@ class FirebirdQBTest extends QBTest { $this->assertIsA($sql_res, '\\Query\\Driver\\Firebird_Result'); } - public function testInsertBatch() - { - if (empty($this->db)) return; - - $insert_array = array( - array( - 'id' => 6, - 'key' => 2, - 'val' => 3 - ), - array( - 'id' => 5, - 'key' => 6, - 'val' => 7 - ), - array( - 'id' => 8, - 'key' => 1, - 'val' => 2 - ) - ); - - $query = $this->db->insert_batch('test', $insert_array); - - $this->assertNull($query); - } - // -------------------------------------------------------------------------- public function testTypeList() @@ -169,4 +142,31 @@ class FirebirdQBTest extends QBTest { { $this->assertEquals('', $this->db->util->backup_structure()); } + + // -------------------------------------------------------------------------- + + public function testInsertBatch() + { + $data = array( + array( + 'id' => 544, + 'key' => 3, + 'val' => 7, + ), + array( + 'id' => 89, + 'key' => 34, + 'val' => 57, + ), + array( + 'id' => 48, + 'key' => 403, + 'val' => 97, + ), + ); + + $query = $this->db->insert_batch('test', $data); + + $this->assertIsA($query, 'PDOStatement'); + } } \ No newline at end of file diff --git a/tests/databases/sqlite/SqliteQBTest.php b/tests/databases/sqlite/SqliteQBTest.php index c9334e1..b045d76 100644 --- a/tests/databases/sqlite/SqliteQBTest.php +++ b/tests/databases/sqlite/SqliteQBTest.php @@ -37,33 +37,6 @@ $this->assertTrue($this->db === $db); } - // -------------------------------------------------------------------------- - - public function testInsertBatch() - { - $insert_array = array( - array( - 'id' => 6, - 'key' => 2, - 'val' => 3 - ), - array( - 'id' => 5, - 'key' => 6, - 'val' => 7 - ), - array( - 'id' => 8, - 'key' => 1, - 'val' => 2 - ) - ); - - $query = $this->db->insert_batch('test', $insert_array); - - $this->assertNull($query); - } - // -------------------------------------------------------------------------- public function testQueryExplain()