Don't deprecate SQLLiteral.mapSQL, after all

This commit is contained in:
Gwendal Roué committed 2020-01-25 13:46:00 +01:00
1 parent db8585aa85
commit e999ab5ecb
3 files changed
+42 -5

No files matched your search

+1 -1
View File
@@ -111,8 +111,8 @@ public struct SQLLiteral {
self.init(elements: [.expression(expression)])
}
// TODO: deprecate when SQL interpolation is always available
/// Returns a literal whose SQL is transformed by the given closure.
@available(*, deprecated, message: "Use SQL interpolation instead")
public func mapSQL(_ transform: @escaping (String) -> String) -> SQLLiteral {
return SQLLiteral(elements: [.map(self, transform)])
}
@@ -6,7 +6,7 @@ extension SQLExpression {
/// Converts an expression to an SQLLiteral
///
/// :nodoc:
@available(*, deprecated, message: "Use SQL interpolation instead")
@available(*, deprecated, message: "Use SQLLiteral initializer instead")
public var sqlLiteral: SQLLiteral {
return SQLLiteral(elements: [.expression(self)])
}
@@ -37,7 +37,7 @@ func ~= (_ lhs: SQLExpressible, _ rhs: Column) -> SQLExpression {
// CAST
func castDeprecated(_ value: SQLExpressible, as type: Database.ColumnType) -> SQLExpression {
private func castDeprecated(_ value: SQLExpressible, as type: Database.ColumnType) -> SQLExpression {
let literal = value.sqlExpression.sqlLiteral
let castLiteral = literal.mapSQL { sql in
"CAST(\(sql) AS \(type.rawValue))"
@@ -45,8 +45,14 @@ func castDeprecated(_ value: SQLExpressible, as type: Database.ColumnType) -> SQ
return SQLExpressionLiteral(literal: castLiteral)
}
private func cast(_ value: SQLExpressible, as type: Database.ColumnType) -> SQLExpression {
return SQLLiteral(value.sqlExpression)
.mapSQL({ sql in "CAST(\(sql) AS \(type.rawValue))" })
.sqlExpression
}
#if swift(>=5.0)
func cast<T: SQLExpressible>(_ value: T, as type: Database.ColumnType) -> SQLExpression {
private func castInterpolated<T: SQLExpressible>(_ value: T, as type: Database.ColumnType) -> SQLExpression {
return SQLLiteral("CAST(\(value) AS \(sql: type.rawValue))").sqlExpression
}
#endif
@@ -123,7 +129,6 @@ class QueryInterfaceExtensibilityTests: GRDBTestCase {
}
}
#if swift(>=5.0)
func testCast() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
@@ -154,5 +159,37 @@ class QueryInterfaceExtensibilityTests: GRDBTestCase {
}
}
}
#if swift(>=5.0)
func testCastInterpolated() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
try db.create(table: "records") { t in
t.column("text", .text)
}
struct Record : TableRecord {
static let databaseTableName = "records"
}
try db.execute(sql: "INSERT INTO records (text) VALUES (?)", arguments: ["foo"])
do {
let request = Record.select(castInterpolated(Column("text"), as: .blob))
let dbValue = try DatabaseValue.fetchOne(db, request)!
switch dbValue.storage {
case .blob:
break
default:
XCTFail("Expected data blob")
}
XCTAssertEqual(self.lastSQLQuery, "SELECT CAST(\"text\" AS BLOB) FROM \"records\" LIMIT 1")
}
do {
let request = Record.select(castInterpolated(Column("text"), as: .blob) && true)
_ = try DatabaseValue.fetchOne(db, request)!
XCTAssertEqual(self.lastSQLQuery, "SELECT (CAST(\"text\" AS BLOB)) AND 1 FROM \"records\" LIMIT 1")
}
}
}
#endif
}