mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +00:00
Refactor statement cache, allow SQLRequest to use cached statements
This commit is contained in:
1 parent
cb4c07a5fb
commit
4938568ea7
5 files changed
+136
-16
No files matched your search
+28
-10
@@ -315,8 +315,6 @@ public final class Database {
|
||||
/// Schema Cache
|
||||
var schemaCache: DatabaseSchemaCache // internal so that it can be tested
|
||||
|
||||
/// Statement caches.
|
||||
///
|
||||
/// Statement caches are not part of the schema cache because statements
|
||||
/// belong to this connection, while schema cache can be shared with
|
||||
/// other connections.
|
||||
@@ -325,7 +323,11 @@ public final class Database {
|
||||
/// user, and one for the statements generated by GRDB. Those are separated
|
||||
/// so that GRDB has no opportunity to inadvertently modify the arguments of
|
||||
/// user statements.
|
||||
lazy var statementCache: StatementCache = StatementCache(database: self)
|
||||
enum StatementCacheName {
|
||||
case grdb
|
||||
case user
|
||||
}
|
||||
fileprivate lazy var grdbStatementCache: StatementCache = StatementCache(database: self)
|
||||
fileprivate lazy var userStatementCache: StatementCache = StatementCache(database: self)
|
||||
|
||||
init(path: String, configuration: Configuration, schemaCache: DatabaseSchemaCache) throws {
|
||||
@@ -425,7 +427,7 @@ public final class Database {
|
||||
assert(!isClosed)
|
||||
|
||||
configuration.SQLiteConnectionWillClose?(sqliteConnection)
|
||||
statementCache.clear()
|
||||
grdbStatementCache.clear()
|
||||
userStatementCache.clear()
|
||||
Database.close(connection: sqliteConnection)
|
||||
isClosed = true
|
||||
@@ -440,7 +442,7 @@ public final class Database {
|
||||
func releaseMemory() {
|
||||
sqlite3_db_release_memory(sqliteConnection)
|
||||
schemaCache.clear()
|
||||
statementCache.clear()
|
||||
grdbStatementCache.clear()
|
||||
userStatementCache.clear()
|
||||
}
|
||||
|
||||
@@ -634,7 +636,15 @@ extension Database {
|
||||
/// - returns: An UpdateStatement.
|
||||
/// - throws: A DatabaseError whenever SQLite could not parse the sql query.
|
||||
public func cachedSelectStatement(_ sql: String) throws -> SelectStatement {
|
||||
return try userStatementCache.selectStatement(sql)
|
||||
return try selectStatement(sql, fromCache: .user)
|
||||
}
|
||||
|
||||
/// Returns a prepared statement that can be reused.
|
||||
func selectStatement(_ sql: String, fromCache cacheName: StatementCacheName) throws -> SelectStatement {
|
||||
switch cacheName {
|
||||
case .grdb: return try grdbStatementCache.selectStatement(sql)
|
||||
case .user: return try userStatementCache.selectStatement(sql)
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a new prepared statement that can be reused.
|
||||
@@ -663,7 +673,15 @@ extension Database {
|
||||
/// - returns: An UpdateStatement.
|
||||
/// - throws: A DatabaseError whenever SQLite could not parse the sql query.
|
||||
public func cachedUpdateStatement(_ sql: String) throws -> UpdateStatement {
|
||||
return try userStatementCache.updateStatement(sql)
|
||||
return try updateStatement(sql, fromCache: .grdb)
|
||||
}
|
||||
|
||||
/// Returns a prepared statement that can be reused.
|
||||
func updateStatement(_ sql: String, fromCache cacheName: StatementCacheName) throws -> UpdateStatement {
|
||||
switch cacheName {
|
||||
case .grdb: return try grdbStatementCache.updateStatement(sql)
|
||||
case .user: return try userStatementCache.updateStatement(sql)
|
||||
}
|
||||
}
|
||||
|
||||
/// Executes one or several SQL statements, separated by semi-colons.
|
||||
@@ -1045,7 +1063,7 @@ extension Database {
|
||||
// the automatic statement recompilation (see https://www.sqlite.org/c3ref/prepare.html)
|
||||
// because the automatic statement recompilation only happens a
|
||||
// limited number of times.
|
||||
statementCache.clear()
|
||||
grdbStatementCache.clear()
|
||||
userStatementCache.clear()
|
||||
}
|
||||
|
||||
@@ -1778,7 +1796,7 @@ extension Database {
|
||||
// https://www.sqlite.org/c3ref/reset.html
|
||||
//
|
||||
// So make sure we clear this statement from the cache.
|
||||
statementCache.remove(statement)
|
||||
grdbStatementCache.remove(statement)
|
||||
userStatementCache.remove(statement)
|
||||
}
|
||||
|
||||
@@ -1797,7 +1815,7 @@ extension Database {
|
||||
// https://www.sqlite.org/c3ref/reset.html
|
||||
//
|
||||
// So make sure we clear this statement from the cache.
|
||||
statementCache.remove(statement)
|
||||
grdbStatementCache.remove(statement)
|
||||
userStatementCache.remove(statement)
|
||||
|
||||
switch transactionHookState {
|
||||
|
||||
+33
-2
@@ -101,16 +101,46 @@ public struct SQLRequest : Request {
|
||||
///
|
||||
/// let request = SQLRequest("SELECT * FROM persons")
|
||||
/// let request = SQLRequest("SELECT * FROM persons WHERE id = ?", arguments: [1])
|
||||
public init(_ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) {
|
||||
///
|
||||
/// - parameters:
|
||||
/// - sql: An SQL query.
|
||||
/// - arguments: Optional statement arguments.
|
||||
/// - adapter: Optional RowAdapter.
|
||||
/// - cached: Defaults to false. If true, the request reuses a cached
|
||||
/// prepared statement.
|
||||
/// - returns: A SQLRequest
|
||||
public init(_ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil, cached: Bool = false) {
|
||||
self.init(sql, arguments: arguments, adapter: adapter, fromCache: cached ? .user : nil)
|
||||
}
|
||||
|
||||
/// Creates a new request from an SQL string, optional arguments, and
|
||||
/// optional row adapter.
|
||||
///
|
||||
/// let request = SQLRequest("SELECT * FROM persons")
|
||||
/// let request = SQLRequest("SELECT * FROM persons WHERE id = ?", arguments: [1])
|
||||
///
|
||||
/// - parameters:
|
||||
/// - sql: An SQL query.
|
||||
/// - arguments: Optional statement arguments.
|
||||
/// - adapter: Optional RowAdapter.
|
||||
/// - statementCacheName: Optional statement cache name.
|
||||
/// - returns: A SQLRequest
|
||||
init(_ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil, fromCache statementCacheName: Database.StatementCacheName?) {
|
||||
self.sql = sql
|
||||
self.arguments = arguments
|
||||
self.adapter = adapter
|
||||
self.statementCacheName = statementCacheName
|
||||
}
|
||||
|
||||
/// 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?) {
|
||||
let statement = try db.makeSelectStatement(sql)
|
||||
let statement: SelectStatement
|
||||
if let statementCacheName = statementCacheName {
|
||||
statement = try db.selectStatement(sql, fromCache: statementCacheName)
|
||||
} else {
|
||||
statement = try db.makeSelectStatement(sql)
|
||||
}
|
||||
if let arguments = arguments {
|
||||
try statement.setArgumentsWithValidation(arguments)
|
||||
}
|
||||
@@ -120,6 +150,7 @@ public struct SQLRequest : Request {
|
||||
private let sql: String
|
||||
private let arguments: StatementArguments?
|
||||
private let adapter: RowAdapter?
|
||||
private let statementCacheName: Database.StatementCacheName?
|
||||
}
|
||||
|
||||
/// The protocol for all types that define a way to fetch values from
|
||||
|
||||
@@ -615,7 +615,7 @@ final class DAO {
|
||||
onConflict: onConflict,
|
||||
tableName: databaseTableName,
|
||||
insertedColumns: Array(persistentDictionary.keys))
|
||||
let statement = try db.statementCache.updateStatement(query.sql)
|
||||
let statement = try db.updateStatement(query.sql, fromCache: .grdb)
|
||||
statement.unsafeSetArguments(StatementArguments(persistentDictionary.values))
|
||||
return statement
|
||||
}
|
||||
@@ -657,7 +657,7 @@ final class DAO {
|
||||
tableName: databaseTableName,
|
||||
updatedColumns: updatedColumns,
|
||||
conditionColumns: primaryKeyColumns)
|
||||
let statement = try db.statementCache.updateStatement(query.sql)
|
||||
let statement = try db.updateStatement(query.sql, fromCache: .grdb)
|
||||
statement.unsafeSetArguments(StatementArguments(updatedValues + primaryKeyValues))
|
||||
return statement
|
||||
}
|
||||
@@ -672,7 +672,7 @@ final class DAO {
|
||||
let query = DeleteQuery(
|
||||
tableName: databaseTableName,
|
||||
conditionColumns: primaryKeyColumns)
|
||||
let statement = try db.statementCache.updateStatement(query.sql)
|
||||
let statement = try db.updateStatement(query.sql, fromCache: .grdb)
|
||||
statement.unsafeSetArguments(StatementArguments(primaryKeyValues))
|
||||
return statement
|
||||
}
|
||||
@@ -687,7 +687,7 @@ final class DAO {
|
||||
let query = ExistsQuery(
|
||||
tableName: databaseTableName,
|
||||
conditionColumns: primaryKeyColumns)
|
||||
let statement = try db.statementCache.selectStatement(query.sql)
|
||||
let statement = try db.selectStatement(query.sql, fromCache: .grdb)
|
||||
statement.unsafeSetArguments(StatementArguments(primaryKeyValues))
|
||||
return statement
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user