1515use CodeIgniter \Database \Exceptions \DatabaseException ;
1616use CodeIgniter \Database \RawSql ;
1717use InvalidArgumentException ;
18+ use LogicException ;
1819
1920/**
2021 * Builder for Postgre
@@ -347,18 +348,18 @@ protected function _updateBatch(string $table, array $keys, array $values): stri
347348
348349 $ sql .= "SET \n" ;
349350
350- $ fieldTypes = $ this ->getFieldTypes ($ table );
351+ $ this ->getFieldTypes ($ table );
351352
353+ $ that = $ this ;
352354 $ sql .= implode (
353355 ", \n" ,
354356 array_map (
355- static function ($ key , $ value ) use ($ alias , $ fieldTypes ) {
356- $ type = $ fieldTypes [trim ($ key , '" ' )] ?? null ;
357- $ cast = ($ type === null ) ? '' : ':: ' . $ type ;
357+ static function ($ key , $ value ) use ($ alias , $ that ) {
358+ $ fieldName = trim ($ key , '" ' );
358359
359360 return $ key . ($ value instanceof RawSql ?
360361 ' = ' . $ value :
361- ' = ' . $ alias . '. ' . $ value . $ cast );
362+ ' = ' . $ alias . '. ' . $ that -> castValue ( $ fieldName , $ value ) );
362363 },
363364 array_keys ($ updateFields ),
364365 $ updateFields
@@ -372,7 +373,7 @@ static function ($key, $value) use ($alias, $fieldTypes) {
372373 $ sql .= 'WHERE ' . implode (
373374 ' AND ' ,
374375 array_map (
375- static function ($ key , $ value ) use ($ table , $ alias , $ fieldTypes ) {
376+ static function ($ key , $ value ) use ($ table , $ alias , $ that ) {
376377 if ($ value instanceof RawSql && is_string ($ key )) {
377378 return $ table . '. ' . $ key . ' = ' . $ value ;
378379 }
@@ -381,10 +382,9 @@ static function ($key, $value) use ($table, $alias, $fieldTypes) {
381382 return $ value ;
382383 }
383384
384- $ type = $ fieldTypes [trim ($ value , '" ' )] ?? null ;
385- $ cast = ($ type === null ) ? '' : ':: ' . $ type ;
385+ $ fieldName = trim ($ value , '" ' );
386386
387- return $ table . '. ' . $ value . ' = ' . $ alias . '. ' . $ value . $ cast ;
387+ return $ table . '. ' . $ value . ' = ' . $ alias . '. ' . $ that -> castValue ( $ fieldName , $ value ) ;
388388 },
389389 array_keys ($ constraints ),
390390 $ constraints
@@ -414,17 +414,35 @@ static function ($key, $value) use ($table, $alias, $fieldTypes) {
414414 }
415415
416416 /**
417- * @return array<string, string> [name => type]
417+ * Returns cast value.
418+ *
419+ * @param float|int|string $value Escaped value
420+ */
421+ private function castValue (string $ fieldName , $ value ): string
422+ {
423+ if (! isset ($ this ->QBOptions ['fieldTypes ' ])) {
424+ throw new LogicException (
425+ 'You must call getFieldTypes() before calling castValue(). '
426+ );
427+ }
428+
429+ $ type = $ this ->QBOptions ['fieldTypes ' ][$ fieldName ] ?? null ;
430+
431+ return ($ type === null ) ? $ value : $ value . ':: ' . $ type ;
432+ }
433+
434+ /**
435+ * Gets filed types from database meta data.
418436 */
419- private function getFieldTypes (string $ table ): array
437+ private function getFieldTypes (string $ table ): void
420438 {
421439 $ types = [];
422440
423441 foreach ($ this ->db ->getFieldData ($ table ) as $ field ) {
424442 $ types [$ field ->name ] = $ field ->type ;
425443 }
426444
427- return $ types ;
445+ $ this -> QBOptions [ ' fieldTypes ' ] = $ types ;
428446 }
429447
430448 /**
0 commit comments