mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-24 04:07:47 +00:00
Cleanup
This commit is contained in:
1 parent
a46b4edb0a
commit
a3ad726111
4 files changed
+72
-152
No files matched your search
@@ -90,48 +90,6 @@ struct QueryInterfaceSelectQueryDefinition {
|
||||
return sql
|
||||
}
|
||||
|
||||
/// Part of Request protocol
|
||||
func fetchCount(_ db: Database) throws -> Int {
|
||||
return try Int.fetchOne(db, countQuery)!
|
||||
}
|
||||
|
||||
private var countQuery: QueryInterfaceSelectQueryDefinition {
|
||||
guard groupByExpressions.isEmpty && limit == nil else {
|
||||
// SELECT ... GROUP BY ...
|
||||
// SELECT ... LIMIT ...
|
||||
return trivialCountQuery
|
||||
}
|
||||
|
||||
guard let source = source, case .table = source else {
|
||||
// SELECT ... FROM (something which is not a table)
|
||||
return trivialCountQuery
|
||||
}
|
||||
|
||||
assert(!selection.isEmpty)
|
||||
if selection.count == 1 {
|
||||
guard let count = self.selection[0].count(distinct: isDistinct) else {
|
||||
return trivialCountQuery
|
||||
}
|
||||
var countQuery = unorderedQuery
|
||||
countQuery.isDistinct = false
|
||||
countQuery.selection = [count.sqlSelectable]
|
||||
return countQuery
|
||||
} else {
|
||||
// SELECT [DISTINCT] expr1, expr2, ... FROM tableName ...
|
||||
|
||||
guard !isDistinct else {
|
||||
return trivialCountQuery
|
||||
}
|
||||
|
||||
// SELECT expr1, expr2, ... FROM tableName ...
|
||||
// ->
|
||||
// SELECT COUNT(*) FROM tableName ...
|
||||
var countQuery = unorderedQuery
|
||||
countQuery.selection = [SQLExpressionCount(SQLStar())]
|
||||
return countQuery
|
||||
}
|
||||
}
|
||||
|
||||
func makeDeleteStatement(_ db: Database) throws -> UpdateStatement {
|
||||
guard groupByExpressions.isEmpty else {
|
||||
// Programmer error
|
||||
@@ -164,20 +122,71 @@ struct QueryInterfaceSelectQueryDefinition {
|
||||
return statement
|
||||
}
|
||||
|
||||
/// Remove ordering
|
||||
var unorderedQuery: QueryInterfaceSelectQueryDefinition {
|
||||
var query = self
|
||||
query.isReversed = false
|
||||
query.orderings = []
|
||||
return query
|
||||
}
|
||||
}
|
||||
|
||||
extension QueryInterfaceSelectQueryDefinition : Request {
|
||||
func prepare(_ db: Database) throws -> (SelectStatement, RowAdapter?) {
|
||||
var arguments: StatementArguments? = StatementArguments()
|
||||
let sql = self.sql(&arguments)
|
||||
let statement = try db.makeSelectStatement(sql)
|
||||
try statement.setArgumentsWithValidation(arguments!)
|
||||
return (statement, nil)
|
||||
}
|
||||
|
||||
func fetchCount(_ db: Database) throws -> Int {
|
||||
return try Int.fetchOne(db, countQuery)!
|
||||
}
|
||||
|
||||
private var countQuery: QueryInterfaceSelectQueryDefinition {
|
||||
guard groupByExpressions.isEmpty && limit == nil else {
|
||||
// SELECT ... GROUP BY ...
|
||||
// SELECT ... LIMIT ...
|
||||
return trivialCountQuery
|
||||
}
|
||||
|
||||
guard let source = source, case .table = source else {
|
||||
// SELECT ... FROM (something which is not a table)
|
||||
return trivialCountQuery
|
||||
}
|
||||
|
||||
assert(!selection.isEmpty)
|
||||
if selection.count == 1 {
|
||||
guard let count = self.selection[0].count(distinct: isDistinct) else {
|
||||
return trivialCountQuery
|
||||
}
|
||||
var countQuery = unorderedQuery
|
||||
countQuery.isDistinct = false
|
||||
countQuery.selection = [count.sqlSelectable]
|
||||
return countQuery
|
||||
} else {
|
||||
// SELECT [DISTINCT] expr1, expr2, ... FROM tableName ...
|
||||
|
||||
guard !isDistinct else {
|
||||
return trivialCountQuery
|
||||
}
|
||||
|
||||
// SELECT expr1, expr2, ... FROM tableName ...
|
||||
// ->
|
||||
// SELECT COUNT(*) FROM tableName ...
|
||||
var countQuery = unorderedQuery
|
||||
countQuery.selection = [SQLExpressionCount(SQLStar())]
|
||||
return countQuery
|
||||
}
|
||||
}
|
||||
|
||||
// SELECT COUNT(*) FROM (self)
|
||||
private var trivialCountQuery: QueryInterfaceSelectQueryDefinition {
|
||||
return QueryInterfaceSelectQueryDefinition(
|
||||
select: [SQLExpressionCount(SQLStar())],
|
||||
from: .query(query: unorderedQuery, alias: nil))
|
||||
}
|
||||
|
||||
/// Remove ordering
|
||||
private var unorderedQuery: QueryInterfaceSelectQueryDefinition {
|
||||
var query = self
|
||||
query.isReversed = false
|
||||
query.orderings = []
|
||||
return query
|
||||
}
|
||||
}
|
||||
|
||||
indirect enum SQLSource {
|
||||
|
||||
Reference in new issue
Block a user