adaptersqlite.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\DB;
  9. class AdapterSqlite extends Adapter {
  10. public function fixupStatement($statement) {
  11. $statement = str_replace( '`', '"', $statement );
  12. $statement = str_ireplace( 'NOW()', 'datetime(\'now\')', $statement );
  13. $statement = str_ireplace( 'UNIX_TIMESTAMP()', 'strftime(\'%s\',\'now\')', $statement );
  14. return $statement;
  15. }
  16. public function insertIfNotExist($table, $input) {
  17. // NOTE: For SQLite we have to use this clumsy approach
  18. // otherwise all fieldnames used must have a unique key.
  19. $query = 'SELECT COUNT(*) FROM `' . $table . '` WHERE ';
  20. foreach($input as $key => $value) {
  21. $query .= '`' . $key . '` = ? AND ';
  22. }
  23. $query = substr($query, 0, strlen($query) - 5);
  24. try {
  25. $stmt = $this->conn->prepare($query);
  26. $result = $stmt->execute(array_values($input));
  27. } catch(\Doctrine\DBAL\DBALException $e) {
  28. $entry = 'DB Error: "'.$e->getMessage() . '"<br />';
  29. $entry .= 'Offending command was: ' . $query . '<br />';
  30. \OC_Log::write('core', $entry, \OC_Log::FATAL);
  31. error_log('DB error: '.$entry);
  32. \OC_Template::printErrorPage( $entry );
  33. }
  34. if ($stmt->fetchColumn() === '0') {
  35. $query = 'INSERT INTO `' . $table . '` (`'
  36. . implode('`,`', array_keys($input)) . '`) VALUES('
  37. . str_repeat('?,', count($input)-1).'? ' . ')';
  38. } else {
  39. return 0; //no rows updated
  40. }
  41. try {
  42. $statement = $this->conn->prepare($query);
  43. $result = $statement->execute(array_values($input));
  44. } catch(\Doctrine\DBAL\DBALException $e) {
  45. $entry = 'DB Error: "'.$e->getMessage() . '"<br />';
  46. $entry .= 'Offending command was: ' . $query.'<br />';
  47. \OC_Log::write('core', $entry, \OC_Log::FATAL);
  48. error_log('DB error: ' . $entry);
  49. \OC_Template::printErrorPage( $entry );
  50. }
  51. return $result;
  52. }
  53. }