Skip to content

Commit d9b3fde

Browse files
committed
chore(IDBConnection): Remove insertIfNotExist method
Signed-off-by: provokateurin <[email protected]>
1 parent 366d97b commit d9b3fde

File tree

5 files changed

+0
-147
lines changed

5 files changed

+0
-147
lines changed

lib/private/DB/Adapter.php

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
namespace OC\DB;
99

1010
use Doctrine\DBAL\Exception;
11-
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
1211
use OC\DB\Exceptions\DbalException;
1312

1413
/**
@@ -64,54 +63,6 @@ public function unlockTable() {
6463
$this->conn->commit();
6564
}
6665

67-
/**
68-
* Insert a row if the matching row does not exists. To accomplish proper race condition avoidance
69-
* it is needed that there is also a unique constraint on the values. Then this method will
70-
* catch the exception and return 0.
71-
*
72-
* @param string $table The table name (will replace *PREFIX* with the actual prefix)
73-
* @param array $input data that should be inserted into the table (column name => value)
74-
* @param array|null $compare List of values that should be checked for "if not exists"
75-
* If this is null or an empty array, all keys of $input will be compared
76-
* Please note: text fields (clob) must not be used in the compare array
77-
* @return int number of inserted rows
78-
* @throws Exception
79-
* @deprecated 15.0.0 - use unique index and "try { $db->insert() } catch (UniqueConstraintViolationException $e) {}" instead, because it is more reliable and does not have the risk for deadlocks - see https://github.com/nextcloud/server/pull/12371
80-
*/
81-
public function insertIfNotExist($table, $input, ?array $compare = null) {
82-
$compare = $compare ?: array_keys($input);
83-
84-
// Prepare column names and generate placeholders
85-
$columns = '`' . implode('`,`', array_keys($input)) . '`';
86-
$placeholders = implode(', ', array_fill(0, count($input), '?'));
87-
88-
$query = 'INSERT INTO `' . $table . '` (' . $columns . ') '
89-
. 'SELECT ' . $placeholders . ' '
90-
. 'FROM `' . $table . '` WHERE ';
91-
92-
$inserts = array_values($input);
93-
foreach ($compare as $key) {
94-
$query .= '`' . $key . '`';
95-
if (is_null($input[$key])) {
96-
$query .= ' IS NULL AND ';
97-
} else {
98-
$inserts[] = $input[$key];
99-
$query .= ' = ? AND ';
100-
}
101-
}
102-
$query = substr($query, 0, -5);
103-
$query .= ' HAVING COUNT(*) = 0';
104-
105-
try {
106-
return $this->conn->executeUpdate($query, $inserts);
107-
} catch (UniqueConstraintViolationException $e) {
108-
// This exception indicates a concurrent insert happened between
109-
// the insert and the sub-select in the insert, which is safe to ignore.
110-
// More details: https://github.com/nextcloud/server/pull/12315
111-
return 0;
112-
}
113-
}
114-
11566
/**
11667
* @throws \OCP\DB\Exception
11768
*/

lib/private/DB/AdapterSqlite.php

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
*/
88
namespace OC\DB;
99

