@@ -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" ,
0 commit comments