Passed
Push — master ( 5564a3...f5c0ea )
by Morris
10:35 queued 10s
created

AdapterPgSql::insertIgnoreConflict()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bart Visscher <[email protected]>
6
 * @author Morris Jobke <[email protected]>
7
 *
8
 * @license AGPL-3.0
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
25
namespace OC\DB;
26
27
class AdapterPgSql extends Adapter {
28
	public function lastInsertId($table) {
29
		return $this->conn->fetchColumn('SELECT lastval()');
30
	}
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
	/**
40
	 * @suppress SqlInjectionChecker
41
	 */
42
	public function insertIgnoreConflict(string $table,array $values) : int {
43
		$builder = $this->conn->getQueryBuilder();
44
		$builder->insert($table);
45
		foreach($values as $key => $value) {
46
			$builder->setValue($key, $builder->createNamedParameter($value));
47
		}
48
		$queryString = $builder->getSQL() . ' ON CONFLICT DO NOTHING';
49
		return $this->conn->executeUpdate($queryString, $builder->getParameters(), $builder->getParameterTypes());
50
	}
51
}
52