1
0

AdapterPgSql.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\DB;
  8. class AdapterPgSql extends Adapter {
  9. public function lastInsertId($table) {
  10. $result = $this->conn->executeQuery('SELECT lastval()');
  11. $val = $result->fetchOne();
  12. $result->free();
  13. return (int)$val;
  14. }
  15. public const UNIX_TIMESTAMP_REPLACEMENT = 'cast(extract(epoch from current_timestamp) as integer)';
  16. public function fixupStatement($statement) {
  17. $statement = str_replace('`', '"', $statement);
  18. $statement = str_ireplace('UNIX_TIMESTAMP()', self::UNIX_TIMESTAMP_REPLACEMENT, $statement);
  19. return $statement;
  20. }
  21. public function insertIgnoreConflict(string $table, array $values) : int {
  22. // "upsert" is only available since PgSQL 9.5, but the generic way
  23. // would leave error logs in the DB.
  24. $builder = $this->conn->getQueryBuilder();
  25. $builder->insert($table);
  26. foreach ($values as $key => $value) {
  27. $builder->setValue($key, $builder->createNamedParameter($value));
  28. }
  29. $queryString = $builder->getSQL() . ' ON CONFLICT DO NOTHING';
  30. return $this->conn->executeUpdate($queryString, $builder->getParameters(), $builder->getParameterTypes());
  31. }
  32. }