2013-06-21 00:32:08 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PDO driver database connector
|
|
|
|
* @package framework
|
|
|
|
* @subpackage model
|
|
|
|
*/
|
|
|
|
class PDOConnector extends DBConnector {
|
2014-08-01 07:56:21 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
|
|
|
* Should ATTR_EMULATE_PREPARES flag be used to emulate prepared statements?
|
2014-08-01 07:56:21 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @config
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
private static $emulate_prepare = false;
|
2014-08-01 07:56:21 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
|
|
|
* The PDO connection instance
|
2014-08-01 07:56:21 +02:00
|
|
|
*
|
|
|
|
* @var PDO
|
2013-06-21 00:32:08 +02:00
|
|
|
*/
|
|
|
|
protected $pdoConnection = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of the currently selected database
|
2014-08-01 07:56:21 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $databaseName = null;
|
|
|
|
|
|
|
|
/**
|
2015-06-16 05:04:20 +02:00
|
|
|
* If available, the row count of the last executed statement
|
2014-08-01 07:56:21 +02:00
|
|
|
*
|
2015-06-16 05:04:20 +02:00
|
|
|
* @var int|null
|
2013-06-21 00:32:08 +02:00
|
|
|
*/
|
2015-06-16 05:04:20 +02:00
|
|
|
protected $rowCount = null;
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-16 05:04:20 +02:00
|
|
|
/**
|
|
|
|
* Error generated by the errorInfo() method of the last PDOStatement
|
|
|
|
*
|
2016-01-06 00:34:58 +01:00
|
|
|
* @var array|null
|
2015-06-16 05:04:20 +02:00
|
|
|
*/
|
|
|
|
protected $lastStatementError = null;
|
2014-08-01 07:56:21 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
|
|
|
* List of prepared statements, cached by SQL string
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $cachedStatements = array();
|
2014-08-01 07:56:21 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
|
|
|
* Flush all prepared statements
|
|
|
|
*/
|
|
|
|
public function flushStatements() {
|
|
|
|
$this->cachedStatements = array();
|
|
|
|
}
|
2014-08-01 07:56:21 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
|
|
|
* Retrieve a prepared statement for a given SQL string, or return an already prepared version if
|
|
|
|
* one exists for the given query
|
2014-08-01 07:56:21 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param string $sql
|
|
|
|
* @return PDOStatement
|
|
|
|
*/
|
|
|
|
public function getOrPrepareStatement($sql) {
|
2015-06-16 05:04:20 +02:00
|
|
|
// Return cached statements
|
|
|
|
if(isset($this->cachedStatements[$sql])) {
|
|
|
|
return $this->cachedStatements[$sql];
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-16 05:04:20 +02:00
|
|
|
// Generate new statement
|
|
|
|
$statement = $this->pdoConnection->prepare(
|
|
|
|
$sql,
|
|
|
|
array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY)
|
|
|
|
);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-16 05:04:20 +02:00
|
|
|
// Only cache select statements
|
|
|
|
if(preg_match('/^(\s*)select\b/i', $sql)) {
|
|
|
|
$this->cachedStatements[$sql] = $statement;
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
2015-06-16 05:04:20 +02:00
|
|
|
return $statement;
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
2014-08-01 07:56:21 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
|
|
|
* Is PDO running in emulated mode
|
2014-08-01 07:56:21 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public static function is_emulate_prepare() {
|
|
|
|
return Config::inst()->get('PDOConnector', 'emulate_prepare');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function connect($parameters, $selectDB = false) {
|
|
|
|
$this->flushStatements();
|
|
|
|
|
|
|
|
// Build DSN string
|
|
|
|
// Note that we don't select the database here until explicitly
|
|
|
|
// requested via selectDatabase
|
|
|
|
$driver = $parameters['driver'] . ":";
|
|
|
|
$dsn = array();
|
2014-08-01 07:56:21 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Typically this is false, but some drivers will request this
|
|
|
|
if($selectDB) {
|
|
|
|
// Specify complete file path immediately following driver (SQLLite3)
|
|
|
|
if(!empty($parameters['filepath'])) {
|
|
|
|
$dsn[] = $parameters['filepath'];
|
|
|
|
} elseif(!empty($parameters['database'])) {
|
|
|
|
// Some databases require a selected database at connection (SQLite3, Azure)
|
2014-08-01 07:56:21 +02:00
|
|
|
if($parameters['driver'] === 'sqlsrv') {
|
2013-06-21 00:32:08 +02:00
|
|
|
$dsn[] = "Database={$parameters['database']}";
|
|
|
|
} else {
|
|
|
|
$dsn[] = "dbname={$parameters['database']}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-01 07:56:21 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Syntax for sql server is slightly different
|
|
|
|
if($parameters['driver'] === 'sqlsrv') {
|
|
|
|
$server = $parameters['server'];
|
|
|
|
if (!empty($parameters['port'])) {
|
|
|
|
$server .= ",{$parameters['port']}";
|
|
|
|
}
|
|
|
|
$dsn[] = "Server=$server";
|
|
|
|
} else {
|
|
|
|
if (!empty($parameters['server'])) {
|
|
|
|
// Use Server instead of host for sqlsrv
|
|
|
|
$dsn[] = "host={$parameters['server']}";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($parameters['port'])) {
|
|
|
|
$dsn[] = "port={$parameters['port']}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set charset if given and not null. Can explicitly set to empty string to omit
|
|
|
|
if($parameters['driver'] !== 'sqlsrv') {
|
|
|
|
$charset = isset($parameters['charset'])
|
|
|
|
? $parameters['charset']
|
|
|
|
: 'utf8';
|
|
|
|
if (!empty($charset)) $dsn[] = "charset=$charset";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Connection commands to be run on every re-connection
|
|
|
|
$options = array(
|
2014-08-01 07:56:21 +02:00
|
|
|
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
|
2013-06-21 00:32:08 +02:00
|
|
|
);
|
2014-08-01 07:56:21 +02:00
|
|
|
if(self::is_emulate_prepare()) {
|
|
|
|
$options[PDO::ATTR_EMULATE_PREPARES] = true;
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
|
|
|
// May throw a PDOException if fails
|
2014-08-01 07:56:21 +02:00
|
|
|
$this->pdoConnection = new PDO(
|
|
|
|
$driver.implode(';', $dsn),
|
|
|
|
empty($parameters['username']) ? '' : $parameters['username'],
|
|
|
|
empty($parameters['password']) ? '' : $parameters['password'],
|
|
|
|
$options
|
|
|
|
);
|
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Show selected DB if requested
|
|
|
|
if($this->pdoConnection && $selectDB && !empty($parameters['database'])) {
|
|
|
|
$this->databaseName = $parameters['database'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getVersion() {
|
|
|
|
return $this->pdoConnection->getAttribute(PDO::ATTR_SERVER_VERSION);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function escapeString($value) {
|
|
|
|
$value = $this->quoteString($value);
|
|
|
|
|
|
|
|
// Since the PDO library quotes the value, we should remove this to maintain
|
|
|
|
// consistency with MySQLDatabase::escapeString
|
|
|
|
if (preg_match('/^\'(?<value>.*)\'$/', $value, $matches)) {
|
|
|
|
$value = $matches['value'];
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function quoteString($value) {
|
|
|
|
return $this->pdoConnection->quote($value);
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-16 05:04:20 +02:00
|
|
|
/**
|
|
|
|
* Invoked before any query is executed
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2015-06-16 05:04:20 +02:00
|
|
|
* @param string $sql
|
|
|
|
*/
|
|
|
|
protected function beforeQuery($sql) {
|
|
|
|
// Reset state
|
|
|
|
$this->rowCount = 0;
|
|
|
|
$this->lastStatementError = null;
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-16 05:04:20 +02:00
|
|
|
// Flush if necessary
|
|
|
|
if($this->isQueryDDL($sql)) {
|
|
|
|
$this->flushStatements();
|
|
|
|
}
|
|
|
|
}
|
2014-08-01 07:56:21 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
|
|
|
* Executes a query that doesn't return a resultset
|
2014-08-01 07:56:21 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param string $sql The SQL query to execute
|
|
|
|
* @param integer $errorLevel For errors to this query, raise PHP errors
|
|
|
|
* using this error level.
|
2015-06-16 05:04:20 +02:00
|
|
|
* @return int
|
2013-06-21 00:32:08 +02:00
|
|
|
*/
|
|
|
|
public function exec($sql, $errorLevel = E_USER_ERROR) {
|
2015-06-16 05:04:20 +02:00
|
|
|
$this->beforeQuery($sql);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-16 05:04:20 +02:00
|
|
|
// Directly exec this query
|
|
|
|
$result = $this->pdoConnection->exec($sql);
|
2013-06-21 00:32:08 +02:00
|
|
|
|
|
|
|
// Check for errors
|
2015-06-16 05:04:20 +02:00
|
|
|
if ($result !== false) {
|
|
|
|
return $this->rowCount = $result;
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-16 05:04:20 +02:00
|
|
|
$this->databaseError($this->getLastError(), $errorLevel, $sql);
|
|
|
|
return null;
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function query($sql, $errorLevel = E_USER_ERROR) {
|
2015-06-16 05:04:20 +02:00
|
|
|
$this->beforeQuery($sql);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-16 05:04:20 +02:00
|
|
|
// Directly query against connection
|
|
|
|
$statement = $this->pdoConnection->query($sql);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-16 05:04:20 +02:00
|
|
|
// Generate results
|
|
|
|
return $this->prepareResults($statement, $errorLevel, $sql);
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
2014-08-01 07:56:21 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
|
|
|
* Determines the PDO::PARAM_* type for a given PHP type string
|
|
|
|
* @param string $phpType Type of object in PHP
|
|
|
|
* @return integer PDO Parameter constant value
|
|
|
|
*/
|
|
|
|
public function getPDOParamType($phpType) {
|
|
|
|
switch($phpType) {
|
|
|
|
case 'boolean':
|
|
|
|
return PDO::PARAM_BOOL;
|
|
|
|
case 'NULL':
|
|
|
|
return PDO::PARAM_NULL;
|
|
|
|
case 'integer':
|
|
|
|
return PDO::PARAM_INT;
|
|
|
|
case 'object': // Allowed if the object or resource has a __toString method
|
|
|
|
case 'resource':
|
|
|
|
case 'float': // Not actually returnable from get_type
|
|
|
|
case 'double':
|
|
|
|
case 'string':
|
|
|
|
return PDO::PARAM_STR;
|
|
|
|
case 'blob':
|
|
|
|
return PDO::PARAM_LOB;
|
|
|
|
case 'array':
|
|
|
|
case 'unknown type':
|
|
|
|
default:
|
|
|
|
user_error("Cannot bind parameter as it is an unsupported type ($phpType)", E_USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
2014-08-01 07:56:21 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
|
|
|
* Bind all parameters to a PDOStatement
|
2014-08-01 07:56:21 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param PDOStatement $statement
|
|
|
|
* @param array $parameters
|
|
|
|
*/
|
|
|
|
public function bindParameters(PDOStatement $statement, $parameters) {
|
|
|
|
// Bind all parameters
|
|
|
|
for($index = 0; $index < count($parameters); $index++) {
|
|
|
|
$value = $parameters[$index];
|
|
|
|
$phpType = gettype($value);
|
|
|
|
|
|
|
|
// Allow overriding of parameter type using an associative array
|
|
|
|
if($phpType === 'array') {
|
|
|
|
$phpType = $value['type'];
|
|
|
|
$value = $value['value'];
|
|
|
|
}
|
2014-08-01 07:56:21 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// Check type of parameter
|
|
|
|
$type = $this->getPDOParamType($phpType);
|
|
|
|
if($type === PDO::PARAM_STR) $value = strval($value);
|
|
|
|
|
|
|
|
// Bind this value
|
|
|
|
$statement->bindValue($index+1, $value, $type);
|
|
|
|
}
|
|
|
|
}
|
2014-08-01 07:56:21 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
public function preparedQuery($sql, $parameters, $errorLevel = E_USER_ERROR) {
|
2015-06-16 05:04:20 +02:00
|
|
|
$this->beforeQuery($sql);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-16 05:04:20 +02:00
|
|
|
// Prepare statement
|
|
|
|
$statement = $this->getOrPrepareStatement($sql);
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-16 05:04:20 +02:00
|
|
|
// Bind and invoke statement safely
|
|
|
|
if($statement) {
|
|
|
|
$this->bindParameters($statement, $parameters);
|
2013-06-21 00:32:08 +02:00
|
|
|
$statement->execute($parameters);
|
2015-06-16 05:04:20 +02:00
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-16 05:04:20 +02:00
|
|
|
// Generate results
|
|
|
|
return $this->prepareResults($statement, $errorLevel, $sql);
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-16 05:04:20 +02:00
|
|
|
/**
|
|
|
|
* Given a PDOStatement that has just been executed, generate results
|
|
|
|
* and report any errors
|
2016-01-06 00:34:58 +01:00
|
|
|
*
|
2015-06-16 05:04:20 +02:00
|
|
|
* @param PDOStatement $statement
|
|
|
|
* @param int $errorLevel
|
|
|
|
* @param string $sql
|
|
|
|
* @param array $parameters
|
|
|
|
* @return \PDOQuery
|
|
|
|
*/
|
|
|
|
protected function prepareResults($statement, $errorLevel, $sql, $parameters = array()) {
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-16 05:04:20 +02:00
|
|
|
// Record row-count and errors of last statement
|
|
|
|
if($this->hasError($statement)) {
|
|
|
|
$this->lastStatementError = $statement->errorInfo();
|
|
|
|
} elseif($statement) {
|
|
|
|
// Count and return results
|
|
|
|
$this->rowCount = $statement->rowCount();
|
|
|
|
return new PDOQuery($statement);
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
|
|
|
|
2015-06-16 05:04:20 +02:00
|
|
|
// Ensure statement is closed
|
|
|
|
if($statement) {
|
|
|
|
$statement->closeCursor();
|
|
|
|
unset($statement);
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2015-06-16 05:04:20 +02:00
|
|
|
// Report any errors
|
|
|
|
if($parameters) {
|
|
|
|
$parameters = $this->parameterValues($parameters);
|
|
|
|
}
|
|
|
|
$this->databaseError($this->getLastError(), $errorLevel, $sql, $parameters);
|
|
|
|
return null;
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
2014-08-01 07:56:21 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
/**
|
|
|
|
* Determine if a resource has an attached error
|
2014-08-01 07:56:21 +02:00
|
|
|
*
|
2013-06-21 00:32:08 +02:00
|
|
|
* @param PDOStatement|PDO $resource the resource to check
|
|
|
|
* @return boolean Flag indicating true if the resource has an error
|
|
|
|
*/
|
|
|
|
protected function hasError($resource) {
|
|
|
|
// No error if no resource
|
2015-06-16 05:04:20 +02:00
|
|
|
if(empty($resource)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-08-01 07:56:21 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
// If the error code is empty the statement / connection has not been run yet
|
|
|
|
$code = $resource->errorCode();
|
2015-06-16 05:04:20 +02:00
|
|
|
if(empty($code)) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-21 00:32:08 +02:00
|
|
|
|
|
|
|
// Skip 'ok' and undefined 'warning' types.
|
|
|
|
// @see http://docstore.mik.ua/orelly/java-ent/jenut/ch08_06.htm
|
|
|
|
return $code !== '00000' && $code !== '01000';
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
public function getLastError() {
|
2015-06-16 05:04:20 +02:00
|
|
|
$error = null;
|
|
|
|
if ($this->lastStatementError) {
|
|
|
|
$error = $this->lastStatementError;
|
2013-06-21 00:32:08 +02:00
|
|
|
} elseif($this->hasError($this->pdoConnection)) {
|
|
|
|
$error = $this->pdoConnection->errorInfo();
|
|
|
|
}
|
2015-06-16 05:04:20 +02:00
|
|
|
if ($error) {
|
2013-06-21 00:32:08 +02:00
|
|
|
return sprintf("%s-%s: %s", $error[0], $error[1], $error[2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getGeneratedID($table) {
|
|
|
|
return $this->pdoConnection->lastInsertId();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function affectedRows() {
|
2015-06-16 05:04:20 +02:00
|
|
|
return $this->rowCount;
|
2013-06-21 00:32:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function selectDatabase($name) {
|
|
|
|
$this->exec("USE \"{$name}\"");
|
|
|
|
$this->databaseName = $name;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSelectedDatabase() {
|
|
|
|
return $this->databaseName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function unloadDatabase() {
|
|
|
|
$this->databaseName = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isActive() {
|
|
|
|
return $this->databaseName && $this->pdoConnection;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|