You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Julius Härtl <jus@bitgrid.net>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Ole Ostergaard <ole.c.ostergaard@gmail.com>
  12. * @author Ole Ostergaard <ole.ostergaard@knime.com>
  13. * @author Philipp Schaffrath <github@philipp.schaffrath.email>
  14. * @author Robin Appelman <robin@icewind.nl>
  15. * @author Robin McCorkell <robin@mccorkell.me.uk>
  16. * @author Roeland Jago Douma <roeland@famdouma.nl>
  17. * @author Thomas Müller <thomas.mueller@tmit.eu>
  18. *
  19. * @license AGPL-3.0
  20. *
  21. * This code is free software: you can redistribute it and/or modify
  22. * it under the terms of the GNU Affero General Public License, version 3,
  23. * as published by the Free Software Foundation.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU Affero General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Affero General Public License, version 3,
  31. * along with this program. If not, see <http://www.gnu.org/licenses/>
  32. *
  33. */
  34. namespace OC\DB;
  35. use Doctrine\Common\EventManager;
  36. use Doctrine\DBAL\Cache\QueryCacheProfile;
  37. use Doctrine\DBAL\Configuration;
  38. use Doctrine\DBAL\Driver;
  39. use Doctrine\DBAL\Exception;
  40. use Doctrine\DBAL\Exception\ConstraintViolationException;
  41. use Doctrine\DBAL\Exception\NotNullConstraintViolationException;
  42. use Doctrine\DBAL\Platforms\MySQLPlatform;
  43. use Doctrine\DBAL\Result;
  44. use Doctrine\DBAL\Schema\Schema;
  45. use Doctrine\DBAL\Statement;
  46. use OC\DB\QueryBuilder\QueryBuilder;
  47. use OC\SystemConfig;
  48. use OCP\DB\QueryBuilder\IQueryBuilder;
  49. use OCP\ILogger;
  50. use OCP\PreConditionNotMetException;
  51. class Connection extends \Doctrine\DBAL\Connection {
  52. /** @var string */
  53. protected $tablePrefix;
  54. /** @var \OC\DB\Adapter $adapter */
  55. protected $adapter;
  56. /** @var SystemConfig */
  57. private $systemConfig;
  58. /** @var ILogger */
  59. private $logger;
  60. protected $lockedTable = null;
  61. /** @var int */
  62. protected $queriesBuilt = 0;
  63. /** @var int */
  64. protected $queriesExecuted = 0;
  65. /**
  66. * @throws Exception
  67. */
  68. public function connect() {
  69. try {
  70. return parent::connect();
  71. } catch (Exception $e) {
  72. // throw a new exception to prevent leaking info from the stacktrace
  73. throw new Exception('Failed to connect to the database: ' . $e->getMessage(), $e->getCode());
  74. }
  75. }
  76. public function getStats(): array {
  77. return [
  78. 'built' => $this->queriesBuilt,
  79. 'executed' => $this->queriesExecuted,
  80. ];
  81. }
  82. /**
  83. * Returns a QueryBuilder for the connection.
  84. */
  85. public function getQueryBuilder(): IQueryBuilder {
  86. $this->queriesBuilt++;
  87. return new QueryBuilder(
  88. new ConnectionAdapter($this),
  89. $this->systemConfig,
  90. $this->logger
  91. );
  92. }
  93. /**
  94. * Gets the QueryBuilder for the connection.
  95. *
  96. * @return \Doctrine\DBAL\Query\QueryBuilder
  97. * @deprecated please use $this->getQueryBuilder() instead
  98. */
  99. public function createQueryBuilder() {
  100. $backtrace = $this->getCallerBacktrace();
  101. \OC::$server->getLogger()->debug('Doctrine QueryBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]);
  102. $this->queriesBuilt++;
  103. return parent::createQueryBuilder();
  104. }
  105. /**
  106. * Gets the ExpressionBuilder for the connection.
  107. *
  108. * @return \Doctrine\DBAL\Query\Expression\ExpressionBuilder
  109. * @deprecated please use $this->getQueryBuilder()->expr() instead
  110. */
  111. public function getExpressionBuilder() {
  112. $backtrace = $this->getCallerBacktrace();
  113. \OC::$server->getLogger()->debug('Doctrine ExpressionBuilder retrieved in {backtrace}', ['app' => 'core', 'backtrace' => $backtrace]);
  114. $this->queriesBuilt++;
  115. return parent::getExpressionBuilder();
  116. }
  117. /**
  118. * Get the file and line that called the method where `getCallerBacktrace()` was used
  119. *
  120. * @return string
  121. */
  122. protected function getCallerBacktrace() {
  123. $traces = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
  124. // 0 is the method where we use `getCallerBacktrace`
  125. // 1 is the target method which uses the method we want to log
  126. if (isset($traces[1])) {
  127. return $traces[1]['file'] . ':' . $traces[1]['line'];
  128. }
  129. return '';
  130. }
  131. /**
  132. * @return string
  133. */
  134. public function getPrefix() {
  135. return $this->tablePrefix;
  136. }
  137. /**
  138. * Initializes a new instance of the Connection class.
  139. *
  140. * @param array $params The connection parameters.
  141. * @param \Doctrine\DBAL\Driver $driver
  142. * @param \Doctrine\DBAL\Configuration $config
  143. * @param \Doctrine\Common\EventManager $eventManager
  144. * @throws \Exception
  145. */
  146. public function __construct(array $params, Driver $driver, Configuration $config = null,
  147. EventManager $eventManager = null) {
  148. if (!isset($params['adapter'])) {
  149. throw new \Exception('adapter not set');
  150. }
  151. if (!isset($params['tablePrefix'])) {
  152. throw new \Exception('tablePrefix not set');
  153. }
  154. /**
  155. * @psalm-suppress InternalMethod
  156. */
  157. parent::__construct($params, $driver, $config, $eventManager);
  158. $this->adapter = new $params['adapter']($this);
  159. $this->tablePrefix = $params['tablePrefix'];
  160. $this->systemConfig = \OC::$server->getSystemConfig();
  161. $this->logger = \OC::$server->getLogger();
  162. }
  163. /**
  164. * Prepares an SQL statement.
  165. *
  166. * @param string $statement The SQL statement to prepare.
  167. * @param int $limit
  168. * @param int $offset
  169. *
  170. * @return Statement The prepared statement.
  171. * @throws Exception
  172. */
  173. public function prepare($statement, $limit = null, $offset = null): Statement {
  174. if ($limit === -1) {
  175. $limit = null;
  176. }
  177. if (!is_null($limit)) {
  178. $platform = $this->getDatabasePlatform();
  179. $statement = $platform->modifyLimitQuery($statement, $limit, $offset);
  180. }
  181. $statement = $this->replaceTablePrefix($statement);
  182. $statement = $this->adapter->fixupStatement($statement);
  183. return parent::prepare($statement);
  184. }
  185. /**
  186. * Executes an, optionally parametrized, SQL query.
  187. *
  188. * If the query is parametrized, a prepared statement is used.
  189. * If an SQLLogger is configured, the execution is logged.
  190. *
  191. * @param string $sql The SQL query to execute.
  192. * @param array $params The parameters to bind to the query, if any.
  193. * @param array $types The types the previous parameters are in.
  194. * @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional.
  195. *
  196. * @return Result The executed statement.
  197. *
  198. * @throws \Doctrine\DBAL\Exception
  199. */
  200. public function executeQuery(string $sql, array $params = [], $types = [], QueryCacheProfile $qcp = null): Result {
  201. $sql = $this->replaceTablePrefix($sql);
  202. $sql = $this->adapter->fixupStatement($sql);
  203. $this->queriesExecuted++;
  204. return parent::executeQuery($sql, $params, $types, $qcp);
  205. }
  206. /**
  207. * @throws Exception
  208. */
  209. public function executeUpdate(string $sql, array $params = [], array $types = []): int {
  210. $sql = $this->replaceTablePrefix($sql);
  211. $sql = $this->adapter->fixupStatement($sql);
  212. $this->queriesExecuted++;
  213. return parent::executeUpdate($sql, $params, $types);
  214. }
  215. /**
  216. * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters
  217. * and returns the number of affected rows.
  218. *
  219. * This method supports PDO binding types as well as DBAL mapping types.
  220. *
  221. * @param string $sql The SQL query.
  222. * @param array $params The query parameters.
  223. * @param array $types The parameter types.
  224. *
  225. * @return int The number of affected rows.
  226. *
  227. * @throws \Doctrine\DBAL\Exception
  228. */
  229. public function executeStatement($sql, array $params = [], array $types = []): int {
  230. $sql = $this->replaceTablePrefix($sql);
  231. $sql = $this->adapter->fixupStatement($sql);
  232. $this->queriesExecuted++;
  233. return parent::executeStatement($sql, $params, $types);
  234. }
  235. /**
  236. * Returns the ID of the last inserted row, or the last value from a sequence object,
  237. * depending on the underlying driver.
  238. *
  239. * Note: This method may not return a meaningful or consistent result across different drivers,
  240. * because the underlying database may not even support the notion of AUTO_INCREMENT/IDENTITY
  241. * columns or sequences.
  242. *
  243. * @param string $seqName Name of the sequence object from which the ID should be returned.
  244. *
  245. * @return string the last inserted ID.
  246. * @throws Exception
  247. */
  248. public function lastInsertId($seqName = null) {
  249. if ($seqName) {
  250. $seqName = $this->replaceTablePrefix($seqName);
  251. }
  252. return $this->adapter->lastInsertId($seqName);
  253. }
  254. /**
  255. * @internal
  256. * @throws Exception
  257. */
  258. public function realLastInsertId($seqName = null) {
  259. return parent::lastInsertId($seqName);
  260. }
  261. /**
  262. * Insert a row if the matching row does not exists. To accomplish proper race condition avoidance
  263. * it is needed that there is also a unique constraint on the values. Then this method will
  264. * catch the exception and return 0.
  265. *
  266. * @param string $table The table name (will replace *PREFIX* with the actual prefix)
  267. * @param array $input data that should be inserted into the table (column name => value)
  268. * @param array|null $compare List of values that should be checked for "if not exists"
  269. * If this is null or an empty array, all keys of $input will be compared
  270. * Please note: text fields (clob) must not be used in the compare array
  271. * @return int number of inserted rows
  272. * @throws \Doctrine\DBAL\Exception
  273. * @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
  274. */
  275. public function insertIfNotExist($table, $input, array $compare = null) {
  276. return $this->adapter->insertIfNotExist($table, $input, $compare);
  277. }
  278. public function insertIgnoreConflict(string $table, array $values) : int {
  279. return $this->adapter->insertIgnoreConflict($table, $values);
  280. }
  281. private function getType($value) {
  282. if (is_bool($value)) {
  283. return IQueryBuilder::PARAM_BOOL;
  284. } elseif (is_int($value)) {
  285. return IQueryBuilder::PARAM_INT;
  286. } else {
  287. return IQueryBuilder::PARAM_STR;
  288. }
  289. }
  290. /**
  291. * Insert or update a row value
  292. *
  293. * @param string $table
  294. * @param array $keys (column name => value)
  295. * @param array $values (column name => value)
  296. * @param array $updatePreconditionValues ensure values match preconditions (column name => value)
  297. * @return int number of new rows
  298. * @throws \Doctrine\DBAL\Exception
  299. * @throws PreConditionNotMetException
  300. */
  301. public function setValues($table, array $keys, array $values, array $updatePreconditionValues = []) {
  302. try {
  303. $insertQb = $this->getQueryBuilder();
  304. $insertQb->insert($table)
  305. ->values(
  306. array_map(function ($value) use ($insertQb) {
  307. return $insertQb->createNamedParameter($value, $this->getType($value));
  308. }, array_merge($keys, $values))
  309. );
  310. return $insertQb->execute();
  311. } catch (NotNullConstraintViolationException $e) {
  312. throw $e;
  313. } catch (ConstraintViolationException $e) {
  314. // value already exists, try update
  315. $updateQb = $this->getQueryBuilder();
  316. $updateQb->update($table);
  317. foreach ($values as $name => $value) {
  318. $updateQb->set($name, $updateQb->createNamedParameter($value, $this->getType($value)));
  319. }
  320. $where = $updateQb->expr()->andX();
  321. $whereValues = array_merge($keys, $updatePreconditionValues);
  322. foreach ($whereValues as $name => $value) {
  323. if ($value === '') {
  324. $where->add($updateQb->expr()->emptyString(
  325. $name
  326. ));
  327. } else {
  328. $where->add($updateQb->expr()->eq(
  329. $name,
  330. $updateQb->createNamedParameter($value, $this->getType($value)),
  331. $this->getType($value)
  332. ));
  333. }
  334. }
  335. $updateQb->where($where);
  336. $affected = $updateQb->execute();
  337. if ($affected === 0 && !empty($updatePreconditionValues)) {
  338. throw new PreConditionNotMetException();
  339. }
  340. return 0;
  341. }
  342. }
  343. /**
  344. * Create an exclusive read+write lock on a table
  345. *
  346. * @param string $tableName
  347. *
  348. * @throws \BadMethodCallException When trying to acquire a second lock
  349. * @throws Exception
  350. * @since 9.1.0
  351. */
  352. public function lockTable($tableName) {
  353. if ($this->lockedTable !== null) {
  354. throw new \BadMethodCallException('Can not lock a new table until the previous lock is released.');
  355. }
  356. $tableName = $this->tablePrefix . $tableName;
  357. $this->lockedTable = $tableName;
  358. $this->adapter->lockTable($tableName);
  359. }
  360. /**
  361. * Release a previous acquired lock again
  362. *
  363. * @throws Exception
  364. * @since 9.1.0
  365. */
  366. public function unlockTable() {
  367. $this->adapter->unlockTable();
  368. $this->lockedTable = null;
  369. }
  370. /**
  371. * returns the error code and message as a string for logging
  372. * works with DoctrineException
  373. * @return string
  374. */
  375. public function getError() {
  376. $msg = $this->errorCode() . ': ';
  377. $errorInfo = $this->errorInfo();
  378. if (!empty($errorInfo)) {
  379. $msg .= 'SQLSTATE = '.$errorInfo[0] . ', ';
  380. $msg .= 'Driver Code = '.$errorInfo[1] . ', ';
  381. $msg .= 'Driver Message = '.$errorInfo[2];
  382. }
  383. return $msg;
  384. }
  385. public function errorCode() {
  386. return -1;
  387. }
  388. public function errorInfo() {
  389. return [];
  390. }
  391. /**
  392. * Drop a table from the database if it exists
  393. *
  394. * @param string $table table name without the prefix
  395. *
  396. * @throws Exception
  397. */
  398. public function dropTable($table) {
  399. $table = $this->tablePrefix . trim($table);
  400. $schema = $this->getSchemaManager();
  401. if ($schema->tablesExist([$table])) {
  402. $schema->dropTable($table);
  403. }
  404. }
  405. /**
  406. * Check if a table exists
  407. *
  408. * @param string $table table name without the prefix
  409. *
  410. * @return bool
  411. * @throws Exception
  412. */
  413. public function tableExists($table) {
  414. $table = $this->tablePrefix . trim($table);
  415. $schema = $this->getSchemaManager();
  416. return $schema->tablesExist([$table]);
  417. }
  418. // internal use
  419. /**
  420. * @param string $statement
  421. * @return string
  422. */
  423. protected function replaceTablePrefix($statement) {
  424. return str_replace('*PREFIX*', $this->tablePrefix, $statement);
  425. }
  426. /**
  427. * Check if a transaction is active
  428. *
  429. * @return bool
  430. * @since 8.2.0
  431. */
  432. public function inTransaction() {
  433. return $this->getTransactionNestingLevel() > 0;
  434. }
  435. /**
  436. * Escape a parameter to be used in a LIKE query
  437. *
  438. * @param string $param
  439. * @return string
  440. */
  441. public function escapeLikeParameter($param) {
  442. return addcslashes($param, '\\_%');
  443. }
  444. /**
  445. * Check whether or not the current database support 4byte wide unicode
  446. *
  447. * @return bool
  448. * @since 11.0.0
  449. */
  450. public function supports4ByteText() {
  451. if (!$this->getDatabasePlatform() instanceof MySQLPlatform) {
  452. return true;
  453. }
  454. return $this->getParams()['charset'] === 'utf8mb4';
  455. }
  456. /**
  457. * Create the schema of the connected database
  458. *
  459. * @return Schema
  460. * @throws Exception
  461. */
  462. public function createSchema() {
  463. $schemaManager = new MDB2SchemaManager($this);
  464. $migrator = $schemaManager->getMigrator();
  465. return $migrator->createSchema();
  466. }
  467. /**
  468. * Migrate the database to the given schema
  469. *
  470. * @param Schema $toSchema
  471. *
  472. * @throws Exception
  473. */
  474. public function migrateToSchema(Schema $toSchema) {
  475. $schemaManager = new MDB2SchemaManager($this);
  476. $migrator = $schemaManager->getMigrator();
  477. $migrator->migrate($toSchema);
  478. }
  479. }