mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
Generic connection WIP
This commit is contained in:
1 parent
b31be05ee3
commit
3777ba0df2
189 files changed
+2145
-1993
No files matched your search
@@ -8,7 +8,7 @@ enum SQLColumnGenerator {
|
||||
/// primary key columns in the table that contains the column. If
|
||||
/// the result is nil, the primary key is the hidden rowID.
|
||||
func sql(
|
||||
_ db: Database,
|
||||
_ db: DatabaseBase<some SQLiteAPI>,
|
||||
tableName: String,
|
||||
primaryKeyColumns: () throws -> [SQLColumnDescriptor]?)
|
||||
throws -> String
|
||||
@@ -27,7 +27,7 @@ enum SQLColumnGenerator {
|
||||
}
|
||||
|
||||
private func columnSQL(
|
||||
_ db: Database,
|
||||
_ db: DatabaseBase<some SQLiteAPI>,
|
||||
column: ColumnDefinition,
|
||||
tableName: String,
|
||||
primaryKeyColumns: () throws -> [SQLColumnDescriptor]?)
|
||||
|
||||
@@ -7,14 +7,14 @@
|
||||
/// and columns.
|
||||
///
|
||||
/// - It gathers SQL arguments in order to prevent SQL injection.
|
||||
final class SQLGenerationContext {
|
||||
final class SQLGenerationContext<API: SQLiteAPI> {
|
||||
private enum Parent {
|
||||
case none(db: Database, argumentsSink: StatementArgumentsSink)
|
||||
case none(db: DatabaseBase<API>, argumentsSink: StatementArgumentsSink)
|
||||
case context(SQLGenerationContext)
|
||||
}
|
||||
|
||||
/// A database connection.
|
||||
var db: Database {
|
||||
var db: DatabaseBase<API> {
|
||||
switch parent {
|
||||
case let .none(db: db, argumentsSink: _): return db
|
||||
case let .context(context): return context.db
|
||||
@@ -48,7 +48,7 @@ final class SQLGenerationContext {
|
||||
/// - parameter aliases: An array of table aliases to disambiguate.
|
||||
/// - parameter ctes: An dictionary of available CTEs.
|
||||
init(
|
||||
_ db: Database,
|
||||
_ db: DatabaseBase<API>,
|
||||
argumentsSink: StatementArgumentsSink = StatementArgumentsSink(),
|
||||
aliases: [TableAliasBase] = [],
|
||||
ctes: OrderedDictionary<String, SQLCTE> = [:])
|
||||
|
||||
@@ -5,7 +5,7 @@ struct SQLIndexGenerator {
|
||||
let options: IndexOptions
|
||||
let condition: SQLExpression?
|
||||
|
||||
func sql(_ db: Database) throws -> String {
|
||||
func sql(_ db: DatabaseBase<some SQLiteAPI>) throws -> String {
|
||||
var sql: SQL = "CREATE"
|
||||
|
||||
if options.contains(.unique) {
|
||||
|
||||
@@ -91,14 +91,14 @@ struct SQLQueryGenerator: Refinable {
|
||||
return sql
|
||||
}
|
||||
|
||||
func makePreparedRequest(_ db: Database) throws -> PreparedRequest {
|
||||
try PreparedRequest(
|
||||
func makePreparedRequest<API>(_ db: DatabaseBase<API>) throws -> PreparedRequestBase<API> {
|
||||
try PreparedRequestBase(
|
||||
statement: makeStatement(db),
|
||||
adapter: rowAdapter(SQLGenerationContext(db, ctes: relation.ctes)))
|
||||
}
|
||||
|
||||
/// The number of fetched columns.
|
||||
func columnCount(_ db: Database) throws -> Int {
|
||||
func columnCount(_ db: DatabaseBase<some SQLiteAPI>) throws -> Int {
|
||||
try relation
|
||||
.selectionPromise
|
||||
.resolve(db)
|
||||
@@ -106,7 +106,7 @@ struct SQLQueryGenerator: Refinable {
|
||||
}
|
||||
|
||||
/// Returns a prepared statement
|
||||
func makeStatement(_ db: Database) throws -> Statement {
|
||||
func makeStatement<API>(_ db: DatabaseBase<API>) throws -> StatementBase<API> {
|
||||
// Build
|
||||
let context = SQLGenerationContext(db)
|
||||
let sql = try requestSQL(context)
|
||||
@@ -149,7 +149,7 @@ struct SQLQueryGenerator: Refinable {
|
||||
///
|
||||
/// The optimized region allows us to track individual rowids, and also
|
||||
/// discard some provably empty requests such as `Player.none()`.
|
||||
private func optimizedSelectedRegion(_ db: Database, _ selectedRegion: DatabaseRegion) throws -> DatabaseRegion {
|
||||
private func optimizedSelectedRegion(_ db: DatabaseBase<some SQLiteAPI>, _ selectedRegion: DatabaseRegion) throws -> DatabaseRegion {
|
||||
var optimizedRegion = selectedRegion
|
||||
|
||||
// Give up unless request feeds from a database table
|
||||
@@ -175,7 +175,7 @@ struct SQLQueryGenerator: Refinable {
|
||||
/// If true, executing this query yields at most one row.
|
||||
/// If false, we don't know how many rows this query returns.
|
||||
private func expectsSingleResult(
|
||||
_ db: Database,
|
||||
_ db: DatabaseBase<some SQLiteAPI>,
|
||||
selection: [SQLSelection],
|
||||
filter: SQLExpression?,
|
||||
groupExpressions: [SQLExpression])
|
||||
@@ -207,7 +207,7 @@ struct SQLQueryGenerator: Refinable {
|
||||
|
||||
/// Returns a `DELETE` statement, with `RETURNING` clause if `selection`
|
||||
/// is not empty.
|
||||
func makeDeleteStatement(_ db: Database, selection: [any SQLSelectable] = []) throws -> Statement {
|
||||
func makeDeleteStatement(_ db: DatabaseBase<some SQLiteAPI>, selection: [any SQLSelectable] = []) throws -> Statement {
|
||||
switch try grouping(db) {
|
||||
case .none:
|
||||
guard relation.joins.isEmpty else {
|
||||
@@ -252,7 +252,7 @@ struct SQLQueryGenerator: Refinable {
|
||||
|
||||
/// DELETE FROM table WHERE id IN (SELECT id FROM table ...)
|
||||
/// DELETE FROM table WHERE id IN (SELECT id FROM table ...) RETURNING ...
|
||||
private func makeTrivialDeleteStatement(_ db: Database, selection: [any SQLSelectable]) throws -> Statement {
|
||||
private func makeTrivialDeleteStatement(_ db: DatabaseBase<some SQLiteAPI>, selection: [any SQLSelectable]) throws -> Statement {
|
||||
let tableName = relation.source.tableName
|
||||
let alias = TableAlias(tableName: tableName)
|
||||
let context = SQLGenerationContext(db, aliases: [alias])
|
||||
@@ -280,8 +280,8 @@ struct SQLQueryGenerator: Refinable {
|
||||
///
|
||||
/// Returns nil if assignments is empty.
|
||||
func makeUpdateStatement(
|
||||
_ db: Database,
|
||||
conflictResolution: Database.ConflictResolution,
|
||||
_ db: DatabaseBase<some SQLiteAPI>,
|
||||
conflictResolution: DatabaseConflictResolution,
|
||||
assignments: [ColumnAssignment],
|
||||
selection: [any SQLSelectable] = [])
|
||||
throws -> Statement?
|
||||
@@ -354,8 +354,8 @@ struct SQLQueryGenerator: Refinable {
|
||||
/// UPDATE table SET ... WHERE id IN (SELECT id FROM table ...) RETURNING ...
|
||||
/// Returns nil if assignments is empty
|
||||
private func makeTrivialUpdateStatement(
|
||||
_ db: Database,
|
||||
conflictResolution: Database.ConflictResolution,
|
||||
_ db: DatabaseBase<some SQLiteAPI>,
|
||||
conflictResolution: DatabaseConflictResolution,
|
||||
assignments: [ColumnAssignment],
|
||||
selection: [any SQLSelectable])
|
||||
throws -> Statement?
|
||||
@@ -401,7 +401,7 @@ struct SQLQueryGenerator: Refinable {
|
||||
|
||||
// Support for the RETURNING clause
|
||||
private func makeStatement(
|
||||
_ db: Database,
|
||||
_ db: DatabaseBase<some SQLiteAPI>,
|
||||
sql: String,
|
||||
arguments: StatementArguments,
|
||||
returning selection: [any SQLSelectable],
|
||||
@@ -471,7 +471,7 @@ struct SQLQueryGenerator: Refinable {
|
||||
}
|
||||
|
||||
/// Informs about the query grouping
|
||||
private func grouping(_ db: Database) throws -> GroupingInfo {
|
||||
private func grouping(_ db: DatabaseBase<some SQLiteAPI>) throws -> GroupingInfo {
|
||||
// Empty group clause: no grouping
|
||||
// SELECT * FROM player
|
||||
guard let groupExpressions = try relation.groupPromise?.resolve(db), groupExpressions.isEmpty == false else {
|
||||
|
||||
@@ -9,7 +9,7 @@ struct SQLTableAlterationGenerator {
|
||||
private var name: String
|
||||
private var alterations: [TableAlterationKind] = []
|
||||
|
||||
func sql(_ db: Database) throws -> String {
|
||||
func sql(_ db: DatabaseBase<some SQLiteAPI>) throws -> String {
|
||||
var statements: [String] = []
|
||||
|
||||
for alteration in alterations {
|
||||
|
||||
@@ -17,10 +17,10 @@ struct SQLTableGenerator {
|
||||
|
||||
struct KeyConstraint {
|
||||
var columns: [String]
|
||||
var conflictResolution: Database.ConflictResolution?
|
||||
var conflictResolution: DatabaseConflictResolution?
|
||||
}
|
||||
|
||||
func sql(_ db: Database) throws -> String {
|
||||
func sql(_ db: DatabaseBase<some SQLiteAPI>) throws -> String {
|
||||
var statements: [String] = []
|
||||
|
||||
do {
|
||||
@@ -156,12 +156,12 @@ struct SQLTableGenerator {
|
||||
}
|
||||
|
||||
extension SQLTableGenerator {
|
||||
init(_ db: Database, table: TableDefinition) throws {
|
||||
init(_ db: DatabaseBase<some SQLiteAPI>, table: TableDefinition) throws {
|
||||
var indexOptions: IndexOptions = []
|
||||
if table.options.contains(.ifNotExists) { indexOptions.insert(.ifNotExists) }
|
||||
|
||||
func makeKeyConstraint(
|
||||
_ db: Database,
|
||||
_ db: DatabaseBase<some SQLiteAPI>,
|
||||
constraint: TableDefinition.KeyConstraint,
|
||||
forwardPrimaryKey: SQLPrimaryKeyDescriptor)
|
||||
throws -> SQLTableGenerator.KeyConstraint
|
||||
@@ -258,7 +258,7 @@ extension SQLTableGenerator {
|
||||
}
|
||||
|
||||
private static func forwardPrimaryKeyColumns(
|
||||
_ db: Database,
|
||||
_ db: DatabaseBase<some SQLiteAPI>,
|
||||
primaryKeyConstraint: TableDefinition.KeyConstraint,
|
||||
originTable: String)
|
||||
throws -> [SQLColumnDescriptor]?
|
||||
@@ -291,7 +291,7 @@ extension SQLTableGenerator {
|
||||
}
|
||||
|
||||
private static func makeForeignKeyGenerator(
|
||||
_ db: Database,
|
||||
_ db: DatabaseBase<some SQLiteAPI>,
|
||||
foreignKey: ForeignKeyDefinition,
|
||||
originTable: String,
|
||||
forwardPrimaryKey: SQLPrimaryKeyDescriptor?,
|
||||
@@ -464,7 +464,7 @@ struct SQLPrimaryKeyDescriptor {
|
||||
}
|
||||
|
||||
extension SQLPrimaryKeyDescriptor {
|
||||
static func find(_ db: Database, table: String) throws -> Self {
|
||||
static func find(_ db: DatabaseBase<some SQLiteAPI>, table: String) throws -> Self {
|
||||
let columnInfos = try db.primaryKey(table).columnInfos
|
||||
return SQLPrimaryKeyDescriptor(
|
||||
tableName: table,
|
||||
|
||||
Reference in new issue
Block a user