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.

Adapter.php 4.6KB

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