File tree Expand file tree Collapse file tree 3 files changed +17
-1
lines changed
Expand file tree Collapse file tree 3 files changed +17
-1
lines changed Original file line number Diff line number Diff line change @@ -29,7 +29,17 @@ public struct SQLCastExpression: SQLExpression {
2929
3030 /// See `SQLExpression.serialize(to:)`.
3131 public func serialize( to serializer: inout SQLSerializer ) {
32- SQLFunction ( " CAST " , args: SQLAlias ( self . original, as: self . desiredType) )
32+ let desiredType : any SQLExpression = if
33+ serializer. dialect. name == " mysql " ,
34+ let ident = self . desiredType as? SQLIdentifier ,
35+ ident. string. allSatisfy ( { $0. isASCII && ( $0. isLowercase || $0. isUppercase || $0. isWholeNumber || $0 == " _ " ) } )
36+ {
37+ SQLRaw ( ident. string)
38+ } else {
39+ self . desiredType
40+ }
41+
42+ SQLFunction ( " CAST " , args: SQLAlias ( self . original, as: desiredType) )
3343 . serialize ( to: & serializer)
3444 }
3545}
Original file line number Diff line number Diff line change @@ -302,6 +302,9 @@ struct FluentSQLKitExtrasTests {
302302 func castExpression( ) {
303303 #expect( serialize ( . cast( \FooModel . $field, to: " text " ) ) == #"CAST("foos"."field" AS "text")"# )
304304 #expect( serialize ( . cast( \FooModel . $field, to: . unsafeRaw( " text " ) ) ) == #"CAST("foos"."field" AS text)"# )
305+
306+ #expect( MockSQLDatabase ( dialect: " mysql " ) . serialize ( . cast( \FooModel . $field, to: " text " ) ) . sql == #"CAST("foos"."field" AS text)"# )
307+ #expect( MockSQLDatabase ( dialect: " postgresql " ) . serialize ( . cast( \FooModel . $field, to: " text " ) ) . sql == #"CAST("foos"."field" AS "text")"# )
305308 }
306309 }
307310}
Original file line number Diff line number Diff line change @@ -259,6 +259,9 @@ struct SQLKitExtrasTests {
259259 #expect( serialize ( . cast( " foo " , to: " text " ) ) == #"CAST("foo" AS "text")"# )
260260 #expect( serialize ( . cast( . column( " foo " ) , to: " text " ) ) == #"CAST("foo" AS "text")"# )
261261 #expect( serialize ( . cast( . column( " foo " ) , to: . unsafeRaw( " text " ) ) ) == #"CAST("foo" AS text)"# )
262+
263+ #expect( MockSQLDatabase ( dialect: " mysql " ) . serialize ( SQLCastExpression ( . column( " foo " ) , to: " text " ) ) . sql == #"CAST("foo" AS text)"# )
264+ #expect( MockSQLDatabase ( dialect: " postgresql " ) . serialize ( SQLCastExpression ( . column( " foo " ) , to: " text " ) ) . sql == #"CAST("foo" AS "text")"# )
262265 }
263266
264267 @Test
You can’t perform that action at this time.
0 commit comments