You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.7 KiB

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