diff --git a/GRDB/Core/SQLLiteral.swift b/GRDB/Core/SQLLiteral.swift index 95ef40ffb..16e006f7a 100644 --- a/GRDB/Core/SQLLiteral.swift +++ b/GRDB/Core/SQLLiteral.swift @@ -109,6 +109,21 @@ extension SQLLiteral { } } +extension SQLLiteral { + /// Creates an SQL literal expression. + /// + /// SQLLiteral(sql: "1 + 2").sqlExpression + /// SQLLiteral(sql: "? + ?", arguments: [1, 2]).sqlExpression + /// SQLLiteral(sql: ":one + :two", arguments: ["one": 1, "two": 2]).sqlExpression + public var sqlExpression: SQLExpression { + return SQLExpressionLiteral(sqlLiteral: self) + } + + var sqlSelectable: SQLSelectable { + return SQLSelectionLiteral(sqlLiteral: self) + } +} + extension Sequence where Element == SQLLiteral { /// Returns the concatenated SQLLiteral of this sequence of literals, /// inserting the given separator between each element. @@ -173,3 +188,112 @@ extension SQLLiteral: ExpressibleByStringInterpolation { } } #endif + +// 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") +/// +/// The SQL literal may contain `?` and colon-prefixed arguments: +/// +/// SQLExpressionLiteral(sql: "? + ?", arguments: [1, 2]) +/// SQLExpressionLiteral(sql: ":one + :two", arguments: ["one": 1, "two": 2]) +public struct SQLExpressionLiteral: SQLExpression { + private let sqlLiteral: SQLLiteral + + public var sql: String { return sqlLiteral.sql } + + public var arguments: StatementArguments { return sqlLiteral.arguments } + + // Prefer SQLLiteral.sqlExpression + fileprivate init(sqlLiteral: SQLLiteral) { + self.sqlLiteral = sqlLiteral + } + + /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) + /// + /// Creates an SQL literal expression. + /// + /// SQLExpressionLiteral(sql: "1 + 2") + /// SQLExpressionLiteral(sql: "? + ?", arguments: [1, 2]) + /// SQLExpressionLiteral(sql: ":one + :two", arguments: ["one": 1, "two": 2]) + @available(*, deprecated, message: "Use SQLiteral.sqlExpression instead") + public init(sql: String, arguments: StatementArguments = StatementArguments()) { + self.init(sqlLiteral: SQLLiteral(sql: sql, arguments: arguments)) + } + + /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) + /// + /// Creates an SQL literal expression. + /// + /// SQLExpressionLiteral(literal: SQLLiteral(sql: "1 + 2") + /// SQLExpressionLiteral(literal: SQLLiteral(sql: "? + ?", arguments: [1, 2])) + /// SQLExpressionLiteral(literal: SQLLiteral(sql: ":one + :two", arguments: ["one": 1, "two": 2])) + /// + /// With Swift 5, you can safely embed raw values in your SQL queries, + /// without any risk of syntax errors or SQL injection: + /// + /// SQLExpressionLiteral(literal: "\(1) + \(2)") + @available(*, deprecated, message: "Use SQLiteral.sqlExpression instead") + public init(literal sqlLiteral: SQLLiteral) { + self.init(sqlLiteral: sqlLiteral) + } + + /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) + /// :nodoc: + public func expressionSQL(_ context: inout SQLGenerationContext, wrappedInParenthesis: Bool) -> String { + if wrappedInParenthesis { + return "(\(expressionSQL(&context, wrappedInParenthesis: false)))" + } + return sqlLiteral.resolve(&context) + } + + /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) + /// :nodoc: + public func qualifiedExpression(with alias: TableAlias) -> SQLExpression { + return self + } +} + +// MARK: - SQLSelectionLiteral + +private struct SQLSelectionLiteral: SQLSelectable { + private let sqlLiteral: SQLLiteral + + // Prefer SQLLiteral.sqlSelectable + fileprivate init(sqlLiteral: SQLLiteral) { + self.sqlLiteral = sqlLiteral + } + + func resultColumnSQL(_ context: inout SQLGenerationContext) -> String { + return sqlLiteral.resolve(&context) + } + + func countedSQL(_ context: inout SQLGenerationContext) -> String { + fatalError(""" + Selection literals can't be counted. \ + To resolve this error, select one or several SQLExpressionLiteral instead. + """) + } + + func count(distinct: Bool) -> SQLCount? { + fatalError(""" + Selection literals can't be counted. \ + To resolve this error, select one or several SQLExpressionLiteral instead. + """) + } + + func columnCount(_ db: Database) throws -> Int { + fatalError(""" + Selection literals don't known how many columns they contain. \ + To resolve this error, select one or several SQLExpressionLiteral instead. + """) + } + + func qualifiedSelectable(with alias: TableAlias) -> SQLSelectable { + return self + } +} diff --git a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift index 839724348..f0cc36f57 100644 --- a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift @@ -212,7 +212,7 @@ extension QueryInterfaceRequest: SelectionRequest { as type: RowDecoder.Type = RowDecoder.self) -> QueryInterfaceRequest { - return select(SQLSelectionLiteral(literal: sqlLiteral), as: type) + return select(sqlLiteral.sqlSelectable, as: type) } /// Creates a request which appends *selection*. diff --git a/GRDB/QueryInterface/Request/RequestProtocols.swift b/GRDB/QueryInterface/Request/RequestProtocols.swift index 9c6a8cbf1..672d221b4 100644 --- a/GRDB/QueryInterface/Request/RequestProtocols.swift +++ b/GRDB/QueryInterface/Request/RequestProtocols.swift @@ -91,7 +91,7 @@ extension SelectionRequest { /// .select(literal: SQLLiteral(sql: "email")) public func select(literal sqlLiteral: SQLLiteral) -> Self { // NOT TESTED - return select(SQLSelectionLiteral(literal: sqlLiteral)) + return select(sqlLiteral.sqlSelectable) } /// Creates a request which appends *selection*. @@ -174,7 +174,7 @@ extension FilteredRequest { /// request = request.filter(literal: "name = \("O'Brien")") public func filter(literal sqlLiteral: SQLLiteral) -> Self { // NOT TESTED - return filter(SQLExpressionLiteral(literal: sqlLiteral)) + return filter(sqlLiteral.sqlExpression) } /// Creates a request that matches nothing. @@ -369,7 +369,7 @@ extension AggregatingRequest { /// Creates a request with a new grouping. public func group(literal sqlLiteral: SQLLiteral) -> Self { // NOT TESTED - return group(SQLExpressionLiteral(literal: sqlLiteral)) + return group(sqlLiteral.sqlExpression) } /// Creates a request with the provided *sql* added to the @@ -382,7 +382,7 @@ extension AggregatingRequest { /// eventual set of already applied predicates. public func having(literal sqlLiteral: SQLLiteral) -> Self { // NOT TESTED - return having(SQLExpressionLiteral(literal: sqlLiteral)) + return having(sqlLiteral.sqlExpression) } } @@ -496,7 +496,7 @@ extension OrderedRequest { /// .order(sql: "name") public func order(literal sqlLiteral: SQLLiteral) -> Self { // NOT TESTED - return order(SQLExpressionLiteral(literal: sqlLiteral)) + return order(sqlLiteral.sqlExpression) } } diff --git a/GRDB/QueryInterface/SQL/SQLExpression+QueryInterface.swift b/GRDB/QueryInterface/SQL/SQLExpression+QueryInterface.swift index 5e65d079f..38bc53c5a 100644 --- a/GRDB/QueryInterface/SQL/SQLExpression+QueryInterface.swift +++ b/GRDB/QueryInterface/SQL/SQLExpression+QueryInterface.swift @@ -21,68 +21,6 @@ extension SQLExpression { } } -// 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") -/// -/// The SQL literal may contain `?` and colon-prefixed arguments: -/// -/// SQLExpressionLiteral(sql: "? + ?", arguments: [1, 2]) -/// SQLExpressionLiteral(sql: ":one + :two", arguments: ["one": 1, "two": 2]) -public struct SQLExpressionLiteral: SQLExpression { - private let sqlLiteral: SQLLiteral - - public var sql: String { return sqlLiteral.sql } - - public var arguments: StatementArguments { return sqlLiteral.arguments } - - /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) - /// - /// Creates an SQL literal expression. - /// - /// SQLExpressionLiteral(sql: "1 + 2") - /// SQLExpressionLiteral(sql: "? + ?", arguments: [1, 2]) - /// SQLExpressionLiteral(sql: ":one + :two", arguments: ["one": 1, "two": 2]) - public init(sql: String, arguments: StatementArguments = StatementArguments()) { - self.init(literal: SQLLiteral(sql: sql, arguments: arguments)) - } - - /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) - /// - /// Creates an SQL literal expression. - /// - /// SQLExpressionLiteral(literal: SQLLiteral(sql: "1 + 2") - /// SQLExpressionLiteral(literal: SQLLiteral(sql: "? + ?", arguments: [1, 2])) - /// SQLExpressionLiteral(literal: SQLLiteral(sql: ":one + :two", arguments: ["one": 1, "two": 2])) - /// - /// With Swift 5, you can safely embed raw values in your SQL queries, - /// without any risk of syntax errors or SQL injection: - /// - /// SQLExpressionLiteral(literal: "\(1) + \(2)") - public init(literal sqlLiteral: SQLLiteral) { - self.sqlLiteral = sqlLiteral - } - - /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) - /// :nodoc: - public func expressionSQL(_ context: inout SQLGenerationContext, wrappedInParenthesis: Bool) -> String { - if wrappedInParenthesis { - return "(\(expressionSQL(&context, wrappedInParenthesis: false)))" - } - return sqlLiteral.resolve(&context) - } - - /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) - /// :nodoc: - public func qualifiedExpression(with alias: TableAlias) -> SQLExpression { - return self - } -} - // MARK: - SQLExpressionUnary /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) diff --git a/GRDB/QueryInterface/SQL/SQLSelectable.swift b/GRDB/QueryInterface/SQL/SQLSelectable.swift index 4319989f1..c90ed50c8 100644 --- a/GRDB/QueryInterface/SQL/SQLSelectable.swift +++ b/GRDB/QueryInterface/SQL/SQLSelectable.swift @@ -23,45 +23,6 @@ public protocol SQLSelectable { func qualifiedSelectable(with alias: TableAlias) -> SQLSelectable } -// MARK: - SQLSelectionLiteral - -struct SQLSelectionLiteral: SQLSelectable { - private let sqlLiteral: SQLLiteral - - init(literal sqlLiteral: SQLLiteral) { - self.sqlLiteral = sqlLiteral - } - - func resultColumnSQL(_ context: inout SQLGenerationContext) -> String { - return sqlLiteral.resolve(&context) - } - - func countedSQL(_ context: inout SQLGenerationContext) -> String { - fatalError(""" - Selection literals can't be counted. \ - To resolve this error, select one or several SQLExpressionLiteral instead. - """) - } - - func count(distinct: Bool) -> SQLCount? { - fatalError(""" - Selection literals can't be counted. \ - To resolve this error, select one or several SQLExpressionLiteral instead. - """) - } - - func columnCount(_ db: Database) throws -> Int { - fatalError(""" - Selection literals don't known how many columns they contain. \ - To resolve this error, select one or several SQLExpressionLiteral instead. - """) - } - - func qualifiedSelectable(with alias: TableAlias) -> SQLSelectable { - return self - } -} - // MARK: - Counting /// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features) diff --git a/GRDB/QueryInterface/Schema/TableDefinition.swift b/GRDB/QueryInterface/Schema/TableDefinition.swift index 10a18d68b..726252f90 100644 --- a/GRDB/QueryInterface/Schema/TableDefinition.swift +++ b/GRDB/QueryInterface/Schema/TableDefinition.swift @@ -521,7 +521,7 @@ public final class TableDefinition { /// /// - parameter sql: An SQL snippet public func check(sql: String) { - checkConstraints.append(SQLExpressionLiteral(sql: sql)) + checkConstraints.append(SQLLiteral(sql: sql).sqlExpression) } fileprivate func sql(_ db: Database) throws -> String { @@ -850,7 +850,7 @@ public final class ColumnDefinition { /// - returns: Self so that you can further refine the column definition. @discardableResult public func check(sql: String) -> Self { - checkConstraints.append(SQLExpressionLiteral(sql: sql)) + checkConstraints.append(SQLLiteral(sql: sql).sqlExpression) return self } @@ -882,7 +882,7 @@ public final class ColumnDefinition { /// - returns: Self so that you can further refine the column definition. @discardableResult public func defaults(sql: String) -> Self { - defaultExpression = SQLExpressionLiteral(sql: sql) + defaultExpression = SQLLiteral(sql: sql).sqlExpression return self }