mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
Cleanup
This commit is contained in:
1 parent
a46b4edb0a
commit
a3ad726111
4 files changed
+72
-152
No files matched your search
@@ -1,9 +1,7 @@
|
||||
/// A QueryInterfaceRequest describes an SQL query.
|
||||
///
|
||||
/// See https://github.com/groue/GRDB.swift#the-query-interface
|
||||
public struct QueryInterfaceRequest<T> : TypedRequest {
|
||||
public typealias RowDecoder = T
|
||||
|
||||
public struct QueryInterfaceRequest<T> {
|
||||
let query: QueryInterfaceSelectQueryDefinition
|
||||
|
||||
init(query: QueryInterfaceSelectQueryDefinition) {
|
||||
@@ -11,18 +9,20 @@ public struct QueryInterfaceRequest<T> : TypedRequest {
|
||||
}
|
||||
}
|
||||
|
||||
extension QueryInterfaceRequest : SQLSelectQuery {
|
||||
extension QueryInterfaceRequest : TypedRequest {
|
||||
public typealias RowDecoder = T
|
||||
|
||||
/// This function is an implementation detail of the query interface.
|
||||
/// Do not use it directly.
|
||||
/// A tuple that contains a prepared statement that is ready to be
|
||||
/// executed, and an eventual row adapter.
|
||||
public func prepare(_ db: Database) throws -> (SelectStatement, RowAdapter?) {
|
||||
return try query.prepare(db)
|
||||
}
|
||||
|
||||
/// The number of rows fetched by the request.
|
||||
///
|
||||
/// See https://github.com/groue/GRDB.swift/#the-query-interface
|
||||
///
|
||||
/// # Low Level Query Interface
|
||||
///
|
||||
/// See SQLSelectQuery.selectQuerySQL(_)
|
||||
public func selectQuerySQL(_ arguments: inout StatementArguments?) -> String {
|
||||
return query.selectQuerySQL(&arguments)
|
||||
/// - parameter db: A database connection.
|
||||
public func fetchCount(_ db: Database) throws -> Int {
|
||||
return try query.fetchCount(db)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -230,18 +230,6 @@ extension QueryInterfaceRequest {
|
||||
}
|
||||
}
|
||||
|
||||
extension QueryInterfaceRequest {
|
||||
|
||||
// MARK: Counting
|
||||
|
||||
/// The number of rows fetched by the request.
|
||||
///
|
||||
/// - parameter db: A database connection.
|
||||
public func fetchCount(_ db: Database) throws -> Int {
|
||||
return try query.fetchCount(db)
|
||||
}
|
||||
}
|
||||
|
||||
extension QueryInterfaceRequest {
|
||||
|
||||
// MARK: Deleting
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
// MARK: - SQLSelectQuery
|
||||
|
||||
/// This protocol is an implementation detail of the query interface.
|
||||
/// Do not use it directly.
|
||||
///
|
||||
/// See https://github.com/groue/GRDB.swift/#the-query-interface
|
||||
///
|
||||
/// # Low Level Query Interface
|
||||
///
|
||||
/// SQLSelectQuery is the protocol for types that represent a full select query.
|
||||
public protocol SQLSelectQuery : Request {
|
||||
|
||||
/// This function is an implementation detail of the query interface.
|
||||
/// Do not use it directly.
|
||||
///
|
||||
/// See https://github.com/groue/GRDB.swift/#the-query-interface
|
||||
///
|
||||
/// # Low Level Query Interface
|
||||
///
|
||||
/// Returns the SQL string of the select query.
|
||||
///
|
||||
/// When the arguments parameter is nil, any value must be written down as
|
||||
/// a literal in the returned SQL.
|
||||
///
|
||||
/// When the arguments parameter is not nil, then values may be replaced by
|
||||
/// `?` or colon-prefixed tokens, and fed into arguments.
|
||||
func selectQuerySQL(_ arguments: inout StatementArguments?) -> String
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Request adoption
|
||||
|
||||
extension SQLSelectQuery {
|
||||
|
||||
/// A tuple that contains a prepared statement that is ready to be
|
||||
/// executed, and an eventual row adapter.
|
||||
public func prepare(_ db: Database) throws -> (SelectStatement, RowAdapter?) {
|
||||
var arguments: StatementArguments? = StatementArguments()
|
||||
let sql = self.selectQuerySQL(&arguments)
|
||||
let statement = try db.makeSelectStatement(sql)
|
||||
try statement.setArgumentsWithValidation(arguments!)
|
||||
return (statement, nil)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - QueryInterfaceSelectQueryDefinition
|
||||
|
||||
extension QueryInterfaceSelectQueryDefinition : SQLSelectQuery {
|
||||
/// This function is an implementation detail of the query interface.
|
||||
/// Do not use it directly.
|
||||
///
|
||||
/// See https://github.com/groue/GRDB.swift/#the-query-interface
|
||||
///
|
||||
/// # Low Level Query Interface
|
||||
///
|
||||
/// See SQLSelectQuery.selectQuerySQL(_:arguments:)
|
||||
func selectQuerySQL(_ arguments: inout StatementArguments?) -> String {
|
||||
return sql(&arguments)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user