Adapter.php 4.1 KB

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