@@ -42,6 +42,7 @@ public function __construct(
4242 }
4343
4444 /**
45+ * @psalm-param string[] $columns
4546 * @psalm-suppress MixedArrayOffset
4647 */
4748 public function batchInsert (string $ table , array $ columns , iterable |Generator $ rows , array &$ params = []): string
@@ -56,15 +57,16 @@ public function batchInsert(string $table, array $columns, iterable|Generator $r
5657 $ columnSchemas = [];
5758 }
5859
60+ $ mappedNames = $ this ->getNormalizeColumnNames ($ table , $ columns );
5961 $ values = [];
6062
6163 /** @psalm-var array<array-key, array<array-key, string>> $rows */
6264 foreach ($ rows as $ row ) {
6365 $ placeholders = [];
6466 foreach ($ row as $ index => $ value ) {
65- if (isset ($ columns [$ index ], $ columnSchemas [$ columns [$ index ]])) {
67+ if (isset ($ columns [$ index ], $ mappedNames [ $ columns [ $ index ]], $ columnSchemas [$ mappedNames [ $ columns [$ index] ]])) {
6668 /** @var mixed $value */
67- $ value = $ this ->getTypecastValue ($ value , $ columnSchemas [$ columns [$ index ]]);
69+ $ value = $ this ->getTypecastValue ($ value , $ columnSchemas [$ mappedNames [ $ columns [$ index] ]]);
6870 }
6971
7072 if ($ value instanceof ExpressionInterface) {
@@ -80,9 +82,8 @@ public function batchInsert(string $table, array $columns, iterable|Generator $r
8082 return '' ;
8183 }
8284
83- /** @psalm-var string[] $columns */
8485 foreach ($ columns as $ i => $ name ) {
85- $ columns [$ i ] = $ this ->quoter ->quoteColumnName ($ name );
86+ $ columns [$ i ] = $ this ->quoter ->quoteColumnName ($ mappedNames [ $ name] );
8687 }
8788
8889 return 'INSERT INTO '
@@ -103,6 +104,10 @@ public function delete(string $table, array|string $condition, array &$params):
103104
104105 public function insert (string $ table , QueryInterface |array $ columns , array &$ params = []): string
105106 {
107+ if (!$ columns instanceof QueryInterface) {
108+ $ columns = $ this ->normalizeColumnNames ($ table , $ columns );
109+ }
110+
106111 /**
107112 * @psalm-var string[] $names
108113 * @psalm-var string[] $placeholders
@@ -137,6 +142,8 @@ public function resetSequence(string $tableName, int|string|null $value = null):
137142 */
138143 public function update (string $ table , array $ columns , array |string $ condition , array &$ params = []): string
139144 {
145+ $ columns = $ this ->normalizeColumnNames ($ table , $ columns );
146+
140147 /** @psalm-var string[] $lines */
141148 [$ lines , $ params ] = $ this ->prepareUpdateSets ($ table , $ columns , $ params );
142149 $ sql = 'UPDATE ' . $ this ->quoter ->quoteTableName ($ table ) . ' SET ' . implode (', ' , $ lines );
@@ -342,26 +349,11 @@ static function (Constraint $constraint) {
342349 $ columnNames = [];
343350 $ quoter = $ this ->quoter ;
344351
345- // Need get filtered column names. Without name of table. And remove columns from other tables
346- $ unquotedColumns = $ simpleColumns = [];
347- $ rawTableName = $ this ->schema ->getRawTableName ($ name );
348- foreach ($ columns as $ column ) {
349- $ parts = $ quoter ->getTableNameParts ($ column , true );
350-
351- // Skip columns from other tables
352- if (count ($ parts ) === 2 && $ this ->schema ->getRawTableName ($ parts [0 ]) !== $ rawTableName ) {
353- continue ;
354- }
355-
356- $ columnName = $ quoter ->ensureColumnName ($ parts [count ($ parts )-1 ]);
357- $ unquotedColumns [$ column ] = $ simpleColumns [] = $ quoter ->quoteColumnName ($ columnName );
358- }
359-
360352 // Remove all constraints which do not cover the specified column list.
361353 $ constraints = array_values (
362354 array_filter (
363355 $ constraints ,
364- static function (Constraint $ constraint ) use ($ quoter , $ simpleColumns , &$ columnNames ) {
356+ static function (Constraint $ constraint ) use ($ quoter , $ columns , &$ columnNames ) {
365357 /** @psalm-var string[]|string $getColumnNames */
366358 $ getColumnNames = $ constraint ->getColumnNames () ?? [];
367359 $ constraintColumnNames = [];
@@ -372,7 +364,7 @@ static function (Constraint $constraint) use ($quoter, $simpleColumns, &$columnN
372364 }
373365 }
374366
375- $ result = !array_diff ($ constraintColumnNames , $ simpleColumns );
367+ $ result = !array_diff ($ constraintColumnNames , $ columns );
376368
377369 if ($ result ) {
378370 $ columnNames = array_merge ((array ) $ columnNames , $ constraintColumnNames );
@@ -383,14 +375,8 @@ static function (Constraint $constraint) use ($quoter, $simpleColumns, &$columnN
383375 )
384376 );
385377
386- // restore original column names
387- $ originalColumnNames = [];
388- /** @psalm-var string[] $columnNames */
389- foreach ($ columnNames as $ columnName ) {
390- $ originalColumnNames [] = $ unquotedColumns [$ columnName ] ?? $ columnName ;
391- }
392-
393- return array_unique ($ originalColumnNames );
378+ /** @psalm-var array $columnNames */
379+ return array_unique ($ columnNames );
394380 }
395381
396382 protected function getTypecastValue (mixed $ value , ColumnSchemaInterface $ columnSchema = null ): mixed
@@ -401,4 +387,65 @@ protected function getTypecastValue(mixed $value, ColumnSchemaInterface $columnS
401387
402388 return $ value ;
403389 }
390+
391+ /**
392+ * Normalizes column names
393+ *
394+ * @param string $table the table that data will be saved into.
395+ * @param array $columns the column data (name => value) to be saved into the table or instance of
396+ * {@see QueryInterface} to perform INSERT INTO ... SELECT SQL statement. Passing of
397+ * {@see QueryInterface}.
398+ *
399+ * @return array normalized columns.
400+ */
401+ protected function normalizeColumnNames (string $ table , array $ columns ): array
402+ {
403+ /** @var string[] $columnsList */
404+ $ columnsList = array_keys ($ columns );
405+ $ mappedNames = $ this ->getNormalizeColumnNames ($ table , $ columnsList );
406+
407+ /** @psalm-var mixed[] $normalizedColumns */
408+ $ normalizedColumns = [];
409+
410+ /**
411+ * @var string $name
412+ * @var mixed $value
413+ */
414+ foreach ($ columns as $ name => $ value ) {
415+ $ mappedName = $ mappedNames [$ name ] ?? $ name ;
416+ /** @psalm-suppress MixedAssignment */
417+ $ normalizedColumns [$ mappedName ] = $ value ;
418+ }
419+
420+ return $ normalizedColumns ;
421+ }
422+
423+ /**
424+ * Get map of normalized columns
425+ *
426+ * @param string $table
427+ * @param string[] $columns
428+ *
429+ * @return string[]
430+ */
431+ protected function getNormalizeColumnNames (string $ table , array $ columns ): array
432+ {
433+ $ normalizedNames = [];
434+ $ rawTableName = $ this ->schema ->getRawTableName ($ table );
435+
436+ foreach ($ columns as $ name ) {
437+ $ parts = $ this ->quoter ->getTableNameParts ($ name , true );
438+
439+ if (count ($ parts ) === 2 && $ this ->schema ->getRawTableName ($ parts [0 ]) === $ rawTableName ) {
440+ $ normalizedName = $ parts [count ($ parts ) - 1 ];
441+ } else {
442+ $ normalizedName = $ name ;
443+ }
444+ $ normalizedName = $ this ->quoter ->ensureColumnName ($ normalizedName );
445+
446+ $ normalizedNames [$ name ] = $ normalizedName ;
447+ }
448+
449+ return $ normalizedNames ;
450+ }
404451}
0 commit comments