AdapterPgSql.php 2.4 KB

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 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. use Doctrine\DBAL\DBALException;
  28. class AdapterPgSql extends Adapter {
  29. protected $compatModePre9_5 = null;
  30. public function lastInsertId($table) {
  31. return $this->conn->fetchColumn('SELECT lastval()');
  32. }
  33. const UNIX_TIMESTAMP_REPLACEMENT = 'cast(extract(epoch from current_timestamp) as integer)';
  34. public function fixupStatement($statement) {
  35. $statement = str_replace( '`', '"', $statement );
  36. $statement = str_ireplace( 'UNIX_TIMESTAMP()', self::UNIX_TIMESTAMP_REPLACEMENT, $statement );
  37. return $statement;
  38. }
  39. /**
  40. * @suppress SqlInjectionChecker
  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. $version = $this->conn->fetchColumn('SHOW SERVER_VERSION');
  61. $this->compatModePre9_5 = version_compare($version, '9.5', '<');
  62. return $this->compatModePre9_5;
  63. }
  64. }