2013-02-25 07:19:49 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* @author Joas Schilling <nickvergessen@owncloud.com>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Robin Appelman <icewind@owncloud.com>
|
2016-03-01 16:25:15 +00:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
*
|
2016-01-12 14:02:16 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2015-03-26 10:44:34 +00:00
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2013-02-25 07:19:49 +00:00
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2013-02-25 07:19:49 +00:00
|
|
|
namespace OC\DB;
|
2015-01-06 15:54:41 +00:00
|
|
|
use Doctrine\DBAL\DBALException;
|
2013-02-25 07:19:49 +00:00
|
|
|
use Doctrine\DBAL\Driver;
|
|
|
|
use Doctrine\DBAL\Configuration;
|
|
|
|
use Doctrine\DBAL\Cache\QueryCacheProfile;
|
|
|
|
use Doctrine\Common\EventManager;
|
2015-07-07 15:30:26 +00:00
|
|
|
use OC\DB\QueryBuilder\QueryBuilder;
|
2016-01-26 11:14:55 +00:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2014-09-10 11:07:16 +00:00
|
|
|
use OCP\IDBConnection;
|
2015-08-24 12:21:09 +00:00
|
|
|
use OCP\PreconditionNotMetException;
|
2013-02-25 07:19:49 +00:00
|
|
|
|
2014-09-10 11:07:16 +00:00
|
|
|
class Connection extends \Doctrine\DBAL\Connection implements IDBConnection {
|
2013-08-02 17:53:04 +00:00
|
|
|
/**
|
2013-08-07 20:22:33 +00:00
|
|
|
* @var string $tablePrefix
|
2013-08-02 17:53:04 +00:00
|
|
|
*/
|
2013-08-07 20:22:33 +00:00
|
|
|
protected $tablePrefix;
|
2013-02-25 07:19:49 +00:00
|
|
|
|
2013-08-02 17:53:04 +00:00
|
|
|
/**
|
|
|
|
* @var \OC\DB\Adapter $adapter
|
|
|
|
*/
|
2013-02-25 07:19:49 +00:00
|
|
|
protected $adapter;
|
|
|
|
|
2015-01-06 15:54:41 +00:00
|
|
|
public function connect() {
|
|
|
|
try {
|
|
|
|
return parent::connect();
|
|
|
|
} catch (DBALException $e) {
|
|
|
|
// throw a new exception to prevent leaking info from the stacktrace
|
2015-03-20 02:56:38 +00:00
|
|
|
throw new DBALException('Failed to connect to the database: ' . $e->getMessage(), $e->getCode());
|
2015-01-06 15:54:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-21 12:17:47 +00:00
|
|
|
/**
|
|
|
|
* Returns a QueryBuilder for the connection.
|
|
|
|
*
|
|
|
|
* @return \OCP\DB\QueryBuilder\IQueryBuilder
|
|
|
|
*/
|
|
|
|
public function getQueryBuilder() {
|
|
|
|
return new QueryBuilder($this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the QueryBuilder for the connection.
|
|
|
|
*
|
|
|
|
* @return \Doctrine\DBAL\Query\QueryBuilder
|
|
|
|
* @deprecated please use $this->getQueryBuilder() instead
|
|
|
|
*/
|
|
|
|
public function createQueryBuilder() {
|
|
|
|
$backtrace = $this->getCallerBacktrace();
|
|
|
|
\OC::$server->getLogger()->debug('Doctrine QueryBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]);
|
|
|
|
return parent::createQueryBuilder();
|
|
|
|
}
|
|
|
|
|
2015-01-12 12:57:46 +00:00
|
|
|
/**
|
2015-07-06 10:00:01 +00:00
|
|
|
* Gets the ExpressionBuilder for the connection.
|
|
|
|
*
|
2015-07-21 12:17:47 +00:00
|
|
|
* @return \Doctrine\DBAL\Query\Expression\ExpressionBuilder
|
|
|
|
* @deprecated please use $this->getQueryBuilder()->expr() instead
|
2015-07-06 10:00:01 +00:00
|
|
|
*/
|
|
|
|
public function getExpressionBuilder() {
|
2015-07-21 12:17:47 +00:00
|
|
|
$backtrace = $this->getCallerBacktrace();
|
|
|
|
\OC::$server->getLogger()->debug('Doctrine ExpressionBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]);
|
|
|
|
return parent::getExpressionBuilder();
|
2015-07-06 10:00:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-21 12:17:47 +00:00
|
|
|
* Get the file and line that called the method where `getCallerBacktrace()` was used
|
2015-07-06 10:00:01 +00:00
|
|
|
*
|
2015-07-21 12:17:47 +00:00
|
|
|
* @return string
|
2015-07-06 10:00:01 +00:00
|
|
|
*/
|
2015-07-21 12:17:47 +00:00
|
|
|
protected function getCallerBacktrace() {
|
|
|
|
$traces = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
|
|
|
|
|
|
|
|
// 0 is the method where we use `getCallerBacktrace`
|
|
|
|
// 1 is the target method which uses the method we want to log
|
|
|
|
if (isset($traces[1])) {
|
|
|
|
return $traces[1]['file'] . ':' . $traces[1]['line'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
2015-07-06 10:00:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-01-12 12:57:46 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getPrefix() {
|
|
|
|
return $this->tablePrefix;
|
|
|
|
}
|
|
|
|
|
2013-02-25 07:19:49 +00:00
|
|
|
/**
|
|
|
|
* Initializes a new instance of the Connection class.
|
|
|
|
*
|
|
|
|
* @param array $params The connection parameters.
|
2013-08-02 17:53:04 +00:00
|
|
|
* @param \Doctrine\DBAL\Driver $driver
|
|
|
|
* @param \Doctrine\DBAL\Configuration $config
|
|
|
|
* @param \Doctrine\Common\EventManager $eventManager
|
|
|
|
* @throws \Exception
|
2013-02-25 07:19:49 +00:00
|
|
|
*/
|
|
|
|
public function __construct(array $params, Driver $driver, Configuration $config = null,
|
|
|
|
EventManager $eventManager = null)
|
|
|
|
{
|
|
|
|
if (!isset($params['adapter'])) {
|
2013-08-02 17:53:04 +00:00
|
|
|
throw new \Exception('adapter not set');
|
2013-02-25 07:19:49 +00:00
|
|
|
}
|
2013-08-07 20:22:33 +00:00
|
|
|
if (!isset($params['tablePrefix'])) {
|
|
|
|
throw new \Exception('tablePrefix not set');
|
2013-02-25 07:19:49 +00:00
|
|
|
}
|
2013-02-25 07:19:49 +00:00
|
|
|
parent::__construct($params, $driver, $config, $eventManager);
|
|
|
|
$this->adapter = new $params['adapter']($this);
|
2013-08-07 20:22:33 +00:00
|
|
|
$this->tablePrefix = $params['tablePrefix'];
|
2015-04-13 09:36:06 +00:00
|
|
|
|
|
|
|
parent::setTransactionIsolation(parent::TRANSACTION_READ_COMMITTED);
|
2013-02-25 07:19:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prepares an SQL statement.
|
|
|
|
*
|
|
|
|
* @param string $statement The SQL statement to prepare.
|
2013-08-02 17:53:04 +00:00
|
|
|
* @param int $limit
|
|
|
|
* @param int $offset
|
2013-02-25 07:19:49 +00:00
|
|
|
* @return \Doctrine\DBAL\Driver\Statement The prepared statement.
|
|
|
|
*/
|
|
|
|
public function prepare( $statement, $limit=null, $offset=null ) {
|
2013-02-25 20:46:03 +00:00
|
|
|
if ($limit === -1) {
|
|
|
|
$limit = null;
|
|
|
|
}
|
|
|
|
if (!is_null($limit)) {
|
|
|
|
$platform = $this->getDatabasePlatform();
|
|
|
|
$statement = $platform->modifyLimitQuery($statement, $limit, $offset);
|
2013-02-25 20:35:11 +00:00
|
|
|
}
|
2013-08-07 16:21:49 +00:00
|
|
|
$statement = $this->replaceTablePrefix($statement);
|
|
|
|
$statement = $this->adapter->fixupStatement($statement);
|
|
|
|
|
2015-12-03 15:41:23 +00:00
|
|
|
if(\OC::$server->getSystemConfig()->getValue( 'log_query', false)) {
|
2015-07-03 12:06:40 +00:00
|
|
|
\OCP\Util::writeLog('core', 'DB prepare : '.$statement, \OCP\Util::DEBUG);
|
2013-02-25 21:49:55 +00:00
|
|
|
}
|
2014-09-10 11:11:04 +00:00
|
|
|
return parent::prepare($statement);
|
2013-02-25 07:19:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-06 13:51:08 +00:00
|
|
|
* Executes an, optionally parametrized, SQL query.
|
2013-02-25 07:19:49 +00:00
|
|
|
*
|
2015-03-06 13:51:08 +00:00
|
|
|
* If the query is parametrized, a prepared statement is used.
|
2013-02-25 07:19:49 +00:00
|
|
|
* If an SQLLogger is configured, the execution is logged.
|
|
|
|
*
|
2015-03-06 13:51:08 +00:00
|
|
|
* @param string $query The SQL query to execute.
|
|
|
|
* @param array $params The parameters to bind to the query, if any.
|
|
|
|
* @param array $types The types the previous parameters are in.
|
|
|
|
* @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional.
|
|
|
|
*
|
2013-02-25 07:19:49 +00:00
|
|
|
* @return \Doctrine\DBAL\Driver\Statement The executed statement.
|
2015-03-06 13:51:08 +00:00
|
|
|
*
|
|
|
|
* @throws \Doctrine\DBAL\DBALException
|
2013-02-25 07:19:49 +00:00
|
|
|
*/
|
|
|
|
public function executeQuery($query, array $params = array(), $types = array(), QueryCacheProfile $qcp = null)
|
|
|
|
{
|
2013-03-22 17:36:40 +00:00
|
|
|
$query = $this->replaceTablePrefix($query);
|
2013-02-25 21:49:55 +00:00
|
|
|
$query = $this->adapter->fixupStatement($query);
|
2013-02-25 07:19:49 +00: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.
|
|
|
|
*
|
2015-03-06 13:51:08 +00:00
|
|
|
* @param string $query The SQL query.
|
|
|
|
* @param array $params The query parameters.
|
|
|
|
* @param array $types The parameter types.
|
|
|
|
*
|
2013-02-25 07:19:49 +00:00
|
|
|
* @return integer The number of affected rows.
|
2015-03-06 13:51:08 +00:00
|
|
|
*
|
|
|
|
* @throws \Doctrine\DBAL\DBALException
|
2013-02-25 07:19:49 +00:00
|
|
|
*/
|
|
|
|
public function executeUpdate($query, array $params = array(), array $types = array())
|
|
|
|
{
|
2013-03-22 17:36:40 +00:00
|
|
|
$query = $this->replaceTablePrefix($query);
|
2013-02-25 21:49:55 +00:00
|
|
|
$query = $this->adapter->fixupStatement($query);
|
2013-02-25 07:19:49 +00:00
|
|
|
return parent::executeUpdate($query, $params, $types);
|
|
|
|
}
|
2013-02-25 07:19:49 +00:00
|
|
|
|
2013-03-22 17:36:40 +00: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.
|
|
|
|
*/
|
2015-11-20 12:00:42 +00:00
|
|
|
public function lastInsertId($seqName = null) {
|
2013-03-22 17:36:40 +00:00
|
|
|
if ($seqName) {
|
|
|
|
$seqName = $this->replaceTablePrefix($seqName);
|
|
|
|
}
|
|
|
|
return $this->adapter->lastInsertId($seqName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// internal use
|
2015-03-09 21:12:31 +00:00
|
|
|
public function realLastInsertId($seqName = null) {
|
2013-03-22 17:36:40 +00:00
|
|
|
return parent::lastInsertId($seqName);
|
|
|
|
}
|
|
|
|
|
2013-02-26 07:30:42 +00:00
|
|
|
/**
|
2015-03-11 08:00:47 +00:00
|
|
|
* Insert a row if the matching row does not exists.
|
|
|
|
*
|
|
|
|
* @param string $table The table name (will replace *PREFIX* with the actual prefix)
|
|
|
|
* @param array $input data that should be inserted into the table (column name => value)
|
|
|
|
* @param array|null $compare List of values that should be checked for "if not exists"
|
|
|
|
* If this is null or an empty array, all keys of $input will be compared
|
2015-03-16 14:41:00 +00:00
|
|
|
* Please note: text fields (clob) must not be used in the compare array
|
2015-03-11 08:00:47 +00:00
|
|
|
* @return int number of inserted rows
|
2015-03-09 21:12:31 +00:00
|
|
|
* @throws \Doctrine\DBAL\DBALException
|
2013-02-26 07:30:42 +00:00
|
|
|
*/
|
2015-03-11 08:00:47 +00:00
|
|
|
public function insertIfNotExist($table, $input, array $compare = null) {
|
2015-03-09 16:25:02 +00:00
|
|
|
return $this->adapter->insertIfNotExist($table, $input, $compare);
|
2013-02-26 07:30:42 +00:00
|
|
|
}
|
|
|
|
|
2016-01-15 15:47:17 +00:00
|
|
|
private function getType($value) {
|
|
|
|
if (is_bool($value)) {
|
2016-01-26 11:14:55 +00:00
|
|
|
return IQueryBuilder::PARAM_BOOL;
|
2016-01-15 15:47:17 +00:00
|
|
|
} else if (is_int($value)) {
|
2016-01-26 11:14:55 +00:00
|
|
|
return IQueryBuilder::PARAM_INT;
|
2016-01-15 15:47:17 +00:00
|
|
|
} else {
|
2016-01-26 11:14:55 +00:00
|
|
|
return IQueryBuilder::PARAM_STR;
|
2016-01-15 15:47:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-24 12:21:09 +00:00
|
|
|
/**
|
|
|
|
* Insert or update a row value
|
|
|
|
*
|
|
|
|
* @param string $table
|
|
|
|
* @param array $keys (column name => value)
|
|
|
|
* @param array $values (column name => value)
|
|
|
|
* @param array $updatePreconditionValues ensure values match preconditions (column name => value)
|
|
|
|
* @return int number of new rows
|
|
|
|
* @throws \Doctrine\DBAL\DBALException
|
|
|
|
* @throws PreconditionNotMetException
|
|
|
|
*/
|
|
|
|
public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []) {
|
|
|
|
try {
|
|
|
|
$insertQb = $this->getQueryBuilder();
|
|
|
|
$insertQb->insert($table)
|
|
|
|
->values(
|
|
|
|
array_map(function($value) use ($insertQb) {
|
2016-01-15 15:47:17 +00:00
|
|
|
return $insertQb->createNamedParameter($value, $this->getType($value));
|
2015-08-24 12:21:09 +00:00
|
|
|
}, array_merge($keys, $values))
|
|
|
|
);
|
|
|
|
return $insertQb->execute();
|
2016-01-15 15:13:03 +00:00
|
|
|
} catch (\Doctrine\DBAL\Exception\ConstraintViolationException $e) {
|
2015-08-24 12:21:09 +00:00
|
|
|
// value already exists, try update
|
|
|
|
$updateQb = $this->getQueryBuilder();
|
|
|
|
$updateQb->update($table);
|
|
|
|
foreach ($values as $name => $value) {
|
2016-01-26 11:15:18 +00:00
|
|
|
$updateQb->set($name, $updateQb->createNamedParameter($value, $this->getType($value)));
|
2015-08-24 12:21:09 +00:00
|
|
|
}
|
|
|
|
$where = $updateQb->expr()->andx();
|
|
|
|
$whereValues = array_merge($keys, $updatePreconditionValues);
|
|
|
|
foreach ($whereValues as $name => $value) {
|
|
|
|
$where->add($updateQb->expr()->eq(
|
2016-01-18 15:03:41 +00:00
|
|
|
$name,
|
|
|
|
$updateQb->createNamedParameter($value, $this->getType($value)),
|
|
|
|
$this->getType($value)
|
2015-08-24 12:21:09 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
$updateQb->where($where);
|
|
|
|
$affected = $updateQb->execute();
|
|
|
|
|
2016-03-11 17:22:29 +00:00
|
|
|
if ($affected === 0 && !empty($updatePreconditionValues)) {
|
2015-08-24 12:21:09 +00:00
|
|
|
throw new PreconditionNotMetException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-01 16:20:27 +00: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;
|
|
|
|
}
|
|
|
|
|
2014-12-08 13:35:57 +00:00
|
|
|
/**
|
|
|
|
* Drop a table from the database if it exists
|
|
|
|
*
|
|
|
|
* @param string $table table name without the prefix
|
|
|
|
*/
|
|
|
|
public function dropTable($table) {
|
|
|
|
$table = $this->tablePrefix . trim($table);
|
|
|
|
$schema = $this->getSchemaManager();
|
|
|
|
if($schema->tablesExist(array($table))) {
|
|
|
|
$schema->dropTable($table);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-08 17:00:42 +00:00
|
|
|
/**
|
|
|
|
* Check if a table exists
|
|
|
|
*
|
|
|
|
* @param string $table table name without the prefix
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function tableExists($table){
|
|
|
|
$table = $this->tablePrefix . trim($table);
|
|
|
|
$schema = $this->getSchemaManager();
|
|
|
|
return $schema->tablesExist(array($table));
|
|
|
|
}
|
|
|
|
|
2013-02-25 07:19:49 +00:00
|
|
|
// internal use
|
2013-08-02 17:53:04 +00:00
|
|
|
/**
|
|
|
|
* @param string $statement
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-02-26 07:30:42 +00:00
|
|
|
protected function replaceTablePrefix($statement) {
|
2013-08-07 20:22:33 +00:00
|
|
|
return str_replace( '*PREFIX*', $this->tablePrefix, $statement );
|
2013-02-25 07:19:49 +00:00
|
|
|
}
|
2015-08-10 12:15:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a transaction is active
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
* @since 8.2.0
|
|
|
|
*/
|
|
|
|
public function inTransaction() {
|
|
|
|
return $this->getTransactionNestingLevel() > 0;
|
|
|
|
}
|
2015-11-05 15:31:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Espace a parameter to be used in a LIKE query
|
|
|
|
*
|
|
|
|
* @param string $param
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function escapeLikeParameter($param) {
|
|
|
|
return addcslashes($param, '\\_%');
|
|
|
|
}
|
2013-02-25 07:19:49 +00:00
|
|
|
}
|