2012-03-15 09:25:18 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Query
|
|
|
|
*
|
|
|
|
* Free Query Builder / Database Abstraction Layer
|
|
|
|
*
|
2012-04-20 13:17:39 -04:00
|
|
|
* @package Query
|
|
|
|
* @author Timothy J. Warren
|
2014-01-02 12:36:50 -05:00
|
|
|
* @copyright Copyright (c) 2012 - 2014
|
2012-03-15 09:25:18 -04:00
|
|
|
* @link https://github.com/aviat4ion/Query
|
2012-04-20 13:17:39 -04:00
|
|
|
* @license http://philsturgeon.co.uk/code/dbad-license
|
2012-03-15 09:25:18 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-04-02 17:08:50 -04:00
|
|
|
namespace Query\Driver;
|
|
|
|
|
2014-04-07 18:28:53 -04:00
|
|
|
use Query\Table\Table_Builder;
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2012-03-15 09:25:18 -04:00
|
|
|
/**
|
|
|
|
* Base Database class
|
|
|
|
*
|
|
|
|
* Extends PDO to simplify cross-database issues
|
2012-04-20 13:17:39 -04:00
|
|
|
*
|
|
|
|
* @package Query
|
2014-03-31 16:01:58 -04:00
|
|
|
* @subpackage Drivers
|
2012-03-15 09:25:18 -04:00
|
|
|
*/
|
2014-04-02 17:08:50 -04:00
|
|
|
abstract class Abstract_Driver extends \PDO implements Driver_Interface {
|
2012-03-15 09:25:18 -04:00
|
|
|
|
2014-03-31 16:01:58 -04:00
|
|
|
/**
|
|
|
|
* Reference to the last executed query
|
2014-04-22 14:02:54 -04:00
|
|
|
* @var \PDOStatement
|
2014-03-31 16:01:58 -04:00
|
|
|
*/
|
2012-03-15 09:25:18 -04:00
|
|
|
protected $statement;
|
2012-08-09 12:15:36 -04:00
|
|
|
|
2014-03-31 16:01:58 -04:00
|
|
|
/**
|
2014-04-23 15:53:16 -04:00
|
|
|
* Character to escape identifiers
|
2014-03-31 16:01:58 -04:00
|
|
|
* @var string
|
|
|
|
*/
|
2012-04-10 14:06:34 -04:00
|
|
|
protected $escape_char = '"';
|
2012-08-09 12:15:36 -04:00
|
|
|
|
2014-03-31 16:01:58 -04:00
|
|
|
/**
|
|
|
|
* Reference to sql class
|
|
|
|
* @var SQL_Interface
|
|
|
|
*/
|
2012-04-19 11:42:50 -04:00
|
|
|
public $sql;
|
2012-08-09 12:15:36 -04:00
|
|
|
|
2014-03-31 16:01:58 -04:00
|
|
|
/**
|
|
|
|
* Reference to util class
|
2014-04-22 14:02:54 -04:00
|
|
|
* @var Abstract_Util
|
2014-03-31 16:01:58 -04:00
|
|
|
*/
|
2012-04-19 11:42:50 -04:00
|
|
|
public $util;
|
2012-03-15 09:25:18 -04:00
|
|
|
|
2014-04-07 16:49:49 -04:00
|
|
|
/**
|
|
|
|
* Reference to table_builder class
|
|
|
|
* @var \Query\Table\Table_Builder
|
|
|
|
*/
|
|
|
|
public $table;
|
|
|
|
|
2014-03-31 16:01:58 -04:00
|
|
|
/**
|
|
|
|
* Last query executed
|
|
|
|
* @var string
|
|
|
|
*/
|
2012-09-27 13:41:29 -04:00
|
|
|
public $last_query;
|
2013-02-28 12:22:55 -05:00
|
|
|
|
2014-03-31 16:01:58 -04:00
|
|
|
/**
|
|
|
|
* Prefix to apply to table names
|
|
|
|
* @var string
|
|
|
|
*/
|
2012-11-07 08:42:34 -05:00
|
|
|
public $table_prefix = '';
|
2012-09-27 13:41:29 -04:00
|
|
|
|
2014-04-24 16:25:04 -04:00
|
|
|
/**
|
|
|
|
* Whether the driver supports 'TRUNCATE'
|
|
|
|
* @var bool
|
|
|
|
*/
|
2014-04-24 16:28:48 -04:00
|
|
|
protected $has_truncate = TRUE;
|
2014-04-24 16:25:04 -04:00
|
|
|
|
2012-03-15 09:25:18 -04:00
|
|
|
/**
|
|
|
|
* PDO constructor wrapper
|
2012-04-13 12:49:13 -04:00
|
|
|
*
|
|
|
|
* @param string $dsn
|
|
|
|
* @param string $username
|
|
|
|
* @param string $password
|
|
|
|
* @param array $driver_options
|
2012-03-15 09:25:18 -04:00
|
|
|
*/
|
2014-03-26 20:49:33 -04:00
|
|
|
public function __construct($dsn, $username=NULL, $password=NULL, array $driver_options=array())
|
2012-03-15 09:25:18 -04:00
|
|
|
{
|
2014-04-01 15:41:38 -04:00
|
|
|
// Set PDO to display errors as exceptions, and apply driver options
|
2014-04-02 17:08:50 -04:00
|
|
|
$driver_options[\PDO::ATTR_ERRMODE] = \PDO::ERRMODE_EXCEPTION;
|
2012-03-15 09:25:18 -04:00
|
|
|
parent::__construct($dsn, $username, $password, $driver_options);
|
2012-08-09 12:15:36 -04:00
|
|
|
|
2014-04-24 15:32:09 -04:00
|
|
|
$this->_load_sub_classes();
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
2014-04-07 16:49:49 -04:00
|
|
|
|
2014-04-24 15:32:09 -04:00
|
|
|
/**
|
|
|
|
* Loads the subclasses for the driver
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function _load_sub_classes()
|
|
|
|
{
|
|
|
|
// Load the sql and util class for the driver
|
|
|
|
$this_class = get_class($this);
|
|
|
|
$ns_array = explode("\\", $this_class);
|
|
|
|
$base_class = array_pop($ns_array);
|
|
|
|
$ns = implode('\\', $ns_array);
|
|
|
|
$sql_class = "{$ns}\\SQL\\{$base_class}_SQL";
|
|
|
|
$util_class = "{$ns}\\Util\\{$base_class}_Util";
|
|
|
|
|
|
|
|
$this->sql = new $sql_class();
|
|
|
|
$this->util = new $util_class($this);
|
2014-04-07 18:28:53 -04:00
|
|
|
$this->table = new Table_Builder('', array(), $this);
|
2012-03-15 09:25:18 -04:00
|
|
|
}
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2014-04-10 15:54:43 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allow invoke to work on table object
|
|
|
|
*
|
2014-04-23 15:53:16 -04:00
|
|
|
* @codeCoverageIgnore
|
2014-04-10 15:54:43 -04:00
|
|
|
* @param string $name
|
|
|
|
* @param array $args
|
2014-04-22 14:02:54 -04:00
|
|
|
* @return mixed
|
2014-04-10 15:54:43 -04:00
|
|
|
*/
|
|
|
|
public function __call($name, $args = array())
|
|
|
|
{
|
|
|
|
if (
|
|
|
|
isset($this->$name)
|
|
|
|
&& is_object($this->$name)
|
|
|
|
&& method_exists($this->$name, '__invoke')
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return call_user_func_array(array($this->$name, '__invoke'), $args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-13 12:49:13 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// ! Concrete functions that can be overridden in child classes
|
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2014-04-23 15:53:16 -04:00
|
|
|
/**
|
|
|
|
* Get the SQL class for the current driver
|
|
|
|
*
|
2014-04-24 17:07:50 -04:00
|
|
|
* @return SQL\SQL_Interface
|
2014-04-23 15:53:16 -04:00
|
|
|
*/
|
|
|
|
public function get_sql()
|
|
|
|
{
|
|
|
|
return $this->sql;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Util class for the current driver
|
|
|
|
*
|
2014-04-24 17:07:50 -04:00
|
|
|
* @return Util\Abstract_Util
|
2014-04-23 15:53:16 -04:00
|
|
|
*/
|
|
|
|
public function get_util()
|
|
|
|
{
|
|
|
|
return $this->util;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2012-03-15 09:25:18 -04:00
|
|
|
/**
|
|
|
|
* Simplifies prepared statements for database queries
|
|
|
|
*
|
|
|
|
* @param string $sql
|
|
|
|
* @param array $data
|
2014-04-03 15:05:18 -04:00
|
|
|
* @return \PDOStatement | FALSE
|
|
|
|
* @throws \InvalidArgumentException
|
2012-03-15 09:25:18 -04:00
|
|
|
*/
|
|
|
|
public function prepare_query($sql, $data)
|
|
|
|
{
|
2014-04-01 15:41:38 -04:00
|
|
|
// Prepare the sql, save the statement for easy access later
|
|
|
|
$this->statement = $this->prepare($sql);
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-15 09:25:18 -04:00
|
|
|
if( ! (is_array($data) || is_object($data)))
|
|
|
|
{
|
2014-04-02 17:08:50 -04:00
|
|
|
throw new \InvalidArgumentException("Invalid data argument");
|
2012-03-15 09:25:18 -04:00
|
|
|
}
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-15 09:25:18 -04:00
|
|
|
// Bind the parameters
|
2012-09-25 10:16:01 -04:00
|
|
|
foreach($data as $k => $value)
|
2012-03-15 09:25:18 -04:00
|
|
|
{
|
2014-04-01 15:41:38 -04:00
|
|
|
// Parameters are 1-based, the data is 0-based
|
|
|
|
// So, if the key is numeric, add 1
|
|
|
|
if(is_numeric($k)) $k++;
|
|
|
|
$this->statement->bindValue($k, $value);
|
2012-03-15 09:25:18 -04:00
|
|
|
}
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2014-04-01 15:41:38 -04:00
|
|
|
return $this->statement;
|
2012-03-15 09:25:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create and execute a prepared statement with the provided parameters
|
|
|
|
*
|
|
|
|
* @param string $sql
|
|
|
|
* @param array $params
|
2014-04-03 15:05:18 -04:00
|
|
|
* @return \PDOStatement
|
2012-03-15 09:25:18 -04:00
|
|
|
*/
|
|
|
|
public function prepare_execute($sql, $params)
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2012-03-15 09:25:18 -04:00
|
|
|
$this->statement = $this->prepare_query($sql, $params);
|
|
|
|
$this->statement->execute();
|
|
|
|
|
|
|
|
return $this->statement;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns number of rows affected by an INSERT, UPDATE, DELETE type query
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
2014-02-18 15:18:01 -05:00
|
|
|
public function affected_rows()
|
2012-03-15 09:25:18 -04:00
|
|
|
{
|
|
|
|
// Return number of rows affected
|
|
|
|
return $this->statement->rowCount();
|
|
|
|
}
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-03-15 09:25:18 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-11-07 08:42:34 -05:00
|
|
|
/**
|
2014-04-08 17:13:41 -04:00
|
|
|
* Prefixes a table if it is not already prefixed
|
2012-11-07 20:48:15 -05:00
|
|
|
* @param string $table
|
2012-11-07 08:42:34 -05:00
|
|
|
* @return string
|
|
|
|
*/
|
2014-04-08 17:13:41 -04:00
|
|
|
public function prefix_table($table)
|
2013-02-28 12:22:55 -05:00
|
|
|
{
|
2014-04-01 15:41:38 -04:00
|
|
|
// Add the prefix to the table name
|
|
|
|
// before quoting it
|
|
|
|
if ( ! empty($this->table_prefix))
|
2012-11-07 08:42:34 -05:00
|
|
|
{
|
2014-04-23 15:53:16 -04:00
|
|
|
// Split identifier by period, will split into:
|
2014-04-01 15:41:38 -04:00
|
|
|
// database.schema.table OR
|
|
|
|
// schema.table OR
|
|
|
|
// database.table OR
|
|
|
|
// table
|
|
|
|
$idents = explode('.', $table);
|
|
|
|
$segments = count($idents);
|
|
|
|
|
|
|
|
// Quote the last item, and add the database prefix
|
|
|
|
$idents[$segments - 1] = $this->_prefix(end($idents));
|
|
|
|
|
|
|
|
// Rejoin
|
|
|
|
$table = implode('.', $idents);
|
2012-11-07 08:42:34 -05:00
|
|
|
}
|
2013-02-28 12:22:55 -05:00
|
|
|
|
2014-04-08 17:13:41 -04:00
|
|
|
return $table;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Quote database table name, and set prefix
|
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function quote_table($table)
|
|
|
|
{
|
|
|
|
$table = $this->prefix_table($table);
|
|
|
|
|
2012-11-07 08:42:34 -05:00
|
|
|
// Finally, quote the table
|
|
|
|
return $this->quote_ident($table);
|
|
|
|
}
|
2013-02-28 12:22:55 -05:00
|
|
|
|
2012-11-07 08:42:34 -05:00
|
|
|
// --------------------------------------------------------------------------
|
2013-02-28 12:22:55 -05:00
|
|
|
|
2012-03-15 09:25:18 -04:00
|
|
|
/**
|
|
|
|
* Surrounds the string with the databases identifier escape characters
|
|
|
|
*
|
|
|
|
* @param mixed $ident
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function quote_ident($ident)
|
2014-02-25 13:55:19 -05:00
|
|
|
{
|
2012-03-15 09:25:18 -04:00
|
|
|
if (is_array($ident))
|
|
|
|
{
|
2012-11-07 08:42:34 -05:00
|
|
|
return array_map(array($this, __METHOD__), $ident);
|
2012-03-15 09:25:18 -04:00
|
|
|
}
|
2012-08-09 12:15:36 -04:00
|
|
|
|
2012-08-22 21:17:27 -04:00
|
|
|
// Handle comma-separated identifiers
|
|
|
|
if (strpos($ident, ',') !== FALSE)
|
2012-05-01 14:16:03 -04:00
|
|
|
{
|
2012-08-22 21:17:27 -04:00
|
|
|
$parts = explode(',', $ident);
|
2014-02-25 13:55:19 -05:00
|
|
|
$parts = array_map('mb_trim', $parts);
|
2012-11-07 08:42:34 -05:00
|
|
|
$parts = array_map(array($this, __METHOD__), $parts);
|
2012-08-22 21:17:27 -04:00
|
|
|
$ident = implode(',', $parts);
|
2012-05-01 14:16:03 -04:00
|
|
|
}
|
2012-03-15 09:25:18 -04:00
|
|
|
|
|
|
|
// Split each identifier by the period
|
|
|
|
$hiers = explode('.', $ident);
|
2012-08-22 21:17:27 -04:00
|
|
|
$hiers = array_map('mb_trim', $hiers);
|
2012-03-15 09:25:18 -04:00
|
|
|
|
2013-04-25 08:15:56 -04:00
|
|
|
// Re-compile the string
|
|
|
|
$raw = implode('.', array_map(array($this, '_quote'), $hiers));
|
|
|
|
|
|
|
|
// Fix functions
|
|
|
|
$funcs = array();
|
|
|
|
preg_match_all("#{$this->escape_char}([a-zA-Z0-9_]+(\((.*?)\))){$this->escape_char}#iu", $raw, $funcs, PREG_SET_ORDER);
|
|
|
|
foreach($funcs as $f)
|
|
|
|
{
|
|
|
|
// Unquote the function
|
|
|
|
$raw = str_replace($f[0], $f[1], $raw);
|
|
|
|
|
|
|
|
// Quote the inside identifiers
|
|
|
|
$raw = str_replace($f[3], $this->quote_ident($f[3]), $raw);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $raw;
|
|
|
|
|
2012-05-01 14:16:03 -04:00
|
|
|
}
|
2012-08-09 12:15:36 -04:00
|
|
|
|
2012-03-15 09:25:18 -04:00
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2012-04-10 14:06:34 -04:00
|
|
|
* Return schemas for databases that list them
|
|
|
|
*
|
|
|
|
* @return array
|
2012-03-15 09:25:18 -04:00
|
|
|
*/
|
2012-04-10 14:06:34 -04:00
|
|
|
public function get_schemas()
|
|
|
|
{
|
2013-05-01 15:59:23 -04:00
|
|
|
return NULL;
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
|
2012-03-15 09:25:18 -04:00
|
|
|
/**
|
|
|
|
* Return list of tables for the current database
|
2012-04-10 14:06:34 -04:00
|
|
|
*
|
2012-03-15 09:25:18 -04:00
|
|
|
* @return array
|
|
|
|
*/
|
2012-04-10 14:06:34 -04:00
|
|
|
public function get_tables()
|
|
|
|
{
|
2014-04-03 13:28:30 -04:00
|
|
|
return $this->driver_query('table_list');
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
|
2012-04-03 16:54:33 -04:00
|
|
|
/**
|
|
|
|
* Return list of dbs for the current connection, if possible
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2012-04-10 14:06:34 -04:00
|
|
|
public function get_dbs()
|
|
|
|
{
|
2014-04-03 13:28:30 -04:00
|
|
|
return $this->driver_query('db_list');
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------
|
2012-03-15 09:25:18 -04:00
|
|
|
|
|
|
|
/**
|
2012-04-10 14:06:34 -04:00
|
|
|
* Return list of views for the current database
|
|
|
|
*
|
|
|
|
* @return array
|
2012-03-15 09:25:18 -04:00
|
|
|
*/
|
2012-04-10 14:06:34 -04:00
|
|
|
public function get_views()
|
|
|
|
{
|
2014-04-03 13:28:30 -04:00
|
|
|
return $this->driver_query('view_list');
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------
|
2012-03-15 09:25:18 -04:00
|
|
|
|
|
|
|
/**
|
2012-04-10 14:06:34 -04:00
|
|
|
* Return list of sequences for the current database, if they exist
|
|
|
|
*
|
|
|
|
* @return array
|
2012-03-15 09:25:18 -04:00
|
|
|
*/
|
2012-04-10 14:06:34 -04:00
|
|
|
public function get_sequences()
|
|
|
|
{
|
2014-04-03 13:28:30 -04:00
|
|
|
return $this->driver_query('sequence_list');
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------
|
2012-03-15 09:25:18 -04:00
|
|
|
|
|
|
|
/**
|
2014-04-24 14:50:53 -04:00
|
|
|
* Return list of functions for the current database
|
2012-04-10 14:06:34 -04:00
|
|
|
*
|
2012-03-15 09:25:18 -04:00
|
|
|
* @return array
|
|
|
|
*/
|
2012-04-10 14:06:34 -04:00
|
|
|
public function get_functions()
|
|
|
|
{
|
2014-04-03 13:28:30 -04:00
|
|
|
return $this->driver_query('function_list', FALSE);
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
|
2012-03-15 09:25:18 -04:00
|
|
|
/**
|
2012-04-10 14:06:34 -04:00
|
|
|
* Return list of stored procedures for the current database
|
2012-03-15 09:25:18 -04:00
|
|
|
*
|
2012-04-10 14:06:34 -04:00
|
|
|
* @return array
|
2012-03-15 09:25:18 -04:00
|
|
|
*/
|
2012-04-10 14:06:34 -04:00
|
|
|
public function get_procedures()
|
|
|
|
{
|
2014-04-03 13:28:30 -04:00
|
|
|
return $this->driver_query('procedure_list', FALSE);
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
|
2012-03-15 09:25:18 -04:00
|
|
|
/**
|
2012-04-10 14:06:34 -04:00
|
|
|
* Return list of triggers for the current database
|
2012-03-15 09:25:18 -04:00
|
|
|
*
|
2012-04-10 14:06:34 -04:00
|
|
|
* @return array
|
2012-03-15 09:25:18 -04:00
|
|
|
*/
|
2012-04-10 14:06:34 -04:00
|
|
|
public function get_triggers()
|
|
|
|
{
|
2014-04-03 13:28:30 -04:00
|
|
|
return $this->driver_query('trigger_list', FALSE);
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
2012-03-15 09:25:18 -04:00
|
|
|
|
2012-04-10 14:06:34 -04:00
|
|
|
// -------------------------------------------------------------------------
|
2012-03-15 09:25:18 -04:00
|
|
|
|
|
|
|
/**
|
2014-04-22 14:02:54 -04:00
|
|
|
* Retrieves an array of non-user-created tables for
|
2012-04-10 14:06:34 -04:00
|
|
|
* the connection/database
|
|
|
|
*
|
|
|
|
* @return array
|
2012-03-15 09:25:18 -04:00
|
|
|
*/
|
2012-04-10 14:06:34 -04:00
|
|
|
public function get_system_tables()
|
|
|
|
{
|
2014-04-03 13:28:30 -04:00
|
|
|
return $this->driver_query('system_table_list');
|
2012-04-10 14:06:34 -04:00
|
|
|
}
|
2012-08-09 12:15:36 -04:00
|
|
|
|
2012-05-09 13:54:38 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-08-09 12:15:36 -04:00
|
|
|
|
2012-05-09 13:54:38 -04:00
|
|
|
/**
|
|
|
|
* Retrieve column information for the current database table
|
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_columns($table)
|
|
|
|
{
|
2014-04-23 15:53:16 -04:00
|
|
|
return $this->driver_query($this->get_sql()->column_list($this->prefix_table($table)), FALSE);
|
2012-05-09 13:54:38 -04:00
|
|
|
}
|
2012-08-09 12:15:36 -04:00
|
|
|
|
2012-05-09 13:54:38 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2012-08-09 12:15:36 -04:00
|
|
|
|
2014-04-07 16:49:49 -04:00
|
|
|
/**
|
|
|
|
* Retrieve foreign keys for the table
|
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_fks($table)
|
|
|
|
{
|
2014-04-23 15:53:16 -04:00
|
|
|
return $this->driver_query($this->get_sql()->fk_list($table), FALSE);
|
2014-04-07 16:49:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2014-04-08 14:26:28 -04:00
|
|
|
/**
|
|
|
|
* Retrieve indexes for the table
|
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_indexes($table)
|
|
|
|
{
|
2014-04-23 15:53:16 -04:00
|
|
|
return $this->driver_query($this->get_sql()->index_list($this->prefix_table($table)), FALSE);
|
2014-04-08 14:26:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2012-05-09 13:54:38 -04:00
|
|
|
/**
|
|
|
|
* Retrieve list of data types for the database
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_types()
|
|
|
|
{
|
2014-04-03 13:28:30 -04:00
|
|
|
return $this->driver_query('type_list', FALSE);
|
2012-05-09 13:54:38 -04:00
|
|
|
}
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2012-04-19 11:42:50 -04:00
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2014-04-22 14:02:54 -04:00
|
|
|
* Method to simplify retrieving db results for meta-data queries
|
2012-04-19 11:42:50 -04:00
|
|
|
*
|
2014-04-03 13:28:30 -04:00
|
|
|
* @param string|array|null $query
|
2012-04-19 11:42:50 -04:00
|
|
|
* @param bool $filtered_index
|
2014-04-01 15:41:38 -04:00
|
|
|
* @return array
|
2012-04-19 11:42:50 -04:00
|
|
|
*/
|
2014-04-03 13:28:30 -04:00
|
|
|
public function driver_query($query, $filtered_index=TRUE)
|
2012-04-19 11:42:50 -04:00
|
|
|
{
|
2014-04-03 13:28:30 -04:00
|
|
|
// Call the appropriate method, if it exists
|
2014-04-03 14:44:03 -04:00
|
|
|
if (is_string($query) && method_exists($this->sql, $query))
|
2014-02-25 13:55:19 -05:00
|
|
|
{
|
2014-04-23 15:53:16 -04:00
|
|
|
$query = $this->get_sql()->$query();
|
2014-02-25 13:55:19 -05:00
|
|
|
}
|
|
|
|
|
2014-04-03 13:28:30 -04:00
|
|
|
// 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);
|
2012-04-19 11:42:50 -04:00
|
|
|
|
2014-04-02 17:08:50 -04:00
|
|
|
$flag = ($filtered_index) ? \PDO::FETCH_NUM : \PDO::FETCH_ASSOC;
|
2012-04-19 11:42:50 -04:00
|
|
|
$all = $res->fetchAll($flag);
|
|
|
|
|
2014-04-02 17:08:50 -04:00
|
|
|
return ($filtered_index) ? \db_filter($all, 0) : $all;
|
2012-04-19 11:42:50 -04:00
|
|
|
}
|
2012-08-09 12:15:36 -04:00
|
|
|
|
2012-04-20 16:33:40 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the number of rows returned for a SELECT query
|
|
|
|
*
|
2012-10-23 09:40:11 -04:00
|
|
|
* @see http://us3.php.net/manual/en/pdostatement.rowcount.php#87110
|
2012-04-20 16:33:40 -04:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function num_rows()
|
|
|
|
{
|
2012-09-27 13:41:29 -04:00
|
|
|
$regex = '/^SELECT\s+(?:ALL\s+|DISTINCT\s+)?(?:.*?)\s+FROM\s+(.*)$/i';
|
|
|
|
$output = array();
|
|
|
|
|
|
|
|
if (preg_match($regex, $this->last_query, $output) > 0)
|
|
|
|
{
|
2014-02-17 19:31:06 -05:00
|
|
|
$stmt = $this->query("SELECT COUNT(*) FROM {$output[1]}");
|
2014-02-25 13:55:19 -05:00
|
|
|
return (int) $stmt->fetchColumn();
|
2012-09-27 13:41:29 -04:00
|
|
|
}
|
|
|
|
|
2013-05-01 15:59:23 -04:00
|
|
|
return NULL;
|
2012-04-20 16:33:40 -04:00
|
|
|
}
|
2012-04-19 11:42:50 -04:00
|
|
|
|
2013-05-03 13:07:34 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2014-02-25 13:55:19 -05:00
|
|
|
|
|
|
|
/**
|
2013-05-03 13:07:34 -04:00
|
|
|
* Create sql for batch insert
|
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
* @param array $data
|
2014-04-03 13:28:30 -04:00
|
|
|
* @return array
|
2013-05-03 13:07:34 -04:00
|
|
|
*/
|
|
|
|
public function insert_batch($table, $data=array())
|
|
|
|
{
|
2014-04-23 17:03:46 -04:00
|
|
|
$first_row = current($data);
|
|
|
|
if ( ! is_array($first_row)) return NULL;
|
2014-02-25 13:55:19 -05:00
|
|
|
|
2014-04-23 17:03:46 -04:00
|
|
|
// Values for insertion
|
|
|
|
$vals = array();
|
|
|
|
foreach($data as $group)
|
|
|
|
{
|
|
|
|
$vals = array_merge($vals, array_values($group));
|
|
|
|
}
|
2013-05-03 13:07:34 -04:00
|
|
|
$table = $this->quote_table($table);
|
2014-04-23 17:03:46 -04:00
|
|
|
$fields = array_keys($first_row);
|
2014-02-25 13:55:19 -05:00
|
|
|
|
2014-04-01 15:41:38 -04:00
|
|
|
$sql = "INSERT INTO {$table} ("
|
|
|
|
. implode(',', $this->quote_ident($fields))
|
|
|
|
. ") VALUES ";
|
2014-02-25 13:55:19 -05:00
|
|
|
|
2014-04-03 13:28:30 -04:00
|
|
|
// Create the placeholder groups
|
2013-05-03 13:07:34 -04:00
|
|
|
$params = array_fill(0, count($fields), '?');
|
2014-04-01 15:41:38 -04:00
|
|
|
$param_string = "(" . implode(',', $params) . ")";
|
|
|
|
$param_list = array_fill(0, count($data), $param_string);
|
2014-02-25 13:55:19 -05:00
|
|
|
|
2014-04-23 17:03:46 -04:00
|
|
|
// Append the placeholder groups to the query
|
2014-04-01 15:41:38 -04:00
|
|
|
$sql .= implode(',', $param_list);
|
|
|
|
|
2013-05-03 13:07:34 -04:00
|
|
|
return array($sql, $vals);
|
|
|
|
}
|
2014-04-24 14:50:53 -04:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method for quote_ident
|
|
|
|
*
|
|
|
|
* @param mixed $str
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function _quote($str)
|
|
|
|
{
|
|
|
|
// Check that the current value is a string,
|
|
|
|
// and is not already quoted before quoting
|
|
|
|
// that value, otherwise, return the original value
|
|
|
|
return (
|
|
|
|
strpos($str, $this->escape_char) !== 0
|
|
|
|
&& strrpos($str, $this->escape_char) !== 0
|
|
|
|
&& is_string($str)
|
|
|
|
&& ! is_numeric($str)
|
|
|
|
)
|
|
|
|
? "{$this->escape_char}{$str}{$this->escape_char}"
|
|
|
|
: $str;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the table prefix on the passed string
|
|
|
|
*
|
|
|
|
* @param string $str
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function _prefix($str)
|
|
|
|
{
|
|
|
|
// Don't prefix an already prefixed table
|
|
|
|
if (strpos($str, $this->table_prefix) !== FALSE)
|
|
|
|
{
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->table_prefix.$str;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Empty the passed table
|
|
|
|
*
|
|
|
|
* @param string $table
|
2014-04-24 16:25:04 -04:00
|
|
|
* @return \PDOStatement
|
2014-04-24 14:50:53 -04:00
|
|
|
*/
|
2014-04-24 16:25:04 -04:00
|
|
|
public function truncate($table)
|
|
|
|
{
|
|
|
|
|
|
|
|
$sql = ($this->has_truncate)
|
|
|
|
? 'TRUNCATE '
|
|
|
|
: 'DELETE FROM ';
|
|
|
|
|
|
|
|
$sql .= $this->quote_table($table);
|
|
|
|
|
|
|
|
$this->statement = $this->query($sql);
|
|
|
|
return $this->statement;
|
|
|
|
}
|
2014-04-24 14:50:53 -04:00
|
|
|
|
2012-03-15 09:25:18 -04:00
|
|
|
}
|
2014-02-11 14:29:41 -05:00
|
|
|
// End of db_pdo.php
|