Add insert_batch methods to Firebird and SQLite
This commit is contained in:
parent
600c07f1ca
commit
4809016c31
39
autoload.php
39
autoload.php
@ -38,48 +38,39 @@ require(QBASE_PATH.'common.php');
|
|||||||
* Load query classes
|
* Load query classes
|
||||||
*
|
*
|
||||||
* @subpackage Core
|
* @subpackage Core
|
||||||
|
* @codeCoverageIgnore
|
||||||
* @param string $class
|
* @param string $class
|
||||||
*/
|
*/
|
||||||
function query_autoload($class)
|
function query_autoload($class)
|
||||||
{
|
{
|
||||||
$class_segments = explode('\\', $class);
|
$class_segments = explode('\\', $class);
|
||||||
$class = strtolower(array_pop($class_segments));
|
$class = strtolower(array_pop($class_segments));
|
||||||
|
|
||||||
// Load DB Driver classes
|
// 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
|
// Firebird is a special case, since it's not a PDO driver
|
||||||
if (is_dir($driver_path))
|
if (
|
||||||
|
in_array($class, PDO::getAvailableDrivers())
|
||||||
|
|| function_exists('fbird_connect') && $class === 'firebird'
|
||||||
|
)
|
||||||
{
|
{
|
||||||
// Firebird is a special case, since it's not a PDO driver
|
array_map('do_include', glob("{$driver_path}/*.php"));
|
||||||
if (
|
|
||||||
in_array($class, PDO::getAvailableDrivers())
|
|
||||||
|| function_exists('fbird_connect') && $class === 'firebird'
|
|
||||||
)
|
|
||||||
{
|
|
||||||
|
|
||||||
array_map('do_include', glob("{$driver_path}/*.php"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
// @codeCoverageIgnoreEnd
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load other classes
|
// Load other classes
|
||||||
foreach(array(
|
foreach(array(
|
||||||
QBASE_PATH . "classes/interfaces/{$class}.php",
|
QBASE_PATH . "core/interfaces/{$class}.php",
|
||||||
QBASE_PATH . "classes/abstract/{$class}.php",
|
QBASE_PATH . "core/abstract/{$class}.php",
|
||||||
QBASE_PATH . "classes/{$class}.php"
|
QBASE_PATH . "core/{$class}.php"
|
||||||
) as $path)
|
) as $path)
|
||||||
{
|
{
|
||||||
if (file_exists($path))
|
if (file_exists($path))
|
||||||
{
|
{
|
||||||
// @codeCoverageIgnoreStart
|
|
||||||
require_once($path);
|
require_once($path);
|
||||||
return;
|
|
||||||
// @codeCoverageIgnoreEnd
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -92,7 +92,7 @@ function Query($params = '')
|
|||||||
elseif ( ! is_scalar($params) && ! is_null($params))
|
elseif ( ! is_scalar($params) && ! is_null($params))
|
||||||
{
|
{
|
||||||
$params = new ArrayObject($params, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS);
|
$params = new ArrayObject($params, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS);
|
||||||
|
|
||||||
// Otherwise, return a new connection
|
// Otherwise, return a new connection
|
||||||
return $cmanager->connect($params);
|
return $cmanager->connect($params);
|
||||||
}
|
}
|
||||||
|
@ -291,7 +291,7 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface {
|
|||||||
*/
|
*/
|
||||||
public function get_tables()
|
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()
|
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()
|
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()
|
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()
|
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()
|
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()
|
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()
|
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()
|
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
|
* Method to simplify retreiving db results for meta-data queries
|
||||||
*
|
*
|
||||||
* @param string|array|null $sql
|
* @param string|array|null $query
|
||||||
* @param bool $filtered_index
|
* @param bool $filtered_index
|
||||||
* @return array
|
* @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,
|
// Call the appropriate method, if it exists
|
||||||
// or if the query doesn't apply to the driver
|
if (method_exists($this->sql, $query))
|
||||||
if (is_array($sql) || is_null($sql))
|
|
||||||
{
|
{
|
||||||
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;
|
$flag = ($filtered_index) ? \PDO::FETCH_NUM : \PDO::FETCH_ASSOC;
|
||||||
$all = $res->fetchAll($flag);
|
$all = $res->fetchAll($flag);
|
||||||
@ -471,7 +475,7 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface {
|
|||||||
*
|
*
|
||||||
* @param string $table
|
* @param string $table
|
||||||
* @param array $data
|
* @param array $data
|
||||||
* @return string
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function insert_batch($table, $data=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))
|
. implode(',', $this->quote_ident($fields))
|
||||||
. ") VALUES ";
|
. ") VALUES ";
|
||||||
|
|
||||||
// Create the placholder groups
|
// Create the placeholder groups
|
||||||
$params = array_fill(0, count($fields), '?');
|
$params = array_fill(0, count($fields), '?');
|
||||||
$param_string = "(" . implode(',', $params) . ")";
|
$param_string = "(" . implode(',', $params) . ")";
|
||||||
$param_list = array_fill(0, count($data), $param_string);
|
$param_list = array_fill(0, count($data), $param_string);
|
||||||
|
|
||||||
// Add another grouping for each
|
// For each grouping, add the values
|
||||||
foreach($data as $group)
|
foreach($data as $group)
|
||||||
{
|
{
|
||||||
$vals = array_merge($vals, array_values($group));
|
$vals = array_merge($vals, array_values($group));
|
@ -15,8 +15,6 @@
|
|||||||
|
|
||||||
namespace Query;
|
namespace Query;
|
||||||
|
|
||||||
use \Query\Driver;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generic exception for bad drivers
|
* Generic exception for bad drivers
|
||||||
*
|
*
|
||||||
@ -174,7 +172,7 @@ final class Connection_Manager {
|
|||||||
* @throws BadDBDriverException
|
* @throws BadDBDriverException
|
||||||
*/
|
*/
|
||||||
private function parse_params(\ArrayObject $params)
|
private function parse_params(\ArrayObject $params)
|
||||||
{
|
{
|
||||||
$params->type = strtolower($params->type);
|
$params->type = strtolower($params->type);
|
||||||
$dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql';
|
$dbtype = ($params->type !== 'postgresql') ? $params->type : 'pgsql';
|
||||||
|
|
@ -198,6 +198,7 @@ class Query_Builder implements Query_Builder_Interface {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Destructor
|
* Destructor
|
||||||
|
* @codeCoverageIgnore
|
||||||
*/
|
*/
|
||||||
public function __destruct()
|
public function __destruct()
|
||||||
{
|
{
|
||||||
@ -1085,12 +1086,9 @@ class Query_Builder implements Query_Builder_Interface {
|
|||||||
// Get the generated values and sql string
|
// Get the generated values and sql string
|
||||||
list($sql, $data) = $this->db->insert_batch($table, $data);
|
list($sql, $data) = $this->db->insert_batch($table, $data);
|
||||||
|
|
||||||
if ( ! is_null($sql))
|
return ( ! is_null($sql))
|
||||||
{
|
? $this->_run('', $table, $sql, $data)
|
||||||
return $this->_run('', $table, $sql, $data);
|
: NULL;
|
||||||
}
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
@ -343,12 +343,40 @@ class Firebird extends Abstract_Driver {
|
|||||||
*
|
*
|
||||||
* @param string $table
|
* @param string $table
|
||||||
* @param array $data
|
* @param array $data
|
||||||
* @return string
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function insert_batch($table, $data=array())
|
public function insert_batch($table, $data=array())
|
||||||
{
|
{
|
||||||
// This is not very applicable to the firebird database
|
// Each member of the data array needs to be an array
|
||||||
return NULL;
|
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
|
// End of firebird_driver.php
|
@ -100,10 +100,10 @@ class SQLite extends Abstract_Driver {
|
|||||||
* @param array $data
|
* @param array $data
|
||||||
* @return string
|
* @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
|
// This is not very applicable to the firebird database
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}*/
|
||||||
}
|
}
|
||||||
//End of sqlite_driver.php
|
//End of sqlite_driver.php
|
@ -590,7 +590,7 @@ abstract class QBTest extends Query_TestCase {
|
|||||||
array(
|
array(
|
||||||
'id' => 89,
|
'id' => 89,
|
||||||
'key' => 34,
|
'key' => 34,
|
||||||
'val' => 57,
|
'val' => "10 o'clock",
|
||||||
),
|
),
|
||||||
array(
|
array(
|
||||||
'id' => 48,
|
'id' => 48,
|
||||||
|
@ -80,33 +80,6 @@ class FirebirdQBTest extends QBTest {
|
|||||||
$this->assertIsA($sql_res, '\\Query\\Driver\\Firebird_Result');
|
$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()
|
public function testTypeList()
|
||||||
@ -169,4 +142,31 @@ class FirebirdQBTest extends QBTest {
|
|||||||
{
|
{
|
||||||
$this->assertEquals('', $this->db->util->backup_structure());
|
$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');
|
||||||
|
}
|
||||||
}
|
}
|
@ -37,33 +37,6 @@
|
|||||||
$this->assertTrue($this->db === $db);
|
$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()
|
public function testQueryExplain()
|
||||||
|
Loading…
Reference in New Issue
Block a user