diff --git a/GRDB/Core/Database+Statements.swift b/GRDB/Core/Database+Statements.swift index 680f89b04..687981d0f 100644 --- a/GRDB/Core/Database+Statements.swift +++ b/GRDB/Core/Database+Statements.swift @@ -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)) } diff --git a/GRDB/Core/SQLLiteral.swift b/GRDB/Core/SQLLiteral.swift index 2715ccae9..91995e25b 100644 --- a/GRDB/Core/SQLLiteral.swift +++ b/GRDB/Core/SQLLiteral.swift @@ -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 { diff --git a/GRDB/Core/SQLRequest.swift b/GRDB/Core/SQLRequest.swift index c680666d3..f1f4d14d2 100644 --- a/GRDB/Core/SQLRequest.swift +++ b/GRDB/Core/SQLRequest.swift @@ -27,7 +27,6 @@ public struct SQLRequest : 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) } diff --git a/GRDB/QueryInterface/RequestProtocols.swift b/GRDB/QueryInterface/RequestProtocols.swift index 0a8ed630f..5b11c7733 100644 --- a/GRDB/QueryInterface/RequestProtocols.swift +++ b/GRDB/QueryInterface/RequestProtocols.swift @@ -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)) } } diff --git a/GRDB/QueryInterface/SQLExpression+QueryInterface.swift b/GRDB/QueryInterface/SQLExpression+QueryInterface.swift index 60c5f10ea..f32408fbe 100644 --- a/GRDB/QueryInterface/SQLExpression+QueryInterface.swift +++ b/GRDB/QueryInterface/SQLExpression+QueryInterface.swift @@ -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) diff --git a/GRDB/QueryInterface/TableDefinition.swift b/GRDB/QueryInterface/TableDefinition.swift index 885fbaab3..2b4924699 100644 --- a/GRDB/QueryInterface/TableDefinition.swift +++ b/GRDB/QueryInterface/TableDefinition.swift @@ -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 } diff --git a/Tests/GRDBTests/QueryInterfaceExtensibilityTests.swift b/Tests/GRDBTests/QueryInterfaceExtensibilityTests.swift index f3d64ff01..8c7ba0da2 100644 --- a/Tests/GRDBTests/QueryInterfaceExtensibilityTests.swift +++ b/Tests/GRDBTests/QueryInterfaceExtensibilityTests.swift @@ -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) }