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.

70 lines
2.4 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Ole Ostergaard <ole.c.ostergaard@gmail.com>
  10. * @author Ole Ostergaard <ole.ostergaard@knime.com>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\DB;
  28. class AdapterPgSql extends Adapter {
  29. protected $compatModePre9_5 = null;
  30. public function lastInsertId($table) {
  31. return $this->conn->fetchColumn('SELECT lastval()');
  32. }
  33. public const UNIX_TIMESTAMP_REPLACEMENT = 'cast(extract(epoch from current_timestamp) as integer)';
  34. public function fixupStatement($statement) {
  35. $statement = str_replace('`', '"', $statement);
  36. $statement = str_ireplace('UNIX_TIMESTAMP()', self::UNIX_TIMESTAMP_REPLACEMENT, $statement);
  37. return $statement;
  38. }
  39. public function insertIgnoreConflict(string $table,array $values) : int {
  40. if ($this->isPre9_5CompatMode() === true) {
  41. return parent::insertIgnoreConflict($table, $values);
  42. }
  43. // "upsert" is only available since PgSQL 9.5, but the generic way
  44. // would leave error logs in the DB.
  45. $builder = $this->conn->getQueryBuilder();
  46. $builder->insert($table);
  47. foreach ($values as $key => $value) {
  48. $builder->setValue($key, $builder->createNamedParameter($value));
  49. }
  50. $queryString = $builder->getSQL() . ' ON CONFLICT DO NOTHING';
  51. return $this->conn->executeUpdate($queryString, $builder->getParameters(), $builder->getParameterTypes());
  52. }
  53. protected function isPre9_5CompatMode(): bool {
  54. if ($this->compatModePre9_5 !== null) {
  55. return $this->compatModePre9_5;
  56. }
  57. $version = $this->conn->fetchColumn('SHOW SERVER_VERSION');
  58. $this->compatModePre9_5 = version_compare($version, '9.5', '<');
  59. return $this->compatModePre9_5;
  60. }
  61. }