2016-10-12 22:12:25 -04:00
|
|
|
<?php declare(strict_types=1);
|
2012-03-15 09:25:18 -04:00
|
|
|
/**
|
|
|
|
* Query
|
|
|
|
*
|
2016-09-07 13:17:17 -04:00
|
|
|
* SQL Query Builder / Database Abstraction Layer
|
2012-03-15 09:25:18 -04:00
|
|
|
*
|
2020-04-10 20:54:31 -04:00
|
|
|
* PHP version 7.4
|
2016-09-07 13:17:17 -04:00
|
|
|
*
|
|
|
|
* @package Query
|
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
2020-03-18 11:31:56 -04:00
|
|
|
* @copyright 2012 - 2020 Timothy J. Warren
|
2016-09-07 13:17:17 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2019-12-11 16:49:42 -05:00
|
|
|
* @link https://git.timshomepage.net/aviat/Query
|
|
|
|
* @version 3.0.0
|
2012-03-15 09:25:18 -04:00
|
|
|
*/
|
2016-09-07 17:39:19 -04:00
|
|
|
namespace Query\Drivers;
|
2016-09-07 13:17:17 -04:00
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
use function dbFilter;
|
|
|
|
|
2016-10-12 22:12:25 -04:00
|
|
|
use InvalidArgumentException;
|
2016-09-07 17:39:19 -04:00
|
|
|
use PDO;
|
|
|
|
use PDOStatement;
|
2020-04-17 14:57:56 -04:00
|
|
|
use function is_string;
|
2014-04-07 18:28:53 -04:00
|
|
|
|
2012-03-15 09:25:18 -04:00
|
|
|
/**
|
|
|
|
* Base Database class
|
|
|
|
*
|
|
|
|
* Extends PDO to simplify cross-database issues
|
|
|
|
*/
|
2018-01-22 16:04:29 -05:00
|
|
|
abstract class AbstractDriver
|
|
|
|
extends PDO
|
|
|
|
implements DriverInterface {
|
2012-03-15 09:25:18 -04:00
|
|
|
|
2014-03-31 16:01:58 -04:00
|
|
|
/**
|
|
|
|
* Reference to the last executed query
|
2016-10-12 22:12:25 -04:00
|
|
|
* @var PDOStatement
|
2014-03-31 16:01:58 -04:00
|
|
|
*/
|
2020-04-10 20:53:52 -04:00
|
|
|
protected PDOStatement $statement;
|
2012-08-09 12:15:36 -04:00
|
|
|
|
2014-03-31 16:01:58 -04:00
|
|
|
/**
|
2016-09-07 17:39:19 -04:00
|
|
|
* Start character to escape identifiers
|
2014-03-31 16:01:58 -04:00
|
|
|
* @var string
|
|
|
|
*/
|
2020-04-10 20:53:52 -04:00
|
|
|
protected string $escapeCharOpen = '"';
|
2016-09-07 17:39:19 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* End character to escape identifiers
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-04-10 20:53:52 -04:00
|
|
|
protected string $escapeCharClose = '"';
|
2012-08-09 12:15:36 -04:00
|
|
|
|
2014-03-31 16:01:58 -04:00
|
|
|
/**
|
|
|
|
* Reference to sql class
|
2015-11-10 20:58:32 -05:00
|
|
|
* @var SQLInterface
|
2014-03-31 16:01:58 -04:00
|
|
|
*/
|
2020-04-17 14:57:56 -04:00
|
|
|
protected SQLInterface $driverSQL;
|
2012-08-09 12:15:36 -04:00
|
|
|
|
2014-03-31 16:01:58 -04:00
|
|
|
/**
|
|
|
|
* Reference to util class
|
2015-11-10 20:58:32 -05:00
|
|
|
* @var AbstractUtil
|
2014-03-31 16:01:58 -04:00
|
|
|
*/
|
2020-04-10 20:53:52 -04:00
|
|
|
protected AbstractUtil $util;
|
2012-03-15 09:25:18 -04:00
|
|
|
|
2014-03-31 16:01:58 -04:00
|
|
|
/**
|
|
|
|
* Last query executed
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-04-10 20:53:52 -04:00
|
|
|
protected string $lastQuery = '';
|
2013-02-28 12:22:55 -05:00
|
|
|
|
2014-03-31 16:01:58 -04:00
|
|
|
/**
|
|
|
|
* Prefix to apply to table names
|
|
|
|
* @var string
|
|
|
|
*/
|
2020-04-10 20:53:52 -04:00
|
|
|
protected string $tablePrefix = '';
|
2012-09-27 13:41:29 -04:00
|
|
|
|
2014-04-24 16:25:04 -04:00
|
|
|
/**
|
|
|
|
* Whether the driver supports 'TRUNCATE'
|
2016-09-07 13:10:03 -04:00
|
|
|
* @var boolean
|
2014-04-24 16:25:04 -04:00
|
|
|
*/
|
2020-04-10 20:53:52 -04:00
|
|
|
protected bool $hasTruncate = 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
|
2016-10-13 21:55:23 -04:00
|
|
|
* @param array $driverOptions
|
2012-03-15 09:25:18 -04:00
|
|
|
*/
|
2018-01-24 13:14:03 -05:00
|
|
|
public function __construct(string $dsn, string $username=NULL, string $password=NULL, array $driverOptions=[])
|
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
|
2016-10-13 21:55:23 -04:00
|
|
|
$driverOptions[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
|
|
|
|
parent::__construct($dsn, $username, $password, $driverOptions);
|
2012-08-09 12:15:36 -04:00
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
$this->_loadSubClasses();
|
2014-04-24 15:32:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the subclasses for the driver
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-01-22 15:43:56 -05:00
|
|
|
protected function _loadSubClasses(): void
|
2014-04-24 15:32:09 -04:00
|
|
|
{
|
|
|
|
// Load the sql and util class for the driver
|
2018-01-22 15:43:56 -05:00
|
|
|
$thisClass = \get_class($this);
|
2016-10-13 21:55:23 -04:00
|
|
|
$nsArray = explode("\\", $thisClass);
|
|
|
|
array_pop($nsArray);
|
|
|
|
$driver = array_pop($nsArray);
|
|
|
|
$sqlClass = __NAMESPACE__ . "\\{$driver}\\SQL";
|
|
|
|
$utilClass = __NAMESPACE__ . "\\{$driver}\\Util";
|
2014-04-24 15:32:09 -04:00
|
|
|
|
2020-04-17 14:57:56 -04:00
|
|
|
$this->driverSQL = new $sqlClass();
|
2016-10-13 21:55:23 -04:00
|
|
|
$this->util = new $utilClass($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
|
|
|
*/
|
2016-10-12 22:12:25 -04:00
|
|
|
public function __call(string $name, array $args = [])
|
2014-04-10 15:54:43 -04:00
|
|
|
{
|
|
|
|
if (
|
|
|
|
isset($this->$name)
|
2018-01-22 15:43:56 -05:00
|
|
|
&& \is_object($this->$name)
|
2014-04-10 15:54:43 -04:00
|
|
|
&& method_exists($this->$name, '__invoke')
|
|
|
|
)
|
|
|
|
{
|
2018-01-22 15:43:56 -05:00
|
|
|
return \call_user_func_array([$this->$name, '__invoke'], $args);
|
2014-04-10 15:54:43 -04:00
|
|
|
}
|
2019-12-11 16:49:06 -05:00
|
|
|
|
|
|
|
return NULL;
|
2014-04-10 15:54:43 -04:00
|
|
|
}
|
|
|
|
|
2012-04-13 12:49:13 -04:00
|
|
|
// --------------------------------------------------------------------------
|
2015-07-30 16:40:30 -04:00
|
|
|
// ! Accessors / Mutators
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2016-09-07 17:39:19 -04:00
|
|
|
* Get the last sql query executed
|
2015-07-30 16:40:30 -04:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-10-13 21:55:23 -04:00
|
|
|
public function getLastQuery(): string
|
2015-07-30 16:40:30 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
return $this->lastQuery;
|
2015-07-30 16:40:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the last query sql
|
|
|
|
*
|
2016-10-13 21:55:23 -04:00
|
|
|
* @param string $queryString
|
2015-07-30 16:40:30 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
2018-01-22 15:43:56 -05:00
|
|
|
public function setLastQuery(string $queryString): void
|
2015-07-30 16:40:30 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$this->lastQuery = $queryString;
|
2015-07-30 16:40:30 -04:00
|
|
|
}
|
|
|
|
|
2014-04-23 15:53:16 -04:00
|
|
|
/**
|
|
|
|
* Get the SQL class for the current driver
|
|
|
|
*
|
2015-11-11 09:32:11 -05:00
|
|
|
* @return SQLInterface
|
2014-04-23 15:53:16 -04:00
|
|
|
*/
|
2016-10-13 21:55:23 -04:00
|
|
|
public function getSql(): SQLInterface
|
2014-04-23 15:53:16 -04:00
|
|
|
{
|
2020-04-17 14:57:56 -04:00
|
|
|
return $this->driverSQL;
|
2014-04-23 15:53:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the Util class for the current driver
|
|
|
|
*
|
2015-11-11 09:32:11 -05:00
|
|
|
* @return AbstractUtil
|
2014-04-23 15:53:16 -04:00
|
|
|
*/
|
2016-10-13 21:55:23 -04:00
|
|
|
public function getUtil(): AbstractUtil
|
2014-04-23 15:53:16 -04:00
|
|
|
{
|
|
|
|
return $this->util;
|
|
|
|
}
|
|
|
|
|
2015-07-30 16:40:30 -04:00
|
|
|
/**
|
|
|
|
* Set the common table name prefix
|
|
|
|
*
|
2015-07-31 10:56:16 -04:00
|
|
|
* @param string $prefix
|
2015-07-30 16:40:30 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
2018-01-24 13:14:03 -05:00
|
|
|
public function setTablePrefix(string $prefix): void
|
2015-07-30 16:40:30 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$this->tablePrefix = $prefix;
|
2015-07-30 16:40:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// ! Concrete functions that can be overridden in child classes
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2012-03-15 09:25:18 -04:00
|
|
|
/**
|
|
|
|
* Simplifies prepared statements for database queries
|
|
|
|
*
|
|
|
|
* @param string $sql
|
|
|
|
* @param array $data
|
2016-09-07 17:39:19 -04:00
|
|
|
* @return PDOStatement | FALSE
|
|
|
|
* @throws InvalidArgumentException
|
2012-03-15 09:25:18 -04:00
|
|
|
*/
|
2018-01-26 15:45:46 -05:00
|
|
|
public function prepareQuery(string $sql, array $data): PDOStatement
|
2012-03-15 09:25:18 -04:00
|
|
|
{
|
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
|
|
|
// 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
|
2015-11-11 09:25:21 -05:00
|
|
|
if(is_numeric($k))
|
|
|
|
{
|
|
|
|
$k++;
|
|
|
|
}
|
2014-04-01 15:41:38 -04:00
|
|
|
$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
|
2018-01-24 13:14:03 -05:00
|
|
|
* @throws InvalidArgumentException
|
2016-09-07 17:39:19 -04:00
|
|
|
* @return PDOStatement
|
2012-03-15 09:25:18 -04:00
|
|
|
*/
|
2018-01-26 15:45:46 -05:00
|
|
|
public function prepareExecute(string $sql, array $params): PDOStatement
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$this->statement = $this->prepareQuery($sql, $params);
|
2012-03-15 09:25:18 -04:00
|
|
|
$this->statement->execute();
|
|
|
|
|
|
|
|
return $this->statement;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns number of rows affected by an INSERT, UPDATE, DELETE type query
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
2018-01-22 15:43:56 -05:00
|
|
|
public function affectedRows(): int
|
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-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
|
|
|
|
*/
|
2018-01-24 13:14:03 -05:00
|
|
|
public function prefixTable(string $table): string
|
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
|
2016-10-13 21:55:23 -04:00
|
|
|
if ( ! empty($this->tablePrefix))
|
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
|
2016-10-13 21:55:23 -04:00
|
|
|
$identifiers = explode('.', $table);
|
|
|
|
$segments = count($identifiers);
|
2014-04-01 15:41:38 -04:00
|
|
|
|
|
|
|
// Quote the last item, and add the database prefix
|
2016-10-13 21:55:23 -04:00
|
|
|
$identifiers[$segments - 1] = $this->_prefix(end($identifiers));
|
2014-04-01 15:41:38 -04:00
|
|
|
|
|
|
|
// Rejoin
|
2016-10-13 21:55:23 -04:00
|
|
|
$table = implode('.', $identifiers);
|
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
|
|
|
|
*/
|
2018-01-22 15:43:56 -05:00
|
|
|
public function quoteTable($table): string
|
2014-04-08 17:13:41 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$table = $this->prefixTable($table);
|
2014-04-08 17:13:41 -04:00
|
|
|
|
2012-11-07 08:42:34 -05:00
|
|
|
// Finally, quote the table
|
2016-10-13 21:55:23 -04:00
|
|
|
return $this->quoteIdent($table);
|
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
|
|
|
|
*
|
2016-09-07 17:39:19 -04:00
|
|
|
* @param mixed $identifier
|
2018-01-22 15:43:56 -05:00
|
|
|
* @return string|array
|
2012-03-15 09:25:18 -04:00
|
|
|
*/
|
2016-10-13 21:55:23 -04:00
|
|
|
public function quoteIdent($identifier)
|
2014-02-25 13:55:19 -05:00
|
|
|
{
|
2019-12-10 12:17:40 -05:00
|
|
|
if (is_array($identifier))
|
2012-03-15 09:25:18 -04:00
|
|
|
{
|
2016-09-07 17:39:19 -04:00
|
|
|
return array_map([$this, __METHOD__], $identifier);
|
2012-03-15 09:25:18 -04:00
|
|
|
}
|
2012-08-09 12:15:36 -04:00
|
|
|
|
2020-04-23 17:00:35 -04:00
|
|
|
// Make all the string-handling methods happy
|
|
|
|
$identifier = (string)$identifier;
|
|
|
|
|
2012-08-22 21:17:27 -04:00
|
|
|
// Handle comma-separated identifiers
|
2020-04-23 17:00:35 -04:00
|
|
|
if (strpos($identifier, ',') !== FALSE)
|
2012-05-01 14:16:03 -04:00
|
|
|
{
|
2016-09-07 17:39:19 -04:00
|
|
|
$parts = array_map('mb_trim', explode(',', $identifier));
|
2016-09-07 13:10:03 -04:00
|
|
|
$parts = array_map([$this, __METHOD__], $parts);
|
2016-09-07 17:39:19 -04:00
|
|
|
$identifier = implode(',', $parts);
|
2012-05-01 14:16:03 -04:00
|
|
|
}
|
2012-03-15 09:25:18 -04:00
|
|
|
|
|
|
|
// Split each identifier by the period
|
2016-09-07 17:39:19 -04:00
|
|
|
$hiers = explode('.', $identifier);
|
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
|
2016-09-07 13:10:03 -04:00
|
|
|
$raw = implode('.', array_map([$this, '_quote'], $hiers));
|
2013-04-25 08:15:56 -04:00
|
|
|
|
|
|
|
// Fix functions
|
2016-09-07 13:10:03 -04:00
|
|
|
$funcs = [];
|
2016-10-13 21:55:23 -04:00
|
|
|
preg_match_all("#{$this->escapeCharOpen}([a-zA-Z0-9_]+(\((.*?)\))){$this->escapeCharClose}#iu", $raw, $funcs, PREG_SET_ORDER);
|
2013-04-25 08:15:56 -04:00
|
|
|
foreach($funcs as $f)
|
|
|
|
{
|
|
|
|
// Unquote the function
|
|
|
|
// Quote the inside identifiers
|
2019-12-11 16:49:06 -05:00
|
|
|
$raw = str_replace(array($f[0], $f[3]), array($f[1], $this->quoteIdent($f[3])), $raw);
|
2013-04-25 08:15:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
*/
|
2018-01-22 15:43:56 -05:00
|
|
|
public function getSchemas(): ?array
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2019-12-11 16:49:06 -05:00
|
|
|
// Most DBMSs conflate schemas and databases
|
|
|
|
return $this->getDbs();
|
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
|
|
|
|
*/
|
2018-01-22 15:43:56 -05:00
|
|
|
public function getTables(): ?array
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$tables = $this->driverQuery('tableList');
|
2014-04-28 16:41:46 -04:00
|
|
|
natsort($tables);
|
|
|
|
return $tables;
|
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
|
|
|
|
*/
|
2019-12-11 16:49:06 -05:00
|
|
|
public function getDbs(): ?array
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
return $this->driverQuery('dbList');
|
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
|
|
|
*/
|
2018-01-22 15:43:56 -05:00
|
|
|
public function getViews(): ?array
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$views = $this->driverQuery('viewList');
|
2014-04-28 16:41:46 -04:00
|
|
|
sort($views);
|
|
|
|
return $views;
|
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
|
|
|
*/
|
2018-01-22 15:43:56 -05:00
|
|
|
public function getSequences(): ?array
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
return $this->driverQuery('sequenceList');
|
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
|
|
|
|
*/
|
2018-01-22 15:43:56 -05:00
|
|
|
public function getFunctions(): ?array
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
return $this->driverQuery('functionList', 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
|
|
|
*/
|
2018-01-22 15:43:56 -05:00
|
|
|
public function getProcedures(): ?array
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
return $this->driverQuery('procedureList', 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
|
|
|
*/
|
2018-01-22 15:43:56 -05:00
|
|
|
public function getTriggers(): ?array
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
return $this->driverQuery('triggerList', FALSE);
|
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
|
|
|
*/
|
2018-01-22 15:43:56 -05:00
|
|
|
public function getSystemTables(): ?array
|
2012-04-10 14:06:34 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
return $this->driverQuery('systemTableList');
|
2012-04-10 14:06:34 -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
|
|
|
|
*/
|
2019-12-11 16:49:06 -05:00
|
|
|
public function getColumns(string $table): ?array
|
2012-05-09 13:54:38 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
return $this->driverQuery($this->getSql()->columnList($this->prefixTable($table)), FALSE);
|
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
|
|
|
|
*/
|
2019-12-11 16:49:06 -05:00
|
|
|
public function getFks(string $table): ?array
|
2014-04-07 16:49:49 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
return $this->driverQuery($this->getSql()->fkList($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
|
|
|
|
*/
|
2019-12-11 16:49:06 -05:00
|
|
|
public function getIndexes(string $table): ?array
|
2014-04-08 14:26:28 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
return $this->driverQuery($this->getSql()->indexList($this->prefixTable($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
|
|
|
|
*/
|
2018-01-22 15:43:56 -05:00
|
|
|
public function getTypes(): ?array
|
2012-05-09 13:54:38 -04:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
return $this->driverQuery('typeList', FALSE);
|
2012-05-09 13:54:38 -04:00
|
|
|
}
|
2012-04-10 14:06:34 -04:00
|
|
|
|
2019-12-13 15:11:49 -05:00
|
|
|
/**
|
|
|
|
* Get the version of the database engine
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getVersion(): string
|
|
|
|
{
|
|
|
|
return $this->getAttribute(PDO::ATTR_SERVER_VERSION);
|
|
|
|
}
|
|
|
|
|
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
|
2016-10-13 21:55:23 -04:00
|
|
|
* @param bool $filteredIndex
|
2018-01-22 15:43:56 -05:00
|
|
|
* @return array|null
|
2012-04-19 11:42:50 -04:00
|
|
|
*/
|
2018-01-22 15:43:56 -05:00
|
|
|
public function driverQuery($query, $filteredIndex=TRUE): ?array
|
2012-04-19 11:42:50 -04:00
|
|
|
{
|
2014-04-03 13:28:30 -04:00
|
|
|
// Call the appropriate method, if it exists
|
2020-04-17 14:57:56 -04:00
|
|
|
if (is_string($query) && method_exists($this->driverSQL, $query))
|
2014-02-25 13:55:19 -05:00
|
|
|
{
|
2016-10-13 21:55:23 -04:00
|
|
|
$query = $this->getSql()->$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
|
2019-12-10 12:17:40 -05:00
|
|
|
if ( ! is_string($query))
|
2015-11-11 09:25:21 -05:00
|
|
|
{
|
|
|
|
return $query;
|
|
|
|
}
|
2014-04-03 13:28:30 -04:00
|
|
|
|
|
|
|
// Run the query!
|
|
|
|
$res = $this->query($query);
|
2012-04-19 11:42:50 -04:00
|
|
|
|
2018-01-22 15:43:56 -05:00
|
|
|
$flag = $filteredIndex ? PDO::FETCH_NUM : PDO::FETCH_ASSOC;
|
2012-04-19 11:42:50 -04:00
|
|
|
$all = $res->fetchAll($flag);
|
|
|
|
|
2019-12-10 12:17:40 -05:00
|
|
|
return $filteredIndex ? dbFilter($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
|
2016-09-07 17:39:19 -04:00
|
|
|
* @return int|null
|
2012-04-20 16:33:40 -04:00
|
|
|
*/
|
2018-01-22 15:43:56 -05:00
|
|
|
public function numRows(): ?int
|
2012-04-20 16:33:40 -04:00
|
|
|
{
|
2012-09-27 13:41:29 -04:00
|
|
|
$regex = '/^SELECT\s+(?:ALL\s+|DISTINCT\s+)?(?:.*?)\s+FROM\s+(.*)$/i';
|
2016-09-07 13:10:03 -04:00
|
|
|
$output = [];
|
2012-09-27 13:41:29 -04:00
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
if (preg_match($regex, $this->lastQuery, $output) > 0)
|
2012-09-27 13:41:29 -04:00
|
|
|
{
|
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
|
|
|
|
2014-02-25 13:55:19 -05:00
|
|
|
/**
|
2013-05-03 13:07:34 -04:00
|
|
|
* Create sql for batch insert
|
|
|
|
*
|
|
|
|
* @param string $table
|
2018-01-24 13:14:03 -05:00
|
|
|
* @param mixed $data
|
2019-12-10 12:17:40 -05:00
|
|
|
* @return array<string|array|null>
|
2013-05-03 13:07:34 -04:00
|
|
|
*/
|
2018-01-24 13:14:03 -05:00
|
|
|
public function insertBatch(string $table, array $data=[]): array
|
2013-05-03 13:07:34 -04:00
|
|
|
{
|
2016-09-07 17:39:19 -04:00
|
|
|
$data = (array) $data;
|
2018-01-22 16:04:29 -05:00
|
|
|
$firstRow = (array) current($data);
|
2014-02-25 13:55:19 -05:00
|
|
|
|
2014-04-23 17:03:46 -04:00
|
|
|
// Values for insertion
|
2016-09-07 13:10:03 -04:00
|
|
|
$vals = [];
|
2014-04-23 17:03:46 -04:00
|
|
|
foreach($data as $group)
|
|
|
|
{
|
|
|
|
$vals = array_merge($vals, array_values($group));
|
|
|
|
}
|
2019-12-10 12:17:40 -05:00
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
$table = $this->quoteTable($table);
|
|
|
|
$fields = array_keys($firstRow);
|
2014-02-25 13:55:19 -05:00
|
|
|
|
2014-04-01 15:41:38 -04:00
|
|
|
$sql = "INSERT INTO {$table} ("
|
2016-10-13 21:55:23 -04:00
|
|
|
. implode(',', $this->quoteIdent($fields))
|
2018-01-24 15:03:41 -05:00
|
|
|
. ') 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), '?');
|
2018-01-22 15:43:56 -05:00
|
|
|
$paramString = '(' . implode(',', $params) . ')';
|
2016-10-13 21:55:23 -04:00
|
|
|
$paramList = array_fill(0, count($data), $paramString);
|
2014-02-25 13:55:19 -05:00
|
|
|
|
2014-04-23 17:03:46 -04:00
|
|
|
// Append the placeholder groups to the query
|
2016-10-13 21:55:23 -04:00
|
|
|
$sql .= implode(',', $paramList);
|
2014-04-01 15:41:38 -04:00
|
|
|
|
2016-09-07 13:10:03 -04:00
|
|
|
return [$sql, $vals];
|
2013-05-03 13:07:34 -04:00
|
|
|
}
|
2014-04-24 14:50:53 -04:00
|
|
|
|
2016-09-07 17:39:19 -04:00
|
|
|
/**
|
|
|
|
* Creates a batch update, and executes it.
|
|
|
|
* Returns the number of affected rows
|
|
|
|
*
|
2018-01-26 08:39:30 -05:00
|
|
|
* @param string $table The table to update
|
|
|
|
* @param array $data an array of update values
|
|
|
|
* @param string $where The where key
|
|
|
|
* @return array<string,array,int>
|
2016-09-07 17:39:19 -04:00
|
|
|
*/
|
2018-01-26 08:39:30 -05:00
|
|
|
public function updateBatch(string $table, array $data, string $where): array
|
2016-09-07 17:39:19 -04:00
|
|
|
{
|
2018-01-26 08:39:30 -05:00
|
|
|
$affectedRows = 0;
|
|
|
|
$insertData = [];
|
|
|
|
$fieldLines = [];
|
|
|
|
|
|
|
|
$sql = 'UPDATE ' . $this->quoteTable($table) . ' SET ';
|
|
|
|
|
|
|
|
// Get the keys of the current set of data, except the one used to
|
|
|
|
// set the update condition
|
|
|
|
$fields = array_unique(
|
2019-12-10 12:17:40 -05:00
|
|
|
array_reduce($data, static function ($previous, $current) use (&$affectedRows, $where) {
|
2018-02-09 16:14:40 -05:00
|
|
|
$affectedRows++;
|
2018-01-26 08:39:30 -05:00
|
|
|
$keys = array_diff(array_keys($current), [$where]);
|
|
|
|
|
2018-02-09 16:14:40 -05:00
|
|
|
if ($previous === NULL)
|
|
|
|
{
|
|
|
|
return $keys;
|
|
|
|
}
|
2018-01-26 08:39:30 -05:00
|
|
|
|
2018-02-09 16:14:40 -05:00
|
|
|
return array_merge($previous, $keys);
|
|
|
|
})
|
2018-01-26 08:39:30 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
// Create the CASE blocks for each data set
|
|
|
|
foreach ($fields as $field)
|
|
|
|
{
|
|
|
|
$line = $this->quoteIdent($field) . " = CASE\n";
|
|
|
|
|
|
|
|
$cases = [];
|
|
|
|
foreach ($data as $case)
|
|
|
|
{
|
|
|
|
if (array_key_exists($field, $case))
|
|
|
|
{
|
|
|
|
$insertData[] = $case[$where];
|
|
|
|
$insertData[] = $case[$field];
|
|
|
|
$cases[] = 'WHEN ' . $this->quoteIdent($where) . ' =? '
|
|
|
|
. 'THEN ? ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$line .= implode("\n", $cases) . "\n";
|
|
|
|
$line .= 'ELSE ' . $this->quoteIdent($field) . ' END';
|
|
|
|
|
|
|
|
$fieldLines[] = $line;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql .= implode(",\n", $fieldLines) . "\n";
|
|
|
|
|
|
|
|
$whereValues = array_column($data, $where);
|
|
|
|
foreach ($whereValues as $value)
|
|
|
|
{
|
|
|
|
$insertData[] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the placeholders for the WHERE IN clause
|
|
|
|
$placeholders = array_fill(0, count($whereValues), '?');
|
|
|
|
|
|
|
|
$sql .= 'WHERE ' . $this->quoteIdent($where) . ' IN ';
|
|
|
|
$sql .= '(' . implode(',', $placeholders) . ')';
|
|
|
|
|
|
|
|
return [$sql, $insertData, $affectedRows];
|
2016-09-07 17:39:19 -04:00
|
|
|
}
|
|
|
|
|
2018-01-24 13:14:03 -05:00
|
|
|
/**
|
|
|
|
* Empty the passed table
|
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
* @return PDOStatement
|
|
|
|
*/
|
2018-01-26 15:45:46 -05:00
|
|
|
public function truncate(string $table): PDOStatement
|
2018-01-24 13:14:03 -05:00
|
|
|
{
|
|
|
|
$sql = $this->hasTruncate
|
|
|
|
? 'TRUNCATE TABLE '
|
|
|
|
: 'DELETE FROM ';
|
|
|
|
|
|
|
|
$sql .= $this->quoteTable($table);
|
|
|
|
|
|
|
|
$this->statement = $this->query($sql);
|
|
|
|
return $this->statement;
|
|
|
|
}
|
|
|
|
|
2019-12-13 15:11:49 -05:00
|
|
|
/**
|
|
|
|
* Generate the returning clause for the current database
|
|
|
|
*
|
|
|
|
* @param string $query
|
|
|
|
* @param string $select
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function returning(string $query, string $select): string
|
|
|
|
{
|
|
|
|
return "{$query} RETURNING {$select}";
|
|
|
|
}
|
|
|
|
|
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 (
|
2020-04-17 14:57:56 -04:00
|
|
|
is_string($str)
|
2016-10-13 21:55:23 -04:00
|
|
|
&& strpos($str, $this->escapeCharOpen) !== 0
|
|
|
|
&& strrpos($str, $this->escapeCharClose) !== 0
|
2014-04-24 14:50:53 -04:00
|
|
|
)
|
2016-10-13 21:55:23 -04:00
|
|
|
? "{$this->escapeCharOpen}{$str}{$this->escapeCharClose}"
|
2014-04-24 14:50:53 -04:00
|
|
|
: $str;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the table prefix on the passed string
|
|
|
|
*
|
|
|
|
* @param string $str
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-01-24 13:14:03 -05:00
|
|
|
protected function _prefix(string $str): string
|
2014-04-24 14:50:53 -04:00
|
|
|
{
|
|
|
|
// Don't prefix an already prefixed table
|
2016-10-13 21:55:23 -04:00
|
|
|
if (strpos($str, $this->tablePrefix) !== FALSE)
|
2014-04-24 14:50:53 -04:00
|
|
|
{
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
2016-10-13 21:55:23 -04:00
|
|
|
return $this->tablePrefix . $str;
|
2014-04-24 14:50:53 -04:00
|
|
|
}
|
2019-12-10 12:17:40 -05:00
|
|
|
}
|