AdapterSqlite.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. class AdapterSqlite extends Adapter {
  28. /**
  29. * @param string $tableName
  30. */
  31. public function lockTable($tableName) {
  32. $this->conn->executeUpdate('BEGIN EXCLUSIVE TRANSACTION');
  33. }
  34. public function unlockTable() {
  35. $this->conn->executeUpdate('COMMIT TRANSACTION');
  36. }
  37. public function fixupStatement($statement) {
  38. $statement = preg_replace('/`(\w+)` ILIKE \?/', 'LOWER($1) LIKE LOWER(?)', $statement);
  39. $statement = str_replace( '`', '"', $statement );
  40. $statement = str_ireplace( 'NOW()', 'datetime(\'now\')', $statement );
  41. $statement = str_ireplace('GREATEST(', 'MAX(', $statement);
  42. $statement = str_ireplace( 'UNIX_TIMESTAMP()', 'strftime(\'%s\',\'now\')', $statement );
  43. return $statement;
  44. }
  45. /**
  46. * Insert a row if the matching row does not exists.
  47. *
  48. * @param string $table The table name (will replace *PREFIX* with the actual prefix)
  49. * @param array $input data that should be inserted into the table (column name => value)
  50. * @param array|null $compare List of values that should be checked for "if not exists"
  51. * If this is null or an empty array, all keys of $input will be compared
  52. * Please note: text fields (clob) must not be used in the compare array
  53. * @return int number of inserted rows
  54. * @throws \Doctrine\DBAL\DBALException
  55. */
  56. public function insertIfNotExist($table, $input, array $compare = null) {
  57. if (empty($compare)) {
  58. $compare = array_keys($input);
  59. }
  60. $fieldList = '`' . implode('`,`', array_keys($input)) . '`';
  61. $query = "INSERT INTO `$table` ($fieldList) SELECT "
  62. . str_repeat('?,', count($input)-1).'? '
  63. . " WHERE NOT EXISTS (SELECT 1 FROM `$table` WHERE ";
  64. $inserts = array_values($input);
  65. foreach($compare as $key) {
  66. $query .= '`' . $key . '`';
  67. if (is_null($input[$key])) {
  68. $query .= ' IS NULL AND ';
  69. } else {
  70. $inserts[] = $input[$key];
  71. $query .= ' = ? AND ';
  72. }
  73. }
  74. $query = substr($query, 0, -5);
  75. $query .= ')';
  76. return $this->conn->executeUpdate($query, $inserts);
  77. }
  78. }