mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-22 04:00:39 +00:00
Merge pull request #1893 from groue/dev/support-statement-recompilation
Dev/support statement recompilation
This commit is contained in:
5 files changed
+139
-24
No files matched your search
+18
-13
@@ -154,7 +154,20 @@ public final class Row {
|
||||
let sqliteStatement: SQLiteStatement?
|
||||
|
||||
/// The number of columns in the row.
|
||||
public let count: Int
|
||||
@inlinable
|
||||
public var count: Int {
|
||||
// Rows that map an SQLite statement don't cache the number of
|
||||
// columns, because SQLite may recompile the statement after a
|
||||
// schema change, with a different number of columns.
|
||||
if let sqliteStatement {
|
||||
return Int(sqlite3_column_count(sqliteStatement))
|
||||
}
|
||||
return cachedCount
|
||||
}
|
||||
|
||||
/// The number of columns, when the row does not map an SQLite statement.
|
||||
@usableFromInline
|
||||
let cachedCount: Int
|
||||
|
||||
/// A view on the prefetched associated rows.
|
||||
///
|
||||
@@ -238,7 +251,7 @@ public final class Row {
|
||||
self.statement = statement
|
||||
self.sqliteStatement = statement.sqliteStatement
|
||||
self.impl = StatementRowImpl(sqliteStatement: statement.sqliteStatement, statement: statement)
|
||||
self.count = Int(sqlite3_column_count(sqliteStatement))
|
||||
self.cachedCount = 0 // Unused because sqliteStatement is not nil: see `count`
|
||||
}
|
||||
|
||||
/// Creates a row that maps an SQLite statement. Further calls to
|
||||
@@ -247,7 +260,7 @@ public final class Row {
|
||||
self.statement = nil
|
||||
self.sqliteStatement = sqliteStatement
|
||||
self.impl = SQLiteStatementRowImpl(sqliteStatement: sqliteStatement)
|
||||
self.count = Int(sqlite3_column_count(sqliteStatement))
|
||||
self.cachedCount = 0 // Unused because sqliteStatement is not nil: see `count`
|
||||
}
|
||||
|
||||
/// Creates a row that contain a copy of the current state of the
|
||||
@@ -269,7 +282,7 @@ public final class Row {
|
||||
self.statement = nil
|
||||
self.sqliteStatement = nil
|
||||
self.impl = impl
|
||||
self.count = impl.count
|
||||
self.cachedCount = impl.count
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2785,19 +2798,10 @@ private struct StatementCopyRowImpl: RowImpl {
|
||||
private struct StatementRowImpl: RowImpl {
|
||||
let statement: Statement
|
||||
let sqliteStatement: SQLiteStatement
|
||||
let lowercaseColumnIndexes: [String: Int]
|
||||
|
||||
init(sqliteStatement: SQLiteStatement, statement: Statement) {
|
||||
self.statement = statement
|
||||
self.sqliteStatement = sqliteStatement
|
||||
// Optimize row[columnName]
|
||||
let lowercaseColumnNames = (0..<sqlite3_column_count(sqliteStatement))
|
||||
.map { String(cString: sqlite3_column_name(sqliteStatement, CInt($0))).lowercased() }
|
||||
self.lowercaseColumnIndexes = Dictionary(
|
||||
lowercaseColumnNames
|
||||
.enumerated()
|
||||
.map { ($0.element, $0.offset) },
|
||||
uniquingKeysWith: { (left, _) in left }) // keep leftmost indexes
|
||||
}
|
||||
|
||||
var count: Int {
|
||||
@@ -2844,6 +2848,7 @@ private struct StatementRowImpl: RowImpl {
|
||||
}
|
||||
|
||||
func index(forColumn name: String) -> Int? {
|
||||
let lowercaseColumnIndexes = statement.columnIndexes
|
||||
if let index = lowercaseColumnIndexes[name] {
|
||||
return index
|
||||
}
|
||||
|
||||
+45
-11
@@ -47,6 +47,25 @@ public final class Statement {
|
||||
case rollbackSavepoint(String)
|
||||
}
|
||||
|
||||
/// The column names and indexes of a statement, for a given compilation
|
||||
/// of the statement.
|
||||
private struct ColumnCache {
|
||||
var reprepareCount: CInt
|
||||
var columnNames: [String]
|
||||
/// Indexes of the leftmost columns. Keys are lowercase.
|
||||
var columnIndexes: [String: Int]
|
||||
|
||||
init(sqliteStatement: SQLiteStatement, reprepareCount: CInt) {
|
||||
self.reprepareCount = reprepareCount
|
||||
self.columnNames = (0..<sqlite3_column_count(sqliteStatement)).map {
|
||||
String(cString: sqlite3_column_name(sqliteStatement, $0))
|
||||
}
|
||||
self.columnIndexes = Dictionary(
|
||||
columnNames.enumerated().map { ($0.element.lowercased(), $0.offset) },
|
||||
uniquingKeysWith: { (left, _) in left }) // keep leftmost indexes
|
||||
}
|
||||
}
|
||||
|
||||
/// The raw SQLite statement, suitable for the SQLite C API.
|
||||
public let sqliteStatement: SQLiteStatement
|
||||
|
||||
@@ -59,11 +78,9 @@ public final class Statement {
|
||||
}
|
||||
|
||||
/// The column names, ordered from left to right.
|
||||
public lazy var columnNames: [String] = {
|
||||
// swiftlint:disable:next redundant_self
|
||||
let sqliteStatement = self.sqliteStatement
|
||||
return (0..<CInt(columnCount)).map { String(cString: sqlite3_column_name(sqliteStatement, $0)) }
|
||||
}()
|
||||
public var columnNames: [String] {
|
||||
columnCache.columnNames
|
||||
}
|
||||
|
||||
// The database region is reported by `sqlite3_set_authorizer`, and maybe
|
||||
// refined in `SQLQueryGenerator.makeStatement(_:)` when we have enough
|
||||
@@ -114,12 +131,29 @@ public final class Statement {
|
||||
@usableFromInline
|
||||
unowned let database: Database
|
||||
|
||||
/// Cache for index(ofColumn:). Keys are lowercase.
|
||||
private lazy var columnIndexes: [String: Int] = {
|
||||
Dictionary(
|
||||
columnNames.enumerated().map { ($0.element.lowercased(), $0.offset) },
|
||||
uniquingKeysWith: { (left, _) in left }) // keep leftmost indexes
|
||||
}()
|
||||
/// Indexes of the leftmost columns. Keys are lowercase.
|
||||
var columnIndexes: [String: Int] {
|
||||
columnCache.columnIndexes
|
||||
}
|
||||
|
||||
/// Storage for `columnCache`.
|
||||
private var _columnCache: ColumnCache?
|
||||
|
||||
/// The column names and indexes.
|
||||
///
|
||||
/// SQLite recompiles statements after schema changes (see
|
||||
/// <https://www.sqlite.org/c3ref/prepare.html>), and this can change
|
||||
/// the result columns (think `SELECT *` after a column was added).
|
||||
/// So the cache is rebuilt whenever the statement is recompiled.
|
||||
private var columnCache: ColumnCache {
|
||||
let reprepareCount = sqlite3_stmt_status(sqliteStatement, SQLITE_STMTSTATUS_REPREPARE, 0)
|
||||
if let _columnCache, _columnCache.reprepareCount == reprepareCount {
|
||||
return _columnCache
|
||||
}
|
||||
let columnCache = ColumnCache(sqliteStatement: sqliteStatement, reprepareCount: reprepareCount)
|
||||
_columnCache = columnCache
|
||||
return columnCache
|
||||
}
|
||||
|
||||
/// Creates a prepared statement. Returns nil if the compiled string is
|
||||
/// blank or empty.
|
||||
|
||||
@@ -405,4 +405,31 @@ class RowFromStatementTests : RowTestCase {
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
func testRowsAfterSchemaChange() throws {
|
||||
// SQLite recompiles statements after a schema change, and this can
|
||||
// change the result columns.
|
||||
try makeDatabaseQueue().inDatabase { db in
|
||||
try db.execute(sql: """
|
||||
CREATE TABLE t(a);
|
||||
INSERT INTO t VALUES (1);
|
||||
""")
|
||||
let statement = try db.makeStatement(sql: "SELECT * FROM t")
|
||||
XCTAssertEqual(try Row.fetchAll(statement), [["a": 1]])
|
||||
|
||||
try db.execute(sql: "ALTER TABLE t ADD COLUMN b DEFAULT 2")
|
||||
|
||||
// The cursor is created before the statement is recompiled,
|
||||
// on the first step.
|
||||
let rows = try Row.fetchCursor(statement)
|
||||
let row = try XCTUnwrap(rows.next())
|
||||
XCTAssertEqual(row.count, 2)
|
||||
XCTAssertEqual(Array(row.columnNames), ["a", "b"])
|
||||
XCTAssertEqual(row["b"], 2)
|
||||
XCTAssertEqual(row.copy(), ["a": 1, "b": 2])
|
||||
XCTAssertNil(try rows.next())
|
||||
|
||||
XCTAssertEqual(try Row.fetchAll(statement), [["a": 1, "b": 2]])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -47,6 +47,33 @@ class SQLRequestTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testCachedSQLRequestAfterSchemaChange() throws {
|
||||
// SQLite recompiles statements after a schema change, and this can
|
||||
// change the result columns.
|
||||
let dbQueue = try makeDatabaseQueue(filename: "test.sqlite")
|
||||
try dbQueue.write { db in
|
||||
try db.execute(sql: """
|
||||
CREATE TABLE t(a);
|
||||
INSERT INTO t VALUES (1);
|
||||
""")
|
||||
}
|
||||
|
||||
let request = SQLRequest(sql: "SELECT * FROM t", cached: true)
|
||||
try dbQueue.read { db in
|
||||
XCTAssertEqual(try request.fetchAll(db), [["a": 1]])
|
||||
}
|
||||
|
||||
// Schema change performed by another connection
|
||||
let otherQueue = try makeDatabaseQueue(filename: "test.sqlite")
|
||||
try otherQueue.write { db in
|
||||
try db.execute(sql: "ALTER TABLE t ADD COLUMN b DEFAULT 2")
|
||||
}
|
||||
|
||||
try dbQueue.inDatabase { db in
|
||||
XCTAssertEqual(try request.fetchAll(db), [["a": 1, "b": 2]])
|
||||
}
|
||||
}
|
||||
|
||||
func testSQLLiteralInitializer() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
|
||||
@@ -388,4 +388,26 @@ class SelectStatementTests : GRDBTestCase {
|
||||
XCTAssertEqual(observers.map(\.triggered), [true, true, true, true])
|
||||
}
|
||||
}
|
||||
|
||||
func testColumnsAfterSchemaChange() throws {
|
||||
// SQLite recompiles statements after a schema change, and this can
|
||||
// change the result columns.
|
||||
try makeDatabaseQueue().inDatabase { db in
|
||||
try db.execute(sql: """
|
||||
CREATE TABLE t(a);
|
||||
INSERT INTO t VALUES (1);
|
||||
""")
|
||||
let statement = try db.makeStatement(sql: "SELECT * FROM t")
|
||||
XCTAssertEqual(statement.columnNames, ["a"])
|
||||
XCTAssertEqual(statement.index(ofColumn: "a"), 0)
|
||||
XCTAssertNil(statement.index(ofColumn: "b"))
|
||||
|
||||
try db.execute(sql: "ALTER TABLE t ADD COLUMN b")
|
||||
try statement.execute() // Recompile
|
||||
XCTAssertEqual(statement.columnCount, 2)
|
||||
XCTAssertEqual(statement.columnNames, ["a", "b"])
|
||||
XCTAssertEqual(statement.index(ofColumn: "a"), 0)
|
||||
XCTAssertEqual(statement.index(ofColumn: "b"), 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user