@@ -395,14 +395,20 @@ static function ($key, $value) use ($table, $alias, $that): RawSql|string {
395395 if (isset ($ this ->QBOptions ['setQueryAsData ' ])) {
396396 $ data = $ this ->QBOptions ['setQueryAsData ' ];
397397 } else {
398+ $ updateFields = $ this ->QBOptions ['updateFields ' ] ?? [];
399+
398400 $ data = implode (
399401 " UNION ALL \n" ,
400402 array_map (
401- static fn ($ value ): string => 'SELECT ' . implode (', ' , array_map (
402- static fn ($ key , $ index ): string => $ index . ' ' . $ key ,
403+ fn (array $ value ): string => sprintf ('SELECT %s ' , implode (', ' , array_map (
404+ fn (string $ key , float |int |string $ index ): string => sprintf (
405+ '%s %s ' ,
406+ $ this ->castValue ((string ) $ index , $ this ->getSourceType ($ table , $ key , $ updateFields )),
407+ $ key ,
408+ ),
403409 $ keys ,
404410 $ value ,
405- )),
411+ ))) ,
406412 $ values ,
407413 ),
408414 ) . "\n" ;
@@ -411,6 +417,38 @@ static function ($key, $value) use ($table, $alias, $that): RawSql|string {
411417 return str_replace ('{:_table_:} ' , $ data , $ sql );
412418 }
413419
420+ /**
421+ * Returns the type shared by every destination column fed from the source key, or null when they differ.
422+ *
423+ * @param array<array-key, RawSql|string> $map Destination column to source key.
424+ */
425+ private function getSourceType (string $ table , string $ key , array $ map ): ?string
426+ {
427+ $ columns = array_filter (array_keys ($ map , $ key , true ), is_string (...));
428+
429+ if ($ columns === []) {
430+ return $ this ->getFieldType ($ table , $ key );
431+ }
432+
433+ $ types = array_unique (array_map (fn (string $ column ): ?string => $ this ->getFieldType ($ table , $ column ), $ columns ));
434+
435+ return count ($ types ) === 1 ? reset ($ types ) : null ;
436+ }
437+
438+ /**
439+ * Returns the literal cast to the column type, except a numeric literal for a numeric column, which stays as is so a non-integral value never rounds into a match.
440+ */
441+ private function castValue (string $ value , ?string $ type ): string
442+ {
443+ $ isNumericType = in_array (strtolower ((string ) $ type ), ['smallint ' , 'integer ' , 'bigint ' , 'numeric ' , 'real ' , 'double precision ' ], true );
444+
445+ if ($ isNumericType && is_numeric ($ value )) {
446+ return $ value ;
447+ }
448+
449+ return $ this ->cast ($ value , $ type );
450+ }
451+
414452 /**
415453 * Returns cast expression.
416454 *
@@ -437,11 +475,15 @@ private function getFieldType(string $table, string $fieldName): ?string
437475 foreach ($ this ->db ->getFieldData ($ table ) as $ field ) {
438476 $ type = $ field ->type ;
439477
440- // If `character` (or `char`) lacks a specifier, it is equivalent
441- // to `character(1)`.
442- // See https://www.postgresql.org/docs/current/datatype-character.html
443- if ($ field ->type === 'character ' ) {
444- $ type = $ field ->type . '( ' . $ field ->max_length . ') ' ;
478+ // information_schema reports enum, domain and array columns as `USER-DEFINED` or `ARRAY`, which are not castable names.
479+ if ($ type === 'USER-DEFINED ' || $ type === 'ARRAY ' ) {
480+ continue ;
481+ }
482+
483+ // `character` without a length is `character(1)` and `character(n)` truncates on cast,
484+ // while `bpchar` keeps the full value.
485+ if ($ type === 'character ' ) {
486+ $ type = 'bpchar ' ;
445487 }
446488
447489 $ this ->QBOptions ['fieldTypes ' ][$ table ][$ field ->name ] = $ type ;
@@ -613,14 +655,20 @@ static function ($key, $value) use ($table, $alias, $that): RawSql|string {
613655 if (isset ($ this ->QBOptions ['setQueryAsData ' ])) {
614656 $ data = $ this ->QBOptions ['setQueryAsData ' ];
615657 } else {
658+ $ constraints = $ this ->QBOptions ['constraints ' ] ?? [];
659+
616660 $ data = implode (
617661 " UNION ALL \n" ,
618662 array_map (
619- static fn ($ value ): string => 'SELECT ' . implode (', ' , array_map (
620- static fn ($ key , $ index ): string => $ index . ' ' . $ key ,
663+ fn (array $ value ): string => sprintf ('SELECT %s ' , implode (', ' , array_map (
664+ fn (string $ key , float |int |string $ index ): string => sprintf (
665+ '%s %s ' ,
666+ $ this ->castValue ((string ) $ index , $ this ->getSourceType ($ table , $ key , $ constraints )),
667+ $ key ,
668+ ),
621669 $ keys ,
622670 $ value ,
623- )),
671+ ))) ,
624672 $ values ,
625673 ),
626674 ) . "\n" ;
0 commit comments