@@ -685,13 +685,15 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
685
685
return '' ; // @codeCoverageIgnore
686
686
}
687
687
688
+ $ alias = $ this ->QBOptions ['alias ' ] ?? '"_upsert" ' ;
689
+
688
690
$ updateFields = $ this ->QBOptions ['updateFields ' ] ?? $ this ->updateFields ($ keys , false , $ constraints )->QBOptions ['updateFields ' ] ?? [];
689
691
690
692
$ sql = 'MERGE INTO ' . $ fullTableName . "\nUSING ( \n" ;
691
693
692
694
$ sql .= '{:_table_:} ' ;
693
695
694
- $ sql .= ' ) "_upsert" ( ' ;
696
+ $ sql .= " ) { $ alias } ( " ;
695
697
696
698
$ sql .= implode (', ' , $ keys );
697
699
@@ -702,9 +704,20 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
702
704
$ sql .= implode (
703
705
' AND ' ,
704
706
array_map (
705
- static fn ($ key ) => ($ key instanceof RawSql ?
706
- $ key :
707
- $ fullTableName . '. ' . $ key . ' = "_upsert". ' . $ key ),
707
+ static fn ($ key , $ value ) => (
708
+ ($ value instanceof RawSql && is_string ($ key ))
709
+ ?
710
+ $ fullTableName . '. ' . $ key . ' = ' . $ value
711
+ :
712
+ (
713
+ $ value instanceof RawSql
714
+ ?
715
+ $ value
716
+ :
717
+ $ fullTableName . '. ' . $ value . ' = ' . $ alias . '. ' . $ value
718
+ )
719
+ ),
720
+ array_keys ($ constraints ),
708
721
$ constraints
709
722
)
710
723
) . ") \n" ;
@@ -716,7 +729,7 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
716
729
array_map (
717
730
static fn ($ key , $ value ) => $ key . ($ value instanceof RawSql ?
718
731
' = ' . $ value :
719
- ' = "_upsert". ' . $ value ),
732
+ " = { $ alias } . { $ value}" ),
720
733
array_keys ($ updateFields ),
721
734
$ updateFields
722
735
)
@@ -729,10 +742,10 @@ protected function _upsertBatch(string $table, array $keys, array $values): stri
729
742
', ' ,
730
743
array_map (
731
744
static fn ($ columnName ) => $ columnName === $ tableIdentity
732
- ? ' CASE WHEN "_upsert". ' . $ columnName . ' IS NULL THEN (SELECT '
745
+ ? " CASE WHEN { $ alias } . { $ columnName} IS NULL THEN (SELECT "
733
746
. 'isnull(IDENT_CURRENT( \'' . $ fullTableName . '\')+IDENT_INCR( \''
734
- . $ fullTableName . '\' ),1)) ELSE "_upsert". ' . $ columnName . ' END'
735
- : ' "_upsert". ' . $ columnName ,
747
+ . $ fullTableName . " ' ),1)) ELSE { $ alias } . { $ columnName} END"
748
+ : "{ $ alias } . { $ columnName}" ,
736
749
$ keys
737
750
)
738
751
) . '); '
0 commit comments