This commit is contained in:
Gwendal Roué committed 2020-04-29 22:16:19 +02:00
1 parent a15233eff1
commit 94fed7965b
3 files changed
+13 -14

No files matched your search

+9 -10
View File
@@ -13,32 +13,29 @@ public struct SQLLiteral {
/// SQLLiteral is an array of elements which can be qualified with
/// table aliases.
enum Element {
case sql((SQLGenerationContext) throws -> String)
case sql(String, StatementArguments = StatementArguments())
case expression(SQLExpression)
case selectable(SQLSelectable)
case orderingTerm(SQLOrderingTerm)
case custom((SQLGenerationContext) throws -> String)
static func sql(_ sql: String, _ arguments: StatementArguments = StatementArguments()) -> Element {
.sql { context in
fileprivate func sql(_ context: SQLGenerationContext) throws -> String {
switch self {
case let .sql(sql, arguments):
if context.append(arguments: arguments) == false {
// We don't know how to look for `?` in sql and
// replace them with literals.
fatalError("Not implemented: turning an SQL parameter into an SQL literal value")
}
return sql
}
}
fileprivate func sql(_ context: SQLGenerationContext) throws -> String {
switch self {
case let .sql(function):
return try function(context)
case let .expression(expression):
return try expression.expressionSQL(context, wrappedInParenthesis: false)
case let .selectable(selectable):
return try selectable.resultColumnSQL(context)
case let .orderingTerm(orderingTerm):
return try orderingTerm.orderingTermSQL(context)
case let .custom(function):
return try function(context)
}
}
@@ -52,6 +49,8 @@ public struct SQLLiteral {
return .selectable(selectable.qualifiedSelectable(with: alias))
case let .orderingTerm(orderingTerm):
return .orderingTerm(orderingTerm.qualifiedOrdering(with: alias))
case .custom:
return self
}
}
}
@@ -155,7 +155,7 @@ extension SQLInterpolation {
/// SELECT * FROM player WHERE score = (\(subQuery))
/// """
public mutating func appendInterpolation<RowDecoder>(_ request: SQLRequest<RowDecoder>) {
elements.append(.sql(request.sqlLiteral.sql))
elements.append(.custom(request.sqlLiteral.sql))
}
/// Appends the request SQL (not wrapped inside parentheses).
@@ -166,7 +166,7 @@ extension SQLInterpolation {
/// SELECT * FROM player WHERE score = (\(subQuery))
/// """
public mutating func appendInterpolation<RowDecoder>(_ request: QueryInterfaceRequest<RowDecoder>) {
elements.append(.sql { context in
elements.append(.custom { context in
try SQLQueryGenerator(query: request.query).sql(context)
})
}
@@ -7,7 +7,7 @@ class SQLExpressionLiteralTests: GRDBTestCase {
func testWithArguments() throws {
try DatabaseQueue().inDatabase { db in
let expression = Column("foo").collating(.nocase) == "'fooéı👨👨🏿🇫🇷🇨🇮'" && Column("baz") >= 1
var context = SQLGenerationContext.sqlLiteralContext(db)
let context = SQLGenerationContext.sqlLiteralContext(db)
let sql = try expression.expressionSQL(context, wrappedInParenthesis: true)
XCTAssertEqual(sql, "((\"foo\" = ? COLLATE NOCASE) AND (\"baz\" >= ?))")
let values = context.arguments.values
@@ -20,7 +20,7 @@ class SQLExpressionLiteralTests: GRDBTestCase {
func testWithoutArguments() throws {
try DatabaseQueue().inDatabase { db in
let expression = Column("foo").collating(.nocase) == "'fooéı👨👨🏿🇫🇷🇨🇮'" && Column("baz") >= 1
var context = SQLGenerationContext.sqlLiteralContext(db, argumentsSink: .forRawSQL)
let context = SQLGenerationContext.sqlLiteralContext(db, argumentsSink: .forRawSQL)
let sql = try expression.expressionSQL(context, wrappedInParenthesis: true)
XCTAssert(context.arguments.isEmpty)
XCTAssertEqual(sql, "((\"foo\" = '''fooéı👨👨🏿🇫🇷🇨🇮''' COLLATE NOCASE) AND (\"baz\" >= 1))")