adaptersqlite.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * @author Bart Visscher <bartv@thisnet.nl>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <icewind@owncloud.com>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. *
  9. * @copyright Copyright (c) 2015, ownCloud, Inc.
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\DB;
  26. class AdapterSqlite extends Adapter {
  27. public function fixupStatement($statement) {
  28. $statement = preg_replace('/`(\w+)` ILIKE \?/', 'LOWER($1) LIKE LOWER(?)', $statement);
  29. $statement = str_replace( '`', '"', $statement );
  30. $statement = str_ireplace( 'NOW()', 'datetime(\'now\')', $statement );
  31. $statement = str_ireplace( 'UNIX_TIMESTAMP()', 'strftime(\'%s\',\'now\')', $statement );
  32. return $statement;
  33. }
  34. /**
  35. * Insert a row if the matching row does not exists.
  36. *
  37. * @param string $table The table name (will replace *PREFIX* with the actual prefix)
  38. * @param array $input data that should be inserted into the table (column name => value)
  39. * @param array|null $compare List of values that should be checked for "if not exists"
  40. * If this is null or an empty array, all keys of $input will be compared
  41. * Please note: text fields (clob) must not be used in the compare array
  42. * @return int number of inserted rows
  43. * @throws \Doctrine\DBAL\DBALException
  44. */
  45. public function insertIfNotExist($table, $input, array $compare = null) {
  46. if (empty($compare)) {
  47. $compare = array_keys($input);
  48. }
  49. $fieldList = '`' . implode('`,`', array_keys($input)) . '`';
  50. $query = "INSERT INTO `$table` ($fieldList) SELECT "
  51. . str_repeat('?,', count($input)-1).'? '
  52. . " WHERE NOT EXISTS (SELECT 1 FROM `$table` WHERE ";
  53. $inserts = array_values($input);
  54. foreach($compare as $key) {
  55. $query .= '`' . $key . '`';
  56. if (is_null($input[$key])) {
  57. $query .= ' IS NULL AND ';
  58. } else {
  59. $inserts[] = $input[$key];
  60. $query .= ' = ? AND ';
  61. }
  62. }
  63. $query = substr($query, 0, strlen($query) - 5);
  64. $query .= ')';
  65. return $this->conn->executeUpdate($query, $inserts);
  66. }
  67. }