mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-29 04:24: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
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user