mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
Merge cte support into AllColumns
This commit is contained in:
1 parent
a1f0f2c9b7
commit
91dd13e2be
4 files changed
+75
-66
No files matched your search
@@ -115,7 +115,7 @@ extension CommonTableExpression {
|
||||
var relationForAll: SQLRelation {
|
||||
SQLRelation(
|
||||
source: .table(tableName: tableName, alias: nil),
|
||||
selectionPromise: DatabasePromise(value: [_AllCTEColumns(cte: cte, alias: nil)]))
|
||||
selectionPromise: DatabasePromise(value: [AllColumns(cte: cte)]))
|
||||
}
|
||||
|
||||
/// Creates a request for all rows of the common table expression.
|
||||
@@ -150,6 +150,43 @@ extension CommonTableExpression {
|
||||
}
|
||||
}
|
||||
|
||||
/// A low-level common table expression
|
||||
struct SQLCTE {
|
||||
/// The columns of the common table expression.
|
||||
///
|
||||
/// When nil, the CTE selects the columns of the request:
|
||||
///
|
||||
/// -- Columns a, b
|
||||
/// WITH t AS (SELECT 1 AS a, 2 AS b) ...
|
||||
///
|
||||
/// When not nil, `columns` provides the columns of the CTE:
|
||||
///
|
||||
/// -- Column id
|
||||
/// WITH t(id) AS (SELECT 1) ...
|
||||
var columns: [String]?
|
||||
|
||||
/// The common table expression request.
|
||||
///
|
||||
/// WITH t AS (SELECT ...)
|
||||
/// ~~~~~~~~~~
|
||||
var request: _FetchRequest
|
||||
|
||||
/// The number of columns in the common table expression.
|
||||
func columnsCount(_ db: Database) throws -> Int {
|
||||
if let columns = columns {
|
||||
// No need to hit the database
|
||||
return columns.count
|
||||
}
|
||||
|
||||
// Compile request. We can freely use the statement cache because we
|
||||
// do not execute the statement or modify its arguments.
|
||||
let context = SQLGenerationContext(db)
|
||||
let sql = try request.requestSQL(context, forSingleResult: false)
|
||||
let statement = try db.cachedSelectStatement(sql: sql)
|
||||
return statement.columnCount
|
||||
}
|
||||
}
|
||||
|
||||
extension TableRecord {
|
||||
/// Creates an association to a common table expression that you can join
|
||||
/// or include in another request.
|
||||
@@ -343,55 +380,3 @@ extension TableRecord {
|
||||
all().with(cte)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - _AllCTEColumns
|
||||
|
||||
#warning("TODO: merge with AllColumns")
|
||||
/// :nodoc:
|
||||
public struct _AllCTEColumns {
|
||||
var cte: SQLCTE
|
||||
var alias: TableAlias?
|
||||
}
|
||||
|
||||
extension _AllCTEColumns: SQLSelectable, Refinable {
|
||||
/// :nodoc:
|
||||
public func _count(distinct: Bool) -> _SQLCount? {
|
||||
if let alias = alias {
|
||||
return _SQLQualifiedAllColumns(alias: alias)._count(distinct: distinct)
|
||||
} else {
|
||||
return AllColumns()._count(distinct: distinct)
|
||||
}
|
||||
}
|
||||
|
||||
/// :nodoc:
|
||||
public func _qualifiedSelectable(with alias: TableAlias) -> SQLSelectable {
|
||||
// Never requalify
|
||||
if self.alias != nil {
|
||||
return self
|
||||
}
|
||||
return with(\.alias, alias)
|
||||
}
|
||||
|
||||
/// :nodoc:
|
||||
public func _columnCount(_ db: Database) throws -> Int {
|
||||
if let columns = cte.columns {
|
||||
return columns.count
|
||||
}
|
||||
|
||||
// Compile request. We can freely use the statement cache because we
|
||||
// do not execute the statement or modify its arguments.
|
||||
let context = SQLGenerationContext(db)
|
||||
let sql = try cte.request.requestSQL(context, forSingleResult: false)
|
||||
let statement = try db.cachedSelectStatement(sql: sql)
|
||||
return statement.columnCount
|
||||
}
|
||||
|
||||
/// :nodoc:
|
||||
public func _accept<Visitor: _SQLSelectableVisitor>(_ visitor: inout Visitor) throws {
|
||||
if let alias = alias {
|
||||
return try _SQLQualifiedAllColumns(alias: alias)._accept(&visitor)
|
||||
} else {
|
||||
return try AllColumns()._accept(&visitor)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ struct SQLQuery {
|
||||
// `(a AND b AND c)` instead of `((a AND b) AND c)`.
|
||||
var havingExpressionsPromise: DatabasePromise<[SQLExpression]> = DatabasePromise(value: [])
|
||||
var limit: SQLLimit?
|
||||
var ctes = SQLCTEs()
|
||||
var ctes = SQLQueryCTEs()
|
||||
}
|
||||
|
||||
extension SQLQuery: Refinable {
|
||||
@@ -155,16 +155,11 @@ extension SQLQuery {
|
||||
}
|
||||
}
|
||||
|
||||
struct SQLCTE {
|
||||
var columns: [String]?
|
||||
var request: _FetchRequest
|
||||
}
|
||||
|
||||
/// A collection of common table expressions
|
||||
struct SQLCTEs: Refinable {
|
||||
/// The collection of common table expressions in an `SQLQuery`.
|
||||
struct SQLQueryCTEs: Refinable {
|
||||
var isRecursive: Bool = false
|
||||
|
||||
/// A (table name -> CTE) dictionary
|
||||
/// A [table name: CTE] dictionary
|
||||
var ctes: OrderedDictionary<String, SQLCTE> = [:]
|
||||
|
||||
var isEmpty: Bool { ctes.isEmpty }
|
||||
|
||||
@@ -15,7 +15,24 @@
|
||||
/// // SELECT *, rowid FROM player
|
||||
/// let request = Player.all()
|
||||
public struct AllColumns {
|
||||
/// When nil, select all columns from a regular database table.
|
||||
/// When not nil, select all columns from a common table expression.
|
||||
var cte: SQLCTE?
|
||||
|
||||
/// The `*` selection.
|
||||
///
|
||||
/// For example:
|
||||
///
|
||||
/// // SELECT * FROM player
|
||||
/// Player.select(AllColumns())
|
||||
public init() { }
|
||||
|
||||
/// The `*` selection for a common table expression.
|
||||
///
|
||||
/// WITH t AS (...) SELECT * FROM t
|
||||
init(cte: SQLCTE) {
|
||||
self.cte = cte
|
||||
}
|
||||
}
|
||||
|
||||
extension AllColumns: SQLSelectable {
|
||||
@@ -35,11 +52,15 @@ extension AllColumns: SQLSelectable {
|
||||
|
||||
/// :nodoc:
|
||||
public func _qualifiedSelectable(with alias: TableAlias) -> SQLSelectable {
|
||||
_SQLQualifiedAllColumns(alias: alias)
|
||||
_SQLQualifiedAllColumns(alias: alias, cte: cte)
|
||||
}
|
||||
|
||||
/// :nodoc:
|
||||
public func _columnCount(_ db: Database) throws -> Int {
|
||||
if let cte = cte {
|
||||
return try cte.columnsCount(db)
|
||||
}
|
||||
|
||||
fatalError("Can't compute number of columns without an alias")
|
||||
}
|
||||
|
||||
@@ -55,10 +76,14 @@ extension AllColumns: SQLSelectable {
|
||||
///
|
||||
/// :nodoc:
|
||||
public struct _SQLQualifiedAllColumns {
|
||||
/// When nil, select all columns from a regular database table.
|
||||
/// When not nil, select all columns from a common table expression.
|
||||
var cte: SQLCTE?
|
||||
let alias: TableAlias
|
||||
|
||||
init(alias: TableAlias) {
|
||||
init(alias: TableAlias, cte: SQLCTE?) {
|
||||
self.alias = alias
|
||||
self.cte = cte
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,7 +99,11 @@ extension _SQLQualifiedAllColumns: SQLSelectable {
|
||||
|
||||
/// :nodoc:
|
||||
public func _columnCount(_ db: Database) throws -> Int {
|
||||
try db.columns(in: alias.tableName).count
|
||||
if let cte = cte {
|
||||
return try cte.columnsCount(db)
|
||||
} else {
|
||||
return try db.columns(in: alias.tableName).count
|
||||
}
|
||||
}
|
||||
|
||||
/// :nodoc:
|
||||
|
||||
@@ -6,7 +6,7 @@ struct SQLQueryGenerator: Refinable {
|
||||
private let havingExpressionsPromise: DatabasePromise<[SQLExpression]>
|
||||
private let limit: SQLLimit?
|
||||
private let singleResult: Bool
|
||||
private let ctes: SQLCTEs
|
||||
private let ctes: SQLQueryCTEs
|
||||
// For database region
|
||||
private let prefetchedAssociations: [_SQLAssociation]
|
||||
|
||||
|
||||
Reference in new issue
Block a user