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.4KB

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