Skip to content

Commit a535ca5

Browse files
committed
Change fromQuery() to setQuery()
1 parent 3e5a8d6 commit a535ca5

10 files changed

Lines changed: 49 additions & 43 deletions

File tree

system/Database/BaseBuilder.php

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class BaseBuilder
169169
* tableIdentity?: string,
170170
* updateFields?: array,
171171
* constraints?: array,
172-
* fromQuery?: string,
172+
* setQuery?: string,
173173
* sql?: string,
174174
* alias?: string
175175
* }
@@ -1925,9 +1925,9 @@ public function upsert($set = null, ?bool $escape = null)
19251925
*/
19261926
public function upsertBatch($set = null, ?bool $escape = null, int $batchSize = 100)
19271927
{
1928-
$this->fromQuery($set);
1928+
$this->setQuery($set);
19291929

1930-
if (isset($this->QBOptions['fromQuery'])) {
1930+
if (isset($this->QBOptions['setQuery'])) {
19311931
$sql = $this->_upsertBatch($this->QBFrom[0], $this->QBKeys, []);
19321932

19331933
if ($sql === '') {
@@ -1983,8 +1983,8 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
19831983
$this->QBOptions['sql'] = $sql;
19841984
}
19851985

1986-
if (isset($this->QBOptions['fromQuery'])) {
1987-
$data = $this->QBOptions['fromQuery'];
1986+
if (isset($this->QBOptions['setQuery'])) {
1987+
$data = $this->QBOptions['setQuery'];
19881988
} else {
19891989
$data = 'VALUES ' . implode(', ', $this->formatValues($values)) . "\n";
19901990
}
@@ -2089,9 +2089,10 @@ public function onConstraint($set)
20892089
* Sets data source as a query for insert/update/upsert
20902090
*
20912091
* @param BaseBuilder|RawSql $query
2092+
* @param string $alias
20922093
* @param array|string|null $columns an array or comma delimited string of columns
20932094
*/
2094-
public function fromQuery($query, $columns = null): BaseBuilder
2095+
public function setQuery($query, $alias = null, $columns = null): BaseBuilder
20952096
{
20962097
if ($query instanceof BaseBuilder) {
20972098
$query = $query->getCompiledSelect();
@@ -2111,13 +2112,17 @@ public function fromQuery($query, $columns = null): BaseBuilder
21112112
$columns = $this->fieldsFromQuery($query);
21122113
}
21132114

2114-
$this->QBOptions['fromQuery'] = $query;
2115-
$this->QBKeys = $columns;
2116-
$this->QBSet = [];
2115+
if ($alias !== null) {
2116+
$this->setAlias($alias);
2117+
}
21172118

2118-
foreach ($this->QBKeys as $key => $value) {
2119-
$this->QBKeys[$key] = $this->db->escapeChar . $value . $this->db->escapeChar;
2119+
foreach ($columns as $key => $value) {
2120+
$columns[$key] = $this->db->escapeChar . $value . $this->db->escapeChar;
21202121
}
2122+
2123+
$this->QBOptions['setQuery'] = $query;
2124+
$this->QBKeys = $columns;
2125+
$this->QBSet = [];
21212126
}
21222127

21232128
return $this;
@@ -2148,9 +2153,9 @@ protected function formatValues(array $values): array
21482153
*/
21492154
public function insertBatch($set = null, ?bool $escape = null, int $batchSize = 100)
21502155
{
2151-
$this->fromQuery($set);
2156+
$this->setQuery($set);
21522157

2153-
if (isset($this->QBOptions['fromQuery'])) {
2158+
if (isset($this->QBOptions['setQuery'])) {
21542159
$sql = $this->_insertBatch($this->QBFrom[0], $this->QBKeys, []);
21552160

21562161
if ($sql === '') {
@@ -2196,8 +2201,8 @@ protected function _insertBatch(string $table, array $keys, array $values): stri
21962201
$this->QBOptions['sql'] = $sql;
21972202
}
21982203

2199-
if (isset($this->QBOptions['fromQuery'])) {
2200-
$data = $this->QBOptions['fromQuery'];
2204+
if (isset($this->QBOptions['setQuery'])) {
2205+
$data = $this->QBOptions['setQuery'];
22012206
} else {
22022207
$data = 'VALUES ' . implode(', ', $this->formatValues($values));
22032208
}
@@ -2518,11 +2523,11 @@ protected function validateUpdate(): bool
25182523
*/
25192524
public function updateBatch($set = null, $constraints = null, int $batchSize = 100)
25202525
{
2521-
$this->fromQuery($set);
2526+
$this->setQuery($set);
25222527

25232528
$this->onConstraint($constraints);
25242529

2525-
if (isset($this->QBOptions['fromQuery'])) {
2530+
if (isset($this->QBOptions['setQuery'])) {
25262531
$sql = $this->_updateBatch($this->QBFrom[0], $this->QBKeys, []);
25272532

25282533
if ($sql === '') {
@@ -2621,8 +2626,8 @@ protected function _updateBatch(string $table, array $keys, array $values): stri
26212626
$this->QBOptions['sql'] = $sql;
26222627
}
26232628

2624-
if (isset($this->QBOptions['fromQuery'])) {
2625-
$data = $this->QBOptions['fromQuery'];
2629+
if (isset($this->QBOptions['setQuery'])) {
2630+
$data = $this->QBOptions['setQuery'];
26262631
} else {
26272632
$data = implode(
26282633
" UNION ALL\n",

system/Database/MySQLi/Builder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ protected function _updateBatch(string $table, array $keys, array $values): stri
124124
$this->QBOptions['sql'] = $sql;
125125
}
126126

127-
if (isset($this->QBOptions['fromQuery'])) {
128-
$data = $this->QBOptions['fromQuery'];
127+
if (isset($this->QBOptions['setQuery'])) {
128+
$data = $this->QBOptions['setQuery'];
129129
} else {
130130
$data = implode(
131131
" UNION ALL\n",

system/Database/OCI8/Builder.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ protected function _insertBatch(string $table, array $keys, array $values): stri
8181
$this->QBOptions['sql'] = $sql;
8282
}
8383

84-
if (isset($this->QBOptions['fromQuery'])) {
85-
$data = $this->QBOptions['fromQuery'];
84+
if (isset($this->QBOptions['setQuery'])) {
85+
$data = $this->QBOptions['setQuery'];
8686
} else {
8787
$data = implode(
8888
" FROM DUAL UNION ALL\n",
@@ -307,8 +307,8 @@ protected function _updateBatch(string $table, array $keys, array $values): stri
307307
$this->QBOptions['sql'] = $sql;
308308
}
309309

310-
if (isset($this->QBOptions['fromQuery'])) {
311-
$data = $this->QBOptions['fromQuery'];
310+
if (isset($this->QBOptions['setQuery'])) {
311+
$data = $this->QBOptions['setQuery'];
312312
} else {
313313
$data = implode(
314314
" UNION ALL\n",
@@ -416,8 +416,8 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
416416
$this->QBOptions['sql'] = $sql;
417417
}
418418

419-
if (isset($this->QBOptions['fromQuery'])) {
420-
$data = $this->QBOptions['fromQuery'];
419+
if (isset($this->QBOptions['setQuery'])) {
420+
$data = $this->QBOptions['setQuery'];
421421
} else {
422422
$data = implode(
423423
" FROM DUAL UNION ALL\n",

system/Database/Postgre/Builder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ protected function _insertBatch(string $table, array $keys, array $values): stri
209209
$this->QBOptions['sql'] = $sql;
210210
}
211211

212-
if (isset($this->QBOptions['fromQuery'])) {
213-
$data = $this->QBOptions['fromQuery'];
212+
if (isset($this->QBOptions['setQuery'])) {
213+
$data = $this->QBOptions['setQuery'];
214214
} else {
215215
$data = 'VALUES ' . implode(', ', $this->formatValues($values));
216216
}
@@ -400,8 +400,8 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
400400
$this->QBOptions['sql'] = $sql;
401401
}
402402

403-
if (isset($this->QBOptions['fromQuery'])) {
404-
$data = $this->QBOptions['fromQuery'];
403+
if (isset($this->QBOptions['setQuery'])) {
404+
$data = $this->QBOptions['setQuery'];
405405
} else {
406406
$data = 'VALUES ' . implode(', ', $this->formatValues($values)) . "\n";
407407
}

system/Database/SQLSRV/Builder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ protected function _insertBatch(string $table, array $keys, array $values): stri
190190
$this->QBOptions['sql'] = $sql;
191191
}
192192

193-
if (isset($this->QBOptions['fromQuery'])) {
194-
$data = $this->QBOptions['fromQuery'];
193+
if (isset($this->QBOptions['setQuery'])) {
194+
$data = $this->QBOptions['setQuery'];
195195
} else {
196196
$data = 'VALUES ' . implode(', ', $this->formatValues($values));
197197
}
@@ -756,8 +756,8 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
756756
$this->QBOptions['sql'] = $sql;
757757
}
758758

759-
if (isset($this->QBOptions['fromQuery'])) {
760-
$data = $this->QBOptions['fromQuery'];
759+
if (isset($this->QBOptions['setQuery'])) {
760+
$data = $this->QBOptions['setQuery'];
761761
} else {
762762
$data = 'VALUES ' . implode(', ', $this->formatValues($values)) . "\n";
763763
}

system/Database/SQLite3/Builder.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ protected function _updateBatch(string $table, array $keys, array $values): stri
9393
return ''; // @codeCoverageIgnore
9494
}
9595

96-
if (count($constraints) > 1 || isset($this->QBOptions['fromQuery']) || (current($constraints) instanceof RawSql)) {
96+
if (count($constraints) > 1 || isset($this->QBOptions['setQuery']) || (current($constraints) instanceof RawSql)) {
9797
throw new DatabaseException('You are trying to use a feature which requires SQLite version 3.33 or higher.');
9898
}
9999

@@ -201,10 +201,10 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
201201
$this->QBOptions['sql'] = $sql;
202202
}
203203

204-
if (isset($this->QBOptions['fromQuery'])) {
205-
$hasWhere = stripos($this->QBOptions['fromQuery'], 'WHERE') > 0;
204+
if (isset($this->QBOptions['setQuery'])) {
205+
$hasWhere = stripos($this->QBOptions['setQuery'], 'WHERE') > 0;
206206

207-
$data = $this->QBOptions['fromQuery'] . ($hasWhere ? '' : "\nWHERE 1 = 1\n");
207+
$data = $this->QBOptions['setQuery'] . ($hasWhere ? '' : "\nWHERE 1 = 1\n");
208208
} else {
209209
$data = 'VALUES ' . implode(', ', $this->formatValues($values)) . "\n";
210210
}

tests/system/Database/Live/InsertTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public function testInsertBatchWithQueryAndRawSqlAndManualColumns()
239239
->getCompiledSelect();
240240

241241
$this->db->table('user')
242-
->fromQuery(new RawSql($sql), 'name, email, country')
242+
->setQuery(new RawSql($sql), null, 'name, email, country')
243243
->insertBatch();
244244

245245
$this->seeInDatabase('user', ['name' => 'New User user2']);

user_guide_src/source/changelogs/v4.3.0.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ Query Builder
211211
- Added ``deleteBatch()`` method to QueryBuilder. See :ref:`delete-batch`.
212212
- Added ``when()`` and ``whenNot()`` methods to conditionally add clauses to the query. See :ref:`BaseBuilder::when() <db-builder-when>` for details.
213213
- Improved the SQL structure for ``Builder::updateBatch()``. See :ref:`update-batch` for the details.
214-
- Added ``BaseBuilder::fromQuery()`` which allows insert, update, upsert from a query. See :ref:`insert-batch-data`.
214+
- Added ``BaseBuilder::setQueryAsData()`` which allows insert, update, upsert, delete from a query. See :ref:`insert-batch-data`.
215215

216216
Forge
217217
-----

user_guide_src/source/database/query_builder.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1393,10 +1393,11 @@ Class Reference
13931393

13941394
Specifies the ``FROM`` clause of a query using a subquery.
13951395

1396-
.. php:method:: fromQuery($query[, $columns = null])
1396+
.. php:method:: setQuery($query[, $alias[, $columns = null]])
13971397
13981398
:param BaseBuilder|RawSql $query: Instance of the BaseBuilder or RawSql
13991399
:param array|string|null $columns: Array or comma delimited string of columns in the query
1400+
:param string|null $alias: Alias for query
14001401
:returns: ``BaseBuilder`` instance (method chaining)
14011402
:rtype: ``BaseBuilder``
14021403

user_guide_src/source/database/query_builder/117.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
$sql = $builder
1111
->ignore(true)
12-
->fromQuery(new RawSql($query), 'name, country, email')
12+
->setQuery(new RawSql($query), null, 'name, country, email')
1313
->insertBatch();
1414
/* MySQLi produces:
1515
INSERT IGNORE INTO `db_user` (`name`, `country`, `email`)

0 commit comments

Comments
 (0)