adaptersqlite.php 2.7 KB

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