mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
SQLGenerationContext no longer needs to be mutated
This commit is contained in:
1 parent
e47a584c3e
commit
a15233eff1
19 files changed
+140
-141
No files matched your search
@@ -68,7 +68,7 @@ struct SQLQueryGenerator: Refinable {
|
||||
// Build an SQK generation context with all aliases found in the query,
|
||||
// so that we can disambiguate tables that are used several times with
|
||||
// SQL aliases.
|
||||
var context = SQLGenerationContext.queryContext(db, argumentsSink: argumentsSink, aliases: relation.allAliases)
|
||||
let context = SQLGenerationContext.queryContext(db, argumentsSink: argumentsSink, aliases: relation.allAliases)
|
||||
|
||||
var sql = "SELECT"
|
||||
|
||||
@@ -83,40 +83,40 @@ struct SQLQueryGenerator: Refinable {
|
||||
let columnCount = try selection[0].columnCount(context.db)
|
||||
GRDBPrecondition(columnCount == 1, "A single column must be selected.")
|
||||
}
|
||||
sql += try " " + selection.map { try $0.resultColumnSQL(&context) }.joined(separator: ", ")
|
||||
sql += try " " + selection.map { try $0.resultColumnSQL(context) }.joined(separator: ", ")
|
||||
|
||||
sql += " FROM "
|
||||
sql += try relation.source.sql(&context)
|
||||
sql += try relation.source.sql(context)
|
||||
|
||||
for (_, join) in relation.joins {
|
||||
sql += " "
|
||||
sql += try join.sql(&context, leftAlias: relation.sourceAlias)
|
||||
sql += try join.sql(context, leftAlias: relation.sourceAlias)
|
||||
}
|
||||
|
||||
let filters = try relation.filtersPromise.resolve(context.db)
|
||||
if filters.isEmpty == false {
|
||||
sql += " WHERE "
|
||||
sql += try filters.joined(operator: .and).expressionSQL(&context, wrappedInParenthesis: false)
|
||||
sql += try filters.joined(operator: .and).expressionSQL(context, wrappedInParenthesis: false)
|
||||
}
|
||||
|
||||
if let groupExpressions = try groupPromise?.resolve(context.db), !groupExpressions.isEmpty {
|
||||
sql += " GROUP BY "
|
||||
sql += try groupExpressions
|
||||
.map { try $0.expressionSQL(&context, wrappedInParenthesis: false) }
|
||||
.map { try $0.expressionSQL(context, wrappedInParenthesis: false) }
|
||||
.joined(separator: ", ")
|
||||
}
|
||||
|
||||
let havingExpressions = try havingExpressionsPromise.resolve(context.db)
|
||||
if havingExpressions.isEmpty == false {
|
||||
sql += " HAVING "
|
||||
sql += try havingExpressions.joined(operator: .and).expressionSQL(&context, wrappedInParenthesis: false)
|
||||
sql += try havingExpressions.joined(operator: .and).expressionSQL(context, wrappedInParenthesis: false)
|
||||
}
|
||||
|
||||
let orderings = try relation.ordering.resolve(context.db)
|
||||
if !orderings.isEmpty {
|
||||
sql += " ORDER BY "
|
||||
sql += try orderings
|
||||
.map { try $0.orderingTermSQL(&context) }
|
||||
.map { try $0.orderingTermSQL(context) }
|
||||
.joined(separator: ", ")
|
||||
}
|
||||
|
||||
@@ -175,22 +175,22 @@ struct SQLQueryGenerator: Refinable {
|
||||
return try makeTrivialDeleteStatement(db)
|
||||
}
|
||||
|
||||
var context = SQLGenerationContext.queryContext(db, aliases: relation.allAliases)
|
||||
let context = SQLGenerationContext.queryContext(db, aliases: relation.allAliases)
|
||||
|
||||
var sql = try "DELETE FROM " + relation.source.sql(&context)
|
||||
var sql = try "DELETE FROM " + relation.source.sql(context)
|
||||
|
||||
let filters = try relation.filtersPromise.resolve(db)
|
||||
if filters.isEmpty == false {
|
||||
sql += " WHERE "
|
||||
sql += try filters
|
||||
.joined(operator: .and)
|
||||
.expressionSQL(&context, wrappedInParenthesis: false)
|
||||
.expressionSQL(context, wrappedInParenthesis: false)
|
||||
}
|
||||
|
||||
if let limit = limit {
|
||||
let orderings = try relation.ordering.resolve(db)
|
||||
if !orderings.isEmpty {
|
||||
sql += try " ORDER BY " + orderings.map { try $0.orderingTermSQL(&context) }.joined(separator: ", ")
|
||||
sql += try " ORDER BY " + orderings.map { try $0.orderingTermSQL(context) }.joined(separator: ", ")
|
||||
}
|
||||
sql += " LIMIT " + limit.sql
|
||||
}
|
||||
@@ -215,11 +215,11 @@ struct SQLQueryGenerator: Refinable {
|
||||
fatalError("Can't delete without any database table")
|
||||
}
|
||||
|
||||
var context = SQLGenerationContext.sqlLiteralContext(db)
|
||||
let context = SQLGenerationContext.sqlLiteralContext(db)
|
||||
let primaryKey = try db.primaryKeyExpression(tableName)
|
||||
|
||||
var sql = "DELETE FROM \(tableName.quotedDatabaseIdentifier) WHERE "
|
||||
sql += try primaryKey.expressionSQL(&context, wrappedInParenthesis: false)
|
||||
sql += try primaryKey.expressionSQL(context, wrappedInParenthesis: false)
|
||||
sql += " IN ("
|
||||
sql += try map(\.relation, { $0.selectOnly([primaryKey]) }).sql(db, argumentsSink: context.argumentsSink)
|
||||
sql += ")"
|
||||
@@ -256,7 +256,7 @@ struct SQLQueryGenerator: Refinable {
|
||||
return nil
|
||||
}
|
||||
|
||||
var context = SQLGenerationContext.queryContext(db, aliases: relation.allAliases)
|
||||
let context = SQLGenerationContext.queryContext(db, aliases: relation.allAliases)
|
||||
|
||||
var sql = "UPDATE "
|
||||
|
||||
@@ -264,10 +264,10 @@ struct SQLQueryGenerator: Refinable {
|
||||
sql += "OR \(conflictResolution.rawValue) "
|
||||
}
|
||||
|
||||
sql += try relation.source.sql(&context)
|
||||
sql += try relation.source.sql(context)
|
||||
|
||||
let assignmentsSQL = try assignments
|
||||
.map { try $0.sql(&context) }
|
||||
.map { try $0.sql(context) }
|
||||
.joined(separator: ", ")
|
||||
sql += " SET " + assignmentsSQL
|
||||
|
||||
@@ -276,13 +276,13 @@ struct SQLQueryGenerator: Refinable {
|
||||
sql += " WHERE "
|
||||
sql += try filters
|
||||
.joined(operator: .and)
|
||||
.expressionSQL(&context, wrappedInParenthesis: false)
|
||||
.expressionSQL(context, wrappedInParenthesis: false)
|
||||
}
|
||||
|
||||
if let limit = limit {
|
||||
let orderings = try relation.ordering.resolve(db)
|
||||
if !orderings.isEmpty {
|
||||
sql += try " ORDER BY " + orderings.map { try $0.orderingTermSQL(&context) }.joined(separator: ", ")
|
||||
sql += try " ORDER BY " + orderings.map { try $0.orderingTermSQL(context) }.joined(separator: ", ")
|
||||
}
|
||||
sql += " LIMIT " + limit.sql
|
||||
}
|
||||
@@ -319,7 +319,7 @@ struct SQLQueryGenerator: Refinable {
|
||||
return nil
|
||||
}
|
||||
|
||||
var context = SQLGenerationContext.sqlLiteralContext(db)
|
||||
let context = SQLGenerationContext.sqlLiteralContext(db)
|
||||
let primaryKey = try db.primaryKeyExpression(tableName)
|
||||
|
||||
// UPDATE table...
|
||||
@@ -331,13 +331,13 @@ struct SQLQueryGenerator: Refinable {
|
||||
|
||||
// SET column = value...
|
||||
let assignmentsSQL = try assignments
|
||||
.map { try $0.sql(&context) }
|
||||
.map { try $0.sql(context) }
|
||||
.joined(separator: ", ")
|
||||
sql += " SET " + assignmentsSQL
|
||||
|
||||
// WHERE id IN (SELECT id FROM ...)
|
||||
sql += " WHERE "
|
||||
sql += try primaryKey.expressionSQL(&context, wrappedInParenthesis: false)
|
||||
sql += try primaryKey.expressionSQL(context, wrappedInParenthesis: false)
|
||||
sql += " IN ("
|
||||
sql += try map(\.relation, { $0.selectOnly([primaryKey]) }).sql(db, argumentsSink: context.argumentsSink)
|
||||
sql += ")"
|
||||
@@ -636,7 +636,7 @@ private enum SQLQualifiedSource {
|
||||
}
|
||||
}
|
||||
|
||||
func sql(_ context: inout SQLGenerationContext) throws -> String {
|
||||
func sql(_ context: SQLGenerationContext) throws -> String {
|
||||
switch self {
|
||||
case let .table(tableName, alias):
|
||||
if let aliasName = context.aliasName(for: alias) {
|
||||
@@ -683,8 +683,8 @@ private struct SQLQualifiedJoin: Refinable {
|
||||
self.relation = SQLQualifiedRelation(child.relation)
|
||||
}
|
||||
|
||||
func sql(_ context: inout SQLGenerationContext, leftAlias: TableAlias) throws -> String {
|
||||
try sql(&context, leftAlias: leftAlias, allowingInnerJoin: true)
|
||||
func sql(_ context: SQLGenerationContext, leftAlias: TableAlias) throws -> String {
|
||||
try sql(context, leftAlias: leftAlias, allowingInnerJoin: true)
|
||||
}
|
||||
|
||||
/// Removes all selections from joins
|
||||
@@ -693,7 +693,7 @@ private struct SQLQualifiedJoin: Refinable {
|
||||
}
|
||||
|
||||
private func sql(
|
||||
_ context: inout SQLGenerationContext,
|
||||
_ context: SQLGenerationContext,
|
||||
leftAlias: TableAlias,
|
||||
allowingInnerJoin allowsInnerJoin: Bool)
|
||||
throws -> String
|
||||
@@ -715,7 +715,7 @@ private struct SQLQualifiedJoin: Refinable {
|
||||
}
|
||||
|
||||
// JOIN table...
|
||||
var sql = try "\(kind.rawValue) \(relation.source.sql(&context))"
|
||||
var sql = try "\(kind.rawValue) \(relation.source.sql(context))"
|
||||
let rightAlias = relation.sourceAlias
|
||||
|
||||
// ... ON <join conditions> AND <other filters>
|
||||
@@ -725,13 +725,13 @@ private struct SQLQualifiedJoin: Refinable {
|
||||
let filterSQL = try filters
|
||||
.joined(operator: .and)
|
||||
.qualifiedExpression(with: rightAlias)
|
||||
.expressionSQL(&context, wrappedInParenthesis: false)
|
||||
.expressionSQL(context, wrappedInParenthesis: false)
|
||||
sql += " ON \(filterSQL)"
|
||||
}
|
||||
|
||||
for (_, join) in relation.joins {
|
||||
// Right becomes left as we dig further
|
||||
sql += try " \(join.sql(&context, leftAlias: rightAlias, allowingInnerJoin: allowsInnerJoin))"
|
||||
sql += try " \(join.sql(context, leftAlias: rightAlias, allowingInnerJoin: allowsInnerJoin))"
|
||||
}
|
||||
|
||||
return sql
|
||||
|
||||
Reference in new issue
Block a user