mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
SQLLiteral.mapSQL
This commit is contained in:
1 parent
e7995b9cd4
commit
85f618e16e
7 files changed
+40
-46
No files matched your search
@@ -126,7 +126,6 @@ extension Database {
|
||||
/// - arguments: Statement arguments.
|
||||
/// - throws: A DatabaseError whenever an SQLite error occurs.
|
||||
public func execute(rawSQL sql: String, arguments: StatementArguments = StatementArguments()) throws {
|
||||
// TODO: force sql parameter name: execute(sql:...)
|
||||
try execute(literal: SQLLiteral(sql: sql, arguments: arguments))
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,13 @@ public struct SQLLiteral {
|
||||
self.sql = sql
|
||||
self.arguments = arguments
|
||||
}
|
||||
|
||||
/// Returns a literal whose SQL is transformed by the given closure.
|
||||
public func mapSQL(_ transform: (String) throws -> String) rethrows -> SQLLiteral {
|
||||
var result = self
|
||||
result.sql = try transform(sql)
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
extension SQLLiteral {
|
||||
|
||||
@@ -27,7 +27,6 @@ public struct SQLRequest<T> : FetchRequest {
|
||||
/// prepared statement.
|
||||
/// - returns: A SQLRequest
|
||||
public init(rawSQL sql: String, arguments: StatementArguments = StatementArguments(), adapter: RowAdapter? = nil, cached: Bool = false) {
|
||||
// TODO: force sql parameter name: init(sql:...)
|
||||
self.init(literal: SQLLiteral(sql: sql, arguments: arguments), adapter: adapter, fromCache: cached ? .public : nil)
|
||||
}
|
||||
|
||||
|
||||
@@ -332,12 +332,10 @@ extension AggregatingRequest {
|
||||
//
|
||||
// request = Player.group(sql: "teamId, level")
|
||||
//
|
||||
// This is why we use the "unsafe" flag, so that the SQLExpressionLiteral
|
||||
// does not output its safe wrapping parenthesis, and generates
|
||||
// invalid SQL.
|
||||
var expression = SQLExpressionLiteral(literal: sqlLiteral)
|
||||
expression.unsafeRaw = true
|
||||
return group(expression)
|
||||
// This is why we use the "unsafeLiteral" initializer, so that the
|
||||
// SQLExpressionLiteral does not wrap input in parentheses, and
|
||||
// generates invalid SQL `GROUP BY (teamId, level)`.
|
||||
return group(SQLExpressionLiteral(unsafeLiteral: sqlLiteral))
|
||||
}
|
||||
|
||||
/// Creates a request with the provided *sql* added to the
|
||||
@@ -462,12 +460,10 @@ extension OrderedRequest {
|
||||
//
|
||||
// request = Player.order(sql: "teamId, level")
|
||||
//
|
||||
// This is why we use the "unsafe" flag, so that the SQLExpressionLiteral
|
||||
// does not output its safe wrapping parenthesis, and generates
|
||||
// invalid SQL.
|
||||
var expression = SQLExpressionLiteral(literal: sqlLiteral)
|
||||
expression.unsafeRaw = true
|
||||
return order([expression])
|
||||
// This is why we use the "unsafeLiteral" initializer, so that the
|
||||
// SQLExpressionLiteral does not wrap input in parentheses, and
|
||||
// generates invalid SQL `ORDER BY (teamId, level)`.
|
||||
return order(SQLExpressionLiteral(unsafeLiteral: sqlLiteral))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,21 +27,15 @@ extension SQLExpression {
|
||||
///
|
||||
/// SQLExpressionLiteral is an expression built from a raw SQL snippet.
|
||||
///
|
||||
/// SQLExpressionLiteral("1 + 2")
|
||||
/// SQLExpressionLiteral(rawSQL: "1 + 2")
|
||||
///
|
||||
/// The SQL literal may contain `?` and colon-prefixed arguments:
|
||||
///
|
||||
/// SQLExpressionLiteral("? + ?", arguments: [1, 2])
|
||||
/// SQLExpressionLiteral(":one + :two", arguments: ["one": 1, "two": 2])
|
||||
/// SQLExpressionLiteral(rawSQL: "? + ?", arguments: [1, 2])
|
||||
/// SQLExpressionLiteral(rawSQL: ":one + :two", arguments: ["one": 1, "two": 2])
|
||||
public struct SQLExpressionLiteral : SQLExpression {
|
||||
private let sqlLiteral: SQLLiteral
|
||||
|
||||
/// If safe, an SQLExpressionLiteral("foo") wraps itself in parenthesis,
|
||||
/// and outputs "(foo)" in SQL queries. This avoids any bug due to operator
|
||||
/// precedence. When unsafe, the expression literal does not wrap itself
|
||||
/// in parenthesis and outputs its raw sql.
|
||||
var unsafeRaw: Bool = false
|
||||
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
///
|
||||
/// Creates an SQL literal expression.
|
||||
@@ -50,7 +44,6 @@ public struct SQLExpressionLiteral : SQLExpression {
|
||||
/// SQLExpressionLiteral(rawSQL: "? + ?", arguments: [1, 2])
|
||||
/// SQLExpressionLiteral(rawSQL: ":one + :two", arguments: ["one": 1, "two": 2])
|
||||
public init(rawSQL sql: String, arguments: StatementArguments = StatementArguments()) {
|
||||
// TODO: force sql parameter name: fetchCursor(db, rawSQL: sql:...)
|
||||
self.init(literal: SQLLiteral(sql: sql, arguments: arguments))
|
||||
}
|
||||
|
||||
@@ -67,6 +60,13 @@ public struct SQLExpressionLiteral : SQLExpression {
|
||||
///
|
||||
/// SQLExpressionLiteral(literal: "\(1) + \(2)")
|
||||
public init(literal sqlLiteral: SQLLiteral) {
|
||||
self.init(unsafeLiteral: sqlLiteral.mapSQL { "(\($0))" })
|
||||
}
|
||||
|
||||
/// Creates an SQL literal expression without wrapping the SQL literal
|
||||
/// inside parentheses. It is unsafe because the result expression can not
|
||||
/// be safely composed with other expressions.
|
||||
init(unsafeLiteral sqlLiteral: SQLLiteral) {
|
||||
self.sqlLiteral = sqlLiteral
|
||||
}
|
||||
|
||||
@@ -80,11 +80,7 @@ public struct SQLExpressionLiteral : SQLExpression {
|
||||
fatalError("Not implemented")
|
||||
}
|
||||
}
|
||||
if unsafeRaw {
|
||||
return sqlLiteral.sql
|
||||
} else {
|
||||
return "(" + sqlLiteral.sql + ")"
|
||||
}
|
||||
return sqlLiteral.sql
|
||||
}
|
||||
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
|
||||
@@ -416,9 +416,9 @@ public final class TableDefinition {
|
||||
///
|
||||
/// - parameter sql: An SQL snippet
|
||||
public func check(sql: String) {
|
||||
var expression = SQLExpressionLiteral(rawSQL: sql)
|
||||
expression.unsafeRaw = true // It's safe because this expression can't be composed with others
|
||||
checkConstraints.append(expression)
|
||||
// We do not want to wrap the SQL snippet inside parentheses around the
|
||||
// checked SQL. This is why we use the "unsafeLiteral" initializer.
|
||||
checkConstraints.append(SQLExpressionLiteral(unsafeLiteral: SQLLiteral(sql: sql)))
|
||||
}
|
||||
|
||||
fileprivate func sql(_ db: Database) throws -> String {
|
||||
@@ -721,9 +721,9 @@ public final class ColumnDefinition {
|
||||
/// - returns: Self so that you can further refine the column definition.
|
||||
@discardableResult
|
||||
public func check(sql: String) -> Self {
|
||||
var expression = SQLExpressionLiteral(rawSQL: sql)
|
||||
expression.unsafeRaw = true // It's safe because this expression can't be composed with others
|
||||
checkConstraints.append(expression)
|
||||
// We do not want to wrap the SQL snippet inside parentheses around the
|
||||
// checked SQL. This is why we use the "unsafeLiteral" initializer.
|
||||
checkConstraints.append(SQLExpressionLiteral(unsafeLiteral: SQLLiteral(sql: sql)))
|
||||
return self
|
||||
}
|
||||
|
||||
@@ -755,9 +755,9 @@ public final class ColumnDefinition {
|
||||
/// - returns: Self so that you can further refine the column definition.
|
||||
@discardableResult
|
||||
public func defaults(sql: String) -> Self {
|
||||
var expression = SQLExpressionLiteral(rawSQL: sql)
|
||||
expression.unsafeRaw = true // It's safe because this expression can't be composed with others
|
||||
defaultExpression = expression
|
||||
// We do not want to wrap the SQL snippet inside parentheses around the
|
||||
// checked SQL. This is why we use the "unsafeLiteral" initializer.
|
||||
defaultExpression = SQLExpressionLiteral(unsafeLiteral: SQLLiteral(sql: sql))
|
||||
return self
|
||||
}
|
||||
|
||||
|
||||
@@ -40,14 +40,11 @@ func ~= (_ lhs: SQLExpressible, _ rhs: Column) -> SQLExpression {
|
||||
// CAST
|
||||
|
||||
func cast(_ value: SQLExpressible, as type: Database.ColumnType) -> SQLExpression {
|
||||
// Turn the value into an SQLLiteral
|
||||
let sqlLiteral = value.sqlExpression.sqlLiteral
|
||||
|
||||
// Build our "CAST(value AS type)" sql snippet
|
||||
let sql = "CAST(\(sqlLiteral.sql) AS \(type.rawValue))"
|
||||
|
||||
// And return a new literal expression, preserving input arguments
|
||||
return SQLExpressionLiteral(rawSQL: sql, arguments: sqlLiteral.arguments)
|
||||
let literal = value.sqlExpression.sqlLiteral
|
||||
let castLiteral = literal.mapSQL { sql in
|
||||
"CAST(\(sql) AS \(type.rawValue))"
|
||||
}
|
||||
return SQLExpressionLiteral(literal: castLiteral)
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user