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.

145 lines
4.6 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Jonny007-MKD <1-23-4-5@web.de>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Ole Ostergaard <ole.c.ostergaard@gmail.com>
  11. * @author Ole Ostergaard <ole.ostergaard@knime.com>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OC\DB;
  31. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  32. /**
  33. * This handles the way we use to write queries, into something that can be
  34. * handled by the database abstraction layer.
  35. */
  36. class Adapter {
  37. /**
  38. * @var \OC\DB\Connection $conn
  39. */
  40. protected $conn;
  41. public function __construct($conn) {
  42. $this->conn = $conn;
  43. }
  44. /**
  45. * @param string $table name
  46. * @return int id of last insert statement
  47. */
  48. public function lastInsertId($table) {
  49. return $this->conn->realLastInsertId($table);
  50. }
  51. /**
  52. * @param string $statement that needs to be changed so the db can handle it
  53. * @return string changed statement
  54. */
  55. public function fixupStatement($statement) {
  56. return $statement;
  57. }
  58. /**
  59. * Create an exclusive read+write lock on a table
  60. *
  61. * @param string $tableName
  62. * @since 9.1.0
  63. */
  64. public function lockTable($tableName) {
  65. $this->conn->beginTransaction();
  66. $this->conn->executeUpdate('LOCK TABLE `' .$tableName . '` IN EXCLUSIVE MODE');
  67. }
  68. /**
  69. * Release a previous acquired lock again
  70. *
  71. * @since 9.1.0
  72. */
  73. public function unlockTable() {
  74. $this->conn->commit();
  75. }
  76. /**
  77. * Insert a row if the matching row does not exists. To accomplish proper race condition avoidance
  78. * it is needed that there is also a unique constraint on the values. Then this method will
  79. * catch the exception and return 0.
  80. *
  81. * @param string $table The table name (will replace *PREFIX* with the actual prefix)
  82. * @param array $input data that should be inserted into the table (column name => value)
  83. * @param array|null $compare List of values that should be checked for "if not exists"
  84. * If this is null or an empty array, all keys of $input will be compared
  85. * Please note: text fields (clob) must not be used in the compare array
  86. * @return int number of inserted rows
  87. * @throws \Doctrine\DBAL\DBALException
  88. * @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
  89. */
  90. public function insertIfNotExist($table, $input, array $compare = null) {
  91. if (empty($compare)) {
  92. $compare = array_keys($input);
  93. }
  94. $query = 'INSERT INTO `' .$table . '` (`'
  95. . implode('`,`', array_keys($input)) . '`) SELECT '
  96. . str_repeat('?,', count($input)-1).'? ' // Is there a prettier alternative?
  97. . 'FROM `' . $table . '` WHERE ';
  98. $inserts = array_values($input);
  99. foreach ($compare as $key) {
  100. $query .= '`' . $key . '`';
  101. if (is_null($input[$key])) {
  102. $query .= ' IS NULL AND ';
  103. } else {
  104. $inserts[] = $input[$key];
  105. $query .= ' = ? AND ';
  106. }
  107. }
  108. $query = substr($query, 0, -5);
  109. $query .= ' HAVING COUNT(*) = 0';
  110. try {
  111. return $this->conn->executeUpdate($query, $inserts);
  112. } catch (UniqueConstraintViolationException $e) {
  113. // if this is thrown then a concurrent insert happened between the insert and the sub-select in the insert, that should have avoided it
  114. // it's fine to ignore this then
  115. //
  116. // more discussions about this can be found at https://github.com/nextcloud/server/pull/12315
  117. return 0;
  118. }
  119. }
  120. public function insertIgnoreConflict(string $table,array $values) : int {
  121. try {
  122. $builder = $this->conn->getQueryBuilder();
  123. $builder->insert($table);
  124. foreach ($values as $key => $value) {
  125. $builder->setValue($key, $builder->createNamedParameter($value));
  126. }
  127. return $builder->execute();
  128. } catch (UniqueConstraintViolationException $e) {
  129. return 0;
  130. }
  131. }
  132. }