2013-02-25 07:19:49 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 15:07:57 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2019-12-03 18:57:53 +00:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
2020-04-29 09:57:22 +00:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2019-12-03 18:57:53 +00:00
|
|
|
* @author Ole Ostergaard <ole.c.ostergaard@gmail.com>
|
|
|
|
* @author Ole Ostergaard <ole.ostergaard@knime.com>
|
2015-03-26 10:44:34 +00:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 18:57:53 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-03-26 10:44:34 +00:00
|
|
|
*
|
2013-02-25 07:19:49 +00:00
|
|
|
*/
|
|
|
|
namespace OC\DB;
|
|
|
|
|
|
|
|
class AdapterPgSql extends Adapter {
|
2019-07-30 22:53:20 +00:00
|
|
|
protected $compatModePre9_5 = null;
|
|
|
|
|
2013-03-22 17:36:40 +00:00
|
|
|
public function lastInsertId($table) {
|
2021-01-03 14:28:31 +00:00
|
|
|
$result = $this->conn->executeQuery('SELECT lastval()');
|
|
|
|
$val = $result->fetchOne();
|
|
|
|
$result->free();
|
|
|
|
return (int)$val;
|
2013-03-22 17:36:40 +00:00
|
|
|
}
|
2013-02-25 21:49:55 +00:00
|
|
|
|
2020-04-10 14:54:27 +00:00
|
|
|
public const UNIX_TIMESTAMP_REPLACEMENT = 'cast(extract(epoch from current_timestamp) as integer)';
|
2013-02-25 21:49:55 +00:00
|
|
|
public function fixupStatement($statement) {
|
2020-04-09 14:07:47 +00:00
|
|
|
$statement = str_replace('`', '"', $statement);
|
|
|
|
$statement = str_ireplace('UNIX_TIMESTAMP()', self::UNIX_TIMESTAMP_REPLACEMENT, $statement);
|
2013-02-25 21:49:55 +00:00
|
|
|
return $statement;
|
|
|
|
}
|
2019-01-21 16:54:40 +00:00
|
|
|
|
2022-01-12 19:44:38 +00:00
|
|
|
public function insertIgnoreConflict(string $table, array $values) : int {
|
2020-04-10 12:19:56 +00:00
|
|
|
if ($this->isPre9_5CompatMode() === true) {
|
2019-07-30 22:53:20 +00: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 16:54:40 +00:00
|
|
|
$builder = $this->conn->getQueryBuilder();
|
2019-02-26 13:08:55 +00:00
|
|
|
$builder->insert($table);
|
2019-07-30 22:53:20 +00:00
|
|
|
foreach ($values as $key => $value) {
|
2019-01-21 16:54:40 +00:00
|
|
|
$builder->setValue($key, $builder->createNamedParameter($value));
|
|
|
|
}
|
|
|
|
$queryString = $builder->getSQL() . ' ON CONFLICT DO NOTHING';
|
2019-02-26 13:08:55 +00:00
|
|
|
return $this->conn->executeUpdate($queryString, $builder->getParameters(), $builder->getParameterTypes());
|
2019-01-21 16:54:40 +00:00
|
|
|
}
|
2019-07-30 22:53:20 +00:00
|
|
|
|
|
|
|
protected function isPre9_5CompatMode(): bool {
|
2020-04-10 12:19:56 +00:00
|
|
|
if ($this->compatModePre9_5 !== null) {
|
2019-07-30 22:53:20 +00:00
|
|
|
return $this->compatModePre9_5;
|
|
|
|
}
|
|
|
|
|
2021-01-03 14:28:31 +00:00
|
|
|
$result = $this->conn->executeQuery('SHOW SERVER_VERSION');
|
|
|
|
$version = $result->fetchOne();
|
|
|
|
$result->free();
|
2019-07-30 22:53:20 +00:00
|
|
|
$this->compatModePre9_5 = version_compare($version, '9.5', '<');
|
|
|
|
|
|
|
|
return $this->compatModePre9_5;
|
|
|
|
}
|
2013-02-25 07:19:49 +00:00
|
|
|
}
|