2013-02-25 11:19:49 +04:00
|
|
|
<?php
|
2021-01-03 17:28:31 +03:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2013-02-25 11:19:49 +04:00
|
|
|
/**
|
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>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2020-12-16 16:54:15 +03:00
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Ole Ostergaard <ole.c.ostergaard@gmail.com>
|
|
|
|
* @author Ole Ostergaard <ole.ostergaard@knime.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Philipp Schaffrath <github@philipp.schaffrath.email>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-03-01 19:25:15 +03:00
|
|
|
* @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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
2013-02-25 11:19:49 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2013-02-25 11:19:49 +04:00
|
|
|
namespace OC\DB;
|
2016-05-19 13:34:40 +03:00
|
|
|
|
2019-11-22 22:52:10 +03:00
|
|
|
use Doctrine\Common\EventManager;
|
|
|
|
use Doctrine\DBAL\Cache\QueryCacheProfile;
|
|
|
|
use Doctrine\DBAL\Configuration;
|
2013-02-25 11:19:49 +04:00
|
|
|
use Doctrine\DBAL\Driver;
|
2021-01-03 17:28:31 +03:00
|
|
|
use Doctrine\DBAL\Exception;
|
2017-01-10 14:10:36 +03:00
|
|
|
use Doctrine\DBAL\Exception\ConstraintViolationException;
|
2020-11-09 19:33:32 +03:00
|
|
|
use Doctrine\DBAL\Exception\NotNullConstraintViolationException;
|
2021-01-03 17:28:31 +03:00
|
|
|
use Doctrine\DBAL\Platforms\MySQLPlatform;
|
2021-03-24 22:27:39 +03:00
|
|
|
use Doctrine\DBAL\Platforms\OraclePlatform;
|
|
|
|
use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
|
|
|
|
use Doctrine\DBAL\Platforms\SqlitePlatform;
|
2021-01-03 17:28:31 +03:00
|
|
|
use Doctrine\DBAL\Result;
|
2017-06-01 17:56:34 +03:00
|
|
|
use Doctrine\DBAL\Schema\Schema;
|
2021-01-03 17:28:31 +03:00
|
|
|
use Doctrine\DBAL\Statement;
|
2015-07-07 18:30:26 +03:00
|
|
|
use OC\DB\QueryBuilder\QueryBuilder;
|
2020-10-30 00:35:06 +03:00
|
|
|
use OC\SystemConfig;
|
2016-01-26 14:14:55 +03:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2020-10-30 00:35:06 +03:00
|
|
|
use OCP\ILogger;
|
2016-05-19 11:05:53 +03:00
|
|
|
use OCP\PreConditionNotMetException;
|
2013-02-25 11:19:49 +04:00
|
|
|
|
2021-02-09 18:12:15 +03:00
|
|
|
class Connection extends \Doctrine\DBAL\Connection {
|
2020-10-30 00:35:06 +03:00
|
|
|
/** @var string */
|
2013-08-08 00:22:33 +04:00
|
|
|
protected $tablePrefix;
|
2013-02-25 11:19:49 +04:00
|
|
|
|
2020-10-30 00:35:06 +03:00
|
|
|
/** @var \OC\DB\Adapter $adapter */
|
2013-02-25 11:19:49 +04:00
|
|
|
protected $adapter;
|
|
|
|
|
2020-10-30 00:35:06 +03:00
|
|
|
/** @var SystemConfig */
|
|
|
|
private $systemConfig;
|
|
|
|
|
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
|
|
|
|
2016-05-19 13:34:40 +03:00
|
|
|
protected $lockedTable = null;
|
|
|
|
|
2020-09-25 15:47:14 +03:00
|
|
|
/** @var int */
|
|
|
|
protected $queriesBuilt = 0;
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
protected $queriesExecuted = 0;
|
|
|
|
|
2021-01-12 14:24:36 +03:00
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2015-01-06 18:54:41 +03:00
|
|
|
public function connect() {
|
|
|
|
try {
|
|
|
|
return parent::connect();
|
2021-01-03 17:28:31 +03:00
|
|
|
} catch (Exception $e) {
|
2015-01-06 18:54:41 +03:00
|
|
|
// throw a new exception to prevent leaking info from the stacktrace
|
2021-01-03 17:28:31 +03:00
|
|
|
throw new Exception('Failed to connect to the database: ' . $e->getMessage(), $e->getCode());
|
2015-01-06 18:54:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-25 15:47:14 +03:00
|
|
|
public function getStats(): array {
|
|
|
|
return [
|
|
|
|
'built' => $this->queriesBuilt,
|
|
|
|
'executed' => $this->queriesExecuted,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-07-21 15:17:47 +03:00
|
|
|
/**
|
|
|
|
* Returns a QueryBuilder for the connection.
|
|
|
|
*/
|
2021-01-03 17:28:31 +03:00
|
|
|
public function getQueryBuilder(): IQueryBuilder {
|
2020-09-25 15:47:14 +03:00
|
|
|
$this->queriesBuilt++;
|
2016-10-28 14:48:58 +03:00
|
|
|
return new QueryBuilder(
|
2021-01-03 17:28:31 +03:00
|
|
|
new ConnectionAdapter($this),
|
2020-10-30 00:35:06 +03:00
|
|
|
$this->systemConfig,
|
|
|
|
$this->logger
|
2016-10-28 14:48:58 +03:00
|
|
|
);
|
2015-07-21 15:17:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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]);
|
2020-09-25 15:47:14 +03:00
|
|
|
$this->queriesBuilt++;
|
2015-07-21 15:17:47 +03:00
|
|
|
return parent::createQueryBuilder();
|
|
|
|
}
|
|
|
|
|
2015-01-12 15:57:46 +03:00
|
|
|
/**
|
2015-07-06 13:00:01 +03:00
|
|
|
* Gets the ExpressionBuilder for the connection.
|
|
|
|
*
|
2015-07-21 15:17:47 +03:00
|
|
|
* @return \Doctrine\DBAL\Query\Expression\ExpressionBuilder
|
|
|
|
* @deprecated please use $this->getQueryBuilder()->expr() instead
|
2015-07-06 13:00:01 +03:00
|
|
|
*/
|
|
|
|
public function getExpressionBuilder() {
|
2015-07-21 15:17:47 +03:00
|
|
|
$backtrace = $this->getCallerBacktrace();
|
|
|
|
\OC::$server->getLogger()->debug('Doctrine ExpressionBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]);
|
2020-09-25 15:47:14 +03:00
|
|
|
$this->queriesBuilt++;
|
2015-07-21 15:17:47 +03:00
|
|
|
return parent::getExpressionBuilder();
|
2015-07-06 13:00:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-07-21 15:17:47 +03:00
|
|
|
* Get the file and line that called the method where `getCallerBacktrace()` was used
|
2015-07-06 13:00:01 +03:00
|
|
|
*
|
2015-07-21 15:17:47 +03:00
|
|
|
* @return string
|
2015-07-06 13:00:01 +03:00
|
|
|
*/
|
2015-07-21 15:17:47 +03: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 13:00:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-01-12 15:57:46 +03:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getPrefix() {
|
|
|
|
return $this->tablePrefix;
|
|
|
|
}
|
|
|
|
|
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,
|
2020-04-10 15:19:56 +03:00
|
|
|
EventManager $eventManager = null) {
|
2013-02-25 11:19:49 +04:00
|
|
|
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
|
|
|
}
|
2021-02-09 18:12:15 +03:00
|
|
|
/**
|
|
|
|
* @psalm-suppress InternalMethod
|
|
|
|
*/
|
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'];
|
2020-10-30 00:35:06 +03:00
|
|
|
|
|
|
|
$this->systemConfig = \OC::$server->getSystemConfig();
|
|
|
|
$this->logger = \OC::$server->getLogger();
|
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
|
2021-01-12 14:24:36 +03:00
|
|
|
*
|
2021-01-03 17:28:31 +03:00
|
|
|
* @return Statement The prepared statement.
|
2021-01-12 14:24:36 +03:00
|
|
|
* @throws Exception
|
2013-02-25 11:19:49 +04:00
|
|
|
*/
|
2021-01-03 17:28:31 +03:00
|
|
|
public function prepare($statement, $limit = null, $offset = null): Statement {
|
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
|
|
|
}
|
2013-08-07 20:21:49 +04:00
|
|
|
$statement = $this->replaceTablePrefix($statement);
|
|
|
|
$statement = $this->adapter->fixupStatement($statement);
|
|
|
|
|
2014-09-10 15:11:04 +04:00
|
|
|
return parent::prepare($statement);
|
2013-02-25 11:19:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-06 16:51:08 +03:00
|
|
|
* Executes an, optionally parametrized, SQL query.
|
2013-02-25 11:19:49 +04:00
|
|
|
*
|
2015-03-06 16:51:08 +03:00
|
|
|
* If the query is parametrized, a prepared statement is used.
|
2013-02-25 11:19:49 +04:00
|
|
|
* If an SQLLogger is configured, the execution is logged.
|
|
|
|
*
|
2020-10-29 11:31:37 +03:00
|
|
|
* @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.
|
|
|
|
*
|
2021-01-03 17:28:31 +03:00
|
|
|
* @return Result The executed statement.
|
2015-03-06 16:51:08 +03:00
|
|
|
*
|
2021-01-03 17:28:31 +03:00
|
|
|
* @throws \Doctrine\DBAL\Exception
|
2013-02-25 11:19:49 +04:00
|
|
|
*/
|
2021-01-03 17:28:31 +03:00
|
|
|
public function executeQuery(string $sql, array $params = [], $types = [], QueryCacheProfile $qcp = null): Result {
|
2020-10-29 11:31:37 +03:00
|
|
|
$sql = $this->replaceTablePrefix($sql);
|
|
|
|
$sql = $this->adapter->fixupStatement($sql);
|
2020-09-25 15:47:14 +03:00
|
|
|
$this->queriesExecuted++;
|
2020-10-29 11:31:37 +03:00
|
|
|
return parent::executeQuery($sql, $params, $types, $qcp);
|
|
|
|
}
|
|
|
|
|
2021-01-12 14:24:36 +03:00
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2021-01-03 17:28:31 +03:00
|
|
|
public function executeUpdate(string $sql, array $params = [], array $types = []): int {
|
2020-11-03 13:03:02 +03:00
|
|
|
$sql = $this->replaceTablePrefix($sql);
|
|
|
|
$sql = $this->adapter->fixupStatement($sql);
|
|
|
|
$this->queriesExecuted++;
|
2020-10-29 11:31:37 +03:00
|
|
|
return parent::executeUpdate($sql, $params, $types);
|
2013-02-25 11:19:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2020-10-29 11:31:37 +03:00
|
|
|
* @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.
|
|
|
|
*
|
2021-01-03 17:28:31 +03:00
|
|
|
* @return int The number of affected rows.
|
2015-03-06 16:51:08 +03:00
|
|
|
*
|
2021-01-03 17:28:31 +03:00
|
|
|
* @throws \Doctrine\DBAL\Exception
|
2013-02-25 11:19:49 +04:00
|
|
|
*/
|
2021-01-03 17:28:31 +03:00
|
|
|
public function executeStatement($sql, array $params = [], array $types = []): int {
|
2020-10-29 11:31:37 +03:00
|
|
|
$sql = $this->replaceTablePrefix($sql);
|
|
|
|
$sql = $this->adapter->fixupStatement($sql);
|
2020-09-25 15:47:14 +03:00
|
|
|
$this->queriesExecuted++;
|
2020-10-29 11:31:37 +03:00
|
|
|
return parent::executeStatement($sql, $params, $types);
|
2013-02-25 11:19:49 +04:00
|
|
|
}
|
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.
|
2021-01-12 14:24:36 +03:00
|
|
|
*
|
2021-01-03 17:28:31 +03:00
|
|
|
* @return string the last inserted ID.
|
2021-01-12 14:24:36 +03:00
|
|
|
* @throws Exception
|
2013-03-22 21:36:40 +04:00
|
|
|
*/
|
2015-11-20 15:00:42 +03: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);
|
|
|
|
}
|
|
|
|
|
2021-01-12 14:24:36 +03:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2015-03-10 00:12:31 +03:00
|
|
|
public function realLastInsertId($seqName = null) {
|
2013-03-22 21:36:40 +04:00
|
|
|
return parent::lastInsertId($seqName);
|
|
|
|
}
|
|
|
|
|
2013-02-26 11:30:42 +04:00
|
|
|
/**
|
2018-11-09 12:35:12 +03:00
|
|
|
* 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.
|
2015-03-11 11:00:47 +03:00
|
|
|
*
|
|
|
|
* @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 17:41:00 +03:00
|
|
|
* Please note: text fields (clob) must not be used in the compare array
|
2015-03-11 11:00:47 +03:00
|
|
|
* @return int number of inserted rows
|
2021-01-03 17:28:31 +03:00
|
|
|
* @throws \Doctrine\DBAL\Exception
|
2018-11-09 14:13:30 +03:00
|
|
|
* @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
|
2013-02-26 11:30:42 +04:00
|
|
|
*/
|
2015-03-11 11:00:47 +03:00
|
|
|
public function insertIfNotExist($table, $input, array $compare = null) {
|
2015-03-09 19:25:02 +03:00
|
|
|
return $this->adapter->insertIfNotExist($table, $input, $compare);
|
2013-02-26 11:30:42 +04:00
|
|
|
}
|
|
|
|
|
2019-02-26 16:08:55 +03:00
|
|
|
public function insertIgnoreConflict(string $table, array $values) : int {
|
|
|
|
return $this->adapter->insertIgnoreConflict($table, $values);
|
2019-01-21 19:54:16 +03:00
|
|
|
}
|
|
|
|
|
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;
|
2020-04-10 11:35:09 +03:00
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-24 15:21:09 +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
|
2021-01-03 17:28:31 +03:00
|
|
|
* @throws \Doctrine\DBAL\Exception
|
2016-05-19 11:05:53 +03:00
|
|
|
* @throws PreConditionNotMetException
|
2015-08-24 15:21:09 +03:00
|
|
|
*/
|
|
|
|
public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []) {
|
|
|
|
try {
|
|
|
|
$insertQb = $this->getQueryBuilder();
|
|
|
|
$insertQb->insert($table)
|
|
|
|
->values(
|
2020-04-09 14:53:40 +03:00
|
|
|
array_map(function ($value) use ($insertQb) {
|
2016-01-15 18:47:17 +03:00
|
|
|
return $insertQb->createNamedParameter($value, $this->getType($value));
|
2015-08-24 15:21:09 +03:00
|
|
|
}, array_merge($keys, $values))
|
|
|
|
);
|
|
|
|
return $insertQb->execute();
|
2020-11-09 19:33:32 +03:00
|
|
|
} catch (NotNullConstraintViolationException $e) {
|
|
|
|
throw $e;
|
2017-01-10 14:10:36 +03:00
|
|
|
} catch (ConstraintViolationException $e) {
|
2015-08-24 15:21:09 +03:00
|
|
|
// value already exists, try update
|
|
|
|
$updateQb = $this->getQueryBuilder();
|
|
|
|
$updateQb->update($table);
|
|
|
|
foreach ($values as $name => $value) {
|
2016-01-26 14:15:18 +03:00
|
|
|
$updateQb->set($name, $updateQb->createNamedParameter($value, $this->getType($value)));
|
2015-08-24 15:21:09 +03:00
|
|
|
}
|
2016-05-19 13:34:40 +03:00
|
|
|
$where = $updateQb->expr()->andX();
|
2015-08-24 15:21:09 +03:00
|
|
|
$whereValues = array_merge($keys, $updatePreconditionValues);
|
|
|
|
foreach ($whereValues as $name => $value) {
|
2020-11-09 19:33:05 +03:00
|
|
|
if ($value === '') {
|
|
|
|
$where->add($updateQb->expr()->emptyString(
|
|
|
|
$name
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
$where->add($updateQb->expr()->eq(
|
|
|
|
$name,
|
|
|
|
$updateQb->createNamedParameter($value, $this->getType($value)),
|
|
|
|
$this->getType($value)
|
|
|
|
));
|
|
|
|
}
|
2015-08-24 15:21:09 +03:00
|
|
|
}
|
|
|
|
$updateQb->where($where);
|
|
|
|
$affected = $updateQb->execute();
|
|
|
|
|
2016-03-11 20:22:29 +03:00
|
|
|
if ($affected === 0 && !empty($updatePreconditionValues)) {
|
2016-05-19 11:05:53 +03:00
|
|
|
throw new PreConditionNotMetException();
|
2015-08-24 15:21:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-19 13:34:40 +03:00
|
|
|
/**
|
|
|
|
* Create an exclusive read+write lock on a table
|
|
|
|
*
|
|
|
|
* @param string $tableName
|
2021-01-12 14:24:36 +03:00
|
|
|
*
|
2016-05-19 13:34:40 +03:00
|
|
|
* @throws \BadMethodCallException When trying to acquire a second lock
|
2021-01-12 14:24:36 +03:00
|
|
|
* @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
|
|
|
|
*
|
2021-01-12 14:24:36 +03:00
|
|
|
* @throws Exception
|
2016-05-19 13:34:40 +03:00
|
|
|
* @since 9.1.0
|
|
|
|
*/
|
|
|
|
public function unlockTable() {
|
|
|
|
$this->adapter->unlockTable();
|
|
|
|
$this->lockedTable = null;
|
|
|
|
}
|
|
|
|
|
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();
|
2021-01-03 17:28:31 +03:00
|
|
|
if (!empty($errorInfo)) {
|
2013-07-01 20:20:27 +04:00
|
|
|
$msg .= 'SQLSTATE = '.$errorInfo[0] . ', ';
|
|
|
|
$msg .= 'Driver Code = '.$errorInfo[1] . ', ';
|
|
|
|
$msg .= 'Driver Message = '.$errorInfo[2];
|
|
|
|
}
|
|
|
|
return $msg;
|
|
|
|
}
|
|
|
|
|
2021-01-03 17:28:31 +03:00
|
|
|
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
|
2021-01-12 14:24:36 +03:00
|
|
|
*
|
|
|
|
* @throws Exception
|
2014-12-08 16:35:57 +03:00
|
|
|
*/
|
|
|
|
public function dropTable($table) {
|
|
|
|
$table = $this->tablePrefix . trim($table);
|
|
|
|
$schema = $this->getSchemaManager();
|
2020-04-10 15:19:56 +03:00
|
|
|
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
|
2021-01-12 14:24:36 +03:00
|
|
|
*
|
2014-12-08 20:00:42 +03:00
|
|
|
* @return bool
|
2021-01-12 14:24:36 +03:00
|
|
|
* @throws Exception
|
2014-12-08 20:00:42 +03:00
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
public function tableExists($table) {
|
2014-12-08 20:00:42 +03:00
|
|
|
$table = $this->tablePrefix . trim($table);
|
|
|
|
$schema = $this->getSchemaManager();
|
2020-03-26 11:30:18 +03:00
|
|
|
return $schema->tablesExist([$table]);
|
2014-12-08 20:00:42 +03:00
|
|
|
}
|
|
|
|
|
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) {
|
2020-04-09 17:07:47 +03:00
|
|
|
return str_replace('*PREFIX*', $this->tablePrefix, $statement);
|
2013-02-25 11:19:49 +04:00
|
|
|
}
|
2015-08-10 15:15:44 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a transaction is active
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
* @since 8.2.0
|
|
|
|
*/
|
|
|
|
public function inTransaction() {
|
|
|
|
return $this->getTransactionNestingLevel() > 0;
|
|
|
|
}
|
2015-11-05 18:31:56 +03:00
|
|
|
|
|
|
|
/**
|
2017-12-20 17:51:37 +03:00
|
|
|
* Escape a parameter to be used in a LIKE query
|
2015-11-05 18:31:56 +03:00
|
|
|
*
|
|
|
|
* @param string $param
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function escapeLikeParameter($param) {
|
|
|
|
return addcslashes($param, '\\_%');
|
|
|
|
}
|
2016-10-20 15:26:09 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether or not the current database support 4byte wide unicode
|
|
|
|
*
|
|
|
|
* @return bool
|
2016-11-15 20:51:52 +03:00
|
|
|
* @since 11.0.0
|
2016-10-20 15:26:09 +03:00
|
|
|
*/
|
|
|
|
public function supports4ByteText() {
|
2021-01-03 17:28:31 +03:00
|
|
|
if (!$this->getDatabasePlatform() instanceof MySQLPlatform) {
|
2015-07-30 11:57:16 +03:00
|
|
|
return true;
|
|
|
|
}
|
2017-03-22 01:08:05 +03:00
|
|
|
return $this->getParams()['charset'] === 'utf8mb4';
|
2016-10-20 15:26:09 +03:00
|
|
|
}
|
2017-06-01 17:56:34 +03:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the schema of the connected database
|
|
|
|
*
|
|
|
|
* @return Schema
|
2021-01-12 14:24:36 +03:00
|
|
|
* @throws Exception
|
2017-06-01 17:56:34 +03:00
|
|
|
*/
|
|
|
|
public function createSchema() {
|
2021-03-24 22:27:39 +03:00
|
|
|
$migrator = $this->getMigrator();
|
2017-06-01 17:56:34 +03:00
|
|
|
return $migrator->createSchema();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Migrate the database to the given schema
|
|
|
|
*
|
|
|
|
* @param Schema $toSchema
|
2021-01-12 14:24:36 +03:00
|
|
|
*
|
|
|
|
* @throws Exception
|
2017-06-01 17:56:34 +03:00
|
|
|
*/
|
|
|
|
public function migrateToSchema(Schema $toSchema) {
|
2021-03-24 22:27:39 +03:00
|
|
|
$migrator = $this->getMigrator();
|
2017-06-01 17:56:34 +03:00
|
|
|
$migrator->migrate($toSchema);
|
|
|
|
}
|
2021-03-24 22:27:39 +03:00
|
|
|
|
|
|
|
private function getMigrator() {
|
|
|
|
// TODO properly inject those dependencies
|
|
|
|
$random = \OC::$server->getSecureRandom();
|
|
|
|
$platform = $this->getDatabasePlatform();
|
|
|
|
$config = \OC::$server->getConfig();
|
|
|
|
$dispatcher = \OC::$server->getEventDispatcher();
|
|
|
|
if ($platform instanceof SqlitePlatform) {
|
|
|
|
return new SQLiteMigrator($this, $config, $dispatcher);
|
|
|
|
} elseif ($platform instanceof OraclePlatform) {
|
|
|
|
return new OracleMigrator($this, $config, $dispatcher);
|
|
|
|
} elseif ($platform instanceof MySQLPlatform) {
|
|
|
|
return new MySQLMigrator($this, $config, $dispatcher);
|
|
|
|
} elseif ($platform instanceof PostgreSQL94Platform) {
|
|
|
|
return new PostgreSqlMigrator($this, $config, $dispatcher);
|
|
|
|
} else {
|
|
|
|
return new Migrator($this, $config, $dispatcher);
|
|
|
|
}
|
|
|
|
}
|
2013-02-25 11:19:49 +04:00
|
|
|
}
|