mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-29 04:24:21 +00:00
SQLRequestProtocol, close FetchRequest
This commit is contained in:
1 parent
a3cc5745ac
commit
17e745f52b
14 files changed
+581
-420
No files matched your search
@@ -1,15 +1,14 @@
|
||||
/// [**Experimental**](http://github.com/groue/GRDB.swift#what-are-experimental-features)
|
||||
///
|
||||
/// SQLGenerationContext support for SQL generation:
|
||||
/// SQLGenerationContext supports SQL generation:
|
||||
///
|
||||
/// - It provides a database connection so that request elements can perform
|
||||
/// database introspection in order to build their SQL representation.
|
||||
/// - It provides a database connection during SQL generation, for any purpose
|
||||
/// such as schema introspection.
|
||||
///
|
||||
/// - It provides unique table aliases in order to disambiguates table names
|
||||
/// and columns.
|
||||
///
|
||||
/// - Request elements can turn database values as SQLite statement arguments,
|
||||
/// in order to prevent SQL injection.
|
||||
/// - It gathers SQL arguments in order to prevent SQL injection.
|
||||
///
|
||||
/// :nodoc:
|
||||
public final class SQLGenerationContext {
|
||||
@@ -75,7 +74,9 @@ public final class SQLGenerationContext {
|
||||
}
|
||||
|
||||
/// Returns whether arguments could be appended.
|
||||
/// May be false for SQLGenerationContext.rawSQLContext
|
||||
///
|
||||
/// A false result means that the generation context does not support
|
||||
/// SQL arguments.
|
||||
func append(arguments: StatementArguments) -> Bool {
|
||||
argumentsSink.append(arguments: arguments)
|
||||
}
|
||||
@@ -144,7 +145,7 @@ class StatementArgumentsSink {
|
||||
|
||||
// fileprivate so that SQLGenerationContext.append(arguments:) is the only
|
||||
// available api.
|
||||
/// Returns whether arguments could be appended.
|
||||
/// Returns false for SQLGenerationContext.rawSQLContext
|
||||
fileprivate func append(arguments: StatementArguments) -> Bool {
|
||||
if arguments.isEmpty {
|
||||
return true
|
||||
|
||||
@@ -59,11 +59,11 @@ struct SQLQueryGenerator: Refinable {
|
||||
self.singleResult = singleResult
|
||||
}
|
||||
|
||||
func sql(_ parentContext: SQLGenerationContext) throws -> String {
|
||||
func requestSQL(_ context: 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(parent: parentContext, aliases: relation.allAliases)
|
||||
let context = SQLGenerationContext(parent: context, aliases: relation.allAliases)
|
||||
|
||||
var sql = "SELECT"
|
||||
|
||||
@@ -137,8 +137,25 @@ struct SQLQueryGenerator: Refinable {
|
||||
return sql
|
||||
}
|
||||
|
||||
func prepare(_ db: Database) throws -> (SelectStatement, RowAdapter?) {
|
||||
try (makeSelectStatement(db), rowAdapter(db))
|
||||
func makePreparedRequest(_ db: Database) throws -> PreparedRequest {
|
||||
try PreparedRequest(
|
||||
statement: makeSelectStatement(db),
|
||||
adapter: rowAdapter(db))
|
||||
}
|
||||
|
||||
/// Returns a select statement
|
||||
func makeSelectStatement(_ db: Database) throws -> SelectStatement {
|
||||
// Build
|
||||
let context = SQLGenerationContext(db)
|
||||
let sql = try requestSQL(context)
|
||||
|
||||
// Compile & set arguments
|
||||
let statement = try db.makeSelectStatement(sql: sql)
|
||||
statement.arguments = context.arguments
|
||||
|
||||
// Optimize databaseRegion
|
||||
statement.selectedRegion = try optimizedSelectedRegion(db, statement.selectedRegion)
|
||||
return statement
|
||||
}
|
||||
|
||||
private func optimizedSelectedRegion(_ db: Database, _ selectedRegion: DatabaseRegion) throws -> DatabaseRegion {
|
||||
@@ -321,7 +338,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(context)
|
||||
sql += try map(\.relation, { $0.selectOnly([primaryKey]) }).requestSQL(context)
|
||||
sql += ")"
|
||||
|
||||
let statement = try db.makeUpdateStatement(sql: sql)
|
||||
@@ -439,7 +456,7 @@ struct SQLQueryGenerator: Refinable {
|
||||
sql += " WHERE "
|
||||
sql += try primaryKey.expressionSQL(context, wrappedInParenthesis: false)
|
||||
sql += " IN ("
|
||||
sql += try map(\.relation, { $0.selectOnly([primaryKey]) }).sql(context)
|
||||
sql += try map(\.relation, { $0.selectOnly([primaryKey]) }).requestSQL(context)
|
||||
sql += ")"
|
||||
|
||||
let statement = try db.makeUpdateStatement(sql: sql)
|
||||
@@ -447,21 +464,6 @@ struct SQLQueryGenerator: Refinable {
|
||||
return statement
|
||||
}
|
||||
|
||||
/// Returns a select statement
|
||||
func makeSelectStatement(_ db: Database) throws -> SelectStatement {
|
||||
// Build
|
||||
let context = SQLGenerationContext(db)
|
||||
let sql = try self.sql(context)
|
||||
|
||||
// Compile & set arguments
|
||||
let statement = try db.makeSelectStatement(sql: sql)
|
||||
statement.arguments = context.arguments
|
||||
|
||||
// Optimize databaseRegion
|
||||
statement.selectedRegion = try optimizedSelectedRegion(db, statement.selectedRegion)
|
||||
return statement
|
||||
}
|
||||
|
||||
/// Informs about the query grouping
|
||||
private enum GroupingInfo {
|
||||
/// No grouping at all: SELECT ... FROM player
|
||||
@@ -757,7 +759,7 @@ private enum SQLQualifiedSource {
|
||||
return "\(tableName.quotedDatabaseIdentifier)"
|
||||
}
|
||||
case let .subquery(generator):
|
||||
let sql = try generator.sql(context)
|
||||
let sql = try generator.requestSQL(context)
|
||||
return "(\(sql))"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user