mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
SELECT * FROM cte is no longer a special case, because SQLGenerationContext knows about the available CTEs
This commit is contained in:
1 parent
7c023d5c1f
commit
c4443eead8
6 files changed
+65
-88
No files matched your search
@@ -11,26 +11,6 @@ public struct SQLSelection {
|
||||
/// All columns, qualified: `player.*`
|
||||
case qualifiedAllColumns(TableAlias)
|
||||
|
||||
// As long as the CTE is embedded here, the following request will fail
|
||||
// at runtime, in `columnCount(_:)`, because we can't access the number of
|
||||
// columns in the CTE:
|
||||
//
|
||||
// let association = Player.association(to: cte)
|
||||
// Player.including(required: association.select(AllColumns(), ...))
|
||||
//
|
||||
// The need for this should not be frequent. And the user has
|
||||
// two workarounds:
|
||||
//
|
||||
// - provide explicit columns in the CTE definition.
|
||||
// - prefer `annotated(with:)` when she wants to extend the selection.
|
||||
//
|
||||
// TODO: Make `cteRequestOrAssociation.select(AllColumns())` possible.
|
||||
/// All columns of a common table expression
|
||||
case allCTEColumns(SQLCTE)
|
||||
|
||||
/// All columns of a common table expression, qualified
|
||||
case qualifiedAllCTEColumns(SQLCTE, TableAlias)
|
||||
|
||||
/// An expression
|
||||
case expression(SQLExpression)
|
||||
|
||||
@@ -51,16 +31,6 @@ public struct SQLSelection {
|
||||
self.init(impl: .qualifiedAllColumns(alias))
|
||||
}
|
||||
|
||||
/// All columns of a common table expression
|
||||
static func allCTEColumns(_ cte: SQLCTE) -> Self {
|
||||
self.init(impl: .allCTEColumns(cte))
|
||||
}
|
||||
|
||||
/// All columns of a common table expression, qualified
|
||||
static func qualifiedAllCTEColumns(_ cte: SQLCTE, _ alias: TableAlias) -> Self {
|
||||
self.init(impl: .qualifiedAllCTEColumns(cte, alias))
|
||||
}
|
||||
|
||||
/// An expression
|
||||
static func expression(_ expression: SQLExpression) -> Self {
|
||||
self.init(impl: .expression(expression))
|
||||
@@ -83,21 +53,15 @@ extension SQLSelection {
|
||||
/// Returns the number of columns in the selection.
|
||||
///
|
||||
/// Returns nil when the number of columns is unknown.
|
||||
func columnCount(_ db: Database) throws -> Int? {
|
||||
func columnCount(_ context: SQLGenerationContext) throws -> Int? {
|
||||
switch impl {
|
||||
case .allColumns:
|
||||
// Likely a GRDB bug: we can't count the number of columns in an
|
||||
// unqualified table.
|
||||
return nil
|
||||
|
||||
|
||||
case let .qualifiedAllColumns(alias):
|
||||
return try db.columns(in: alias.tableName).count
|
||||
|
||||
case let .allCTEColumns(cte):
|
||||
return try cte.columnsCount(db)
|
||||
|
||||
case let .qualifiedAllCTEColumns(cte, _):
|
||||
return try cte.columnsCount(db)
|
||||
return try context.columnCount(in: alias.tableName)
|
||||
|
||||
case .expression,
|
||||
.aliasedExpression:
|
||||
@@ -114,12 +78,10 @@ extension SQLSelection {
|
||||
/// TODO: deprecate `count(selection)`, and get rid of this property.
|
||||
var countExpression: SQLExpression {
|
||||
switch impl {
|
||||
case .allColumns,
|
||||
.allCTEColumns:
|
||||
case .allColumns:
|
||||
return .countAll
|
||||
|
||||
case .qualifiedAllColumns,
|
||||
.qualifiedAllCTEColumns:
|
||||
case .qualifiedAllColumns:
|
||||
// COUNT(player.*) is not valid SQL
|
||||
fatalError("Uncountable selection")
|
||||
|
||||
@@ -135,8 +97,7 @@ extension SQLSelection {
|
||||
/// If the selection can be counted, return how to count it.
|
||||
func count(distinct: Bool) -> SQLCount? {
|
||||
switch impl {
|
||||
case .allColumns,
|
||||
.allCTEColumns:
|
||||
case .allColumns:
|
||||
// SELECT DISTINCT * FROM tableName ...
|
||||
if distinct {
|
||||
// Can't count
|
||||
@@ -148,8 +109,7 @@ extension SQLSelection {
|
||||
// SELECT COUNT(*) FROM tableName ...
|
||||
return .all
|
||||
|
||||
case .qualifiedAllColumns,
|
||||
.qualifiedAllCTEColumns:
|
||||
case .qualifiedAllColumns:
|
||||
return nil
|
||||
|
||||
case let .expression(expression),
|
||||
@@ -183,12 +143,10 @@ extension SQLSelection {
|
||||
/// statement arguments.
|
||||
func countedSQL(_ context: SQLGenerationContext) throws -> String {
|
||||
switch impl {
|
||||
case .allColumns,
|
||||
.allCTEColumns:
|
||||
case .allColumns:
|
||||
return "*"
|
||||
|
||||
case let .qualifiedAllColumns(alias),
|
||||
let .qualifiedAllCTEColumns(_, alias):
|
||||
case let .qualifiedAllColumns(alias):
|
||||
if context.qualifier(for: alias) != nil {
|
||||
// SELECT COUNT(t.*) is invalid SQL
|
||||
fatalError("Not implemented, or invalid query")
|
||||
@@ -223,12 +181,10 @@ extension SQLSelection {
|
||||
/// statement arguments.
|
||||
func sql(_ context: SQLGenerationContext) throws -> String {
|
||||
switch impl {
|
||||
case .allColumns,
|
||||
.allCTEColumns:
|
||||
case .allColumns:
|
||||
return "*"
|
||||
|
||||
case let .qualifiedAllColumns(alias),
|
||||
let .qualifiedAllCTEColumns(_, alias):
|
||||
case let .qualifiedAllColumns(alias):
|
||||
if let qualifier = context.qualifier(for: alias) {
|
||||
return qualifier.quotedDatabaseIdentifier + ".*"
|
||||
}
|
||||
@@ -277,16 +233,12 @@ extension SQLSelection {
|
||||
/// Returns a qualified selection
|
||||
func qualified(with alias: TableAlias) -> SQLSelection {
|
||||
switch impl {
|
||||
case .qualifiedAllColumns,
|
||||
.qualifiedAllCTEColumns:
|
||||
case .qualifiedAllColumns:
|
||||
return self
|
||||
|
||||
case .allColumns:
|
||||
return .qualifiedAllColumns(alias)
|
||||
|
||||
case let .allCTEColumns(cte):
|
||||
return .qualifiedAllCTEColumns(cte, alias)
|
||||
|
||||
case let .expression(expression):
|
||||
return .expression(expression.qualified(with: alias))
|
||||
|
||||
@@ -305,9 +257,9 @@ extension Array where Element == SQLSelection {
|
||||
/// This method raises a fatal error if the selection contains a literal,
|
||||
///
|
||||
/// See `SQLSelection.columnCount(_:)` for testability.
|
||||
func columnCount(_ db: Database) throws -> Int {
|
||||
func columnCount(_ context: SQLGenerationContext) throws -> Int {
|
||||
try reduce(0) { acc, selection in
|
||||
guard let count = try selection.columnCount(db) else {
|
||||
guard let count = try selection.columnCount(context) else {
|
||||
// Found an SQL literal:
|
||||
// - Player.select(sql: "id, name, score")
|
||||
// - Player.select(literal: "id, name, score")
|
||||
|
||||
@@ -38,7 +38,7 @@ extension SQLSubquery {
|
||||
/// // We know that "SELECT 1 AS a, 2 AS b" selects two columns,
|
||||
/// // so we can find cte columns in the row:
|
||||
/// row.scopes["cte"] // [a:1, b:2]
|
||||
func columnsCount(_ db: Database) throws -> Int {
|
||||
func columnCount(_ db: Database) throws -> Int {
|
||||
switch impl {
|
||||
case let .literal(sqlLiteral):
|
||||
// Compile request. We can freely use the statement cache because we
|
||||
@@ -49,7 +49,7 @@ extension SQLSubquery {
|
||||
return statement.columnCount
|
||||
|
||||
case let .relation(relation):
|
||||
return try SQLQueryGenerator(relation: relation).columnsCount(db)
|
||||
return try SQLQueryGenerator(relation: relation).columnCount(db)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user