mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
SQLQueryGenerator handles the single result optimization. Make sure a single column is selected for expression/collection requests
This commit is contained in:
1 parent
4b9336f5c5
commit
473bc3a88e
2 files changed
+30
-23
No files matched your search
@@ -47,14 +47,7 @@ extension QueryInterfaceRequest: FetchRequest {
|
||||
/// - returns: A prepared statement and an eventual row adapter.
|
||||
/// :nodoc:
|
||||
public func makePreparedRequest(_ db: Database, forSingleResult singleResult: Bool) throws -> PreparedRequest {
|
||||
var query = self.query
|
||||
|
||||
// Optimize query by setting a limit of 1 when appropriate
|
||||
if singleResult && !query.expectsSingleResult {
|
||||
query.limit = SQLLimit(limit: 1, offset: query.limit?.offset)
|
||||
}
|
||||
|
||||
let (statement, adapter) = try SQLQueryGenerator(query).prepare(db)
|
||||
let (statement, adapter) = try SQLQueryGenerator(query, forSingleResult: singleResult).prepare(db)
|
||||
let associations = query.relation.prefetchedAssociations
|
||||
if associations.isEmpty {
|
||||
return PreparedRequest(statement: statement, adapter: adapter)
|
||||
@@ -409,23 +402,15 @@ extension QueryInterfaceRequest: Refinable {
|
||||
extension QueryInterfaceRequest: SQLCollection {
|
||||
/// :nodoc
|
||||
public func collectionSQL(_ context: inout SQLGenerationContext) throws -> String {
|
||||
try SQLQueryGenerator(query).sql(&context)
|
||||
try SQLQueryGenerator(query, requiresSingleColumn: true).sql(&context)
|
||||
}
|
||||
}
|
||||
|
||||
extension QueryInterfaceRequest: SQLExpression {
|
||||
/// :nodoc
|
||||
public func expressionSQL(_ context: inout SQLGenerationContext, wrappedInParenthesis: Bool) throws -> String {
|
||||
var query = self.query
|
||||
|
||||
#warning("TODO: Let SQLQueryGenerator handle forSingleResult")
|
||||
#warning("TODO: Here we want SQLQueryGenerator to fatal error if there isn't a single column in the output")
|
||||
// Optimize query by setting a limit of 1 when appropriate
|
||||
if !query.expectsSingleResult {
|
||||
query.limit = SQLLimit(limit: 1, offset: query.limit?.offset)
|
||||
}
|
||||
|
||||
return try "(" + SQLQueryGenerator(query).sql(&context) + ")"
|
||||
let generator = SQLQueryGenerator(query, forSingleResult: true, requiresSingleColumn: true)
|
||||
return try "(" + generator.sql(&context) + ")"
|
||||
}
|
||||
|
||||
/// :nodoc
|
||||
|
||||
@@ -5,8 +5,19 @@ struct SQLQueryGenerator: Refinable {
|
||||
private let groupPromise: DatabasePromise<[SQLExpression]>?
|
||||
private let havingExpressionsPromise: DatabasePromise<[SQLExpression]>
|
||||
private let limit: SQLLimit?
|
||||
private let requiresSingleColumn: Bool
|
||||
|
||||
init(_ query: SQLQuery) {
|
||||
/// Creates an SQL query generator.
|
||||
///
|
||||
/// - parameter singleResult: A hint as to whether the query should be
|
||||
/// optimized for a single result.
|
||||
/// - parameter requiresSingleColumn: If true, it is a programmer error
|
||||
/// to provide a query which selects more that one column.
|
||||
init(
|
||||
_ query: SQLQuery,
|
||||
forSingleResult singleResult: Bool = false,
|
||||
requiresSingleColumn: Bool = false)
|
||||
{
|
||||
// To generate SQL, we need a "qualified" relation, where all tables,
|
||||
// expressions, etc, are identified with table aliases.
|
||||
//
|
||||
@@ -38,9 +49,15 @@ struct SQLQueryGenerator: Refinable {
|
||||
groupPromise = query.groupPromise?.map { $0.map { $0.qualifiedExpression(with: alias) } }
|
||||
havingExpressionsPromise = query.havingExpressionsPromise.map { $0.map { $0.qualifiedExpression(with: alias) } }
|
||||
|
||||
// Preserve other flags
|
||||
// Optimize query by setting a limit of 1 when appropriate
|
||||
if singleResult && !query.expectsSingleResult {
|
||||
limit = SQLLimit(limit: 1, offset: query.limit?.offset)
|
||||
} else {
|
||||
limit = query.limit
|
||||
}
|
||||
|
||||
isDistinct = query.isDistinct
|
||||
limit = query.limit
|
||||
self.requiresSingleColumn = requiresSingleColumn
|
||||
}
|
||||
|
||||
func sql(_ context: inout SQLGenerationContext) throws -> String {
|
||||
@@ -51,7 +68,12 @@ struct SQLQueryGenerator: Refinable {
|
||||
}
|
||||
|
||||
let selection = try relation.selectionPromise.resolve(context.db)
|
||||
GRDBPrecondition(!selection.isEmpty, "Can't generate SQL with empty selection")
|
||||
GRDBPrecondition(!selection.isEmpty, "Can't generate SQL with an empty selection")
|
||||
if requiresSingleColumn {
|
||||
GRDBPrecondition(selection.count == 1, "A single column must be selected.")
|
||||
let columnCount = try selection[0].columnCount(context.db)
|
||||
try GRDBPrecondition(columnCount == 1, "A single column must be selected.")
|
||||
}
|
||||
sql += try " " + selection.map { try $0.resultColumnSQL(&context) }.joined(separator: ", ")
|
||||
|
||||
sql += " FROM "
|
||||
|
||||
Reference in new issue
Block a user