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.

AdapterPgSql.php 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. $result = $this->conn->executeQuery('SELECT lastval()');
  32. $val = $result->fetchOne();
  33. $result->free();
  34. return (int)$val;
  35. }
  36. public const UNIX_TIMESTAMP_REPLACEMENT = 'cast(extract(epoch from current_timestamp) as integer)';
  37. public function fixupStatement($statement) {
  38. $statement = str_replace('`', '"', $statement);
  39. $statement = str_ireplace('UNIX_TIMESTAMP()', self::UNIX_TIMESTAMP_REPLACEMENT, $statement);
  40. return $statement;
  41. }
  42. public function insertIgnoreConflict(string $table, array $values) : int {
  43. if ($this->isPre9_5CompatMode() === true) {
  44. return parent::insertIgnoreConflict($table, $values);
  45. }
  46. // "upsert" is only available since PgSQL 9.5, but the generic way
  47. // would leave error logs in the DB.
  48. $builder = $this->conn->getQueryBuilder();
  49. $builder->insert($table);
  50. foreach ($values as $key => $value) {
  51. $builder->setValue($key, $builder->createNamedParameter($value));
  52. }
  53. $queryString = $builder->getSQL() . ' ON CONFLICT DO NOTHING';
  54. return $this->conn->executeUpdate($queryString, $builder->getParameters(), $builder->getParameterTypes());
  55. }
  56. protected function isPre9_5CompatMode(): bool {
  57. if ($this->compatModePre9_5 !== null) {
  58. return $this->compatModePre9_5;
  59. }
  60. $result = $this->conn->executeQuery('SHOW SERVER_VERSION');
  61. $version = $result->fetchOne();
  62. $result->free();
  63. $this->compatModePre9_5 = version_compare($version, '9.5', '<');
  64. return $this->compatModePre9_5;
  65. }
  66. }