mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +00:00
Rename SQLString to SQLLiteral
This commit is contained in:
1 parent
98e2d009c6
commit
f018c7f823
34 files changed
+532
-575
No files matched your search
@@ -108,7 +108,7 @@ extension QueryInterfaceRequest : DerivableRequest, AggregatingRequest {
|
||||
/// }
|
||||
public func select<RowDecoder>(sql: String, arguments: StatementArguments? = nil, as type: RowDecoder.Type) -> QueryInterfaceRequest<RowDecoder> {
|
||||
// TODO: make arguments non optional
|
||||
return select(SQLString(sql: sql, arguments: arguments ?? .init()), as: type)
|
||||
return select(SQLLiteral(sql: sql, arguments: arguments ?? .init()), as: type)
|
||||
}
|
||||
|
||||
/// Creates a request which selects *sql*, and fetches values of
|
||||
@@ -119,7 +119,7 @@ extension QueryInterfaceRequest : DerivableRequest, AggregatingRequest {
|
||||
/// let request = Player.
|
||||
/// .filter(primaryKey: 42)
|
||||
/// .select(
|
||||
/// SQLString(
|
||||
/// SQLLiteral(
|
||||
/// sql: "IFNULL(name, ?)",
|
||||
/// arguments: ["Anonymous"]),
|
||||
/// as: String.self)
|
||||
@@ -134,12 +134,12 @@ extension QueryInterfaceRequest : DerivableRequest, AggregatingRequest {
|
||||
/// let request = Player.
|
||||
/// .filter(primaryKey: 42)
|
||||
/// .select(
|
||||
/// SQLString("IFNULL(name, \("Anonymous"))"),
|
||||
/// SQLLiteral("IFNULL(name, \("Anonymous"))"),
|
||||
/// as: String.self)
|
||||
/// let name: String? = try request.fetchOne(db)
|
||||
/// }
|
||||
public func select<RowDecoder>(_ sqlString: SQLString, as type: RowDecoder.Type) -> QueryInterfaceRequest<RowDecoder> {
|
||||
return select(SQLSelectionLiteral(sqlString), as: type)
|
||||
public func select<RowDecoder>(_ sqlLiteral: SQLLiteral, as type: RowDecoder.Type) -> QueryInterfaceRequest<RowDecoder> {
|
||||
return select(SQLSelectionLiteral(sqlLiteral), as: type)
|
||||
}
|
||||
|
||||
/// Creates a request which appends *selection*.
|
||||
@@ -374,15 +374,15 @@ extension TableRecord {
|
||||
/// let request = Player.select(sql: "id, email")
|
||||
public static func select(sql: String, arguments: StatementArguments? = nil) -> QueryInterfaceRequest<Self> {
|
||||
// TODO: make arguments non optional
|
||||
return select(SQLString(sql: sql, arguments: arguments ?? .init()))
|
||||
return select(SQLLiteral(sql: sql, arguments: arguments ?? .init()))
|
||||
}
|
||||
|
||||
/// Creates a request which selects *sql*.
|
||||
///
|
||||
/// // SELECT id, email FROM player
|
||||
/// let request = Player.select(SQLString("id, email"))
|
||||
public static func select(_ sqlString: SQLString) -> QueryInterfaceRequest<Self> {
|
||||
return all().select(sqlString)
|
||||
/// let request = Player.select(SQLLiteral("id, email"))
|
||||
public static func select(_ sqlLiteral: SQLLiteral) -> QueryInterfaceRequest<Self> {
|
||||
return all().select(sqlLiteral)
|
||||
}
|
||||
|
||||
/// Creates a request which selects *selection*, and fetches values of
|
||||
@@ -419,7 +419,7 @@ extension TableRecord {
|
||||
/// }
|
||||
public static func select<RowDecoder>(sql: String, arguments: StatementArguments? = nil, as type: RowDecoder.Type) -> QueryInterfaceRequest<RowDecoder> {
|
||||
// TODO: make arguments non optional
|
||||
return all().select(SQLString(sql: sql, arguments: arguments ?? .init()), as: type)
|
||||
return all().select(SQLLiteral(sql: sql, arguments: arguments ?? .init()), as: type)
|
||||
}
|
||||
|
||||
/// Creates a request which selects *sql*, and fetches values of
|
||||
@@ -427,11 +427,11 @@ extension TableRecord {
|
||||
///
|
||||
/// try dbQueue.read { db in
|
||||
/// // SELECT max(score) FROM player
|
||||
/// let request = Player.select(SQLString("max(score)"), as: Int.self)
|
||||
/// let request = Player.select(SQLLiteral("max(score)"), as: Int.self)
|
||||
/// let maxScore: Int? = try request.fetchOne(db)
|
||||
/// }
|
||||
public static func select<RowDecoder>(_ sqlString: SQLString, as type: RowDecoder.Type) -> QueryInterfaceRequest<RowDecoder> {
|
||||
return all().select(sqlString, as: type)
|
||||
public static func select<RowDecoder>(_ sqlLiteral: SQLLiteral, as type: RowDecoder.Type) -> QueryInterfaceRequest<RowDecoder> {
|
||||
return all().select(sqlLiteral, as: type)
|
||||
}
|
||||
|
||||
/// Creates a request with the provided *predicate*.
|
||||
@@ -510,25 +510,25 @@ extension TableRecord {
|
||||
/// for individual requests with the `TableRecord.select` method.
|
||||
public static func filter(sql: String, arguments: StatementArguments? = nil) -> QueryInterfaceRequest<Self> {
|
||||
// TODO: make arguments non optional
|
||||
return filter(SQLString(sql: sql, arguments: arguments ?? .init()))
|
||||
return filter(SQLLiteral(sql: sql, arguments: arguments ?? .init()))
|
||||
}
|
||||
|
||||
/// Creates a request with the provided *predicate*.
|
||||
///
|
||||
/// // SELECT * FROM player WHERE email = 'arthur@example.com'
|
||||
/// let request = Player.filter(SQLString(sql: "email = ?", arguments: ["arthur@example.com"]))
|
||||
/// let request = Player.filter(SQLLiteral(sql: "email = ?", arguments: ["arthur@example.com"]))
|
||||
///
|
||||
/// With Swift 5, you can safely embed raw values in your SQL queries,
|
||||
/// without any risk of syntax errors or SQL injection:
|
||||
///
|
||||
/// let request = Player.filter(SQLString("email = \("arthur@example.com")))
|
||||
/// let request = Player.filter(SQLLiteral("email = \("arthur@example.com")))
|
||||
///
|
||||
/// The selection defaults to all columns. This default can be changed for
|
||||
/// all requests by the `TableRecord.databaseSelection` property, or
|
||||
/// for individual requests with the `TableRecord.select` method.
|
||||
public static func filter(_ sqlString: SQLString) -> QueryInterfaceRequest<Self> {
|
||||
public static func filter(_ sqlLiteral: SQLLiteral) -> QueryInterfaceRequest<Self> {
|
||||
// NOT TESTED
|
||||
return all().filter(sqlString)
|
||||
return all().filter(sqlLiteral)
|
||||
}
|
||||
|
||||
/// Creates a request sorted according to the
|
||||
|
||||
@@ -53,7 +53,7 @@ extension SelectionRequest {
|
||||
/// .select(sql: "email")
|
||||
public func select(sql: String, arguments: StatementArguments? = nil) -> Self {
|
||||
// TODO: make arguments non optional
|
||||
return select(SQLString(sql: sql, arguments: arguments ?? .init()))
|
||||
return select(SQLLiteral(sql: sql, arguments: arguments ?? .init()))
|
||||
}
|
||||
|
||||
/// Creates a request which selects *sql*.
|
||||
@@ -61,7 +61,7 @@ extension SelectionRequest {
|
||||
/// // SELECT id, email, score + 1000 FROM player
|
||||
/// let bonus = 1000
|
||||
/// var request = Player.all()
|
||||
/// request = request.select(SQLString(sql: """
|
||||
/// request = request.select(SQLLiteral(sql: """
|
||||
/// id, email, score + ?
|
||||
/// """, arguments: [bonus]))
|
||||
///
|
||||
@@ -71,7 +71,7 @@ extension SelectionRequest {
|
||||
/// // SELECT id, email, score + 1000 FROM player
|
||||
/// let bonus = 1000
|
||||
/// var request = Player.all()
|
||||
/// request = request.select(SQLString("""
|
||||
/// request = request.select(SQLLiteral("""
|
||||
/// id, email, score + \(bonus)
|
||||
/// """)
|
||||
///
|
||||
@@ -80,10 +80,10 @@ extension SelectionRequest {
|
||||
/// // SELECT email FROM player
|
||||
/// request
|
||||
/// .select(...)
|
||||
/// .select(SQLString(sql: "email"))
|
||||
public func select(_ sqlString: SQLString) -> Self {
|
||||
/// .select(SQLLiteral(sql: "email"))
|
||||
public func select(_ sqlLiteral: SQLLiteral) -> Self {
|
||||
// NOT TESTED
|
||||
return select(SQLSelectionLiteral(sqlString))
|
||||
return select(SQLSelectionLiteral(sqlLiteral))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,7 +124,7 @@ extension FilteredRequest {
|
||||
/// request = request.filter(sql: "email = ?", arguments: ["arthur@example.com"])
|
||||
public func filter(sql: String, arguments: StatementArguments? = nil) -> Self {
|
||||
// TODO: make arguments non optional
|
||||
return filter(SQLString(sql: sql, arguments: arguments ?? .init()))
|
||||
return filter(SQLLiteral(sql: sql, arguments: arguments ?? .init()))
|
||||
}
|
||||
|
||||
/// Creates a request with the provided *predicate* added to the
|
||||
@@ -132,7 +132,7 @@ extension FilteredRequest {
|
||||
///
|
||||
/// // SELECT * FROM player WHERE email = 'arthur@example.com'
|
||||
/// var request = Player.all()
|
||||
/// request = request.filter(SQLString(sql: """
|
||||
/// request = request.filter(SQLLiteral(sql: """
|
||||
/// email = ?
|
||||
/// """, arguments: ["arthur@example.com"])
|
||||
///
|
||||
@@ -141,12 +141,12 @@ extension FilteredRequest {
|
||||
///
|
||||
/// // SELECT * FROM player WHERE email = 'arthur@example.com'
|
||||
/// var request = Player.all()
|
||||
/// request = request.filter(SQLString("""
|
||||
/// request = request.filter(SQLLiteral("""
|
||||
/// email = \("arthur@example.com")
|
||||
/// """)
|
||||
public func filter(_ sqlString: SQLString) -> Self {
|
||||
public func filter(_ sqlLiteral: SQLLiteral) -> Self {
|
||||
// NOT TESTED
|
||||
return filter(SQLExpressionLiteral(sqlString))
|
||||
return filter(SQLExpressionLiteral(sqlLiteral))
|
||||
}
|
||||
|
||||
/// Creates a request that matches nothing.
|
||||
@@ -324,11 +324,11 @@ extension AggregatingRequest {
|
||||
/// Creates a request with a new grouping.
|
||||
public func group(sql: String, arguments: StatementArguments? = nil) -> Self {
|
||||
// TODO: make arguments non optional
|
||||
return group(SQLString(sql: sql, arguments: arguments ?? .init()))
|
||||
return group(SQLLiteral(sql: sql, arguments: arguments ?? .init()))
|
||||
}
|
||||
|
||||
/// Creates a request with a new grouping.
|
||||
public func group(_ sqlString: SQLString) -> Self {
|
||||
public func group(_ sqlLiteral: SQLLiteral) -> Self {
|
||||
// NOT TESTED
|
||||
// This "expression" is not a real expression. We support raw sql which
|
||||
// actually contains several expressions:
|
||||
@@ -338,7 +338,7 @@ extension AggregatingRequest {
|
||||
// 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(sqlString)
|
||||
var expression = SQLExpressionLiteral(sqlLiteral)
|
||||
expression.unsafeRaw = true
|
||||
return group(expression)
|
||||
}
|
||||
@@ -347,14 +347,14 @@ extension AggregatingRequest {
|
||||
/// eventual set of already applied predicates.
|
||||
public func having(sql: String, arguments: StatementArguments? = nil) -> Self {
|
||||
// TODO: make arguments non optional
|
||||
return having(SQLString(sql: sql, arguments: arguments ?? .init()))
|
||||
return having(SQLLiteral(sql: sql, arguments: arguments ?? .init()))
|
||||
}
|
||||
|
||||
/// Creates a request with the provided *sql* added to the
|
||||
/// eventual set of already applied predicates.
|
||||
public func having(_ sqlString: SQLString) -> Self {
|
||||
public func having(_ sqlLiteral: SQLLiteral) -> Self {
|
||||
// NOT TESTED
|
||||
return having(SQLExpressionLiteral(sqlString))
|
||||
return having(SQLExpressionLiteral(sqlLiteral))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -445,7 +445,7 @@ extension OrderedRequest {
|
||||
/// .order(sql: "name")
|
||||
public func order(sql: String, arguments: StatementArguments? = nil) -> Self {
|
||||
// TODO: make arguments non optional
|
||||
return order(SQLString(sql: sql, arguments: arguments ?? .init()))
|
||||
return order(SQLLiteral(sql: sql, arguments: arguments ?? .init()))
|
||||
}
|
||||
|
||||
/// Creates a request with the provided *sql* used for sorting.
|
||||
@@ -460,7 +460,7 @@ extension OrderedRequest {
|
||||
/// request
|
||||
/// .order(sql: "email")
|
||||
/// .order(sql: "name")
|
||||
public func order(_ sqlString: SQLString) -> Self {
|
||||
public func order(_ sqlLiteral: SQLLiteral) -> Self {
|
||||
// NOT TESTED
|
||||
// This "expression" is not a real expression. We support raw sql which
|
||||
// actually contains several expressions:
|
||||
@@ -470,7 +470,7 @@ extension OrderedRequest {
|
||||
// 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(sqlString)
|
||||
var expression = SQLExpressionLiteral(sqlLiteral)
|
||||
expression.unsafeRaw = true
|
||||
return order([expression])
|
||||
}
|
||||
|
||||
@@ -3,19 +3,19 @@
|
||||
extension SQLExpression {
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
///
|
||||
/// Converts an expression to an SQLString
|
||||
/// Converts an expression to an SQLLiteral
|
||||
///
|
||||
/// :nodoc:
|
||||
public var sqlString: SQLString {
|
||||
public var sqlLiteral: SQLLiteral {
|
||||
var context = SQLGenerationContext.literalGenerationContext(withArguments: true)
|
||||
let sql = expressionSQL(&context)
|
||||
return SQLString(sql: sql, arguments: context.arguments!)
|
||||
return SQLLiteral(sql: sql, arguments: context.arguments!)
|
||||
}
|
||||
|
||||
/// The expression as a quoted SQL literal (not public in order to avoid abuses)
|
||||
///
|
||||
/// "foo'bar".databaseValue.sql // "'foo''bar'""
|
||||
var sql: String {
|
||||
/// "foo'bar".databaseValue.quotedSQL() // "'foo''bar'""
|
||||
func quotedSQL() -> String {
|
||||
var context = SQLGenerationContext.literalGenerationContext(withArguments: false)
|
||||
return expressionSQL(&context)
|
||||
}
|
||||
@@ -34,7 +34,7 @@ extension SQLExpression {
|
||||
/// SQLExpressionLiteral("? + ?", arguments: [1, 2])
|
||||
/// SQLExpressionLiteral(":one + :two", arguments: ["one": 1, "two": 2])
|
||||
public struct SQLExpressionLiteral : SQLExpression {
|
||||
private let sqlString: SQLString
|
||||
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
|
||||
@@ -52,39 +52,39 @@ public struct SQLExpressionLiteral : SQLExpression {
|
||||
public init(_ sql: String, arguments: StatementArguments? = nil) {
|
||||
// TODO: make arguments non optional
|
||||
// TODO: force sql parameter name: fetchCursor(db, sql:...)
|
||||
self.init(SQLString(sql: sql, arguments: arguments ?? .init()))
|
||||
self.init(SQLLiteral(sql: sql, arguments: arguments ?? .init()))
|
||||
}
|
||||
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
///
|
||||
/// Creates an SQL literal expression.
|
||||
///
|
||||
/// SQLExpressionLiteral(SQLString(sql: "1 + 2")
|
||||
/// SQLExpressionLiteral(SQLString(sql: "? + ?", arguments: [1, 2]))
|
||||
/// SQLExpressionLiteral(SQLString(sql: ":one + :two", arguments: ["one": 1, "two": 2]))
|
||||
/// SQLExpressionLiteral(SQLLiteral(sql: "1 + 2")
|
||||
/// SQLExpressionLiteral(SQLLiteral(sql: "? + ?", arguments: [1, 2]))
|
||||
/// SQLExpressionLiteral(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(SQLString("\(1) + \(2)"))
|
||||
public init(_ sqlString: SQLString) {
|
||||
self.sqlString = sqlString
|
||||
/// SQLExpressionLiteral(SQLLiteral("\(1) + \(2)"))
|
||||
public init(_ sqlLiteral: SQLLiteral) {
|
||||
self.sqlLiteral = sqlLiteral
|
||||
}
|
||||
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
/// :nodoc:
|
||||
public func expressionSQL(_ context: inout SQLGenerationContext) -> String {
|
||||
if !sqlString.arguments.isEmpty {
|
||||
if context.appendArguments(sqlString.arguments) == false {
|
||||
if !sqlLiteral.arguments.isEmpty {
|
||||
if context.appendArguments(sqlLiteral.arguments) == false {
|
||||
// GRDB limitation: we don't know how to look for `?` in sql and
|
||||
// replace them with with literals.
|
||||
fatalError("Not implemented")
|
||||
}
|
||||
}
|
||||
if unsafeRaw {
|
||||
return sqlString.sql
|
||||
return sqlLiteral.sql
|
||||
} else {
|
||||
return "(" + sqlString.sql + ")"
|
||||
return "(" + sqlLiteral.sql + ")"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,21 +26,21 @@ public protocol SQLSelectable {
|
||||
// MARK: - SQLSelectionLiteral
|
||||
|
||||
struct SQLSelectionLiteral : SQLSelectable {
|
||||
private let sqlString: SQLString
|
||||
private let sqlLiteral: SQLLiteral
|
||||
|
||||
init(_ sqlString: SQLString) {
|
||||
self.sqlString = sqlString
|
||||
init(_ sqlLiteral: SQLLiteral) {
|
||||
self.sqlLiteral = sqlLiteral
|
||||
}
|
||||
|
||||
func resultColumnSQL(_ context: inout SQLGenerationContext) -> String {
|
||||
if !sqlString.arguments.isEmpty {
|
||||
if context.appendArguments(sqlString.arguments) == false {
|
||||
if !sqlLiteral.arguments.isEmpty {
|
||||
if context.appendArguments(sqlLiteral.arguments) == false {
|
||||
// GRDB limitation: we don't know how to look for `?` in sql and
|
||||
// replace them with with literals.
|
||||
fatalError("Not implemented")
|
||||
}
|
||||
}
|
||||
return sqlString.sql
|
||||
return sqlLiteral.sql
|
||||
}
|
||||
|
||||
func countedSQL(_ context: inout SQLGenerationContext) -> String {
|
||||
|
||||
@@ -507,7 +507,7 @@ public final class TableDefinition {
|
||||
for checkExpression in checkConstraints {
|
||||
var chunks: [String] = []
|
||||
chunks.append("CHECK")
|
||||
chunks.append("(" + checkExpression.sql + ")")
|
||||
chunks.append("(" + checkExpression.quotedSQL() + ")")
|
||||
items.append(chunks.joined(separator: " "))
|
||||
}
|
||||
|
||||
@@ -861,12 +861,12 @@ public final class ColumnDefinition {
|
||||
|
||||
for checkConstraint in checkConstraints {
|
||||
chunks.append("CHECK")
|
||||
chunks.append("(" + checkConstraint.sql + ")")
|
||||
chunks.append("(" + checkConstraint.quotedSQL() + ")")
|
||||
}
|
||||
|
||||
if let defaultExpression = defaultExpression {
|
||||
chunks.append("DEFAULT")
|
||||
chunks.append(defaultExpression.sql)
|
||||
chunks.append(defaultExpression.quotedSQL())
|
||||
}
|
||||
|
||||
if let collationName = collationName {
|
||||
@@ -943,7 +943,7 @@ private struct IndexDefinition {
|
||||
chunks.append("\(table.quotedDatabaseIdentifier)(\((columns.map { $0.quotedDatabaseIdentifier } as [String]).joined(separator: ", ")))")
|
||||
if let condition = condition {
|
||||
chunks.append("WHERE")
|
||||
chunks.append(condition.sql)
|
||||
chunks.append(condition.quotedSQL())
|
||||
}
|
||||
return chunks.joined(separator: " ")
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user