1
0

AdapterSqlite.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\DB;
  27. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  28. class AdapterSqlite extends Adapter {
  29. /**
  30. * @param string $tableName
  31. */
  32. public function lockTable($tableName) {
  33. $this->conn->executeUpdate('BEGIN EXCLUSIVE TRANSACTION');
  34. }
  35. public function unlockTable() {
  36. $this->conn->executeUpdate('COMMIT TRANSACTION');
  37. }
  38. public function fixupStatement($statement) {
  39. $statement = preg_replace('/`(\w+)` ILIKE \?/', 'LOWER($1) LIKE LOWER(?)', $statement);
  40. $statement = str_replace( '`', '"', $statement );
  41. $statement = str_ireplace( 'NOW()', 'datetime(\'now\')', $statement );
  42. $statement = str_ireplace('GREATEST(', 'MAX(', $statement);
  43. $statement = str_ireplace( 'UNIX_TIMESTAMP()', 'strftime(\'%s\',\'now\')', $statement );
  44. return $statement;
  45. }
  46. /**
  47. * Insert a row if the matching row does not exists. To accomplish proper race condition avoidance
  48. * it is needed that there is also a unique constraint on the values. Then this method will
  49. * catch the exception and return 0.
  50. *
  51. * @param string $table The table name (will replace *PREFIX* with the actual prefix)
  52. * @param array $input data that should be inserted into the table (column name => value)
  53. * @param array|null $compare List of values that should be checked for "if not exists"
  54. * If this is null or an empty array, all keys of $input will be compared
  55. * Please note: text fields (clob) must not be used in the compare array
  56. * @return int number of inserted rows
  57. * @throws \Doctrine\DBAL\DBALException
  58. * @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
  59. */
  60. public function insertIfNotExist($table, $input, array $compare = null) {
  61. if (empty($compare)) {
  62. $compare = array_keys($input);
  63. }
  64. $fieldList = '`' . implode('`,`', array_keys($input)) . '`';
  65. $query = "INSERT INTO `$table` ($fieldList) SELECT "
  66. . str_repeat('?,', count($input)-1).'? '
  67. . " WHERE NOT EXISTS (SELECT 1 FROM `$table` WHERE ";
  68. $inserts = array_values($input);
  69. foreach($compare as $key) {
  70. $query .= '`' . $key . '`';
  71. if (is_null($input[$key])) {
  72. $query .= ' IS NULL AND ';
  73. } else {
  74. $inserts[] = $input[$key];
  75. $query .= ' = ? AND ';
  76. }
  77. }
  78. $query = substr($query, 0, -5);
  79. $query .= ')';
  80. try {
  81. return $this->conn->executeUpdate($query, $inserts);
  82. } catch (UniqueConstraintViolationException $e) {
  83. // if this is thrown then a concurrent insert happened between the insert and the sub-select in the insert, that should have avoided it
  84. // it's fine to ignore this then
  85. //
  86. // more discussions about this can be found at https://github.com/nextcloud/server/pull/12315
  87. return 0;
  88. }
  89. }
  90. }