mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
Allow subqueries to refer to table aliases from parent query
This commit is contained in:
1 parent
a4d7f39ad8
commit
a26901cb80
3 files changed
+100
-28
No files matched your search
@@ -59,15 +59,11 @@ struct SQLQueryGenerator: Refinable {
|
||||
self.singleResult = singleResult
|
||||
}
|
||||
|
||||
func sql(
|
||||
_ db: Database,
|
||||
argumentsSink: StatementArgumentsSink = StatementArgumentsSink())
|
||||
throws -> String
|
||||
{
|
||||
func sql(_ parentContext: SQLGenerationContext) throws -> String {
|
||||
// Build an SQL generation context with all aliases found in the query,
|
||||
// so that we can disambiguate tables that are used several times with
|
||||
// SQL aliases.
|
||||
let context = SQLGenerationContext(db, argumentsSink: argumentsSink, aliases: relation.allAliases)
|
||||
let context = SQLGenerationContext(parent: parentContext, aliases: relation.allAliases)
|
||||
|
||||
var sql = "SELECT"
|
||||
|
||||
@@ -125,7 +121,7 @@ struct SQLQueryGenerator: Refinable {
|
||||
|
||||
var limit = self.limit
|
||||
if try singleResult && !expectsSingleResult(
|
||||
db,
|
||||
context.db,
|
||||
selection: selection,
|
||||
filters: filters,
|
||||
groupExpressions: groupExpressions)
|
||||
@@ -141,11 +137,6 @@ struct SQLQueryGenerator: Refinable {
|
||||
return sql
|
||||
}
|
||||
|
||||
// Convenience
|
||||
func sql(_ context: SQLGenerationContext) throws -> String {
|
||||
try sql(context.db, argumentsSink: context.argumentsSink)
|
||||
}
|
||||
|
||||
func prepare(_ db: Database) throws -> (SelectStatement, RowAdapter?) {
|
||||
try (makeSelectStatement(db), rowAdapter(db))
|
||||
}
|
||||
@@ -330,7 +321,7 @@ struct SQLQueryGenerator: Refinable {
|
||||
var sql = "DELETE FROM \(tableName.quotedDatabaseIdentifier) WHERE "
|
||||
sql += try primaryKey.expressionSQL(context, wrappedInParenthesis: false)
|
||||
sql += " IN ("
|
||||
sql += try map(\.relation, { $0.selectOnly([primaryKey]) }).sql(db, argumentsSink: context.argumentsSink)
|
||||
sql += try map(\.relation, { $0.selectOnly([primaryKey]) }).sql(context)
|
||||
sql += ")"
|
||||
|
||||
let statement = try db.makeUpdateStatement(sql: sql)
|
||||
@@ -448,7 +439,7 @@ struct SQLQueryGenerator: Refinable {
|
||||
sql += " WHERE "
|
||||
sql += try primaryKey.expressionSQL(context, wrappedInParenthesis: false)
|
||||
sql += " IN ("
|
||||
sql += try map(\.relation, { $0.selectOnly([primaryKey]) }).sql(db, argumentsSink: context.argumentsSink)
|
||||
sql += try map(\.relation, { $0.selectOnly([primaryKey]) }).sql(context)
|
||||
sql += ")"
|
||||
|
||||
let statement = try db.makeUpdateStatement(sql: sql)
|
||||
@@ -459,12 +450,12 @@ struct SQLQueryGenerator: Refinable {
|
||||
/// Returns a select statement
|
||||
func makeSelectStatement(_ db: Database) throws -> SelectStatement {
|
||||
// Build
|
||||
let argumentsSink = StatementArgumentsSink()
|
||||
let sql = try self.sql(db, argumentsSink: argumentsSink)
|
||||
let context = SQLGenerationContext(db)
|
||||
let sql = try self.sql(context)
|
||||
|
||||
// Compile & set arguments
|
||||
let statement = try db.makeSelectStatement(sql: sql)
|
||||
statement.arguments = argumentsSink.arguments
|
||||
statement.arguments = context.arguments
|
||||
|
||||
// Optimize databaseRegion
|
||||
statement.selectedRegion = try optimizedSelectedRegion(db, statement.selectedRegion)
|
||||
|
||||
Reference in new issue
Block a user