From 4f500de04bf4259e3683fc43984387f08d3d1e9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 9 May 2020 18:44:19 +0200 Subject: [PATCH] Expose SQLLiteral.sql(_:) This is needed for custom FetchRequest types --- GRDB/Core/SQLLiteral.swift | 11 ++-- Tests/GRDBTests/FetchRequestTests.swift | 72 ++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 9 deletions(-) diff --git a/GRDB/Core/SQLLiteral.swift b/GRDB/Core/SQLLiteral.swift index f389b27cf..cf2c5d21b 100644 --- a/GRDB/Core/SQLLiteral.swift +++ b/GRDB/Core/SQLLiteral.swift @@ -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 } diff --git a/Tests/GRDBTests/FetchRequestTests.swift b/Tests/GRDBTests/FetchRequestTests.swift index bfb535f74..02725ea6d 100644 --- a/Tests/GRDBTests/FetchRequestTests.swift +++ b/Tests/GRDBTests/FetchRequestTests.swift @@ -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("SELECT \(request == 1)").fetchAll(db) + XCTAssertEqual(lastSQLQuery, "SELECT (SELECT * FROM \"table1\") = 1") + + // SQLCollection + _ = try SQLRequest("SELECT \(request.contains(1))").fetchAll(db) + XCTAssertEqual(lastSQLQuery, "SELECT 1 IN (SELECT * FROM \"table1\")") + + // SQLRequestProtocol + _ = try SQLRequest("SELECT * FROM (\(request))").fetchAll(db) + XCTAssertEqual(lastSQLQuery, "SELECT * FROM (SELECT * FROM \"table1\")") + } + } + func testRequestAsSQLExpression() throws { struct CustomRequest : FetchRequest { typealias RowDecoder = Row