10-
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
11-
1210
class AdapterSqlite extends Adapter {
1311
/**
1412
* @param string $tableName
@@ -30,53 +28,6 @@ public function fixupStatement($statement) {
3028
return $statement;
3129
}
3230

33-
/**
34-
* Insert a row if the matching row does not exists. To accomplish proper race condition avoidance
35-
* it is needed that there is also a unique constraint on the values. Then this method will
36-
* catch the exception and return 0.
37-
*
38-
* @param string $table The table name (will replace *PREFIX* with the actual prefix)
39-
* @param array $input data that should be inserted into the table (column name => value)
40-
* @param array|null $compare List of values that should be checked for "if not exists"
41-
* If this is null or an empty array, all keys of $input will be compared
42-
* Please note: text fields (clob) must not be used in the compare array
43-
* @return int number of inserted rows
44-
* @throws \Doctrine\DBAL\Exception
45-
* @deprecated 15.0.0 - use unique index and "try { $db->insert() } catch (UniqueConstraintViolationException $e) {}" instead, because it is more reliable and does not have the risk for deadlocks - see https://github.com/nextcloud/server/pull/12371
46-
*/
47-
public function insertIfNotExist($table, $input, ?array $compare = null) {
48-
if (empty($compare)) {
49-
$compare = array_keys($input);
50-
}
51-
$fieldList = '`' . implode('`,`', array_keys($input)) . '`';
52-
$query = "INSERT INTO `$table` ($fieldList) SELECT "
53-
. str_repeat('?,', count($input) - 1) . '? '
54-
. " WHERE NOT EXISTS (SELECT 1 FROM `$table` WHERE ";
55-
56-
$inserts = array_values($input);
57-
foreach ($compare as $key) {
58-
$query .= '`' . $key . '`';
59-
if (is_null($input[$key])) {
60-
$query .= ' IS NULL AND ';
61-
} else {
62-
$inserts[] = $input[$key];
63-
$query .= ' = ? AND ';
64-
}
65-
}
66-
$query = substr($query, 0, -5);
67-
$query .= ')';
68-
69-
try {
70-
return $this->conn->executeUpdate($query, $inserts);
71-
} catch (UniqueConstraintViolationException $e) {
72-
// if this is thrown then a concurrent insert happened between the insert and the sub-select in the insert, that should have avoided it
73-
// it's fine to ignore this then
74-
//
75-
// more discussions about this can be found at https://github.com/nextcloud/server/pull/12315
76-
return 0;
77-
}
78-
}
79-
8031
public function insertIgnoreConflict(string $table, array $values): int {
8132
$builder = $this->conn->getQueryBuilder();
8233
$builder->insert($table);

lib/private/DB/Connection.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -531,29 +531,6 @@ public function realLastInsertId($seqName = null) {
531531
return parent::lastInsertId($seqName);
532532
}
533533

534-
/**
535-
* Insert a row if the matching row does not exists. To accomplish proper race condition avoidance
536-
* it is needed that there is also a unique constraint on the values. Then this method will
537-
* catch the exception and return 0.
538-
*
539-
* @param string $table The table name (will replace *PREFIX* with the actual prefix)
540-
* @param array $input data that should be inserted into the table (column name => value)
541-
* @param array|null $compare List of values that should be checked for "if not exists"
542-
* If this is null or an empty array, all keys of $input will be compared
543-
* Please note: text fields (clob) must not be used in the compare array
544-
* @return int number of inserted rows
545-
* @throws \Doctrine\DBAL\Exception
546-
* @deprecated 15.0.0 - use unique index and "try { $db->insert() } catch (UniqueConstraintViolationException $e) {}" instead, because it is more reliable and does not have the risk for deadlocks - see https://github.com/nextcloud/server/pull/12371
547-
*/
548-
public function insertIfNotExist($table, $input, ?array $compare = null) {
549-
try {
550-
return $this->adapter->insertIfNotExist($table, $input, $compare);
551-
} catch (\Exception $e) {
552-
$this->logDatabaseException($e);
553-
throw $e;
554-
}
555-
}
556-
557534
public function insertIgnoreConflict(string $table, array $values) : int {
558535
try {
559536
return $this->adapter->insertIgnoreConflict($table, $values);

lib/private/DB/ConnectionAdapter.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,6 @@ public function lastInsertId(string $table): int {
7878
}
7979
}
8080

81-
public function insertIfNotExist(string $table, array $input, ?array $compare = null) {
82-
try {
83-
return $this->inner->insertIfNotExist($table, $input, $compare);
84-
} catch (Exception $e) {
85-
throw DbalException::wrap($e);
86-
}
87-
}
88-
8981
public function insertIgnoreConflict(string $table, array $values): int {
9082
try {
9183
return $this->inner->insertIgnoreConflict($table, $values);

lib/public/IDBConnection.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -133,24 +133,6 @@ public function executeStatement($sql, array $params = [], array $types = []): i
133133
*/
134134
public function lastInsertId(string $table): int;
135135

136-
/**
137-
* Insert a row if the matching row does not exists. To accomplish proper race condition avoidance
138-
* it is needed that there is also a unique constraint on the values. Then this method will
139-
* catch the exception and return 0.
140-
*
141-
* @param string $table The table name (will replace *PREFIX* with the actual prefix)
142-
* @param array $input data that should be inserted into the table (column name => value)
143-
* @param array|null $compare List of values that should be checked for "if not exists"
144-
* If this is null or an empty array, all keys of $input will be compared
145-
* Please note: text fields (clob) must not be used in the compare array
146-
* @return int number of inserted rows
147-
* @throws Exception used to be the removed dbal exception, since 21.0.0 it's \OCP\DB\Exception
148-
* @since 6.0.0 - parameter $compare was added in 8.1.0, return type changed from boolean in 8.1.0
149-
* @deprecated 15.0.0 - use unique index and "try { $db->insert() } catch (\OCP\DB\Exception $e) { if ($e->getReason() === \OCP\DB\Exception::REASON_CONSTRAINT_VIOLATION) {} }" instead, because it is more reliable and does not have the risk for deadlocks - see https://github.com/nextcloud/server/pull/12371
150-
*/
151-
public function insertIfNotExist(string $table, array $input, ?array $compare = null);
152-
153-
154136
/**
155137
*
156138
* Insert a row if the row does not exist. Eventual conflicts during insert will be ignored.

0 commit comments

Comments
 (0)