From a15233eff13d7b401fd554f5eeb36e1ef11df507 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 29 Apr 2020 21:59:04 +0200 Subject: [PATCH] SQLGenerationContext no longer needs to be mutated --- GRDB/Core/Database+Statements.swift | 4 +- GRDB/Core/DatabaseValue.swift | 2 +- GRDB/Core/SQLLiteral.swift | 36 ++++---- GRDB/Core/SQLRequest.swift | 12 +-- GRDB/FTS/FTS5Tokenizer.swift | 2 +- GRDB/Fixit/GRDB-5.0.swift | 2 +- .../Request/QueryInterfaceRequest.swift | 10 +-- GRDB/QueryInterface/SQL/Column.swift | 4 +- .../SQL/SQLCollatedExpression.swift | 4 +- GRDB/QueryInterface/SQL/SQLCollection.swift | 6 +- GRDB/QueryInterface/SQL/SQLExpressible.swift | 12 +-- .../SQL/SQLExpression+QueryInterface.swift | 82 +++++++++---------- GRDB/QueryInterface/SQL/SQLExpression.swift | 8 +- GRDB/QueryInterface/SQL/SQLOrdering.swift | 12 +-- .../SQL/SQLSelectable+QueryInterface.swift | 16 ++-- GRDB/QueryInterface/SQL/SQLSelectable.swift | 4 +- .../SQLGeneration/SQLGenerationContext.swift | 3 +- .../SQLGeneration/SQLQueryGenerator.swift | 58 ++++++------- .../GRDBTests/SQLExpressionLiteralTests.swift | 4 +- 19 files changed, 140 insertions(+), 141 deletions(-) diff --git a/GRDB/Core/Database+Statements.swift b/GRDB/Core/Database+Statements.swift index 82adb9049..7fa3b3872 100644 --- a/GRDB/Core/Database+Statements.swift +++ b/GRDB/Core/Database+Statements.swift @@ -158,8 +158,8 @@ extension Database { // // And before we return, we'll check that all arguments were consumed. - var context = SQLGenerationContext.sqlLiteralContext(self) - let sql = try sqlLiteral.sql(&context) + let context = SQLGenerationContext.sqlLiteralContext(self) + let sql = try sqlLiteral.sql(context) var arguments = context.arguments let initialValuesCount = arguments.values.count diff --git a/GRDB/Core/DatabaseValue.swift b/GRDB/Core/DatabaseValue.swift index e75e4adea..f83e50453 100644 --- a/GRDB/Core/DatabaseValue.swift +++ b/GRDB/Core/DatabaseValue.swift @@ -224,7 +224,7 @@ extension DatabaseValue { extension DatabaseValue { /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) /// :nodoc: - public func expressionSQL(_ context: inout SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { + public func expressionSQL(_ context: SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { if isNull { // fast path for NULL return "NULL" diff --git a/GRDB/Core/SQLLiteral.swift b/GRDB/Core/SQLLiteral.swift index 7da12d896..937467a2c 100644 --- a/GRDB/Core/SQLLiteral.swift +++ b/GRDB/Core/SQLLiteral.swift @@ -13,7 +13,7 @@ public struct SQLLiteral { /// SQLLiteral is an array of elements which can be qualified with /// table aliases. enum Element { - case sql((inout SQLGenerationContext) throws -> String) + case sql((SQLGenerationContext) throws -> String) case expression(SQLExpression) case selectable(SQLSelectable) case orderingTerm(SQLOrderingTerm) @@ -29,16 +29,16 @@ public struct SQLLiteral { } } - fileprivate func sql(_ context: inout SQLGenerationContext) throws -> String { + fileprivate func sql(_ context: SQLGenerationContext) throws -> String { switch self { case let .sql(function): - return try function(&context) + return try function(context) case let .expression(expression): - return try expression.expressionSQL(&context, wrappedInParenthesis: false) + return try expression.expressionSQL(context, wrappedInParenthesis: false) case let .selectable(selectable): - return try selectable.resultColumnSQL(&context) + return try selectable.resultColumnSQL(context) case let .orderingTerm(orderingTerm): - return try orderingTerm.orderingTermSQL(&context) + return try orderingTerm.orderingTermSQL(context) } } @@ -91,13 +91,13 @@ public struct SQLLiteral { /// - parameter db: A database connection. /// - returns: A tuple made of a raw SQL string, and statement arguments. public func build(_ db: Database) throws -> (sql: String, arguments: StatementArguments) { - var context = SQLGenerationContext.sqlLiteralContext(db) - let sql = try self.sql(&context) + let context = SQLGenerationContext.sqlLiteralContext(db) + let sql = try self.sql(context) return (sql: sql, arguments: context.arguments) } - func sql(_ context: inout SQLGenerationContext) throws -> String { - try elements.map { try $0.sql(&context) }.joined() + func sql(_ context: SQLGenerationContext) throws -> String { + try elements.map { try $0.sql(context) }.joined() } fileprivate func qualified(with alias: TableAlias) -> SQLLiteral { @@ -249,11 +249,11 @@ private struct _SQLExpressionLiteral: SQLExpression { /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) /// :nodoc: - func expressionSQL(_ context: inout SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { + func expressionSQL(_ context: SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { if wrappedInParenthesis { - return try "(\(expressionSQL(&context, wrappedInParenthesis: false)))" + return try "(\(expressionSQL(context, wrappedInParenthesis: false)))" } - return try sqlLiteral.sql(&context) + return try sqlLiteral.sql(context) } /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) @@ -273,11 +273,11 @@ private struct _SQLSelectionLiteral: SQLSelectable { self.sqlLiteral = sqlLiteral } - func resultColumnSQL(_ context: inout SQLGenerationContext) throws -> String { - try sqlLiteral.sql(&context) + func resultColumnSQL(_ context: SQLGenerationContext) throws -> String { + try sqlLiteral.sql(context) } - func countedSQL(_ context: inout SQLGenerationContext) throws -> String { + func countedSQL(_ context: SQLGenerationContext) throws -> String { fatalError(""" Selection literals can't be counted. \ To resolve this error, select one or several literal expressions instead. \ @@ -323,8 +323,8 @@ private struct _SQLOrderingLiteral: SQLOrderingTerm { """) } - func orderingTermSQL(_ context: inout SQLGenerationContext) throws -> String { - try sqlLiteral.sql(&context) + func orderingTermSQL(_ context: SQLGenerationContext) throws -> String { + try sqlLiteral.sql(context) } func qualifiedOrdering(with alias: TableAlias) -> SQLOrderingTerm { diff --git a/GRDB/Core/SQLRequest.swift b/GRDB/Core/SQLRequest.swift index 0b9815001..94339a943 100644 --- a/GRDB/Core/SQLRequest.swift +++ b/GRDB/Core/SQLRequest.swift @@ -108,8 +108,8 @@ public struct SQLRequest: FetchRequest { /// /// :nodoc: public func makePreparedRequest(_ db: Database, forSingleResult singleResult: Bool) throws -> PreparedRequest { - var context = SQLGenerationContext.sqlLiteralContext(db) - let sql = try sqlLiteral.sql(&context) + let context = SQLGenerationContext.sqlLiteralContext(db) + let sql = try sqlLiteral.sql(context) let statement: SelectStatement switch cache { case .none: @@ -126,8 +126,8 @@ public struct SQLRequest: FetchRequest { extension SQLRequest: SQLCollection { /// :nodoc - public func collectionSQL(_ context: inout SQLGenerationContext) throws -> String { - try sqlLiteral.sql(&context) + public func collectionSQL(_ context: SQLGenerationContext) throws -> String { + try sqlLiteral.sql(context) } } @@ -141,8 +141,8 @@ extension SQLRequest: SQLRequestExpressible { private struct SQLRequestExpression: SQLExpression { var request: SQLRequest - func expressionSQL(_ context: inout SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { - try "(" + request.sqlLiteral.sql(&context) + ")" + func expressionSQL(_ context: SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { + try "(" + request.sqlLiteral.sql(context) + ")" } func qualifiedExpression(with alias: TableAlias) -> SQLExpression { diff --git a/GRDB/FTS/FTS5Tokenizer.swift b/GRDB/FTS/FTS5Tokenizer.swift index 480895802..a78962417 100644 --- a/GRDB/FTS/FTS5Tokenizer.swift +++ b/GRDB/FTS/FTS5Tokenizer.swift @@ -87,7 +87,7 @@ extension FTS5Tokenizer { let nText = Int32(buffer.count) var context = TokenizeContext() - try withUnsafeMutablePointer(to: &context) { contextPointer in + try withUnsafeMutablePointer(to: context) { contextPointer in let code = tokenize( context: UnsafeMutableRawPointer(contextPointer), tokenization: tokenization, diff --git a/GRDB/Fixit/GRDB-5.0.swift b/GRDB/Fixit/GRDB-5.0.swift index d9e95643f..7aabf57e2 100644 --- a/GRDB/Fixit/GRDB-5.0.swift +++ b/GRDB/Fixit/GRDB-5.0.swift @@ -151,7 +151,7 @@ public struct SQLExpressionLiteral: SQLExpression { public init(literal sqlLiteral: SQLLiteral) { preconditionFailure() } - public func expressionSQL(_ context: inout SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String + public func expressionSQL(_ context: SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { preconditionFailure() } public func qualifiedExpression(with alias: TableAlias) -> SQLExpression diff --git a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift index 75aef6146..434f4d3ae 100644 --- a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift @@ -407,7 +407,7 @@ extension QueryInterfaceRequest: Refinable { extension QueryInterfaceRequest: SQLCollection { /// :nodoc - public func collectionSQL(_ context: inout SQLGenerationContext) throws -> String { + public func collectionSQL(_ context: SQLGenerationContext) throws -> String { let generator = SQLQueryGenerator(query: query, requiresSingleColumn: true) return try generator.sql(context) } @@ -423,7 +423,7 @@ extension QueryInterfaceRequest: SQLRequestExpressible { private struct QueryInterfaceRequestExpression: SQLExpression { var request: QueryInterfaceRequest - func expressionSQL(_ context: inout SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { + func expressionSQL(_ context: SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { let generator = SQLQueryGenerator( query: request.query, forSingleResult: true, @@ -643,10 +643,10 @@ public struct ColumnAssignment { var column: ColumnExpression var value: SQLExpressible - func sql(_ context: inout SQLGenerationContext) throws -> String { - try column.expressionSQL(&context, wrappedInParenthesis: false) + + func sql(_ context: SQLGenerationContext) throws -> String { + try column.expressionSQL(context, wrappedInParenthesis: false) + " = " + - value.sqlExpression.expressionSQL(&context, wrappedInParenthesis: false) + value.sqlExpression.expressionSQL(context, wrappedInParenthesis: false) } } diff --git a/GRDB/QueryInterface/SQL/Column.swift b/GRDB/QueryInterface/SQL/Column.swift index a0e1f36b1..ca7888a13 100644 --- a/GRDB/QueryInterface/SQL/Column.swift +++ b/GRDB/QueryInterface/SQL/Column.swift @@ -27,7 +27,7 @@ public protocol ColumnExpression: SQLExpression { extension ColumnExpression { /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) /// :nodoc: - public func expressionSQL(_ context: inout SQLGenerationContext, wrappedInParenthesis: Bool) -> String { + public func expressionSQL(_ context: SQLGenerationContext, wrappedInParenthesis: Bool) -> String { name.quotedDatabaseIdentifier } @@ -73,7 +73,7 @@ struct QualifiedColumn: ColumnExpression { self.alias = alias } - func expressionSQL(_ context: inout SQLGenerationContext, wrappedInParenthesis: Bool) -> String { + func expressionSQL(_ context: SQLGenerationContext, wrappedInParenthesis: Bool) -> String { if let qualifier = context.qualifier(for: alias) { return qualifier.quotedDatabaseIdentifier + "." + name.quotedDatabaseIdentifier } diff --git a/GRDB/QueryInterface/SQL/SQLCollatedExpression.swift b/GRDB/QueryInterface/SQL/SQLCollatedExpression.swift index 309adc136..5af8e7122 100644 --- a/GRDB/QueryInterface/SQL/SQLCollatedExpression.swift +++ b/GRDB/QueryInterface/SQL/SQLCollatedExpression.swift @@ -87,8 +87,8 @@ extension SQLCollatedExpression: SQLOrderingTerm { /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) /// :nodoc: - public func orderingTermSQL(_ context: inout SQLGenerationContext) throws -> String { - try sqlExpression.orderingTermSQL(&context) + public func orderingTermSQL(_ context: SQLGenerationContext) throws -> String { + try sqlExpression.orderingTermSQL(context) } /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) diff --git a/GRDB/QueryInterface/SQL/SQLCollection.swift b/GRDB/QueryInterface/SQL/SQLCollection.swift index 8418395de..05a79d099 100644 --- a/GRDB/QueryInterface/SQL/SQLCollection.swift +++ b/GRDB/QueryInterface/SQL/SQLCollection.swift @@ -9,7 +9,7 @@ public protocol SQLCollection { /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) /// /// Returns an SQL string that represents the collection. - func collectionSQL(_ context: inout SQLGenerationContext) throws -> String + func collectionSQL(_ context: SQLGenerationContext) throws -> String /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) /// @@ -44,9 +44,9 @@ struct SQLExpressionsArray: SQLCollection { self.expressions = expressions.map(\.sqlExpression) } - func collectionSQL(_ context: inout SQLGenerationContext) throws -> String { + func collectionSQL(_ context: SQLGenerationContext) throws -> String { try expressions - .map { try $0.expressionSQL(&context, wrappedInParenthesis: false) } + .map { try $0.expressionSQL(context, wrappedInParenthesis: false) } .joined(separator: ", ") } diff --git a/GRDB/QueryInterface/SQL/SQLExpressible.swift b/GRDB/QueryInterface/SQL/SQLExpressible.swift index ca95b8f1a..914ce66da 100644 --- a/GRDB/QueryInterface/SQL/SQLExpressible.swift +++ b/GRDB/QueryInterface/SQL/SQLExpressible.swift @@ -78,8 +78,8 @@ extension SQLExpressible where Self: SQLOrderingTerm { /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) /// :nodoc: - public func orderingTermSQL(_ context: inout SQLGenerationContext) throws -> String { - try sqlExpression.expressionSQL(&context, wrappedInParenthesis: false) + public func orderingTermSQL(_ context: SQLGenerationContext) throws -> String { + try sqlExpression.expressionSQL(context, wrappedInParenthesis: false) } } @@ -89,14 +89,14 @@ extension SQLExpressible where Self: SQLSelectable { /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) /// :nodoc: - public func resultColumnSQL(_ context: inout SQLGenerationContext) throws -> String { - try sqlExpression.expressionSQL(&context, wrappedInParenthesis: false) + public func resultColumnSQL(_ context: SQLGenerationContext) throws -> String { + try sqlExpression.expressionSQL(context, wrappedInParenthesis: false) } /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) /// :nodoc: - public func countedSQL(_ context: inout SQLGenerationContext) throws -> String { - try sqlExpression.expressionSQL(&context, wrappedInParenthesis: false) + public func countedSQL(_ context: SQLGenerationContext) throws -> String { + try sqlExpression.expressionSQL(context, wrappedInParenthesis: false) } /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) diff --git a/GRDB/QueryInterface/SQL/SQLExpression+QueryInterface.swift b/GRDB/QueryInterface/SQL/SQLExpression+QueryInterface.swift index 302f0961c..1755e5cd6 100644 --- a/GRDB/QueryInterface/SQL/SQLExpression+QueryInterface.swift +++ b/GRDB/QueryInterface/SQL/SQLExpression+QueryInterface.swift @@ -5,8 +5,8 @@ extension SQLExpression { /// /// try "foo'bar".databaseValue.quotedSQL(db) // "'foo''bar'"" func quotedSQL(_ db: Database) throws -> String { - var context = SQLGenerationContext.sqlLiteralContext(db, argumentsSink: StatementArgumentsSink.forRawSQL) - return try expressionSQL(&context, wrappedInParenthesis: false) + let context = SQLGenerationContext.sqlLiteralContext(db, argumentsSink: StatementArgumentsSink.forRawSQL) + return try expressionSQL(context, wrappedInParenthesis: false) } } @@ -70,13 +70,13 @@ public struct SQLExpressionUnary: SQLExpression { /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) /// :nodoc: - public func expressionSQL(_ context: inout SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { + public func expressionSQL(_ context: SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { if wrappedInParenthesis { - return try "(\(expressionSQL(&context, wrappedInParenthesis: false)))" + return try "(\(expressionSQL(context, wrappedInParenthesis: false)))" } return try op.sql + (op.needsRightSpace ? " " : "") - + expression.expressionSQL(&context, wrappedInParenthesis: true) + + expression.expressionSQL(context, wrappedInParenthesis: true) } /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) @@ -168,14 +168,14 @@ public struct SQLExpressionBinary: SQLExpression { /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) /// :nodoc: - public func expressionSQL(_ context: inout SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { + public func expressionSQL(_ context: SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { if wrappedInParenthesis { - return try "(\(expressionSQL(&context, wrappedInParenthesis: false)))" + return try "(\(expressionSQL(context, wrappedInParenthesis: false)))" } return try """ - \(lhs.expressionSQL(&context, wrappedInParenthesis: true)) \ + \(lhs.expressionSQL(context, wrappedInParenthesis: true)) \ \(op.sql) \ - \(rhs.expressionSQL(&context, wrappedInParenthesis: true)) + \(rhs.expressionSQL(context, wrappedInParenthesis: true)) """ } @@ -265,14 +265,14 @@ struct SQLExpressionBinaryReduce: SQLExpression { } } - func expressionSQL(_ context: inout SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { + func expressionSQL(_ context: SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { guard let first = expressions.first else { - return try op.neutralValue.sqlExpression.expressionSQL(&context, wrappedInParenthesis: false) + return try op.neutralValue.sqlExpression.expressionSQL(context, wrappedInParenthesis: false) } if expressions.count == 1 { - return try first.expressionSQL(&context, wrappedInParenthesis: wrappedInParenthesis) + return try first.expressionSQL(context, wrappedInParenthesis: wrappedInParenthesis) } - let expressionSQLs = try expressions.map { try $0.expressionSQL(&context, wrappedInParenthesis: true) } + let expressionSQLs = try expressions.map { try $0.expressionSQL(context, wrappedInParenthesis: true) } let joiner = " \(op.sql) " if wrappedInParenthesis { return "(\(expressionSQLs.joined(separator: joiner)))" @@ -378,14 +378,14 @@ struct SQLExpressionEqual: SQLExpression { } } - func expressionSQL(_ context: inout SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { + func expressionSQL(_ context: SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { if wrappedInParenthesis { - return try "(\(expressionSQL(&context, wrappedInParenthesis: false)))" + return try "(\(expressionSQL(context, wrappedInParenthesis: false)))" } return try """ - \(lhs.expressionSQL(&context, wrappedInParenthesis: true)) \ + \(lhs.expressionSQL(context, wrappedInParenthesis: true)) \ \(op.rawValue) \ - \(rhs.expressionSQL(&context, wrappedInParenthesis: true)) + \(rhs.expressionSQL(context, wrappedInParenthesis: true)) """ } @@ -449,14 +449,14 @@ struct SQLExpressionContains: SQLExpression { self.isNegated = negated } - func expressionSQL(_ context: inout SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { + func expressionSQL(_ context: SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { if wrappedInParenthesis { - return try "(\(expressionSQL(&context, wrappedInParenthesis: false)))" + return try "(\(expressionSQL(context, wrappedInParenthesis: false)))" } return try """ - \(expression.expressionSQL(&context, wrappedInParenthesis: true)) \ + \(expression.expressionSQL(context, wrappedInParenthesis: true)) \ \(isNegated ? "NOT IN" : "IN") \ - (\(collection.collectionSQL(&context))) + (\(collection.collectionSQL(context))) """ } @@ -518,16 +518,16 @@ struct SQLExpressionBetween: SQLExpression { self.isNegated = negated } - func expressionSQL(_ context: inout SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { + func expressionSQL(_ context: SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { if wrappedInParenthesis { - return try "(\(expressionSQL(&context, wrappedInParenthesis: false)))" + return try "(\(expressionSQL(context, wrappedInParenthesis: false)))" } return try """ - \(expression.expressionSQL(&context, wrappedInParenthesis: true)) \ + \(expression.expressionSQL(context, wrappedInParenthesis: true)) \ \(isNegated ? "NOT BETWEEN" : "BETWEEN") \ - \(lowerBound.expressionSQL(&context, wrappedInParenthesis: true)) \ + \(lowerBound.expressionSQL(context, wrappedInParenthesis: true)) \ AND \ - \(upperBound.expressionSQL(&context, wrappedInParenthesis: true)) + \(upperBound.expressionSQL(context, wrappedInParenthesis: true)) """ } @@ -611,11 +611,11 @@ public struct SQLExpressionFunction: SQLExpression { /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) /// :nodoc: - public func expressionSQL(_ context: inout SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { + public func expressionSQL(_ context: SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { var sql = functionName.sql sql += "(" sql += try arguments - .map { try $0.expressionSQL(&context, wrappedInParenthesis: false) } + .map { try $0.expressionSQL(context, wrappedInParenthesis: false) } .joined(separator: ", ") sql += ")" return sql @@ -642,8 +642,8 @@ struct SQLExpressionCount: SQLExpression { self.counted = counted } - func expressionSQL(_ context: inout SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { - try "COUNT(" + counted.countedSQL(&context) + ")" + func expressionSQL(_ context: SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { + try "COUNT(" + counted.countedSQL(context) + ")" } func qualifiedExpression(with alias: TableAlias) -> SQLExpression { @@ -664,8 +664,8 @@ struct SQLExpressionCountDistinct: SQLExpression { self.counted = counted } - func expressionSQL(_ context: inout SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { - try "COUNT(DISTINCT " + counted.expressionSQL(&context, wrappedInParenthesis: false) + ")" + func expressionSQL(_ context: SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { + try "COUNT(DISTINCT " + counted.expressionSQL(context, wrappedInParenthesis: false) + ")" } func qualifiedExpression(with alias: TableAlias) -> SQLExpression { @@ -692,12 +692,12 @@ struct SQLExpressionIsEmpty: SQLExpression { SQLExpressionIsEmpty(countExpression, isEmpty: !isEmpty) } - func expressionSQL(_ context: inout SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { + func expressionSQL(_ context: SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { if wrappedInParenthesis { - return try "(\(expressionSQL(&context, wrappedInParenthesis: false)))" + return try "(\(expressionSQL(context, wrappedInParenthesis: false)))" } return try """ - \(countExpression.expressionSQL(&context, wrappedInParenthesis: true)) \ + \(countExpression.expressionSQL(context, wrappedInParenthesis: true)) \ \(isEmpty ? "= 0" : "> 0") """ } @@ -713,14 +713,14 @@ struct TableMatchExpression: SQLExpression { var alias: TableAlias var pattern: SQLExpression - func expressionSQL(_ context: inout SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { + func expressionSQL(_ context: SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { if wrappedInParenthesis { - return try "(\(expressionSQL(&context, wrappedInParenthesis: false)))" + return try "(\(expressionSQL(context, wrappedInParenthesis: false)))" } return try """ \(context.resolvedName(for: alias).quotedDatabaseIdentifier) \ MATCH \ - \(pattern.expressionSQL(&context, wrappedInParenthesis: true)) + \(pattern.expressionSQL(context, wrappedInParenthesis: true)) """ } @@ -746,12 +746,12 @@ struct SQLExpressionCollate: SQLExpression { self.collationName = collationName } - func expressionSQL(_ context: inout SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { + func expressionSQL(_ context: SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { if wrappedInParenthesis { - return try "(\(expressionSQL(&context, wrappedInParenthesis: false)))" + return try "(\(expressionSQL(context, wrappedInParenthesis: false)))" } return try """ - \(expression.expressionSQL(&context, wrappedInParenthesis: false)) \ + \(expression.expressionSQL(context, wrappedInParenthesis: false)) \ COLLATE \ \(collationName.rawValue) """ diff --git a/GRDB/QueryInterface/SQL/SQLExpression.swift b/GRDB/QueryInterface/SQL/SQLExpression.swift index 6418b2681..67c4c1eb4 100644 --- a/GRDB/QueryInterface/SQL/SQLExpression.swift +++ b/GRDB/QueryInterface/SQL/SQLExpression.swift @@ -16,7 +16,7 @@ public protocol SQLExpression: SQLSpecificExpressible, SQLSelectable, SQLOrderin /// statement arguments. /// - parameter wrappedInParenthesis: If true, the returned SQL should be /// wrapped inside parenthesis. - func expressionSQL(_ context: inout SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String + func expressionSQL(_ context: SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) /// @@ -126,11 +126,11 @@ struct SQLExpressionNot: SQLExpression { self.expression = expression } - func expressionSQL(_ context: inout SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { + func expressionSQL(_ context: SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String { if wrappedInParenthesis { - return try "(\(expressionSQL(&context, wrappedInParenthesis: false)))" + return try "(\(expressionSQL(context, wrappedInParenthesis: false)))" } - return try "NOT \(expression.expressionSQL(&context, wrappedInParenthesis: true))" + return try "NOT \(expression.expressionSQL(context, wrappedInParenthesis: true))" } var negated: SQLExpression { diff --git a/GRDB/QueryInterface/SQL/SQLOrdering.swift b/GRDB/QueryInterface/SQL/SQLOrdering.swift index 8516e1784..9deca0855 100644 --- a/GRDB/QueryInterface/SQL/SQLOrdering.swift +++ b/GRDB/QueryInterface/SQL/SQLOrdering.swift @@ -15,7 +15,7 @@ public protocol SQLOrderingTerm { /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) /// /// Returns an SQL string that represents the ordering term. - func orderingTermSQL(_ context: inout SQLGenerationContext) throws -> String + func orderingTermSQL(_ context: SQLGenerationContext) throws -> String /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) func qualifiedOrdering(with alias: TableAlias) -> SQLOrderingTerm @@ -46,17 +46,17 @@ enum SQLOrdering: SQLOrderingTerm { } } - func orderingTermSQL(_ context: inout SQLGenerationContext) throws -> String { + func orderingTermSQL(_ context: SQLGenerationContext) throws -> String { switch self { case .asc(let expression): - return try expression.expressionSQL(&context, wrappedInParenthesis: false) + " ASC" + return try expression.expressionSQL(context, wrappedInParenthesis: false) + " ASC" case .desc(let expression): - return try expression.expressionSQL(&context, wrappedInParenthesis: false) + " DESC" + return try expression.expressionSQL(context, wrappedInParenthesis: false) + " DESC" #if GRDBCUSTOMSQLITE case .ascNullsLast(let expression): - return try expression.expressionSQL(&context, wrappedInParenthesis: false) + " ASC NULLS LAST" + return try expression.expressionSQL(context, wrappedInParenthesis: false) + " ASC NULLS LAST" case .descNullsFirst(let expression): - return try expression.expressionSQL(&context, wrappedInParenthesis: false) + " DESC NULLS FIRST" + return try expression.expressionSQL(context, wrappedInParenthesis: false) + " DESC NULLS FIRST" #endif } } diff --git a/GRDB/QueryInterface/SQL/SQLSelectable+QueryInterface.swift b/GRDB/QueryInterface/SQL/SQLSelectable+QueryInterface.swift index 036469aac..6e7b64633 100644 --- a/GRDB/QueryInterface/SQL/SQLSelectable+QueryInterface.swift +++ b/GRDB/QueryInterface/SQL/SQLSelectable+QueryInterface.swift @@ -21,13 +21,13 @@ public struct AllColumns { extension AllColumns: SQLSelectable { /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) /// :nodoc: - public func resultColumnSQL(_ context: inout SQLGenerationContext) -> String { + public func resultColumnSQL(_ context: SQLGenerationContext) -> String { "*" } /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) /// :nodoc: - public func countedSQL(_ context: inout SQLGenerationContext) -> String { + public func countedSQL(_ context: SQLGenerationContext) -> String { "*" } @@ -70,14 +70,14 @@ struct QualifiedAllColumns { } extension QualifiedAllColumns: SQLSelectable { - func resultColumnSQL(_ context: inout SQLGenerationContext) -> String { + func resultColumnSQL(_ context: SQLGenerationContext) -> String { if let qualifier = context.qualifier(for: alias) { return qualifier.quotedDatabaseIdentifier + ".*" } return "*" } - func countedSQL(_ context: inout SQLGenerationContext) -> String { + func countedSQL(_ context: SQLGenerationContext) -> String { // TODO: restore the check below. // // It is currently disabled because of AssociationAggregateTests.testHasManyIsEmpty: @@ -125,12 +125,12 @@ struct SQLAliasedExpression: SQLSelectable { self.name = name } - func resultColumnSQL(_ context: inout SQLGenerationContext) throws -> String { - try expression.resultColumnSQL(&context) + " AS " + name.quotedDatabaseIdentifier + func resultColumnSQL(_ context: SQLGenerationContext) throws -> String { + try expression.resultColumnSQL(context) + " AS " + name.quotedDatabaseIdentifier } - func countedSQL(_ context: inout SQLGenerationContext) throws -> String { - try expression.countedSQL(&context) + func countedSQL(_ context: SQLGenerationContext) throws -> String { + try expression.countedSQL(context) } func count(distinct: Bool) -> SQLCount? { diff --git a/GRDB/QueryInterface/SQL/SQLSelectable.swift b/GRDB/QueryInterface/SQL/SQLSelectable.swift index 4c3901d0b..e7ef0c34d 100644 --- a/GRDB/QueryInterface/SQL/SQLSelectable.swift +++ b/GRDB/QueryInterface/SQL/SQLSelectable.swift @@ -8,10 +8,10 @@ /// :nodoc: public protocol SQLSelectable { /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) - func resultColumnSQL(_ context: inout SQLGenerationContext) throws -> String + func resultColumnSQL(_ context: SQLGenerationContext) throws -> String /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) - func countedSQL(_ context: inout SQLGenerationContext) throws -> String + func countedSQL(_ context: SQLGenerationContext) throws -> String /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) func count(distinct: Bool) -> SQLCount? diff --git a/GRDB/QueryInterface/SQLGeneration/SQLGenerationContext.swift b/GRDB/QueryInterface/SQLGeneration/SQLGenerationContext.swift index 95928bc86..f03c8e986 100644 --- a/GRDB/QueryInterface/SQLGeneration/SQLGenerationContext.swift +++ b/GRDB/QueryInterface/SQLGeneration/SQLGenerationContext.swift @@ -51,10 +51,9 @@ public struct SQLGenerationContext { qualifierNeeded: aliases.count > 1) } - // TODO: make it not mutating /// Returns whether arguments could be appended. /// May be false for SQLGenerationContext.rawSQLContext - mutating func append(arguments: StatementArguments) -> Bool { + func append(arguments: StatementArguments) -> Bool { argumentsSink.append(arguments: arguments) } diff --git a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift index 964be2cd5..0dc8dfeb5 100644 --- a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift +++ b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift @@ -68,7 +68,7 @@ struct SQLQueryGenerator: Refinable { // Build an SQK generation context with all aliases found in the query, // so that we can disambiguate tables that are used several times with // SQL aliases. - var context = SQLGenerationContext.queryContext(db, argumentsSink: argumentsSink, aliases: relation.allAliases) + let context = SQLGenerationContext.queryContext(db, argumentsSink: argumentsSink, aliases: relation.allAliases) var sql = "SELECT" @@ -83,40 +83,40 @@ struct SQLQueryGenerator: Refinable { let columnCount = try selection[0].columnCount(context.db) GRDBPrecondition(columnCount == 1, "A single column must be selected.") } - sql += try " " + selection.map { try $0.resultColumnSQL(&context) }.joined(separator: ", ") + sql += try " " + selection.map { try $0.resultColumnSQL(context) }.joined(separator: ", ") sql += " FROM " - sql += try relation.source.sql(&context) + sql += try relation.source.sql(context) for (_, join) in relation.joins { sql += " " - sql += try join.sql(&context, leftAlias: relation.sourceAlias) + sql += try join.sql(context, leftAlias: relation.sourceAlias) } let filters = try relation.filtersPromise.resolve(context.db) if filters.isEmpty == false { sql += " WHERE " - sql += try filters.joined(operator: .and).expressionSQL(&context, wrappedInParenthesis: false) + sql += try filters.joined(operator: .and).expressionSQL(context, wrappedInParenthesis: false) } if let groupExpressions = try groupPromise?.resolve(context.db), !groupExpressions.isEmpty { sql += " GROUP BY " sql += try groupExpressions - .map { try $0.expressionSQL(&context, wrappedInParenthesis: false) } + .map { try $0.expressionSQL(context, wrappedInParenthesis: false) } .joined(separator: ", ") } let havingExpressions = try havingExpressionsPromise.resolve(context.db) if havingExpressions.isEmpty == false { sql += " HAVING " - sql += try havingExpressions.joined(operator: .and).expressionSQL(&context, wrappedInParenthesis: false) + sql += try havingExpressions.joined(operator: .and).expressionSQL(context, wrappedInParenthesis: false) } let orderings = try relation.ordering.resolve(context.db) if !orderings.isEmpty { sql += " ORDER BY " sql += try orderings - .map { try $0.orderingTermSQL(&context) } + .map { try $0.orderingTermSQL(context) } .joined(separator: ", ") } @@ -175,22 +175,22 @@ struct SQLQueryGenerator: Refinable { return try makeTrivialDeleteStatement(db) } - var context = SQLGenerationContext.queryContext(db, aliases: relation.allAliases) + let context = SQLGenerationContext.queryContext(db, aliases: relation.allAliases) - var sql = try "DELETE FROM " + relation.source.sql(&context) + var sql = try "DELETE FROM " + relation.source.sql(context) let filters = try relation.filtersPromise.resolve(db) if filters.isEmpty == false { sql += " WHERE " sql += try filters .joined(operator: .and) - .expressionSQL(&context, wrappedInParenthesis: false) + .expressionSQL(context, wrappedInParenthesis: false) } if let limit = limit { let orderings = try relation.ordering.resolve(db) if !orderings.isEmpty { - sql += try " ORDER BY " + orderings.map { try $0.orderingTermSQL(&context) }.joined(separator: ", ") + sql += try " ORDER BY " + orderings.map { try $0.orderingTermSQL(context) }.joined(separator: ", ") } sql += " LIMIT " + limit.sql } @@ -215,11 +215,11 @@ struct SQLQueryGenerator: Refinable { fatalError("Can't delete without any database table") } - var context = SQLGenerationContext.sqlLiteralContext(db) + let context = SQLGenerationContext.sqlLiteralContext(db) let primaryKey = try db.primaryKeyExpression(tableName) var sql = "DELETE FROM \(tableName.quotedDatabaseIdentifier) WHERE " - sql += try primaryKey.expressionSQL(&context, wrappedInParenthesis: false) + sql += try primaryKey.expressionSQL(context, wrappedInParenthesis: false) sql += " IN (" sql += try map(\.relation, { $0.selectOnly([primaryKey]) }).sql(db, argumentsSink: context.argumentsSink) sql += ")" @@ -256,7 +256,7 @@ struct SQLQueryGenerator: Refinable { return nil } - var context = SQLGenerationContext.queryContext(db, aliases: relation.allAliases) + let context = SQLGenerationContext.queryContext(db, aliases: relation.allAliases) var sql = "UPDATE " @@ -264,10 +264,10 @@ struct SQLQueryGenerator: Refinable { sql += "OR \(conflictResolution.rawValue) " } - sql += try relation.source.sql(&context) + sql += try relation.source.sql(context) let assignmentsSQL = try assignments - .map { try $0.sql(&context) } + .map { try $0.sql(context) } .joined(separator: ", ") sql += " SET " + assignmentsSQL @@ -276,13 +276,13 @@ struct SQLQueryGenerator: Refinable { sql += " WHERE " sql += try filters .joined(operator: .and) - .expressionSQL(&context, wrappedInParenthesis: false) + .expressionSQL(context, wrappedInParenthesis: false) } if let limit = limit { let orderings = try relation.ordering.resolve(db) if !orderings.isEmpty { - sql += try " ORDER BY " + orderings.map { try $0.orderingTermSQL(&context) }.joined(separator: ", ") + sql += try " ORDER BY " + orderings.map { try $0.orderingTermSQL(context) }.joined(separator: ", ") } sql += " LIMIT " + limit.sql } @@ -319,7 +319,7 @@ struct SQLQueryGenerator: Refinable { return nil } - var context = SQLGenerationContext.sqlLiteralContext(db) + let context = SQLGenerationContext.sqlLiteralContext(db) let primaryKey = try db.primaryKeyExpression(tableName) // UPDATE table... @@ -331,13 +331,13 @@ struct SQLQueryGenerator: Refinable { // SET column = value... let assignmentsSQL = try assignments - .map { try $0.sql(&context) } + .map { try $0.sql(context) } .joined(separator: ", ") sql += " SET " + assignmentsSQL // WHERE id IN (SELECT id FROM ...) sql += " WHERE " - sql += try primaryKey.expressionSQL(&context, wrappedInParenthesis: false) + sql += try primaryKey.expressionSQL(context, wrappedInParenthesis: false) sql += " IN (" sql += try map(\.relation, { $0.selectOnly([primaryKey]) }).sql(db, argumentsSink: context.argumentsSink) sql += ")" @@ -636,7 +636,7 @@ private enum SQLQualifiedSource { } } - func sql(_ context: inout SQLGenerationContext) throws -> String { + func sql(_ context: SQLGenerationContext) throws -> String { switch self { case let .table(tableName, alias): if let aliasName = context.aliasName(for: alias) { @@ -683,8 +683,8 @@ private struct SQLQualifiedJoin: Refinable { self.relation = SQLQualifiedRelation(child.relation) } - func sql(_ context: inout SQLGenerationContext, leftAlias: TableAlias) throws -> String { - try sql(&context, leftAlias: leftAlias, allowingInnerJoin: true) + func sql(_ context: SQLGenerationContext, leftAlias: TableAlias) throws -> String { + try sql(context, leftAlias: leftAlias, allowingInnerJoin: true) } /// Removes all selections from joins @@ -693,7 +693,7 @@ private struct SQLQualifiedJoin: Refinable { } private func sql( - _ context: inout SQLGenerationContext, + _ context: SQLGenerationContext, leftAlias: TableAlias, allowingInnerJoin allowsInnerJoin: Bool) throws -> String @@ -715,7 +715,7 @@ private struct SQLQualifiedJoin: Refinable { } // JOIN table... - var sql = try "\(kind.rawValue) \(relation.source.sql(&context))" + var sql = try "\(kind.rawValue) \(relation.source.sql(context))" let rightAlias = relation.sourceAlias // ... ON AND @@ -725,13 +725,13 @@ private struct SQLQualifiedJoin: Refinable { let filterSQL = try filters .joined(operator: .and) .qualifiedExpression(with: rightAlias) - .expressionSQL(&context, wrappedInParenthesis: false) + .expressionSQL(context, wrappedInParenthesis: false) sql += " ON \(filterSQL)" } for (_, join) in relation.joins { // Right becomes left as we dig further - sql += try " \(join.sql(&context, leftAlias: rightAlias, allowingInnerJoin: allowsInnerJoin))" + sql += try " \(join.sql(context, leftAlias: rightAlias, allowingInnerJoin: allowsInnerJoin))" } return sql diff --git a/Tests/GRDBTests/SQLExpressionLiteralTests.swift b/Tests/GRDBTests/SQLExpressionLiteralTests.swift index f3f5a1db9..604980e67 100644 --- a/Tests/GRDBTests/SQLExpressionLiteralTests.swift +++ b/Tests/GRDBTests/SQLExpressionLiteralTests.swift @@ -8,7 +8,7 @@ class SQLExpressionLiteralTests: GRDBTestCase { try DatabaseQueue().inDatabase { db in let expression = Column("foo").collating(.nocase) == "'fooéı👨👨🏿🇫🇷🇨🇮'" && Column("baz") >= 1 var context = SQLGenerationContext.sqlLiteralContext(db) - let sql = try expression.expressionSQL(&context, wrappedInParenthesis: true) + let sql = try expression.expressionSQL(context, wrappedInParenthesis: true) XCTAssertEqual(sql, "((\"foo\" = ? COLLATE NOCASE) AND (\"baz\" >= ?))") let values = context.arguments.values XCTAssertEqual(values.count, 2) @@ -21,7 +21,7 @@ class SQLExpressionLiteralTests: GRDBTestCase { try DatabaseQueue().inDatabase { db in let expression = Column("foo").collating(.nocase) == "'fooéı👨👨🏿🇫🇷🇨🇮'" && Column("baz") >= 1 var context = SQLGenerationContext.sqlLiteralContext(db, argumentsSink: .forRawSQL) - let sql = try expression.expressionSQL(&context, wrappedInParenthesis: true) + let sql = try expression.expressionSQL(context, wrappedInParenthesis: true) XCTAssert(context.arguments.isEmpty) XCTAssertEqual(sql, "((\"foo\" = '''fooéı👨👨🏿🇫🇷🇨🇮''' COLLATE NOCASE) AND (\"baz\" >= 1))") }