mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
Deprecate SQLExpressionLiteral, hide SQLSelectionLiteral, introduce SQLLiteral.sqlExpression and .sqlSelectable
This commit is contained in:
1 parent
d7ef314a15
commit
5e67ca5566
6 files changed
+133
-110
No files matched your search
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -212,7 +212,7 @@ extension QueryInterfaceRequest: SelectionRequest {
|
||||
as type: RowDecoder.Type = RowDecoder.self)
|
||||
-> QueryInterfaceRequest<RowDecoder>
|
||||
{
|
||||
return select(SQLSelectionLiteral(literal: sqlLiteral), as: type)
|
||||
return select(sqlLiteral.sqlSelectable, as: type)
|
||||
}
|
||||
|
||||
/// Creates a request which appends *selection*.
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user