Skip to content

Commit 3870aea

Browse files
committed
Add a couple of useful Fluent keypath-taking SQLQueryString interpolations
1 parent c3904cd commit 3870aea

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#if FluentSQLKitExtras
2+
import FluentKit
3+
import SQLKit
4+
5+
extension SQLQueryString {
6+
/// Allow specifying a Fluent keypath as an identifier interpolation in an `SQLQueryString`.
7+
@inlinable
8+
public mutating func appendInterpolation(ident: KeyPath<some Schema, some QueryAddressableProperty>) {
9+
self.appendInterpolation(.identifier(ident))
10+
}
11+
12+
/// Allow specifying a Fluent keypath as an column interpolation in an `SQLQueryString`.
13+
@inlinable
14+
public mutating func appendInterpolation(col: KeyPath<some Schema, some QueryAddressableProperty>) {
15+
self.appendInterpolation(.column(col))
16+
}
17+
}
18+
#endif
19+

Tests/SQLKitExtrasTests/FluentSQLKitExtrasTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,12 @@ struct FluentSQLKitExtrasTests {
306306
#expect(MockSQLDatabase(dialect: "mysql").serialize(.cast(\FooModel.$field, to: "text")).sql == #"CAST("foos"."field" AS text)"#)
307307
#expect(MockSQLDatabase(dialect: "postgresql").serialize(.cast(\FooModel.$field, to: "text")).sql == #"CAST("foos"."field" AS "text")"#)
308308
}
309+
310+
@Test
311+
func queryStringExpression() {
312+
#expect(serialize("foo \(ident: \FooModel.$field) bar" as SQLQueryString) == #"foo "field" bar"#)
313+
#expect(serialize("foo \(col: \FooModel.$field) bar" as SQLQueryString) == #"foo "foos"."field" bar"#)
314+
}
309315
}
310316
}
311317

0 commit comments

Comments
 (0)