mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
Expose SQLLiteral.sql(_:)
This is needed for custom FetchRequest types
This commit is contained in:
1 parent
a77b195c7c
commit
4f500de04b
2 files changed
+74
-9
No files matched your search
@@ -98,7 +98,10 @@ public struct SQLLiteral {
|
||||
return (sql: sql, arguments: context.arguments)
|
||||
}
|
||||
|
||||
func sql(_ context: SQLGenerationContext) throws -> String {
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
///
|
||||
/// Returns the literal SQL string given an SQL generation context.
|
||||
public func sql(_ context: SQLGenerationContext) throws -> String {
|
||||
try elements.map { try $0.sql(context) }.joined()
|
||||
}
|
||||
|
||||
@@ -231,8 +234,6 @@ extension SQLLiteral: ExpressibleByStringInterpolation {
|
||||
|
||||
// MARK: - _SQLExpressionLiteral
|
||||
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
///
|
||||
/// SQLExpressionLiteral is an expression built from a raw SQL snippet.
|
||||
///
|
||||
/// SQLExpressionLiteral(sql: "1 + 2")
|
||||
@@ -249,8 +250,6 @@ private struct _SQLExpressionLiteral: SQLExpression {
|
||||
self.sqlLiteral = sqlLiteral
|
||||
}
|
||||
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
/// :nodoc:
|
||||
func expressionSQL(_ context: SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String {
|
||||
if wrappedInParenthesis {
|
||||
return try "(\(expressionSQL(context, wrappedInParenthesis: false)))"
|
||||
@@ -258,8 +257,6 @@ private struct _SQLExpressionLiteral: SQLExpression {
|
||||
return try sqlLiteral.sql(context)
|
||||
}
|
||||
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
/// :nodoc:
|
||||
func qualifiedExpression(with alias: TableAlias) -> SQLExpression {
|
||||
sqlLiteral.qualified(with: alias).sqlExpression
|
||||
}
|
||||
|
||||
@@ -110,7 +110,7 @@ class FetchRequestTests: GRDBTestCase {
|
||||
func requestSQL(_ context: SQLGenerationContext, forSingleResult singleResult: Bool) throws -> String {
|
||||
"INVALID"
|
||||
}
|
||||
|
||||
|
||||
func fetchCount(_ db: Database) throws -> Int { 2 }
|
||||
}
|
||||
|
||||
@@ -142,7 +142,7 @@ class FetchRequestTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.read { db in
|
||||
// Row
|
||||
@@ -369,6 +369,74 @@ class FetchRequestTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testSQLLiteralBasedFetchRequest() throws {
|
||||
struct CustomRequest : FetchRequest {
|
||||
typealias RowDecoder = String
|
||||
func requestSQL(_ context: SQLGenerationContext, forSingleResult singleResult: Bool) throws -> String {
|
||||
let literal: SQLLiteral = "SELECT \("O'Brien")"
|
||||
return try literal.sql(context)
|
||||
}
|
||||
}
|
||||
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
let value = try CustomRequest().fetchOne(db)
|
||||
XCTAssertEqual(value, "O'Brien")
|
||||
XCTAssertEqual(lastSQLQuery, "SELECT 'O''Brien'")
|
||||
}
|
||||
}
|
||||
|
||||
func testFetchRequestInconsistency() throws {
|
||||
// What happens when requestSQL(_:forSingleResult:)
|
||||
// and makePreparedRequest(_:forSingleResult:) do not agree?
|
||||
struct Table1: TableRecord { }
|
||||
struct Table2: TableRecord { }
|
||||
struct CustomRequest : FetchRequest {
|
||||
typealias RowDecoder = Row
|
||||
func requestSQL(_ context: SQLGenerationContext, forSingleResult singleResult: Bool) throws -> String {
|
||||
try Table1.all().requestSQL(context, forSingleResult: singleResult)
|
||||
}
|
||||
func makePreparedRequest(_ db: Database, forSingleResult singleResult: Bool) throws -> PreparedRequest {
|
||||
try Table2.all().makePreparedRequest(db, forSingleResult: singleResult)
|
||||
}
|
||||
}
|
||||
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.create(table: "table1") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
}
|
||||
try db.create(table: "table2") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
}
|
||||
|
||||
let request = CustomRequest()
|
||||
|
||||
// DatabaseRegionConvertible
|
||||
let region = try request.databaseRegion(db)
|
||||
XCTAssertEqual(region.description, "table1(*)")
|
||||
|
||||
// FetchRequest
|
||||
_ = try request.fetchAll(db)
|
||||
XCTAssertEqual(lastSQLQuery, "SELECT * FROM \"table2\"")
|
||||
|
||||
_ = try request.fetchCount(db)
|
||||
XCTAssertEqual(lastSQLQuery, "SELECT COUNT(*) FROM (SELECT * FROM \"table1\")")
|
||||
|
||||
// SQLExpression
|
||||
_ = try SQLRequest<Row>("SELECT \(request == 1)").fetchAll(db)
|
||||
XCTAssertEqual(lastSQLQuery, "SELECT (SELECT * FROM \"table1\") = 1")
|
||||
|
||||
// SQLCollection
|
||||
_ = try SQLRequest<Row>("SELECT \(request.contains(1))").fetchAll(db)
|
||||
XCTAssertEqual(lastSQLQuery, "SELECT 1 IN (SELECT * FROM \"table1\")")
|
||||
|
||||
// SQLRequestProtocol
|
||||
_ = try SQLRequest<Row>("SELECT * FROM (\(request))").fetchAll(db)
|
||||
XCTAssertEqual(lastSQLQuery, "SELECT * FROM (SELECT * FROM \"table1\")")
|
||||
}
|
||||
}
|
||||
|
||||
func testRequestAsSQLExpression() throws {
|
||||
struct CustomRequest : FetchRequest {
|
||||
typealias RowDecoder = Row
|
||||
|
||||
Reference in new issue
Block a user