nextcloud/lib/private/DB/Connection.php

530 lines
15 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
/**
2016-07-21 18:07:57 +03:00
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
2015-03-26 13:44:34 +03:00
* @author Bart Visscher <bartv@thisnet.nl>
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
2016-07-21 18:07:57 +03:00
* @author Joas Schilling <coding@schilljs.com>
* @author Julius Härtl <jus@bitgrid.net>
2015-03-26 13:44:34 +03:00
* @author Morris Jobke <hey@morrisjobke.de>
* @author Ole Ostergaard <ole.c.ostergaard@gmail.com>
* @author Ole Ostergaard <ole.ostergaard@knime.com>
* @author Philipp Schaffrath <github@philipp.schaffrath.email>
2016-07-21 19:13:36 +03:00
* @author Robin Appelman <robin@icewind.nl>
* @author Robin McCorkell <robin@mccorkell.me.uk>
2016-07-21 18:07:57 +03:00
* @author Roeland Jago Douma <roeland@famdouma.nl>
2015-03-26 13:44:34 +03:00
* @author Thomas Müller <thomas.mueller@tmit.eu>
*
* @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/>
2015-03-26 13:44:34 +03:00
*
*/
namespace OC\DB;
2016-05-19 13:34:40 +03:00
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Exception\ConstraintViolationException;
use Doctrine\DBAL\Exception\NotNullConstraintViolationException;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Result;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Statement;
2015-07-07 18:30:26 +03:00
use OC\DB\QueryBuilder\QueryBuilder;
use OC\SystemConfig;
2016-01-26 14:14:55 +03:00
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\ILogger;
2016-05-19 11:05:53 +03:00
use OCP\PreConditionNotMetException;
class Connection extends \Doctrine\DBAL\Connection {
/** @var string */
2013-08-08 00:22:33 +04:00
protected $tablePrefix;
/** @var \OC\DB\Adapter $adapter */
protected $adapter;
/** @var SystemConfig */
private $systemConfig;
/** @var ILogger */
private $logger;
2016-05-19 13:34:40 +03:00
protected $lockedTable = null;
/** @var int */
protected $queriesBuilt = 0;
/** @var int */
protected $queriesExecuted = 0;
/**
* @throws Exception
*/
public function connect() {
try {
return parent::connect();
} catch (Exception $e) {
// throw a new exception to prevent leaking info from the stacktrace
throw new Exception('Failed to connect to the database: ' . $e->getMessage(), $e->getCode());
}
}
public function getStats(): array {
return [
'built' => $this->queriesBuilt,
'executed' => $this->queriesExecuted,
];
}
/**
* Returns a QueryBuilder for the connection.
*/
public function getQueryBuilder(): IQueryBuilder {
$this->queriesBuilt++;
return new QueryBuilder(
new ConnectionAdapter($this),
$this->systemConfig,
$this->logger
);
}
/**
* 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]);
$this->queriesBuilt++;
return parent::createQueryBuilder();
}
/**
* Gets the ExpressionBuilder for the connection.
*
* @return \Doctrine\DBAL\Query\Expression\ExpressionBuilder
* @deprecated please use $this->getQueryBuilder()->expr() instead
*/
public function getExpressionBuilder() {
$backtrace = $this->getCallerBacktrace();
\OC::$server->getLogger()->debug('Doctrine ExpressionBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]);
$this->queriesBuilt++;
return parent::getExpressionBuilder();
}
/**
* Get the file and line that called the method where `getCallerBacktrace()` was used
*
* @return string
*/
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 '';
}
/**
* @return string
*/
public function getPrefix() {
return $this->tablePrefix;
}
/**
* 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
*/
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-08-08 00:22:33 +04:00
if (!isset($params['tablePrefix'])) {
throw new \Exception('tablePrefix not set');
}
/**
* @psalm-suppress InternalMethod
*/
parent::__construct($params, $driver, $config, $eventManager);
$this->adapter = new $params['adapter']($this);
2013-08-08 00:22:33 +04:00
$this->tablePrefix = $params['tablePrefix'];
$this->systemConfig = \OC::$server->getSystemConfig();
$this->logger = \OC::$server->getLogger();
}
/**
* 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
*
* @return Statement The prepared statement.
* @throws Exception
*/
public function prepare($statement, $limit = null, $offset = null): Statement {
if ($limit === -1) {
$limit = null;
}
if (!is_null($limit)) {
$platform = $this->getDatabasePlatform();
$statement = $platform->modifyLimitQuery($statement, $limit, $offset);
}
$statement = $this->replaceTablePrefix($statement);
$statement = $this->adapter->fixupStatement($statement);
return parent::prepare($statement);
}
/**
2015-03-06 16:51:08 +03:00
* Executes an, optionally parametrized, SQL query.
*
2015-03-06 16:51:08 +03:00
* If the query is parametrized, a prepared statement is used.
* If an SQLLogger is configured, the execution is logged.
*
* @param string $sql The SQL query to execute.
2015-03-06 16:51:08 +03:00
* @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.
*
* @return Result The executed statement.
2015-03-06 16:51:08 +03:00
*
* @throws \Doctrine\DBAL\Exception
*/
public function executeQuery(string $sql, array $params = [], $types = [], QueryCacheProfile $qcp = null): Result {
$sql = $this->replaceTablePrefix($sql);
$sql = $this->adapter->fixupStatement($sql);
$this->queriesExecuted++;
return parent::executeQuery($sql, $params, $types, $qcp);
}
/**
* @throws Exception
*/
public function executeUpdate(string $sql, array $params = [], array $types = []): int {
$sql = $this->replaceTablePrefix($sql);
$sql = $this->adapter->fixupStatement($sql);
$this->queriesExecuted++;
return parent::executeUpdate($sql, $params, $types);
}
/**
* 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 $sql The SQL query.
2015-03-06 16:51:08 +03:00
* @param array $params The query parameters.
* @param array $types The parameter types.
*
* @return int The number of affected rows.
2015-03-06 16:51:08 +03:00
*
* @throws \Doctrine\DBAL\Exception
*/
public function executeStatement($sql, array $params = [], array $types = []): int {
$sql = $this->replaceTablePrefix($sql);
$sql = $this->adapter->fixupStatement($sql);
$this->queriesExecuted++;
return parent::executeStatement($sql, $params, $types);
}
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 the last inserted ID.
* @throws Exception
2013-03-22 21:36:40 +04:00
*/
public function lastInsertId($seqName = null) {
2013-03-22 21:36:40 +04:00
if ($seqName) {
$seqName = $this->replaceTablePrefix($seqName);
}
return $this->adapter->lastInsertId($seqName);
}
/**
* @internal
* @throws Exception
*/
public function realLastInsertId($seqName = null) {
2013-03-22 21:36:40 +04:00
return parent::lastInsertId($seqName);
}
/**
* Insert a row if the matching row does not exists. To accomplish proper race condition avoidance
* it is needed that there is also a unique constraint on the values. Then this method will
* catch the exception and return 0.
*
* @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
* Please note: text fields (clob) must not be used in the compare array
* @return int number of inserted rows
* @throws \Doctrine\DBAL\Exception
* @deprecated 15.0.0 - use unique index and "try { $db->insert() } catch (UniqueConstraintViolationException $e) {}" instead, because it is more reliable and does not have the risk for deadlocks - see https://github.com/nextcloud/server/pull/12371
*/
public function insertIfNotExist($table, $input, array $compare = null) {
return $this->adapter->insertIfNotExist($table, $input, $compare);
}
public function insertIgnoreConflict(string $table, array $values) : int {
return $this->adapter->insertIgnoreConflict($table, $values);
}
2016-01-15 18:47:17 +03:00
private function getType($value) {
if (is_bool($value)) {
2016-01-26 14:14:55 +03:00
return IQueryBuilder::PARAM_BOOL;
} elseif (is_int($value)) {
2016-01-26 14:14:55 +03:00
return IQueryBuilder::PARAM_INT;
2016-01-15 18:47:17 +03:00
} else {
2016-01-26 14:14:55 +03:00
return IQueryBuilder::PARAM_STR;
2016-01-15 18:47:17 +03: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\Exception
2016-05-19 11:05:53 +03:00
* @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 18:47:17 +03:00
return $insertQb->createNamedParameter($value, $this->getType($value));
}, array_merge($keys, $values))
);
return $insertQb->execute();
} catch (NotNullConstraintViolationException $e) {
throw $e;
} catch (ConstraintViolationException $e) {
// value already exists, try update
$updateQb = $this->getQueryBuilder();
$updateQb->update($table);
foreach ($values as $name => $value) {
$updateQb->set($name, $updateQb->createNamedParameter($value, $this->getType($value)));
}
2016-05-19 13:34:40 +03:00
$where = $updateQb->expr()->andX();
$whereValues = array_merge($keys, $updatePreconditionValues);
foreach ($whereValues as $name => $value) {
if ($value === '') {
$where->add($updateQb->expr()->emptyString(
$name
));
} else {
$where->add($updateQb->expr()->eq(
$name,
$updateQb->createNamedParameter($value, $this->getType($value)),
$this->getType($value)
));
}
}
$updateQb->where($where);
$affected = $updateQb->execute();
if ($affected === 0 && !empty($updatePreconditionValues)) {
2016-05-19 11:05:53 +03:00
throw new PreConditionNotMetException();
}
return 0;
}
}
2016-05-19 13:34:40 +03:00
/**
* Create an exclusive read+write lock on a table
*
* @param string $tableName
*
2016-05-19 13:34:40 +03:00
* @throws \BadMethodCallException When trying to acquire a second lock
* @throws Exception
2016-05-19 13:34:40 +03:00
* @since 9.1.0
*/
public function lockTable($tableName) {
if ($this->lockedTable !== null) {
throw new \BadMethodCallException('Can not lock a new table until the previous lock is released.');
}
$tableName = $this->tablePrefix . $tableName;
$this->lockedTable = $tableName;
$this->adapter->lockTable($tableName);
}
/**
* Release a previous acquired lock again
*
* @throws Exception
2016-05-19 13:34:40 +03:00
* @since 9.1.0
*/
public function unlockTable() {
$this->adapter->unlockTable();
$this->lockedTable = null;
}
/**
* 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 (!empty($errorInfo)) {
$msg .= 'SQLSTATE = '.$errorInfo[0] . ', ';
$msg .= 'Driver Code = '.$errorInfo[1] . ', ';
$msg .= 'Driver Message = '.$errorInfo[2];
}
return $msg;
}
public function errorCode() {
return -1;
}
public function errorInfo() {
return [];
}
2014-12-08 16:35:57 +03:00
/**
* Drop a table from the database if it exists
*
* @param string $table table name without the prefix
*
* @throws Exception
2014-12-08 16:35:57 +03:00
*/
public function dropTable($table) {
$table = $this->tablePrefix . trim($table);
$schema = $this->getSchemaManager();
if ($schema->tablesExist([$table])) {
2014-12-08 16:35:57 +03:00
$schema->dropTable($table);
}
}
2014-12-08 20:00:42 +03:00
/**
* Check if a table exists
*
* @param string $table table name without the prefix
*
2014-12-08 20:00:42 +03:00
* @return bool
* @throws Exception
2014-12-08 20:00:42 +03:00
*/
public function tableExists($table) {
2014-12-08 20:00:42 +03:00
$table = $this->tablePrefix . trim($table);
$schema = $this->getSchemaManager();
return $schema->tablesExist([$table]);
2014-12-08 20:00:42 +03:00
}
// internal use
2013-08-02 21:53:04 +04:00
/**
* @param string $statement
* @return string
*/
protected function replaceTablePrefix($statement) {
return str_replace('*PREFIX*', $this->tablePrefix, $statement);
}
/**
* Check if a transaction is active
*
* @return bool
* @since 8.2.0
*/
public function inTransaction() {
return $this->getTransactionNestingLevel() > 0;
}
/**
* Escape a parameter to be used in a LIKE query
*
* @param string $param
* @return string
*/
public function escapeLikeParameter($param) {
return addcslashes($param, '\\_%');
}
/**
* Check whether or not the current database support 4byte wide unicode
*
* @return bool
* @since 11.0.0
*/
public function supports4ByteText() {
if (!$this->getDatabasePlatform() instanceof MySQLPlatform) {
return true;
}
return $this->getParams()['charset'] === 'utf8mb4';
}
/**
* Create the schema of the connected database
*
* @return Schema
* @throws Exception
*/
public function createSchema() {
$schemaManager = new MDB2SchemaManager($this);
$migrator = $schemaManager->getMigrator();
return $migrator->createSchema();
}
/**
* Migrate the database to the given schema
*
* @param Schema $toSchema
*
* @throws Exception
*/
public function migrateToSchema(Schema $toSchema) {
$schemaManager = new MDB2SchemaManager($this);
$migrator = $schemaManager->getMigrator();
$migrator->migrate($toSchema);
}
}