Add insert_batch methods to Firebird and SQLite

This commit is contained in:
Timothy Warren 2014-04-03 13:28:30 -04:00
parent 600c07f1ca
commit 4809016c31
16 changed files with 105 additions and 113 deletions

View File

@ -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
}
}
}

View File

@ -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);
}

View File

@ -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));

View File

@ -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';

View File

@ -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;
}
// --------------------------------------------------------------------------

View File

@ -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

View File

@ -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

View File

@ -590,7 +590,7 @@ abstract class QBTest extends Query_TestCase {
array(
'id' => 89,
'key' => 34,
'val' => 57,
'val' => "10 o'clock",
),
array(
'id' => 48,

View File

@ -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');
}
}

View File

@ -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()