2013-02-25 11:19:49 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\DB;
|
|
|
|
use Doctrine\DBAL\Driver;
|
|
|
|
use Doctrine\DBAL\Configuration;
|
|
|
|
use Doctrine\DBAL\Cache\QueryCacheProfile;
|
|
|
|
use Doctrine\Common\EventManager;
|
|
|
|
|
2013-10-22 00:51:56 +04:00
|
|
|
class Connection extends \Doctrine\DBAL\Connection {
|
2013-08-02 21:53:04 +04:00
|
|
|
/**
|
2013-08-08 00:22:33 +04:00
|
|
|
* @var string $tablePrefix
|
2013-08-02 21:53:04 +04:00
|
|
|
*/
|
2013-08-08 00:22:33 +04:00
|
|
|
protected $tablePrefix;
|
2013-02-25 11:19:49 +04:00
|
|
|
|
2013-08-02 21:53:04 +04:00
|
|
|
/**
|
|
|
|
* @var \OC\DB\Adapter $adapter
|
|
|
|
*/
|
2013-02-25 11:19:49 +04:00
|
|
|
protected $adapter;
|
|
|
|
|
2013-08-02 21:53:04 +04:00
|
|
|
/**
|
|
|
|
* @var \Doctrine\DBAL\Driver\Statement[] $preparedQueries
|
|
|
|
*/
|
2013-02-26 00:35:11 +04:00
|
|
|
protected $preparedQueries = array();
|
2013-08-02 21:53:04 +04:00
|
|
|
|
2013-02-26 00:35:11 +04:00
|
|
|
protected $cachingQueryStatementEnabled = true;
|
|
|
|
|
2013-02-25 11:19:49 +04:00
|
|
|
/**
|
|
|
|
* Initializes a new instance of the Connection class.
|
|
|
|
*
|
|
|
|
* @param array $params The connection parameters.
|
2013-08-02 21:53:04 +04:00
|
|
|
* @param \Doctrine\DBAL\Driver $driver
|
|
|
|
* @param \Doctrine\DBAL\Configuration $config
|
|
|
|
* @param \Doctrine\Common\EventManager $eventManager
|
|
|
|
* @throws \Exception
|
2013-02-25 11:19:49 +04:00
|
|
|
*/
|
|
|
|
public function __construct(array $params, Driver $driver, Configuration $config = null,
|
|
|
|
EventManager $eventManager = null)
|
|
|
|
{
|
|
|
|
if (!isset($params['adapter'])) {
|
2013-08-02 21:53:04 +04:00
|
|
|
throw new \Exception('adapter not set');
|
2013-02-25 11:19:49 +04:00
|
|
|
}
|
2013-08-08 00:22:33 +04:00
|
|
|
if (!isset($params['tablePrefix'])) {
|
|
|
|
throw new \Exception('tablePrefix not set');
|
2013-02-25 11:19:49 +04:00
|
|
|
}
|
2013-02-25 11:19:49 +04:00
|
|
|
parent::__construct($params, $driver, $config, $eventManager);
|
|
|
|
$this->adapter = new $params['adapter']($this);
|
2013-08-08 00:22:33 +04:00
|
|
|
$this->tablePrefix = $params['tablePrefix'];
|
2013-02-25 11:19:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepares an SQL statement.
|
|
|
|
*
|
|
|
|
* @param string $statement The SQL statement to prepare.
|
2013-08-02 21:53:04 +04:00
|
|
|
* @param int $limit
|
|
|
|
* @param int $offset
|
2013-02-25 11:19:49 +04:00
|
|
|
* @return \Doctrine\DBAL\Driver\Statement The prepared statement.
|
|
|
|
*/
|
|
|
|
public function prepare( $statement, $limit=null, $offset=null ) {
|
2013-02-26 00:46:03 +04:00
|
|
|
if ($limit === -1) {
|
|
|
|
$limit = null;
|
|
|
|
}
|
|
|
|
if (!is_null($limit)) {
|
|
|
|
$platform = $this->getDatabasePlatform();
|
|
|
|
$statement = $platform->modifyLimitQuery($statement, $limit, $offset);
|
2013-02-26 00:35:11 +04:00
|
|
|
} else {
|
|
|
|
if (isset($this->preparedQueries[$statement]) && $this->cachingQueryStatementEnabled) {
|
|
|
|
return $this->preparedQueries[$statement];
|
|
|
|
}
|
2013-08-07 23:01:31 +04:00
|
|
|
$origStatement = $statement;
|
2013-02-26 00:35:11 +04:00
|
|
|
}
|
2013-08-07 20:21:49 +04:00
|
|
|
$statement = $this->replaceTablePrefix($statement);
|
|
|
|
$statement = $this->adapter->fixupStatement($statement);
|
|
|
|
|
|
|
|
if(\OC_Config::getValue( 'log_query', false)) {
|
2013-02-26 01:49:55 +04:00
|
|
|
\OC_Log::write('core', 'DB prepare : '.$statement, \OC_Log::DEBUG);
|
|
|
|
}
|
2013-02-26 00:35:11 +04:00
|
|
|
$result = parent::prepare($statement);
|
2013-02-26 00:46:03 +04:00
|
|
|
if (is_null($limit) && $this->cachingQueryStatementEnabled) {
|
2013-08-07 23:01:31 +04:00
|
|
|
$this->preparedQueries[$origStatement] = $result;
|
2013-02-26 00:35:11 +04:00
|
|
|
}
|
|
|
|
return $result;
|
2013-02-25 11:19:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes an, optionally parameterized, SQL query.
|
|
|
|
*
|
|
|
|
* If the query is parameterized, a prepared statement is used.
|
|
|
|
* If an SQLLogger is configured, the execution is logged.
|
|
|
|
*
|
|
|
|
* @param string $query The SQL query to execute.
|
2014-02-06 19:30:58 +04:00
|
|
|
* @param string[] $params The parameters to bind to the query, if any.
|
2013-02-25 11:19:49 +04:00
|
|
|
* @param array $types The types the previous parameters are in.
|
|
|
|
* @param QueryCacheProfile $qcp
|
|
|
|
* @return \Doctrine\DBAL\Driver\Statement The executed statement.
|
|
|
|
* @internal PERF: Directly prepares a driver statement, not a wrapper.
|
|
|
|
*/
|
|
|
|
public function executeQuery($query, array $params = array(), $types = array(), QueryCacheProfile $qcp = null)
|
|
|
|
{
|
2013-03-22 21:36:40 +04:00
|
|
|
$query = $this->replaceTablePrefix($query);
|
2013-02-26 01:49:55 +04:00
|
|
|
$query = $this->adapter->fixupStatement($query);
|
2013-02-25 11:19:49 +04:00
|
|
|
return parent::executeQuery($query, $params, $types, $qcp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes an SQL INSERT/UPDATE/DELETE query with the given parameters
|
|
|
|
* and returns the number of affected rows.
|
|
|
|
*
|
|
|
|
* This method supports PDO binding types as well as DBAL mapping types.
|
|
|
|
*
|
|
|
|
* @param string $query The SQL query.
|
|
|
|
* @param array $params The query parameters.
|
|
|
|
* @param array $types The parameter types.
|
|
|
|
* @return integer The number of affected rows.
|
|
|
|
* @internal PERF: Directly prepares a driver statement, not a wrapper.
|
|
|
|
*/
|
|
|
|
public function executeUpdate($query, array $params = array(), array $types = array())
|
|
|
|
{
|
2013-03-22 21:36:40 +04:00
|
|
|
$query = $this->replaceTablePrefix($query);
|
2013-02-26 01:49:55 +04:00
|
|
|
$query = $this->adapter->fixupStatement($query);
|
2013-02-25 11:19:49 +04:00
|
|
|
return parent::executeUpdate($query, $params, $types);
|
|
|
|
}
|
2013-02-25 11:19:49 +04:00
|
|
|
|
2013-03-22 21:36:40 +04:00
|
|
|
/**
|
|
|
|
* Returns the ID of the last inserted row, or the last value from a sequence object,
|
|
|
|
* depending on the underlying driver.
|
|
|
|
*
|
|
|
|
* Note: This method may not return a meaningful or consistent result across different drivers,
|
|
|
|
* because the underlying database may not even support the notion of AUTO_INCREMENT/IDENTITY
|
|
|
|
* columns or sequences.
|
|
|
|
*
|
|
|
|
* @param string $seqName Name of the sequence object from which the ID should be returned.
|
|
|
|
* @return string A string representation of the last inserted ID.
|
|
|
|
*/
|
|
|
|
public function lastInsertId($seqName = null)
|
|
|
|
{
|
|
|
|
if ($seqName) {
|
|
|
|
$seqName = $this->replaceTablePrefix($seqName);
|
|
|
|
}
|
|
|
|
return $this->adapter->lastInsertId($seqName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// internal use
|
|
|
|
public function realLastInsertId($seqName = null)
|
|
|
|
{
|
|
|
|
return parent::lastInsertId($seqName);
|
|
|
|
}
|
|
|
|
|
2013-02-26 11:30:42 +04:00
|
|
|
/**
|
|
|
|
* @brief Insert a row if a matching row doesn't exists.
|
|
|
|
* @param string $table. The table to insert into in the form '*PREFIX*tableName'
|
|
|
|
* @param array $input. An array of fieldname/value pairs
|
2013-08-02 21:53:04 +04:00
|
|
|
* @return bool The return value from execute()
|
2013-02-26 11:30:42 +04:00
|
|
|
*/
|
|
|
|
public function insertIfNotExist($table, $input) {
|
|
|
|
return $this->adapter->insertIfNotExist($table, $input);
|
|
|
|
}
|
|
|
|
|
2013-07-01 20:20:27 +04:00
|
|
|
/**
|
|
|
|
* returns the error code and message as a string for logging
|
|
|
|
* works with DoctrineException
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getError() {
|
|
|
|
$msg = $this->errorCode() . ': ';
|
|
|
|
$errorInfo = $this->errorInfo();
|
|
|
|
if (is_array($errorInfo)) {
|
|
|
|
$msg .= 'SQLSTATE = '.$errorInfo[0] . ', ';
|
|
|
|
$msg .= 'Driver Code = '.$errorInfo[1] . ', ';
|
|
|
|
$msg .= 'Driver Message = '.$errorInfo[2];
|
|
|
|
}
|
|
|
|
return $msg;
|
|
|
|
}
|
|
|
|
|
2013-02-25 11:19:49 +04:00
|
|
|
// internal use
|
2013-08-02 21:53:04 +04:00
|
|
|
/**
|
|
|
|
* @param string $statement
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-02-26 11:30:42 +04:00
|
|
|
protected function replaceTablePrefix($statement) {
|
2013-08-08 00:22:33 +04:00
|
|
|
return str_replace( '*PREFIX*', $this->tablePrefix, $statement );
|
2013-02-25 11:19:49 +04:00
|
|
|
}
|
2013-02-26 00:35:11 +04:00
|
|
|
|
|
|
|
public function enableQueryStatementCaching() {
|
|
|
|
$this->cachingQueryStatementEnabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function disableQueryStatementCaching() {
|
|
|
|
$this->cachingQueryStatementEnabled = false;
|
|
|
|
$this->preparedQueries = array();
|
|
|
|
}
|
2013-02-25 11:19:49 +04:00
|
|
|
}
|