2013-02-25 02:19:49 -05:00
|
|
|
<?php
|
2024-05-23 03:26:56 -04:00
|
|
|
|
2013-02-25 02:19:49 -05:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2013-02-25 02:19:49 -05:00
|
|
|
*/
|
|
|
|
|
namespace OC\DB;
|
|
|
|
|
|
|
|
|
|
class AdapterPgSql extends Adapter {
|
2019-07-30 18:53:20 -04:00
|
|
|
protected $compatModePre9_5 = null;
|
|
|
|
|
|
2013-03-22 13:36:40 -04:00
|
|
|
public function lastInsertId($table) {
|
2021-01-03 09:28:31 -05:00
|
|
|
$result = $this->conn->executeQuery('SELECT lastval()');
|
|
|
|
|
$val = $result->fetchOne();
|
|
|
|
|
$result->free();
|
|
|
|
|
return (int)$val;
|
2013-03-22 13:36:40 -04:00
|
|
|
}
|
2013-02-25 16:49:55 -05:00
|
|
|
|
2020-04-10 10:54:27 -04:00
|
|
|
public const UNIX_TIMESTAMP_REPLACEMENT = 'cast(extract(epoch from current_timestamp) as integer)';
|
2013-02-25 16:49:55 -05:00
|
|
|
public function fixupStatement($statement) {
|
2020-04-09 10:07:47 -04:00
|
|
|
$statement = str_replace('`', '"', $statement);
|
|
|
|
|
$statement = str_ireplace('UNIX_TIMESTAMP()', self::UNIX_TIMESTAMP_REPLACEMENT, $statement);
|
2013-02-25 16:49:55 -05:00
|
|
|
return $statement;
|
|
|
|
|
}
|
2019-01-21 11:54:40 -05:00
|
|
|
|
2022-01-12 14:44:38 -05:00
|
|
|
public function insertIgnoreConflict(string $table, array $values) : int {
|
2020-04-10 08:19:56 -04:00
|
|
|
if ($this->isPre9_5CompatMode() === true) {
|
2019-07-30 18:53:20 -04:00
|
|
|
return parent::insertIgnoreConflict($table, $values);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// "upsert" is only available since PgSQL 9.5, but the generic way
|
|
|
|
|
// would leave error logs in the DB.
|
2019-01-21 11:54:40 -05:00
|
|
|
$builder = $this->conn->getQueryBuilder();
|
2019-02-26 08:08:55 -05:00
|
|
|
$builder->insert($table);
|
2019-07-30 18:53:20 -04:00
|
|
|
foreach ($values as $key => $value) {
|
2019-01-21 11:54:40 -05:00
|
|
|
$builder->setValue($key, $builder->createNamedParameter($value));
|
|
|
|
|
}
|
|
|
|
|
$queryString = $builder->getSQL() . ' ON CONFLICT DO NOTHING';
|
2019-02-26 08:08:55 -05:00
|
|
|
return $this->conn->executeUpdate($queryString, $builder->getParameters(), $builder->getParameterTypes());
|
2019-01-21 11:54:40 -05:00
|
|
|
}
|
2019-07-30 18:53:20 -04:00
|
|
|
|
|
|
|
|
protected function isPre9_5CompatMode(): bool {
|
2020-04-10 08:19:56 -04:00
|
|
|
if ($this->compatModePre9_5 !== null) {
|
2019-07-30 18:53:20 -04:00
|
|
|
return $this->compatModePre9_5;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-03 09:28:31 -05:00
|
|
|
$result = $this->conn->executeQuery('SHOW SERVER_VERSION');
|
|
|
|
|
$version = $result->fetchOne();
|
|
|
|
|
$result->free();
|
2019-07-30 18:53:20 -04:00
|
|
|
$this->compatModePre9_5 = version_compare($version, '9.5', '<');
|
|
|
|
|
|
|
|
|
|
return $this->compatModePre9_5;
|
|
|
|
|
}
|
2013-02-25 02:19:49 -05:00
|
|
|
}
|