Skip to content

Commit ab4b9f7

Browse files
committed
fix long lines of code - use nowdoc for sql
1 parent 4668a27 commit ab4b9f7

File tree

6 files changed

+227
-70
lines changed

6 files changed

+227
-70
lines changed

system/Database/BaseBuilder.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1733,7 +1733,7 @@ public function getWhere($where = null, ?int $limit = null, ?int $offset = 0, bo
17331733
*
17341734
* @return false|int|string[] Number of rows inserted or FALSE on failure, SQL array when testMode
17351735
*/
1736-
public function batchExecute(string $renderMethod, ?array $set = null, ?bool $escape = null, int $batchSize = 100)
1736+
protected function batchExecute(string $renderMethod, ?array $set = null, ?bool $escape = null, int $batchSize = 100)
17371737
{
17381738
if ($set === null) {
17391739
if (empty($this->QBSet)) {
@@ -1745,15 +1745,15 @@ public function batchExecute(string $renderMethod, ?array $set = null, ?bool $es
17451745
}
17461746
} elseif (empty($set)) {
17471747
if ($this->db->DBDebug) {
1748-
throw new DatabaseException('insertBatch() called with no data');
1748+
throw new DatabaseException('insertBatch()/upsertBatch called with no data');
17491749
}
17501750

17511751
return false; // @codeCoverageIgnore
17521752
}
17531753

17541754
$hasQBSet = $set === null;
17551755

1756-
$table = $this->QBFrom[0];
1756+
$table = $this->db->protectIdentifiers($this->QBFrom[0], true, null, false);
17571757

17581758
$affectedRows = 0;
17591759
$savedSQL = [];
@@ -1769,7 +1769,7 @@ public function batchExecute(string $renderMethod, ?array $set = null, ?bool $es
17691769
$this->setBatch(array_slice($set, $i, $batchSize), '', $escape);
17701770
$QBSet = $this->QBSet;
17711771
}
1772-
$sql = $this->{$renderMethod}($this->db->protectIdentifiers($table, true, null, false), $this->QBKeys, $QBSet);
1772+
$sql = $this->{$renderMethod}($table, $this->QBKeys, $QBSet);
17731773

17741774
if ($this->testMode) {
17751775
$savedSQL[] = $sql;
@@ -1909,15 +1909,31 @@ public function upsertBatch(?array $set = null, ?bool $escape = null, int $batch
19091909
*/
19101910
protected function _upsertBatch(string $table, array $keys, array $values): string
19111911
{
1912-
$updateFields = $this->QBOptions['updateFields'] ?? $keys;
1912+
$fieldNames = array_map(static fn ($columnName) => trim($columnName, '`'), $keys);
1913+
1914+
$updateFields = $this->QBOptions['updateFields'] ?? $fieldNames;
1915+
1916+
$sql = 'INSERT INTO ' . $table . ' (' . implode(', ', $keys) . ')' . "\n";
1917+
1918+
$sql .= 'VALUES ' . implode(', ', $this->getValues($values)) . "\n";
1919+
1920+
$sql .= 'ON DUPLICATE KEY UPDATE' . "\n";
1921+
1922+
$sql .= implode(
1923+
",\n",
1924+
array_map(
1925+
static fn ($columnName) => '`' . $columnName . '` = VALUES(`' . $columnName . '`)',
1926+
$updateFields
1927+
)
1928+
);
19131929

1914-
return 'INSERT INTO ' . $table . ' (' . implode(', ', $keys) . ') VALUES ' . implode(', ', $this->getValues($values)) . ' ON DUPLICATE KEY UPDATE ' . implode(', ', array_map(static fn ($columnName) => '`' . trim($columnName, '`') . '` = VALUES(`' . trim($columnName, '`') . '`)', $updateFields));
1930+
return $sql;
19151931
}
19161932

19171933
/**
19181934
* Sets constraints for upsert
19191935
*
1920-
* @param mixed $keys
1936+
* @param string|string[] $keys
19211937
*
19221938
* @return $this
19231939
*/
@@ -1935,7 +1951,7 @@ public function onConstraint($keys)
19351951
/**
19361952
* Sets update fields for upsert
19371953
*
1938-
* @param mixed $keys
1954+
* @param string|string[] $keys
19391955
*
19401956
* @return $this
19411957
*/

system/Database/OCI8/Builder.php

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ protected function _insertBatch(string $table, array $keys, array $values): stri
9494

9595
/**
9696
* Generates a platform-specific upsertBatch string from the supplied data
97+
*
98+
* @throws DatabaseException
9799
*/
98100
protected function _upsertBatch(string $table, array $keys, array $values): string
99101
{
@@ -120,7 +122,10 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
120122
}
121123

122124
if (empty($updateFields)) {
123-
$updateFields = array_filter($fieldNames, static fn ($columnName) => ! (in_array($columnName, $constraints, true)));
125+
$updateFields = array_filter(
126+
$fieldNames,
127+
static fn ($columnName) => ! (in_array($columnName, $constraints, true))
128+
);
124129

125130
$this->QBOptions['updateFields'] = $updateFields;
126131
}
@@ -132,7 +137,12 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
132137
$sql = 'MERGE INTO ' . $table . "\nUSING (\n";
133138

134139
foreach ($values as $value) {
135-
$sql .= 'SELECT ' . implode(', ', array_map(static fn ($columnName, $value) => $value . ' ' . $columnName, $keys, $value)) . " FROM DUAL UNION ALL\n";
140+
$sql .= 'SELECT ';
141+
$sql .= implode(', ', array_map(
142+
static fn ($columnName, $value) => $value . ' ' . $columnName, $keys,
143+
$value
144+
));
145+
$sql .= " FROM DUAL UNION ALL\n";
136146
}
137147

138148
$sql = substr($sql, 0, -11) . "\n";
@@ -142,15 +152,26 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
142152
$onList = [];
143153
$onList[] = '1 != 1';
144154

145-
$onList[] = '(' . implode(' AND ', array_map(static fn ($columnName) => $table . '."' . $columnName . '" = "_upsert"."' . $columnName . '"', $constraints)) . ')';
155+
$onList[] = '(' . implode(
156+
' AND ',
157+
array_map(
158+
static fn ($columnName) => $table . '."' . $columnName . '" = "_upsert"."' . $columnName . '"',
159+
$constraints
160+
)
161+
) . ')';
146162

147-
$sql .= implode(' OR ', $onList) . ")\nWHEN MATCHED THEN UPDATE SET ";
163+
$sql .= implode(' OR ', $onList) . ")\nWHEN MATCHED THEN UPDATE SET\n";
148164

149-
$sql .= implode(', ', array_map(static fn ($columnName) => '"' . $columnName . '"' . ' = "_upsert"."' . $columnName . '"', $updateFields));
165+
$sql .= implode(",\n", array_map(
166+
static fn ($columnName) => '"' . $columnName . '"' . ' = "_upsert"."' . $columnName . '"',
167+
$updateFields
168+
));
150169

151170
$sql .= "\nWHEN NOT MATCHED THEN INSERT (" . implode(', ', $keys) . ")\nVALUES ";
152171

153-
return $sql . (' (' . implode(', ', array_map(static fn ($columnName) => '"_upsert".' . $columnName, $keys)) . ')');
172+
$sql .= (' (' . implode(', ', array_map(static fn ($columnName) => '"_upsert".' . $columnName, $keys)) . ')');
173+
174+
return $sql;
154175
}
155176

156177
/**

system/Database/Postgre/Builder.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ protected function _insertBatch(string $table, array $keys, array $values): stri
201201

202202
/**
203203
* Generates a platform-specific upsertBatch string from the supplied data
204+
*
205+
* @throws DatabaseException
204206
*/
205207
protected function _upsertBatch(string $table, array $keys, array $values): string
206208
{
@@ -242,14 +244,31 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
242244
}
243245
}
244246

245-
$sql = 'INSERT INTO ' . $table . '(' . implode(', ', array_map(static fn ($columnName) => $columnName, $keys)) . ') VALUES ' . implode(', ', $this->getValues($values)) . "\n";
246-
247-
// we can use this sql but maybe we should throw an error here if there are no indexs to update on
248247
if (empty($constraints)) {
249-
return $sql;
248+
throw new DatabaseException('No constraint found for upsert.');
250249
}
251250

252-
return $sql .= 'ON CONFLICT("' . implode('","', $constraints) . '") DO UPDATE SET ' . implode(', ', array_map(static fn ($updateField) => '"' . $updateField . '" = "excluded"."' . $updateField . '"', $updateFields));
251+
$sql = 'INSERT INTO ' . $table . '(';
252+
253+
$sql .= implode(', ', array_map(static fn ($columnName) => $columnName, $keys));
254+
255+
$sql .= ")\n";
256+
257+
$sql .= 'VALUES ' . implode(', ', $this->getValues($values)) . "\n";
258+
259+
$sql .= 'ON CONFLICT("' . implode('","', $constraints) . "\")\n";
260+
261+
$sql .= "DO UPDATE SET\n";
262+
263+
$sql .= implode(
264+
",\n",
265+
array_map(
266+
static fn ($updateField) => '"' . $updateField . '" = "excluded"."' . $updateField . '"',
267+
$updateFields
268+
)
269+
);
270+
271+
return $sql;
253272
}
254273

255274
/**

system/Database/SQLSRV/Builder.php

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ protected function _insertBatch(string $table, array $keys, array $values): stri
185185

186186
/**
187187
* Generates a platform-specific upsertBatch string from the supplied data
188+
*
189+
* @throws DatabaseException
188190
*/
189191
protected function _upsertBatch(string $table, array $keys, array $values): string
190192
{
@@ -197,7 +199,9 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
197199
$tableIdentity = $this->QBOptions['tableIdentity'] ?? [];
198200

199201
if (empty($tableIdentity)) {
200-
if (($query = $this->db->query("SELECT name from syscolumns where id = Object_ID('" . $table . "') and colstat = 1")) === false) {
202+
$sql = "SELECT name from syscolumns where id = Object_ID('" . $table . "') and colstat = 1";
203+
204+
if (($query = $this->db->query($sql)) === false) {
201205
throw new DatabaseException('Failed to get table identity');
202206
}
203207

@@ -242,7 +246,10 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
242246
}
243247

244248
if (empty($updateFields)) {
245-
$updateFields = array_filter($fieldNames, static fn ($columnName) => ! (in_array($columnName, $constraints, true)));
249+
$updateFields = array_filter(
250+
$fieldNames,
251+
static fn ($columnName) => ! (in_array($columnName, $constraints, true))
252+
);
246253

247254
$this->QBOptions['updateFields'] = $updateFields;
248255
}
@@ -251,24 +258,51 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
251258
throw new DatabaseException('No constraint found for upsert.');
252259
}
253260

254-
$sql = 'MERGE INTO ' . $fullTableName . "\nUSING (\n VALUES " . implode(', ', $this->getValues($values)) . "\n";
261+
$sql = 'MERGE INTO ' . $fullTableName . "\nUSING (\n VALUES ";
262+
263+
$sql .= implode(', ', $this->getValues($values)) . "\n";
264+
265+
$sql .= ') "_upsert" (';
266+
267+
$sql .= implode(', ', array_map(static fn ($columnName) => '"' . $columnName . '"', $fieldNames));
255268

256-
$sql .= ') "_upsert" (' . implode(', ', array_map(static fn ($columnName) => '"' . $columnName . '"', $fieldNames)) . ')';
269+
$sql .= ')';
257270

258271
$sql .= "\nON ( ";
259272

260273
$onList = [];
261274
$onList[] = '1 != 1';
262275

263-
$onList[] = '(' . implode(' AND ', array_map(static fn ($columnName) => $fullTableName . '."' . $columnName . '" = "_upsert"."' . $columnName . '"', $constraints)) . ')';
276+
$onList[] = '(' . implode(
277+
' AND ',
278+
array_map(
279+
static fn ($columnName) => $fullTableName . '."' . $columnName . '" = "_upsert"."' . $columnName . '"',
280+
$constraints
281+
)
282+
) . ')';
264283

265-
$sql .= implode(' OR ', $onList) . ")\nWHEN MATCHED THEN UPDATE SET ";
284+
$sql .= implode(' OR ', $onList) . ")\nWHEN MATCHED THEN UPDATE SET\n";
266285

267-
$sql .= implode(', ', array_map(static fn ($columnName) => '"' . $columnName . '" = "_upsert"."' . $columnName . '"', $updateFields));
286+
$sql .= implode(",\n", array_map(
287+
static fn ($columnName) => '"' . $columnName . '" = "_upsert"."' . $columnName . '"',
288+
$updateFields
289+
));
268290

269291
$sql .= "\nWHEN NOT MATCHED THEN INSERT (" . implode(', ', $keys) . ")\nVALUES ";
270292

271-
$sql .= ('(' . implode(', ', array_map(static fn ($columnName) => $columnName === $tableIdentity ? 'CASE WHEN "_upsert".' . $columnName . ' IS NULL THEN (SELECT isnull(IDENT_CURRENT(\'' . $fullTableName . '\')+IDENT_INCR(\'' . $fullTableName . '\'),1)) ELSE "_upsert".' . $columnName . ' END' : '"_upsert".' . $columnName, $keys)) . ');');
293+
$sql .= (
294+
'(' . implode(
295+
', ',
296+
array_map(
297+
static fn ($columnName) => $columnName === $tableIdentity
298+
? 'CASE WHEN "_upsert".' . $columnName . ' IS NULL THEN (SELECT '
299+
. 'isnull(IDENT_CURRENT(\'' . $fullTableName . '\')+IDENT_INCR(\''
300+
. $fullTableName . '\'),1)) ELSE "_upsert".' . $columnName . ' END'
301+
: '"_upsert".' . $columnName,
302+
$keys
303+
)
304+
) . ');'
305+
);
272306

273307
return $identityInFields ? $this->addIdentity($fullTableName, $sql) : $sql;
274308
}

system/Database/SQLite3/Builder.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ protected function _truncate(string $table): string
7373

7474
/**
7575
* Generates a platform-specific upsertBatch string from the supplied data
76+
*
77+
* @throws DatabaseException
7678
*/
7779
protected function _upsertBatch(string $table, array $keys, array $values): string
7880
{
@@ -100,13 +102,30 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
100102
$this->QBOptions['constraints'] = $constraints;
101103
}
102104

103-
$sql = 'INSERT INTO ' . $table . '(' . implode(', ', array_map(static fn ($columnName) => $columnName, $keys)) . ') VALUES ' . implode(', ', $this->getValues($values)) . "\n";
104-
105-
// we can use this sql but maybe we should throw an error here if there are no indexs to update on
106105
if (empty($constraints)) {
107-
return $sql;
106+
throw new DatabaseException('No constraint found for upsert.');
108107
}
109108

110-
return $sql .= 'ON CONFLICT(`' . implode('`,`', $constraints) . '`) DO UPDATE SET ' . implode(', ', array_map(static fn ($updateField) => '`' . $updateField . '` = `excluded`.`' . $updateField . '`', $updateFields));
109+
$sql = 'INSERT INTO ' . $table . '(';
110+
111+
$sql .= implode(', ', array_map(static fn ($columnName) => $columnName, $keys));
112+
113+
$sql .= ")\n";
114+
115+
$sql .= 'VALUES ' . implode(', ', $this->getValues($values)) . "\n";
116+
117+
$sql .= 'ON CONFLICT(`' . implode('`,`', $constraints) . "`)\n";
118+
119+
$sql .= "DO UPDATE SET\n";
120+
121+
$sql .= implode(
122+
",\n",
123+
array_map(
124+
static fn ($updateField) => '`' . $updateField . '` = `excluded`.`' . $updateField . '`',
125+
$updateFields
126+
)
127+
);
128+
129+
return $sql;
111130
}
112131
}

0 commit comments

Comments
 (0)