mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-23 04:04:22 +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
@@ -655,6 +655,14 @@
|
||||
567E55EE1D2BDD3F00CC6F79 /* EncryptionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567156701CB18050007DC145 /* EncryptionTests.swift */; };
|
||||
567E55F31D2BDDFE00CC6F79 /* EncryptionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567156701CB18050007DC145 /* EncryptionTests.swift */; };
|
||||
567E55F41D2BDDFF00CC6F79 /* EncryptionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567156701CB18050007DC145 /* EncryptionTests.swift */; };
|
||||
568068311EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568068301EBBA26100EFB8AA /* SQLRequestTests.swift */; };
|
||||
568068321EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568068301EBBA26100EFB8AA /* SQLRequestTests.swift */; };
|
||||
568068331EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568068301EBBA26100EFB8AA /* SQLRequestTests.swift */; };
|
||||
568068341EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568068301EBBA26100EFB8AA /* SQLRequestTests.swift */; };
|
||||
568068351EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568068301EBBA26100EFB8AA /* SQLRequestTests.swift */; };
|
||||
568068361EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568068301EBBA26100EFB8AA /* SQLRequestTests.swift */; };
|
||||
568068371EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568068301EBBA26100EFB8AA /* SQLRequestTests.swift */; };
|
||||
568068381EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 568068301EBBA26100EFB8AA /* SQLRequestTests.swift */; };
|
||||
5683C2691B4D445E00296494 /* libsqlite3.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 5683C2681B4D445E00296494 /* libsqlite3.dylib */; };
|
||||
568735A01CEDE16C009B9116 /* Betty.jpeg in Resources */ = {isa = PBXBuildFile; fileRef = 5687359E1CEDE16C009B9116 /* Betty.jpeg */; };
|
||||
568735A11CEDE16C009B9116 /* Betty.jpeg in Resources */ = {isa = PBXBuildFile; fileRef = 5687359E1CEDE16C009B9116 /* Betty.jpeg */; };
|
||||
@@ -1945,6 +1953,7 @@
|
||||
567DAF131EAB61ED00FC0928 /* grdb_config.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = grdb_config.c; sourceTree = "<group>"; };
|
||||
567DAF141EAB61ED00FC0928 /* grdb_config.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = grdb_config.h; sourceTree = "<group>"; };
|
||||
567DAF341EAB789800FC0928 /* DatabaseLogErrorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseLogErrorTests.swift; sourceTree = "<group>"; };
|
||||
568068301EBBA26100EFB8AA /* SQLRequestTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLRequestTests.swift; sourceTree = "<group>"; };
|
||||
5683C2681B4D445E00296494 /* libsqlite3.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libsqlite3.dylib; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.4.sdk/usr/lib/libsqlite3.dylib; sourceTree = DEVELOPER_DIR; };
|
||||
5687359E1CEDE16C009B9116 /* Betty.jpeg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = Betty.jpeg; sourceTree = "<group>"; };
|
||||
568BE5191CB035A900270F93 /* sqlite3.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = sqlite3.h; path = SQLCipher/src/sqlite3.h; sourceTree = "<group>"; };
|
||||
@@ -2649,6 +2658,7 @@
|
||||
567A80521D41350C00C7DCEC /* IndexInfoTests.swift */,
|
||||
565EFAED1D0436CE00A8FA9D /* NumericOverflowTests.swift */,
|
||||
56741EA71E66A8B3003E422D /* RequestTests.swift */,
|
||||
568068301EBBA26100EFB8AA /* SQLRequestTests.swift */,
|
||||
56F3E7481E66F83A00BF0F01 /* ResultCodeTests.swift */,
|
||||
56A2381D1B9C74A90082EB20 /* Row */,
|
||||
569C1EB11CF07DDD0042627B /* SchedulingWatchdogTests.swift */,
|
||||
@@ -3995,6 +4005,7 @@
|
||||
562205F51E420E48005860AC /* DatabasePoolSchemaCacheTests.swift in Sources */,
|
||||
562393581DEE013C00A6B01F /* FilterCursorTests.swift in Sources */,
|
||||
560FC59E1CB00B880014AA8E /* FoundationNSDateTests.swift in Sources */,
|
||||
568068321EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */,
|
||||
560FC59F1CB00B880014AA8E /* DatabaseTests.swift in Sources */,
|
||||
560FC5A01CB00B880014AA8E /* QueryInterfaceRequestTests.swift in Sources */,
|
||||
5698ACCF1DA8C2620056AF8C /* RecordPrimaryKeyHiddenRowIDTests.swift in Sources */,
|
||||
@@ -4230,6 +4241,7 @@
|
||||
562205F81E420E49005860AC /* DatabasePoolSchemaCacheTests.swift in Sources */,
|
||||
562393591DEE013C00A6B01F /* FilterCursorTests.swift in Sources */,
|
||||
5671565B1CB16729007DC145 /* TableMapping+QueryInterfaceRequestTests.swift in Sources */,
|
||||
568068331EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */,
|
||||
5671565C1CB16729007DC145 /* FoundationNSDateTests.swift in Sources */,
|
||||
5671565D1CB16729007DC145 /* DatabaseTests.swift in Sources */,
|
||||
5671565E1CB16729007DC145 /* QueryInterfaceRequestTests.swift in Sources */,
|
||||
@@ -4445,6 +4457,7 @@
|
||||
56AFCA681CB1AA9900F48B96 /* RecordMinimalPrimaryKeySingleTests.swift in Sources */,
|
||||
56B14E841D4DAE54000BF4A3 /* RowFromDictionaryLiteralTests.swift in Sources */,
|
||||
56AFCA6B1CB1AA9900F48B96 /* DatabaseTimestampTests.swift in Sources */,
|
||||
568068361EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */,
|
||||
56AFCA6C1CB1AA9900F48B96 /* StatementArguments+FoundationTests.swift in Sources */,
|
||||
562393651DEE06D300A6B01F /* CursorTests.swift in Sources */,
|
||||
5657AB5B1D108BA9006283EF /* FoundationNSStringTests.swift in Sources */,
|
||||
@@ -4574,6 +4587,7 @@
|
||||
56AFCAC11CB1ABC800F48B96 /* RecordMinimalPrimaryKeySingleTests.swift in Sources */,
|
||||
56B14E851D4DAE54000BF4A3 /* RowFromDictionaryLiteralTests.swift in Sources */,
|
||||
56AFCAC41CB1ABC800F48B96 /* DatabaseTimestampTests.swift in Sources */,
|
||||
568068371EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */,
|
||||
56AFCAC51CB1ABC800F48B96 /* StatementArguments+FoundationTests.swift in Sources */,
|
||||
562393661DEE06D300A6B01F /* CursorTests.swift in Sources */,
|
||||
56AFCAC61CB1ABC800F48B96 /* TableMapping+QueryInterfaceRequestTests.swift in Sources */,
|
||||
@@ -4788,6 +4802,7 @@
|
||||
56A238521B9C74A90082EB20 /* RecordMinimalPrimaryKeySingleTests.swift in Sources */,
|
||||
5698AC8D1DA389380056AF8C /* FTS3TableBuilderTests.swift in Sources */,
|
||||
56A238B71B9CA2590082EB20 /* DatabaseTimestampTests.swift in Sources */,
|
||||
568068351EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */,
|
||||
565D5D751BBC70AE00DC9BD4 /* StatementArguments+FoundationTests.swift in Sources */,
|
||||
5623931C1DECC02000A6B01F /* RowFetchTests.swift in Sources */,
|
||||
562393641DEE06D300A6B01F /* CursorTests.swift in Sources */,
|
||||
@@ -4917,6 +4932,7 @@
|
||||
56D496681D813086008276D7 /* RowConvertible+QueryInterfaceRequestTests.swift in Sources */,
|
||||
562393451DEDFE4500A6B01F /* IteratorCursorTests.swift in Sources */,
|
||||
5623934E1DEDFEFB00A6B01F /* EnumeratedCursorTests.swift in Sources */,
|
||||
568068311EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */,
|
||||
56D496B41D8133F8008276D7 /* DatabaseTests.swift in Sources */,
|
||||
56D496951D81317B008276D7 /* MutablePersistableTests.swift in Sources */,
|
||||
5698AC801DA380A20056AF8C /* VirtualTableModuleTests.swift in Sources */,
|
||||
@@ -5218,6 +5234,7 @@
|
||||
F3BA81171CFB305E003DC1BA /* QueryInterfaceExpressionsTests.swift in Sources */,
|
||||
F3BA810B1CFB3056003DC1BA /* Row+FoundationTests.swift in Sources */,
|
||||
5698AC901DA389380056AF8C /* FTS3TableBuilderTests.swift in Sources */,
|
||||
568068381EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */,
|
||||
F3BA80DB1CFB300E003DC1BA /* DatabaseValueConversionTests.swift in Sources */,
|
||||
56741EAF1E66A8B3003E422D /* RequestTests.swift in Sources */,
|
||||
5657AB5D1D108BA9006283EF /* FoundationNSStringTests.swift in Sources */,
|
||||
@@ -5433,6 +5450,7 @@
|
||||
F3BA81131CFB305B003DC1BA /* DatabaseMigratorTests.swift in Sources */,
|
||||
F3BA81381CFB3064003DC1BA /* RecordSubClassTests.swift in Sources */,
|
||||
F3BA81351CFB3064003DC1BA /* RecordEditedTests.swift in Sources */,
|
||||
568068341EBBA26100EFB8AA /* SQLRequestTests.swift in Sources */,
|
||||
5657AB691D108BA9006283EF /* FoundationURLTests.swift in Sources */,
|
||||
56741EAB1E66A8B3003E422D /* RequestTests.swift in Sources */,
|
||||
56B964D61DA521450002DA19 /* FTS5TableBuilderTests.swift in Sources */,
|
||||
|
||||
+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
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import XCTest
|
||||
#if USING_SQLCIPHER
|
||||
import GRDBCipher
|
||||
#elseif USING_CUSTOMSQLITE
|
||||
import GRDBCustomSQLite
|
||||
#else
|
||||
import GRDB
|
||||
#endif
|
||||
|
||||
class SQLRequestTests: GRDBTestCase {
|
||||
|
||||
func testSQLRequest() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
let request = SQLRequest("SELECT 1")
|
||||
let (statement, adapter) = try request.prepare(db)
|
||||
XCTAssertEqual(statement.sql, "SELECT 1")
|
||||
XCTAssertNil(adapter)
|
||||
}
|
||||
}
|
||||
|
||||
func testSQLRequestWithArgumentsAndAdapter() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
let request = SQLRequest("SELECT ?, ?", arguments: [1, 2], adapter: SuffixRowAdapter(fromIndex: 1))
|
||||
let (statement, adapter) = try request.prepare(db)
|
||||
XCTAssertEqual(statement.sql, "SELECT ?, ?")
|
||||
XCTAssertNotNil(adapter)
|
||||
let int = try Int.fetchOne(db, request)!
|
||||
XCTAssertEqual(int, 2)
|
||||
}
|
||||
}
|
||||
|
||||
func testNotCachedSQLRequest() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
let request = SQLRequest("SELECT 1")
|
||||
let (statement1, _) = try request.prepare(db)
|
||||
let (statement2, _) = try request.prepare(db)
|
||||
XCTAssertTrue(statement1 !== statement2)
|
||||
}
|
||||
}
|
||||
|
||||
func testCachedSQLRequest() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
let request = SQLRequest("SELECT 1", cached: true)
|
||||
let (statement1, _) = try request.prepare(db)
|
||||
let (statement2, _) = try request.prepare(db)
|
||||
XCTAssertTrue(statement1 === statement2)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user