@@ -343,6 +343,32 @@ public function typecastSaveField(Field $field, $value)
343
343
throw new Exception ('Unexpected non-scalar value ' );
344
344
}
345
345
346
+ if ($ this instanceof Persistence \Sql) {
347
+ $ isMysql = $ this ->getDatabasePlatform () instanceof Platforms \MySQLPlatform;
348
+ $ isMssql = $ this ->getDatabasePlatform () instanceof Platforms \SQLServerPlatform;
349
+ $ isOracle = $ this ->getDatabasePlatform () instanceof Platforms \OraclePlatform;
350
+
351
+ if (is_bool ($ value )) { // needed for PostgreSQL
352
+ if ($ isMssql || $ isOracle ) { // (1 = 0) is not supported as insert value
353
+ return $ value ? '1 ' : '0 ' ;
354
+ }
355
+
356
+ return new Persistence \Sql \Expression ($ value ? '(1 = 1) ' : '(1 = 0) ' );
357
+ } elseif (is_int ($ value ) || is_float ($ value )) {
358
+ if ($ isMysql
359
+ || $ isOracle
360
+ || $ isMssql ) { // there is no CAST AS NUMERIC
361
+ if (is_float ($ value ) && ($ isMssql || $ isOracle )) {
362
+ return new Persistence \Sql \Expression ('CAST([] AS FLOAT) ' , [$ v ]); // CAST(v AS FLOAT) not supported by MySQL
363
+ }
364
+
365
+ return new Persistence \Sql \Expression ('([] + 0) ' , [$ v ]);
366
+ }
367
+
368
+ return new Persistence \Sql \Expression ('CAST([] AS NUMERIC) ' , [$ v ]);
369
+ }
370
+ }
371
+
346
372
return $ v ;
347
373
} catch (\Exception $ e ) {
348
374
throw (new Exception ('Typecast save error ' , 0 , $ e ))
@@ -354,14 +380,29 @@ public function typecastSaveField(Field $field, $value)
354
380
* Cast specific field value from the way how it's stored inside
355
381
* persistence to a PHP format.
356
382
*
357
- * @param scalar|null $value
383
+ * @param scalar|Persistence\Sql\Expression| null $value
358
384
*
359
385
* @return mixed
360
386
*/
361
387
public function typecastLoadField (Field $ field , $ value )
362
388
{
363
389
if ($ value === null ) {
364
390
return null ;
391
+ } elseif ($ value instanceof Persistence \Sql \Expression
392
+ && in_array (\Closure::bind (fn () => $ value ->template , null , Persistence \Sql \Expression::class)(), ['CAST([] AS NUMERIC) ' , 'CAST([] AS FLOAT) ' , '([]) ' , '([] + 0) ' , '([] + 0.0) ' ], true )
393
+ && array_keys ($ value ->args ) === ['custom ' ]
394
+ && array_keys ($ value ->args ['custom ' ]) === [0 ]) {
395
+ return $ value ->args ['custom ' ][0 ];
396
+ } elseif ($ value instanceof Persistence \Sql \Expression
397
+ && \Closure::bind (fn () => $ value ->template , null , Persistence \Sql \Expression::class)() === '(1 = 1) '
398
+ && array_keys ($ value ->args ) === ['custom ' ]
399
+ && array_keys ($ value ->args ['custom ' ]) === []) {
400
+ return true ;
401
+ } elseif ($ value instanceof Persistence \Sql \Expression
402
+ && \Closure::bind (fn () => $ value ->template , null , Persistence \Sql \Expression::class)() === '(1 = 0) '
403
+ && array_keys ($ value ->args ) === ['custom ' ]
404
+ && array_keys ($ value ->args ['custom ' ]) === []) {
405
+ return false ;
365
406
} elseif (!is_scalar ($ value )) {
366
407
throw new Exception ('Unexpected non-scalar value ' );
367
408
}
0 commit comments