Adapter.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OC\DB;
  30. use Doctrine\DBAL\Exception;
  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. *
  47. * @return int id of last insert statement
  48. * @throws Exception
  49. */
  50. public function lastInsertId($table) {
  51. return (int) $this->conn->realLastInsertId($table);
  52. }
  53. /**
  54. * @param string $statement that needs to be changed so the db can handle it
  55. * @return string changed statement
  56. */
  57. public function fixupStatement($statement) {
  58. return $statement;
  59. }
  60. /**
  61. * Create an exclusive read+write lock on a table
  62. *
  63. * @param string $tableName
  64. * @throws Exception
  65. * @since 9.1.0
  66. */
  67. public function lockTable($tableName) {
  68. $this->conn->beginTransaction();
  69. $this->conn->executeUpdate('LOCK TABLE `' .$tableName . '` IN EXCLUSIVE MODE');
  70. }
  71. /**
  72. * Release a previous acquired lock again
  73. *
  74. * @throws Exception
  75. * @since 9.1.0
  76. */
  77. public function unlockTable() {
  78. $this->conn->commit();
  79. }
  80. /**
  81. * Insert a row if the matching row does not exists. To accomplish proper race condition avoidance
  82. * it is needed that there is also a unique constraint on the values. Then this method will
  83. * catch the exception and return 0.
  84. *
  85. * @param string $table The table name (will replace *PREFIX* with the actual prefix)
  86. * @param array $input data that should be inserted into the table (column name => value)
  87. * @param array|null $compare List of values that should be checked for "if not exists"
  88. * If this is null or an empty array, all keys of $input will be compared
  89. * Please note: text fields (clob) must not be used in the compare array
  90. * @return int number of inserted rows
  91. * @throws Exception
  92. * @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
  93. */
  94. public function insertIfNotExist($table, $input, array $compare = null) {
  95. if (empty($compare)) {
  96. $compare = array_keys($input);
  97. }
  98. $query = 'INSERT INTO `' .$table . '` (`'
  99. . implode('`,`', array_keys($input)) . '`) SELECT '
  100. . str_repeat('?,', count($input) - 1).'? ' // Is there a prettier alternative?
  101. . 'FROM `' . $table . '` WHERE ';
  102. $inserts = array_values($input);
  103. foreach ($compare as $key) {
  104. $query .= '`' . $key . '`';
  105. if (is_null($input[$key])) {
  106. $query .= ' IS NULL AND ';
  107. } else {
  108. $inserts[] = $input[$key];
  109. $query .= ' = ? AND ';
  110. }
  111. }
  112. $query = substr($query, 0, -5);
  113. $query .= ' HAVING COUNT(*) = 0';
  114. try {
  115. return $this->conn->executeUpdate($query, $inserts);
  116. } catch (UniqueConstraintViolationException $e) {
  117. // if this is thrown then a concurrent insert happened between the insert and the sub-select in the insert, that should have avoided it
  118. // it's fine to ignore this then
  119. //
  120. // more discussions about this can be found at https://github.com/nextcloud/server/pull/12315
  121. return 0;
  122. }
  123. }
  124. /**
  125. * @throws \OCP\DB\Exception
  126. */
  127. public function insertIgnoreConflict(string $table, array $values) : int {
  128. try {
  129. $builder = $this->conn->getQueryBuilder();
  130. $builder->insert($table);
  131. foreach ($values as $key => $value) {
  132. $builder->setValue($key, $builder->createNamedParameter($value));
  133. }
  134. return $builder->execute();
  135. } catch (UniqueConstraintViolationException $e) {
  136. return 0;
  137. }
  138. }
  139. }