diff --git a/core/abstract/abstract_driver.php b/core/abstract/abstract_driver.php index 508cf67..8ba1ae9 100644 --- a/core/abstract/abstract_driver.php +++ b/core/abstract/abstract_driver.php @@ -416,7 +416,7 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface { public function driver_query($query, $filtered_index=TRUE) { // Call the appropriate method, if it exists - if (method_exists($this->sql, $query)) + if (is_string($query) && method_exists($this->sql, $query)) { $query = $this->sql->$query(); } diff --git a/core/db_util.php b/core/abstract/abstract_util.php similarity index 98% rename from core/db_util.php rename to core/abstract/abstract_util.php index 670ebb5..909f521 100644 --- a/core/db_util.php +++ b/core/abstract/abstract_util.php @@ -15,15 +15,13 @@ namespace Query\Driver; -use \Query; - /** * Abstract class defining database / table creation methods * * @package Query * @subpackage Drivers */ -abstract class DB_Util { +abstract class Abstract_Util { /** * Reference to the current connection object @@ -139,4 +137,4 @@ abstract class DB_Util { abstract public function backup_data(); } -// End of db_util.php \ No newline at end of file +// End of abstract_util.php \ No newline at end of file diff --git a/core/connection_manager.php b/core/connection_manager.php index 39d4590..2c8133a 100644 --- a/core/connection_manager.php +++ b/core/connection_manager.php @@ -241,3 +241,4 @@ final class Connection_Manager { return $dsn; } } +// End of connection_manager.php diff --git a/drivers/firebird/firebird_driver.php b/drivers/firebird/firebird_driver.php index ac9733d..e357802 100644 --- a/drivers/firebird/firebird_driver.php +++ b/drivers/firebird/firebird_driver.php @@ -353,9 +353,8 @@ class Firebird extends Abstract_Driver { // 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)); + $fields = \array_keys(\current($data)); $insert_template = "INSERT INTO {$table} (" . implode(',', $this->quote_ident($fields)) diff --git a/drivers/firebird/firebird_util.php b/drivers/firebird/firebird_util.php index 5ffd26c..f661cc8 100644 --- a/drivers/firebird/firebird_util.php +++ b/drivers/firebird/firebird_util.php @@ -24,7 +24,7 @@ namespace Query\Driver; * @method array get_tables() * @method object query(string $sql) */ -class Firebird_Util extends DB_Util { +class Firebird_Util extends Abstract_Util { /** * Create an SQL backup file for the current database's structure diff --git a/drivers/mysql/mysql_util.php b/drivers/mysql/mysql_util.php index 449201c..556c42d 100644 --- a/drivers/mysql/mysql_util.php +++ b/drivers/mysql/mysql_util.php @@ -27,12 +27,13 @@ namespace Query\Driver; * @method mixed query(string $sql) * @method string quote(string $str) */ -class MySQL_Util extends DB_Util { +class MySQL_Util extends Abstract_Util { /** * Convienience public function for creating a new MySQL table * * @codeCoverageIgnore + * @deprecated * @param string $name * @param array $columns * @param array $constraints diff --git a/drivers/pgsql/pgsql_util.php b/drivers/pgsql/pgsql_util.php index 3fdacca..8c50c52 100644 --- a/drivers/pgsql/pgsql_util.php +++ b/drivers/pgsql/pgsql_util.php @@ -23,7 +23,7 @@ namespace Query\Driver; * @method mixed query(string $sql) * @method array get_tables() */ -class PgSQL_Util extends DB_Util { +class PgSQL_Util extends Abstract_Util { /** * Create an SQL backup file for the current database's structure diff --git a/drivers/sqlite/sqlite_driver.php b/drivers/sqlite/sqlite_driver.php index 80196dd..d0f994c 100644 --- a/drivers/sqlite/sqlite_driver.php +++ b/drivers/sqlite/sqlite_driver.php @@ -100,10 +100,42 @@ 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; - }*/ + // If greater than version 3.7.11, supports the same syntax as + // MySQL and Postgres + if (version_compare($this->getAttribute(\PDO::ATTR_SERVER_VERSION), '3.7.11', '>=')) + { + return parent::insert_batch($table, $data); + } + + // -------------------------------------------------------------------------- + // Otherwise, do a union query as an analogue to a 'proper' batch insert + // -------------------------------------------------------------------------- + + // Each member of the data array needs to be an array + if ( ! is_array(current($data))) return NULL; + + // Start the block of sql statements + $table = $this->quote_table($table); + $sql = "INSERT INTO {$table} \n"; + + // Create a key-value mapping for each field + $first = array_shift($data); + $cols = array(); + foreach($first as $colname => $datum) + { + $cols[] = $this->_quote($datum) . ' AS ' . $this->quote_ident($colname); + } + $sql .= "SELECT " . implode(', ', $cols) . "\n"; + + foreach($data as $item) + { + $vals = array_map(array($this, 'quote'), $item); + $sql .= "UNION SELECT " . implode(',', $vals) . "\n"; + } + + return array($sql, NULL); + } } //End of sqlite_driver.php \ No newline at end of file diff --git a/drivers/sqlite/sqlite_util.php b/drivers/sqlite/sqlite_util.php index 633bed8..0e8a174 100644 --- a/drivers/sqlite/sqlite_util.php +++ b/drivers/sqlite/sqlite_util.php @@ -23,12 +23,13 @@ namespace Query\Driver; * @method mixed query(string $sql) * @method string quote(string $str) */ -class SQLite_Util extends DB_Util { +class SQLite_Util extends Abstract_Util { /** * Convenience public function to create a new table * * @codeCoverageIgnore + * @deprecated * @param string $name //Name of the table * @param array $columns //columns as straight array and/or column => type pairs * @param array $constraints // column => constraint pairs @@ -101,7 +102,6 @@ class SQLite_Util extends DB_Util { /** * Create an SQL backup file for the current database's data * - * @codeCoverageIgnore * @param array $excluded * @return string */ diff --git a/tests/databases/sqlite/SqliteQBTest.php b/tests/databases/sqlite/SqliteQBTest.php index b045d76..7da121d 100644 --- a/tests/databases/sqlite/SqliteQBTest.php +++ b/tests/databases/sqlite/SqliteQBTest.php @@ -24,8 +24,6 @@ // Set up in the bootstrap to mitigate // connection locking issues $this->db = Query('test_sqlite'); - - // echo '
SQLite Queries
'; } // --------------------------------------------------------------------------