Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AdapterSqlite.php 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 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. class AdapterSqlite extends Adapter {
  30. /**
  31. * @param string $tableName
  32. */
  33. public function lockTable($tableName) {
  34. $this->conn->executeUpdate('BEGIN EXCLUSIVE TRANSACTION');
  35. }
  36. public function unlockTable() {
  37. $this->conn->executeUpdate('COMMIT TRANSACTION');
  38. }
  39. public function fixupStatement($statement) {
  40. $statement = preg_replace('/`(\w+)` ILIKE \?/', 'LOWER($1) LIKE LOWER(?)', $statement);
  41. $statement = str_replace('`', '"', $statement);
  42. $statement = str_ireplace('NOW()', 'datetime(\'now\')', $statement);
  43. $statement = str_ireplace('GREATEST(', 'MAX(', $statement);
  44. $statement = str_ireplace('UNIX_TIMESTAMP()', 'strftime(\'%s\',\'now\')', $statement);
  45. return $statement;
  46. }
  47. /**
  48. * Insert a row if the matching row does not exists. To accomplish proper race condition avoidance
  49. * it is needed that there is also a unique constraint on the values. Then this method will
  50. * catch the exception and return 0.
  51. *
  52. * @param string $table The table name (will replace *PREFIX* with the actual prefix)
  53. * @param array $input data that should be inserted into the table (column name => value)
  54. * @param array|null $compare List of values that should be checked for "if not exists"
  55. * If this is null or an empty array, all keys of $input will be compared
  56. * Please note: text fields (clob) must not be used in the compare array
  57. * @return int number of inserted rows
  58. * @throws \Doctrine\DBAL\Exception
  59. * @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
  60. */
  61. public function insertIfNotExist($table, $input, array $compare = null) {
  62. if (empty($compare)) {
  63. $compare = array_keys($input);
  64. }
  65. $fieldList = '`' . implode('`,`', array_keys($input)) . '`';
  66. $query = "INSERT INTO `$table` ($fieldList) SELECT "
  67. . str_repeat('?,', count($input) - 1).'? '
  68. . " WHERE NOT EXISTS (SELECT 1 FROM `$table` WHERE ";
  69. $inserts = array_values($input);
  70. foreach ($compare as $key) {
  71. $query .= '`' . $key . '`';
  72. if (is_null($input[$key])) {
  73. $query .= ' IS NULL AND ';
  74. } else {
  75. $inserts[] = $input[$key];
  76. $query .= ' = ? AND ';
  77. }
  78. }
  79. $query = substr($query, 0, -5);
  80. $query .= ')';
  81. try {
  82. return $this->conn->executeUpdate($query, $inserts);
  83. } catch (UniqueConstraintViolationException $e) {
  84. // if this is thrown then a concurrent insert happened between the insert and the sub-select in the insert, that should have avoided it
  85. // it's fine to ignore this then
  86. //
  87. // more discussions about this can be found at https://github.com/nextcloud/server/pull/12315
  88. return 0;
  89. }
  90. }
  91. }