Merge branch 'development'

This commit is contained in:
Gwendal Roué committed 2026-11-01 18:24:21 +01:00
commit 92a554cd0d
34 files changed
+1600 -125

No files matched your search

+14
View File
@@ -7,6 +7,7 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception:
#### 7.x Releases
- `7.12.x` Releases - [7.12.0](#7120)
- `7.11.x` Releases - [7.11.0](#7110) - [7.11.1](#7111)
- `7.10.x` Releases - [7.10.0](#7100)
- `7.9.x` Releases - [7.9.0](#790)
@@ -143,6 +144,19 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception:
---
## 7.12.0
Released November 1, 2026
- **New**: Add support for Strings that contain the NUL character by [@groue](https://github.com/groue) and [@nesevis](https://github.com/nesevis) in [#1885](https://github.com/groue/GRDB.swift/pull/1885)
- **New**: Keep SQLite's error message when a database fails to open by [@builtbysharath](https://github.com/builtbysharath) in [#1889](https://github.com/groue/GRDB.swift/pull/1889)
- **Fixed**: Fix doubled word in doc comment by [@latent-9](https://github.com/latent-9) in [#1876](https://github.com/groue/GRDB.swift/pull/1876)
- **Fixed**: Fix an accidentally quadratic behavior in SQLite statement compilation by [@groue](https://github.com/groue) and [@guidedways](https://github.com/guidedways) in [#1881](https://github.com/groue/GRDB.swift/pull/1881)
- **Fixed**: Work around an SQLite bug in WAL snapshot ordering by [@groue](https://github.com/groue) and [@vburojevic](https://github.com/vburojevic) in [#1884](https://github.com/groue/GRDB.swift/pull/1884)
- **Fixed**: Fix upsert for WITHOUT ROWID tables with an INTEGER primary key by [@nesevis](https://github.com/nesevis) in [#1879](https://github.com/groue/GRDB.swift/pull/1879)
- **Fixed**: Support for statement recompilation after the columns are modified in the database schema by [@groue](https://github.com/groue) in [#1893](https://github.com/groue/GRDB.swift/pull/1893)
- **Fixed**: Fix SharedValueObservation deadlock, ordering, and cancellation by [@groue](https://github.com/groue) in [#1894](https://github.com/groue/GRDB.swift/pull/1894)
## 7.11.1
Released June 18, 2026
+1 -1
View File
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'GRDB.swift'
s.version = '7.11.1'
s.version = '7.12.0'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.summary = 'A toolkit for SQLite databases, with a focus on application development.'
+19 -4
View File
@@ -511,11 +511,12 @@ extension Database {
// > DESC" clause, it does not become an alias for the rowid [...]
//
// FIXME: We ignore the exception, and consider all INTEGER primary
// keys as aliases for the rowid:
if pkColumn.type.uppercased() == "INTEGER" {
// keys of rowid tables as aliases for the rowid:
let tableHasRowID = try fetchTableHasRowID(table)
if pkColumn.type.uppercased() == "INTEGER" && tableHasRowID {
return .rowID(pkColumn)
} else {
return try .regular([pkColumn], tableHasRowID: fetchTableHasRowID(table))
return .regular([pkColumn], tableHasRowID: tableHasRowID)
}
default:
@@ -1605,7 +1606,19 @@ extension ForeignKeyViolation: CustomStringConvertible {
/// pk.columns // ["citizenID", "countryIsoCode"]
/// pk.rowIDColumn // nil
/// pk.isRowID // false
///
/// // CREATE TABLE passport (
/// // id INTEGER PRIMARY KEY,
/// // name TEXT
/// // ) WITHOUT ROWID
/// let pk = try db.primaryKey("passport")
/// pk.columns // ["id"]
/// pk.rowIDColumn // nil
/// pk.isRowID // false
/// ```
///
/// An `INTEGER PRIMARY KEY` is an alias for the rowid only in rowid
/// tables. In a `WITHOUT ROWID` table, it is a regular primary key.
public struct PrimaryKeyInfo: Sendable {
private enum Impl {
/// The hidden rowID.
@@ -1659,7 +1672,9 @@ public struct PrimaryKeyInfo: Sendable {
}
/// When not nil, the name of the column that contains the
/// `INTEGER PRIMARY KEY`.
/// `INTEGER PRIMARY KEY` of a rowid table.
///
/// This is nil for `WITHOUT ROWID` tables, which have no rowid.
public var rowIDColumn: String? {
switch impl {
case .hiddenRowID:
+4 -1
View File
@@ -494,8 +494,11 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
// https://www.sqlite.org/c3ref/close.html
// > Calling sqlite3_close() or sqlite3_close_v2() with a NULL
// > pointer argument is a harmless no-op.
//
// Read the error message before the connection is closed.
let message = sqliteConnection.map { String(cString: sqlite3_errmsg($0)) }
_ = sqlite3_close(sqliteConnection) // ignore result code
throw DatabaseError(resultCode: code)
throw DatabaseError(resultCode: code, message: message)
}
guard let sqliteConnection else {
throw DatabaseError(resultCode: .SQLITE_INTERNAL) // WTF SQLite?
+8 -4
View File
@@ -105,8 +105,10 @@ public final class DatabaseFunction: Identifiable, Sendable {
sqlite3_result_int64(context, int64)
case .double(let double):
sqlite3_result_double(context, double)
case .string(let string):
sqlite3_result_text(context, string, -1, SQLITE_TRANSIENT)
case .string(var string):
string.withSQLiteText {
sqlite3_result_text(context, $0, $1, SQLITE_TRANSIENT)
}
case .blob(let data):
data.withUnsafeBytes {
sqlite3_result_blob(context, $0.baseAddress, CInt($0.count), SQLITE_TRANSIENT)
@@ -447,8 +449,10 @@ public final class DatabaseFunction: Identifiable, Sendable {
sqlite3_result_int64(sqliteContext, int64)
case .double(let double):
sqlite3_result_double(sqliteContext, double)
case .string(let string):
sqlite3_result_text(sqliteContext, string, -1, SQLITE_TRANSIENT)
case .string(var string):
string.withSQLiteText {
sqlite3_result_text(sqliteContext, $0, $1, SQLITE_TRANSIENT)
}
case .blob(let data):
data.withUnsafeBytes {
sqlite3_result_blob(sqliteContext, $0.baseAddress, CInt($0.count), SQLITE_TRANSIENT)
+39 -3
View File
@@ -41,7 +41,8 @@
/// - ``isModified(byEventsOfKind:)``
/// - ``isModified(by:)``
public struct DatabaseRegion: Sendable {
private let tableRegions: [CaseInsensitiveIdentifier: TableRegion]?
/// Nil means that the region is the full database.
private var tableRegions: [CaseInsensitiveIdentifier: TableRegion]?
private init(tableRegions: [CaseInsensitiveIdentifier: TableRegion]?) {
self.tableRegions = tableRegions
@@ -77,19 +78,26 @@ public struct DatabaseRegion: Sendable {
self.init(tableRegions: [table: TableRegion(columns: nil, rowIds: nil)])
}
/// Full columns in a table: (some columns in a table) × (all rows)
/// Some columns from all rows in a table.
init(table: String, columns: Set<String>) {
let table = CaseInsensitiveIdentifier(rawValue: table)
let columns = Set(columns.map(CaseInsensitiveIdentifier.init))
self.init(tableRegions: [table: TableRegion(columns: columns, rowIds: nil)])
}
/// Full rows in a table: (all columns in a table) × (some rows)
/// All columns from some rows in a table.
init(table: String, rowIds: Set<Int64>) {
let table = CaseInsensitiveIdentifier(rawValue: table)
self.init(tableRegions: [table: TableRegion(columns: nil, rowIds: rowIds)])
}
/// Some columns from some rows in a table.
init(table: String, columns: Set<String>, rowIds: Set<Int64>) {
let table = CaseInsensitiveIdentifier(rawValue: table)
let columns = Set(columns.map(CaseInsensitiveIdentifier.init))
self.init(tableRegions: [table: TableRegion(columns: columns, rowIds: rowIds)])
}
/// Returns the intersection of this region and the given one.
///
/// This method is not public because there is no known public use case for
@@ -162,6 +170,22 @@ public struct DatabaseRegion: Sendable {
self = union(other)
}
/// Inserts all columns and rows from the given table.
mutating func insert(table: String) {
guard tableRegions != nil else { return }
let table = CaseInsensitiveIdentifier(rawValue: table)
tableRegions![table, default: TableRegion()].insertAllRowsAndColumns()
}
/// Inserts the specified column and all rows from the given table.
mutating func insert(table: String, column: String) {
assert(!column.isEmpty)
guard tableRegions != nil else { return }
let table = CaseInsensitiveIdentifier(rawValue: table)
let column = CaseInsensitiveIdentifier(rawValue: column)
tableRegions![table, default: TableRegion(columns: [column])].insertAllRowsAndColumn(column)
}
/// Returns a region suitable for database observation
func observableRegion(_ db: Database) throws -> DatabaseRegion {
// SQLite does not expose schema changes to the
@@ -370,6 +394,18 @@ private struct TableRegion: Equatable {
return TableRegion(columns: columnsUnion, rowIds: rowIdsUnion)
}
mutating func insertAllRowsAndColumns() {
rowIds = nil
columns = nil
}
mutating func insertAllRowsAndColumn(_ column: CaseInsensitiveIdentifier) {
rowIds = nil
if columns != nil {
columns!.insert(column)
}
}
func contains(rowID: Int64) -> Bool {
guard let rowIds else {
return true
+7 -3
View File
@@ -147,7 +147,10 @@ public struct DatabaseValue: Hashable {
case SQLITE_FLOAT:
storage = .double(sqlite3_value_double(sqliteValue))
case SQLITE_TEXT:
storage = .string(String(cString: sqlite3_value_text(sqliteValue)!))
let text = sqlite3_value_text(sqliteValue)
let byteCount = Int(sqlite3_value_bytes(sqliteValue))
let string = String(decoding: UnsafeBufferPointer(start: text, count: byteCount), as: UTF8.self)
storage = .string(string)
case SQLITE_BLOB:
if let bytes = sqlite3_value_blob(sqliteValue) {
let count = Int(sqlite3_value_bytes(sqliteValue))
@@ -171,7 +174,8 @@ public struct DatabaseValue: Hashable {
case SQLITE_FLOAT:
storage = .double(sqlite3_column_double(sqliteStatement, index))
case SQLITE_TEXT:
storage = .string(String(cString: sqlite3_column_text(sqliteStatement, index)))
let string = String(sqliteStatement: sqliteStatement, index: index)
storage = .string(string)
case SQLITE_BLOB:
if let bytes = sqlite3_column_blob(sqliteStatement, index) {
let count = Int(sqlite3_column_bytes(sqliteStatement, index))
@@ -223,7 +227,7 @@ extension DatabaseValue: StatementBinding {
let code = double.bind(to: sqliteStatement, at: index)
try checkBindingSuccess(code: code, sqliteStatement: sqliteStatement)
return try body()
case .string(let string):
case .string(var string):
return try string.withBinding(to: sqliteStatement, at: index, do: body)
case .blob(let data):
return try data.withBinding(to: sqliteStatement, at: index, do: body)
+18 -13
View File
@@ -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
View File
@@ -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.
+2 -2
View File
@@ -117,10 +117,10 @@ final class StatementAuthorizer {
guard let columnName = cString2.map(String.init) else { return SQLITE_OK }
if columnName.isEmpty {
// SELECT COUNT(*) FROM table
selectedRegion.formUnion(DatabaseRegion(table: tableName))
selectedRegion.insert(table: tableName)
} else {
// SELECT column FROM table
selectedRegion.formUnion(DatabaseRegion(table: tableName, columns: [columnName]))
selectedRegion.insert(table: tableName, column: columnName)
}
return SQLITE_OK
+2 -3
View File
@@ -56,9 +56,8 @@ extension Decimal: StatementColumnConvertible {
case SQLITE_FLOAT:
self.init(sqlite3_column_double(sqliteStatement, index))
case SQLITE_TEXT:
self.init(
string: String(cString: sqlite3_column_text(sqliteStatement, index)!),
locale: _posixLocale)
let string = String(sqliteStatement: sqliteStatement, index: index)
self.init(string: string, locale: _posixLocale)
default:
return nil
}
+1 -1
View File
@@ -89,7 +89,7 @@ extension UUID: StatementColumnConvertible {
public init?(sqliteStatement: SQLiteStatement, index: CInt) {
switch sqlite3_column_type(sqliteStatement, index) {
case SQLITE_TEXT:
let string = String(cString: sqlite3_column_text(sqliteStatement, index)!)
let string = String(sqliteStatement: sqliteStatement, index: index)
guard let uuid = UUID(uuidString: string) else {
return nil
}
@@ -592,7 +592,9 @@ extension String: DatabaseValueConvertible, StatementColumnConvertible {
/// - sqliteStatement: A pointer to an SQLite statement.
/// - index: The column index.
public init(sqliteStatement: SQLiteStatement, index: CInt) {
self = String(cString: sqlite3_column_text(sqliteStatement, index)!)
let text = sqlite3_column_text(sqliteStatement, index)
let byteCount = Int(sqlite3_column_bytes(sqliteStatement, index))
self = String(decoding: UnsafeBufferPointer(start: text, count: byteCount), as: UTF8.self)
}
/// Returns a TEXT database value.
@@ -622,23 +624,57 @@ extension String: DatabaseValueConvertible, StatementColumnConvertible {
}
public func bind(to sqliteStatement: SQLiteStatement, at index: CInt) -> CInt {
sqlite3_bind_text(sqliteStatement, index, self, -1, SQLITE_TRANSIENT)
var string = self
return string.withSQLiteText {
sqlite3_bind_text(sqliteStatement, index, $0, $1, SQLITE_TRANSIENT)
}
}
/// Calls the given closure after binding a statement argument.
///
/// The binding is valid only during the execution of this method.
///
/// This method is mutating because the string is made contiguous
/// if necessary.
///
/// - parameter sqliteStatement: An SQLite statement.
/// - parameter index: 1-based index to statement arguments.
/// - parameter body: The closure to execute when argument is bound.
func withBinding<T>(to sqliteStatement: SQLiteStatement, at index: CInt, do body: () throws -> T) throws -> T {
try withCString {
let code = sqlite3_bind_text(sqliteStatement, index, $0, -1, nil /* SQLITE_STATIC */)
mutating func withBinding<T>(
to sqliteStatement: SQLiteStatement,
at index: CInt,
do body: () throws -> T
) throws -> T {
try withSQLiteText {
let code = sqlite3_bind_text(sqliteStatement, index, $0, $1, nil /* SQLITE_STATIC */)
try checkBindingSuccess(code: code, sqliteStatement: sqliteStatement)
return try body()
}
}
/// Runs body over the content of this string as an UTF8 string. This
/// method exists because it deals with the ASCII NUL (\0) characters,
/// unlike C strings.
///
/// This method is mutating because the string is made contiguous
/// if necessary.
mutating func withSQLiteText<T>(
_ body: (UnsafePointer<CChar>, CInt) throws -> T
) rethrows -> T {
try withUTF8 { utf8 in
guard let baseAddress = utf8.baseAddress else {
// Empty string: yield a non-nil pointer to the NUL string
// terminator, so that functions such as sqlite3_bind_text()
// unambiguously interpret it as the empty string.
return try withUnsafePointer(to: 0 as CChar) {
try body($0, 0)
}
}
return try baseAddress.withMemoryRebound(to: CChar.self, capacity: utf8.count) {
try body($0, CInt(utf8.count))
}
}
}
}
@@ -452,8 +452,15 @@ extension ValueConcurrentObserver {
let isModified: Bool
if let currentWALSnapshot = try? WALSnapshot(writerDB) {
let ordering = initialFetchTransaction.walSnapshot.compare(currentWALSnapshot)
assert(ordering <= 0, "Unexpected snapshot ordering")
isModified = ordering < 0
// We expect `ordering <= 0`, since the current
// snapshot is captured after the snapshot of the
// initial fetch. The current one is supposed to be
// identical (no change), or newer (database was
// changed since initial fetch). Yet SQLite has
// a quirk, as revealed by <https://github.com/groue/GRDB.swift/issues/1883>
// That's why we just check for a difference, and do
// not `assert(ordering <= 0)`.
isModified = ordering != 0
} else {
// Can't compare: assume the database was modified.
isModified = true
@@ -169,17 +169,37 @@ public final class SharedValueObservation<Element: Sendable>: @unchecked Sendabl
private let scheduler: any ValueObservationScheduler
private let extent: SharedValueObservationExtent
private let startObservation: ValueObservationStart<Element>
private let lock = NSRecursiveLock() // support synchronous observation events
// Clients are notified outside of the lock, so that a client can be
// cancelled from any thread during a notification without deadlock.
private let lock = NSLock()
// protected by lock
private var clients: [Client]
private var isObserving = false
private var cancellable: AnyDatabaseCancellable?
private var currentSubscription: ValueObservationSubscription? // nil when not observing
private var lastResult: Result<Element, Error>?
/// A subscription to the upstream ValueObservation.
///
/// Events and cancellables of a subscription that is no longer
/// the current one are ignored.
private final class ValueObservationSubscription: @unchecked Sendable {
// @unchecked Sendable because state is protected by
// the lock of SharedValueObservation.
var cancellable: AnyDatabaseCancellable?
}
private final class Client: Sendable {
let onError: @Sendable (Error) -> Void
let onChange: @Sendable (Element) -> Void
private let onError: @Sendable (Error) -> Void
private let onChange: @Sendable (Element) -> Void
private let stateMutex = Mutex(State())
private struct State {
var isCancelled = false
/// True when a ValueObservation event was notified, and we must
/// not notify the last result of the shared observation as the
/// initial event.
var shouldDiscardInitialResult = false
}
init(
onError: @escaping @Sendable (Error) -> Void,
@@ -188,6 +208,54 @@ public final class SharedValueObservation<Element: Sendable>: @unchecked Sendabl
self.onError = onError
self.onChange = onChange
}
/// Notifies the last result of the shared observation as the initial
/// value, unless a ValueObservation event was already notified.
func notifyInitial(_ result: Result<Element, Error>) {
guard shouldNotifyInitialResult() else { return }
switch result {
case let .failure(error):
onError(error)
case let .success(value):
onChange(value)
}
}
/// Notifies a ValueObservation event.
func notifyChange(_ value: Element) {
guard shouldNotifyValueObservationEvent() else { return }
onChange(value)
}
/// Notifies a ValueObservation event.
func notifyError(_ error: Error) {
guard shouldNotifyValueObservationEvent() else { return }
onError(error)
}
/// After this method returns, no new notification starts.
func cancel() {
stateMutex.withLock { $0.isCancelled = true }
}
/// Returns whether a ValueObservation event event should be notified.
private func shouldNotifyValueObservationEvent() -> Bool {
stateMutex.withLock { state in
if state.isCancelled { return false }
// After a ValueObservation event, we must ignore the
// initial result.
state.shouldDiscardInitialResult = true
return true
}
}
/// Returns whether the initial result should be notified: the client
/// is not cancelled, and the initial result should not be discarded.
private func shouldNotifyInitialResult() -> Bool {
stateMutex.withLock { state in
!state.isCancelled && !state.shouldDiscardInitialResult
}
}
}
fileprivate init(
@@ -229,49 +297,65 @@ public final class SharedValueObservation<Element: Sendable>: @unchecked Sendabl
onChange: @escaping @Sendable (Element) -> Void)
-> AnyDatabaseCancellable
{
withLock {
let client = Client(onError: onError, onChange: onChange)
let (newSubscription, lastResult) = lock.withLock {
// State change
clients.append(client)
// Support for reentrancy: a shared immediate observation is
// started from the first value notification of that same shared
// immediate observation. Yeah, users are nasty.
// In this case, self.cancellable is still nil, because we are
// still waiting for the upstream ValueObservation to start.
// But we must not start another one.
let needsStart = !isObserving
// In this case, self.currentSubscription has no cancellable yet,
// because we are still waiting for the upstream ValueObservation
// to start. But we must not start another one.
if currentSubscription != nil {
return (ValueObservationSubscription?.none, self.lastResult)
}
let newSubscription = ValueObservationSubscription()
currentSubscription = newSubscription
return (newSubscription, nil)
}
// Side effect
if let newSubscription {
// Self retains the subscription and its cancellable, so don't have
// the cancellable retain self or the subscription.
let cancellable = startObservation(
// onError
{ [weak self, weak newSubscription] error in
guard let newSubscription else { return }
self?.handleError(error, from: newSubscription)
},
// onChange
{ [weak self, weak newSubscription] element in
guard let newSubscription else { return }
self?.handleChange(element, from: newSubscription)
})
// State change
let client = Client(onError: onError, onChange: onChange)
clients.append(client)
isObserving = true
// Side effect
if needsStart {
// Self retains the cancellable, so don't have the cancellable retain self.
cancellable = startObservation(
// onError
{ [weak self] error in
self?.handleError(error)
},
// onChange
{ [weak self] element in
self?.handleChange(element)
})
} else if let result = lastResult {
// Notify last result as an initial value
scheduler.scheduleInitial {
switch result {
case let .failure(error):
onError(error)
case let .success(value):
onChange(value)
}
// The upstream observation may have been stopped, and maybe
// replaced, while it was starting (for example, if the initial
// fetch fails). In this case, discard its cancellable.
let discardedCancellable: AnyDatabaseCancellable? = lock.withLock {
guard currentSubscription === newSubscription else {
return cancellable
}
newSubscription.cancellable = cancellable
return nil
}
return AnyDatabaseCancellable {
// Retain shared observation (self) until client cancels
self.handleCancel(client)
discardedCancellable?.cancel()
} else if let lastResult {
// Notify last result as an initial value. A fresher result may
// have been scheduled before, and already notified to the
// client: in this case, the last result is not notified.
scheduler.scheduleInitial {
client.notifyInitial(lastResult)
}
}
return AnyDatabaseCancellable {
// Retain shared observation (self) until client cancels
self.handleCancel(client)
}
}
#if canImport(Combine)
@@ -299,55 +383,76 @@ public final class SharedValueObservation<Element: Sendable>: @unchecked Sendabl
}
#endif
private func handleError(_ error: Error) {
withLock {
let notifiedClients = clients
private func handleError(_ error: Error, from source: ValueObservationSubscription) {
let (notifiedClients, stoppedCancellable) = lock.withLock {
guard source === currentSubscription else {
return ([Client](), AnyDatabaseCancellable?.none)
}
// State change
let notifiedClients = clients
var stoppedCancellable: AnyDatabaseCancellable?
clients = []
if extent == .whileObserved {
isObserving = false
cancellable = nil
lastResult = nil
stoppedCancellable = stopObservation()
} else {
lastResult = .failure(error)
}
// Side effect
for client in notifiedClients {
client.onError(error)
}
return (notifiedClients, stoppedCancellable)
}
// Side effects
stoppedCancellable?.cancel()
for client in notifiedClients {
client.notifyError(error)
}
}
private func handleChange(_ value: Element) {
withLock {
private func handleChange(_ value: Element, from source: ValueObservationSubscription) {
let notifiedClients = lock.withLock {
guard source === currentSubscription else {
return [Client]()
}
// State change
lastResult = .success(value)
// Side effect
for client in clients {
client.onChange(value)
}
return clients
}
// Side effect
for client in notifiedClients {
client.notifyChange(value)
}
}
private func handleCancel(_ client: Client) {
withLock {
// Prevent the client from being notified, even if a notification
// is currently in progress.
client.cancel()
let stoppedCancellable = lock.withLock {
// State change
clients.removeFirst(where: { $0 === client })
if clients.isEmpty && extent == .whileObserved {
isObserving = false
cancellable = nil
lastResult = nil
return stopObservation()
}
return nil
}
// Side effect
stoppedCancellable?.cancel()
}
private func withLock<T>(_ execute: () throws -> T) rethrows -> T {
lock.lock()
defer { lock.unlock() }
return try execute()
/// Stops the current upstream observation, and returns its cancellable,
/// so that it can be cancelled outside of the lock.
///
/// Must be called with the lock held.
private func stopObservation() -> AnyDatabaseCancellable? {
let stoppedCancellable = currentSubscription?.cancellable
currentSubscription = nil
lastResult = nil
return stoppedCancellable
}
}
+1 -1
View File
@@ -15,7 +15,7 @@
<a href="https://github.com/groue/GRDB.swift/actions/workflows/CI.yml"><img alt="CI Status" src="https://github.com/groue/GRDB.swift/actions/workflows/CI.yml/badge.svg?branch=master"></a>
</p>
**Latest release**: June 18, 2026 • [version 7.11.1](https://github.com/groue/GRDB.swift/tree/v7.11.1) • [CHANGELOG](CHANGELOG.md) • [Migrating From GRDB 6 to GRDB 7](Documentation/GRDB7MigrationGuide.md)
**Latest release**: November 1, 2026 • [version 7.12.0](https://github.com/groue/GRDB.swift/tree/v7.12.0) • [CHANGELOG](CHANGELOG.md) • [Migrating From GRDB 6 to GRDB 7](Documentation/GRDB7MigrationGuide.md)
**Requirements**: iOS 13.0+ / macOS 10.15+ / tvOS 13.0+ / watchOS 7.0+ &bull; SQLite 3.20.0+ &bull; Swift 6.1+ / Xcode 16.3+
+1 -1
View File
@@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>7.11.1</string>
<string>7.12.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
@@ -89,7 +89,20 @@ class DatabaseFunctionTests: GRDBTestCase {
XCTAssertEqual(try String.fetchOne(db, sql: "SELECT f()")!, "foo")
}
}
func testFunctionReturningStringWithNULCharacter() throws {
let dbQueue = try makeDatabaseQueue()
let fn = DatabaseFunction("f", argumentCount: 0) { dbValues in
// SQLite supports strings that contain a NUL character:
// https://sqlite.org/nulinstr.html
return "foo\u{0}bar"
}
try dbQueue.inDatabase { db in
db.add(function: fn)
XCTAssertEqual(try String.fetchOne(db, sql: "SELECT f()")!, "foo\u{0}bar")
}
}
func testFunctionReturningData() throws {
let dbQueue = try makeDatabaseQueue()
let fn = DatabaseFunction("f", argumentCount: 0) { dbValues in
@@ -167,7 +180,22 @@ class DatabaseFunctionTests: GRDBTestCase {
XCTAssertEqual(try String.fetchOne(db, sql: "SELECT f('foo')")!, "foo")
}
}
func testFunctionArgumentStringWithNULCharacter() throws {
let dbQueue = try makeDatabaseQueue()
let fn = DatabaseFunction("f", argumentCount: 1) { (dbValues: [DatabaseValue]) in
return String.fromDatabaseValue(dbValues[0])
}
try dbQueue.inDatabase { db in
db.add(function: fn)
// A NUL character can not be written in an SQL string literal:
// the string is passed as a statement argument.
XCTAssertEqual(
try String.fetchOne(db, sql: "SELECT f(?)", arguments: ["foo\u{0}bar"])!,
"foo\u{0}bar")
}
}
func testFunctionArgumentBlob() throws {
let dbQueue = try makeDatabaseQueue()
let fn = DatabaseFunction("f", argumentCount: 1) { (dbValues: [DatabaseValue]) in
@@ -52,6 +52,21 @@ class DatabaseQueueTests: GRDBTestCase {
}
}
func testOpenErrorMessage() throws {
// Without URI filenames, this path is a file in a missing "file:"
// directory, and opening fails with SQLITE_CANTOPEN.
let path = "file:" + NSTemporaryDirectory() + "test.sqlite?mode=bogus"
do {
_ = try DatabaseQueue(path: path)
XCTFail("Expected error")
} catch DatabaseError.SQLITE_CANTOPEN {
throw XCTSkip("URI filenames are not enabled")
} catch let error as DatabaseError {
XCTAssertEqual(error.resultCode, .SQLITE_ERROR)
XCTAssertEqual(error.message, "no such access mode: bogus")
}
}
func testAddRemoveFunction() throws {
// Adding a function and then removing it should succeed
let dbQueue = try makeDatabaseQueue()
@@ -28,6 +28,8 @@ private let emojiData = emojiString.data(using: .utf8)
private let nonUTF8Data = Data([0x80])
private let invalidString = "\u{FFFD}" // decoded from nonUTF8Data
private let jpegData = try! Data(contentsOf: testBundle.url(forResource: "Betty", withExtension: "jpeg")!)
private let nulString = "foo\u{0}bar" // https://sqlite.org/nulinstr.html
private let nulStringData = nulString.data(using: .utf8)
class DatabaseValueConversionTests : GRDBTestCase {
@@ -253,6 +255,22 @@ class DatabaseValueConversionTests : GRDBTestCase {
return .rollback
}
// nulString is turned to Text
try dbQueue.inTransaction { db in
try db.execute(sql: "INSERT INTO `values` (textAffinity) VALUES (?)", arguments: [nulString])
let sql = "SELECT textAffinity FROM `values`"
XCTAssertEqual(try DatabaseValue.fetchOne(db, sql: sql)!.storageClass, .text)
try assertDecoding(db, sql, Bool.self, expectedSQLiteConversion: false, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Int.self, expectedSQLiteConversion:0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Int32.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Int64.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Double.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, String.self, expectedSQLiteConversion: nulString, expectedDatabaseValueConversion: nulString)
try assertDecoding(db, sql, Data.self, expectedSQLiteConversion: nulStringData, expectedDatabaseValueConversion: nulStringData)
return .rollback
}
// emojiData is turned to Blob
try dbQueue.inTransaction { db in
@@ -269,6 +287,22 @@ class DatabaseValueConversionTests : GRDBTestCase {
return .rollback
}
// nulStringData is turned to Blob
try dbQueue.inTransaction { db in
try db.execute(sql: "INSERT INTO `values` (textAffinity) VALUES (?)", arguments: [nulStringData])
let sql = "SELECT textAffinity FROM `values`"
XCTAssertEqual(try DatabaseValue.fetchOne(db, sql: sql)!.storageClass, .blob)
try assertDecoding(db, sql, Bool.self, expectedSQLiteConversion: false, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Int.self, expectedSQLiteConversion:0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Int32.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Int64.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Double.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, String.self, expectedSQLiteConversion: nulString, expectedDatabaseValueConversion: nulString)
try assertDecoding(db, sql, Data.self, expectedSQLiteConversion: nulStringData, expectedDatabaseValueConversion: nulStringData)
return .rollback
}
// nonUTF8Data is turned to Blob
try dbQueue.inTransaction { db in
@@ -512,6 +546,22 @@ class DatabaseValueConversionTests : GRDBTestCase {
return .rollback
}
// nulString is turned to Text
try dbQueue.inTransaction { db in
try db.execute(sql: "INSERT INTO `values` (realAffinity) VALUES (?)", arguments: [nulString])
let sql = "SELECT realAffinity FROM `values`"
XCTAssertEqual(try DatabaseValue.fetchOne(db, sql: sql)!.storageClass, .text)
try assertDecoding(db, sql, Bool.self, expectedSQLiteConversion: false, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Int.self, expectedSQLiteConversion:0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Int32.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Int64.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Double.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, String.self, expectedSQLiteConversion: nulString, expectedDatabaseValueConversion: nulString)
try assertDecoding(db, sql, Data.self, expectedSQLiteConversion: nulStringData, expectedDatabaseValueConversion: nulStringData)
return .rollback
}
// emojiData is turned to Blob
try dbQueue.inTransaction { db in
@@ -528,6 +578,22 @@ class DatabaseValueConversionTests : GRDBTestCase {
return .rollback
}
// nulStringData is turned to Blob
try dbQueue.inTransaction { db in
try db.execute(sql: "INSERT INTO `values` (realAffinity) VALUES (?)", arguments: [nulStringData])
let sql = "SELECT realAffinity FROM `values`"
XCTAssertEqual(try DatabaseValue.fetchOne(db, sql: sql)!.storageClass, .blob)
try assertDecoding(db, sql, Bool.self, expectedSQLiteConversion: false, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Int.self, expectedSQLiteConversion:0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Int32.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Int64.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Double.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, String.self, expectedSQLiteConversion: nulString, expectedDatabaseValueConversion: nulString)
try assertDecoding(db, sql, Data.self, expectedSQLiteConversion: nulStringData, expectedDatabaseValueConversion: nulStringData)
return .rollback
}
// nonUTF8Data is turned to Blob
try dbQueue.inTransaction { db in
@@ -699,6 +765,22 @@ class DatabaseValueConversionTests : GRDBTestCase {
return .rollback
}
// nulString is turned to Text
try dbQueue.inTransaction { db in
try db.execute(sql: "INSERT INTO `values` (noneAffinity) VALUES (?)", arguments: [nulString])
let sql = "SELECT noneAffinity FROM `values`"
XCTAssertEqual(try DatabaseValue.fetchOne(db, sql: sql)!.storageClass, .text)
try assertDecoding(db, sql, Bool.self, expectedSQLiteConversion: false, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Int.self, expectedSQLiteConversion:0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Int32.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Int64.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Double.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, String.self, expectedSQLiteConversion: nulString, expectedDatabaseValueConversion: nulString)
try assertDecoding(db, sql, Data.self, expectedSQLiteConversion: nulStringData, expectedDatabaseValueConversion: nulStringData)
return .rollback
}
// emojiData is turned to Blob
try dbQueue.inTransaction { db in
@@ -715,6 +797,22 @@ class DatabaseValueConversionTests : GRDBTestCase {
return .rollback
}
// nullStringData is turned to Blob
try dbQueue.inTransaction { db in
try db.execute(sql: "INSERT INTO `values` (noneAffinity) VALUES (?)", arguments: [nulStringData])
let sql = "SELECT noneAffinity FROM `values`"
XCTAssertEqual(try DatabaseValue.fetchOne(db, sql: sql)!.storageClass, .blob)
try assertDecoding(db, sql, Bool.self, expectedSQLiteConversion: false, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Int.self, expectedSQLiteConversion:0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Int32.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Int64.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Double.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, String.self, expectedSQLiteConversion: nulString, expectedDatabaseValueConversion: nulString)
try assertDecoding(db, sql, Data.self, expectedSQLiteConversion: nulStringData, expectedDatabaseValueConversion: nulStringData)
return .rollback
}
// nonUTF8Data is turned to Blob
try dbQueue.inTransaction { db in
@@ -932,6 +1030,22 @@ class DatabaseValueConversionTests : GRDBTestCase {
return .rollback
}
// nulString is turned to Text
try dbQueue.inTransaction { db in
try db.execute(sql: "INSERT INTO `values` (\(columnName)) VALUES (?)", arguments: [nulString])
let sql = "SELECT \(columnName) FROM `values`"
XCTAssertEqual(try DatabaseValue.fetchOne(db, sql: sql)!.storageClass, .text)
try assertDecoding(db, sql, Bool.self, expectedSQLiteConversion: false, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Int.self, expectedSQLiteConversion:0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Int32.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Int64.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Double.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, String.self, expectedSQLiteConversion: nulString, expectedDatabaseValueConversion: nulString)
try assertDecoding(db, sql, Data.self, expectedSQLiteConversion: nulStringData, expectedDatabaseValueConversion: nulStringData)
return .rollback
}
// emojiData is turned to Blob
try dbQueue.inTransaction { db in
@@ -948,6 +1062,22 @@ class DatabaseValueConversionTests : GRDBTestCase {
return .rollback
}
// nulStringData is turned to Blob
try dbQueue.inTransaction { db in
try db.execute(sql: "INSERT INTO `values` (\(columnName)) VALUES (?)", arguments: [nulStringData])
let sql = "SELECT \(columnName) FROM `values`"
XCTAssertEqual(try DatabaseValue.fetchOne(db, sql: sql)!.storageClass, .blob)
try assertDecoding(db, sql, Bool.self, expectedSQLiteConversion: false, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Int.self, expectedSQLiteConversion:0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Int32.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Int64.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, Double.self, expectedSQLiteConversion: 0, expectedDatabaseValueConversion: nil)
try assertDecoding(db, sql, String.self, expectedSQLiteConversion: nulString, expectedDatabaseValueConversion: nulString)
try assertDecoding(db, sql, Data.self, expectedSQLiteConversion: nulStringData, expectedDatabaseValueConversion: nulStringData)
return .rollback
}
// nonUTF8Data is turned to Blob
try dbQueue.inTransaction { db in
@@ -105,6 +105,36 @@ class PrimaryKeyInfoTests: GRDBTestCase {
}
}
func testIntegerPrimaryKeyWithoutRowID() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
try db.execute(sql: "CREATE TABLE items (id INTEGER PRIMARY KEY, name TEXT) WITHOUT ROWID")
let primaryKey = try db.primaryKey("items")
XCTAssertEqual(primaryKey.columnInfos?.map(\.name), ["id"])
XCTAssertEqual(primaryKey.columnInfos?.map(\.type), ["INTEGER"])
XCTAssertEqual(primaryKey.columns, ["id"])
// An INTEGER primary key is an alias for the rowid in rowid
// tables only: https://www.sqlite.org/lang_createtable.html
XCTAssertNil(primaryKey.rowIDColumn)
XCTAssertFalse(primaryKey.isRowID)
XCTAssertFalse(primaryKey.tableHasRowID)
}
}
func testIntegerPrimaryKeyWithoutRowID2() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
try db.execute(sql: "CREATE TABLE items (id INTEGER, name TEXT, PRIMARY KEY (id)) WITHOUT ROWID")
let primaryKey = try db.primaryKey("items")
XCTAssertEqual(primaryKey.columnInfos?.map(\.name), ["id"])
XCTAssertEqual(primaryKey.columnInfos?.map(\.type), ["INTEGER"])
XCTAssertEqual(primaryKey.columns, ["id"])
XCTAssertNil(primaryKey.rowIDColumn)
XCTAssertFalse(primaryKey.isRowID)
XCTAssertFalse(primaryKey.tableHasRowID)
}
}
func testNonRowIDPrimaryKeyWithoutRowID() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
@@ -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)
}
}
}
@@ -485,6 +485,7 @@ class UpdateStatementTests : GRDBTestCase {
}
try test(value: "Foo")
try test(value: "foo\u{0}bar") // https://sqlite.org/nulinstr.html
try test(value: "")
try test(value: "Hello".data(using: .utf8)!)
try test(value: Data())
@@ -519,6 +520,7 @@ class UpdateStatementTests : GRDBTestCase {
}
try test(value: "Foo")
try test(value: "foo\u{0}bar") // https://sqlite.org/nulinstr.html
try test(value: "")
try test(value: "Hello".data(using: .utf8)!)
try test(value: Data())
@@ -560,6 +562,7 @@ class UpdateStatementTests : GRDBTestCase {
}
try test(value: "Foo")
try test(value: "foo\u{0}bar") // https://sqlite.org/nulinstr.html
try test(value: "")
try test(value: "Hello".data(using: .utf8)!)
try test(value: Data())
@@ -596,6 +599,7 @@ class UpdateStatementTests : GRDBTestCase {
}
try test(value: "Foo")
try test(value: "foo\u{0}bar") // https://sqlite.org/nulinstr.html
try test(value: "")
try test(value: "Hello".data(using: .utf8)!)
try test(value: Data())
+2 -3
View File
@@ -45,9 +45,8 @@ class Issue1838Tests: GRDBTestCase {
}
// Make sure the interrupted state created by the FTS5 bug is no longer active.
let documentCount = try dbQueue.read { db in
try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM documents")!
try dbQueue.read { db in
try db.execute(sql: "SELECT COUNT(*) FROM documents")
}
XCTAssertEqual(documentCount, 0)
}
}
@@ -142,6 +142,64 @@ class DatabaseRegionTests : GRDBTestCase {
XCTAssertEqual(unions.map(\.description), ["foo(a)[1]", "foo(a,b)[1,2]", "foo(a,b)[1,2]", "foo(b)[2]"])
}
func testInsertTable() {
let regions = [
DatabaseRegion.fullDatabase,
DatabaseRegion(),
DatabaseRegion(table: "foo"),
DatabaseRegion(table: "foo", columns: ["a", "b"]),
DatabaseRegion(table: "foo", columns: ["b", "c"]),
DatabaseRegion(table: "foo", rowIds: [1, 2]),
DatabaseRegion(table: "foo", columns: ["b"], rowIds: [2, 3]),
DatabaseRegion(table: "bar")]
var unions: [DatabaseRegion] = []
for var region in regions {
region.insert(table: "foo")
unions.append(region)
}
XCTAssertEqual(unions.map(\.description), [
"full database",
"foo(*)",
"foo(*)",
"foo(*)",
"foo(*)",
"foo(*)",
"foo(*)",
"bar(*),foo(*)",
])
}
func testInsertTableColumn() {
let regions = [
DatabaseRegion.fullDatabase,
DatabaseRegion(),
DatabaseRegion(table: "foo"),
DatabaseRegion(table: "foo", columns: ["a", "b"]),
DatabaseRegion(table: "foo", columns: ["b", "c"]),
DatabaseRegion(table: "foo", rowIds: [1, 2]),
DatabaseRegion(table: "foo", columns: ["b"], rowIds: [2, 3]),
DatabaseRegion(table: "bar")]
var unions: [DatabaseRegion] = []
for var region in regions {
region.insert(table: "foo", column: "a")
unions.append(region)
}
XCTAssertEqual(unions.map(\.description), [
"full database",
"foo(a)",
"foo(*)",
"foo(a,b)",
"foo(a,b,c)",
"foo(*)",
"foo(a,b)",
"bar(*),foo(a)",
])
}
func testRegionIntersection() {
let regions = [
DatabaseRegion.fullDatabase,
@@ -2388,6 +2388,81 @@ extension MutablePersistableRecordTests {
}
}
func test_upsert_INTEGER_primary_key_WITHOUT_ROWID() throws {
#if GRDBCUSTOMSQLITE || SQLITE_HAS_CODEC
guard Database.sqliteLibVersionNumber >= 3035000 else {
throw XCTSkip("UPSERT is not available")
}
#else
guard #available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) else {
throw XCTSkip("UPSERT is not available")
}
#endif
struct MyRecord: Codable, FetchableRecord, MutablePersistableRecord {
var id: Int64
var name: String
}
try makeDatabaseQueue().write { db in
try db.execute(sql: """
CREATE TABLE myRecord(
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
) WITHOUT ROWID;
""")
var record = MyRecord(id: 1, name: "foo")
try record.upsert(db)
XCTAssertEqual(lastSQLQuery, """
INSERT INTO "myRecord" ("id", "name") \
VALUES (1,'foo') \
ON CONFLICT DO UPDATE SET "name" = "excluded"."name"
""")
}
}
func test_upsertAndFetch_INTEGER_primary_key_WITHOUT_ROWID() throws {
#if GRDBCUSTOMSQLITE || SQLITE_HAS_CODEC
guard Database.sqliteLibVersionNumber >= 3035000 else {
throw XCTSkip("UPSERT is not available")
}
#else
guard #available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) else {
throw XCTSkip("UPSERT is not available")
}
#endif
struct MyRecord: Codable, FetchableRecord, MutablePersistableRecord {
var id: Int64
var name: String
}
try makeDatabaseQueue().write { db in
try db.execute(sql: """
CREATE TABLE myRecord(
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
) WITHOUT ROWID;
""")
var record = MyRecord(id: 1, name: "foo")
let upserted = try record.upsertAndFetch(db)
// No rowid in the RETURNING clause: the table has none
XCTAssertEqual(lastSQLQuery, """
INSERT INTO "myRecord" ("id", "name") \
VALUES (1,'foo') \
ON CONFLICT DO UPDATE SET "name" = "excluded"."name" \
RETURNING *
""")
XCTAssertEqual(upserted.id, 1)
XCTAssertEqual(upserted.name, "foo")
}
}
func test_upsertAndFetch_do_update_set_where_with_default_strategy() throws {
#if GRDBCUSTOMSQLITE || SQLITE_HAS_CODEC
guard Database.sqliteLibVersionNumber >= 3035000 else {
@@ -2378,6 +2378,81 @@ extension PersistableRecordTests {
}
}
func test_upsert_INTEGER_primary_key_WITHOUT_ROWID() throws {
#if GRDBCUSTOMSQLITE || SQLITE_HAS_CODEC
guard Database.sqliteLibVersionNumber >= 3035000 else {
throw XCTSkip("UPSERT is not available")
}
#else
guard #available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) else {
throw XCTSkip("UPSERT is not available")
}
#endif
struct MyRecord: Codable, FetchableRecord, PersistableRecord {
var id: Int64
var name: String
}
try makeDatabaseQueue().write { db in
try db.execute(sql: """
CREATE TABLE myRecord(
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
) WITHOUT ROWID;
""")
let record = MyRecord(id: 1, name: "foo")
try record.upsert(db)
XCTAssertEqual(lastSQLQuery, """
INSERT INTO "myRecord" ("id", "name") \
VALUES (1,'foo') \
ON CONFLICT DO UPDATE SET "name" = "excluded"."name"
""")
}
}
func test_upsertAndFetch_INTEGER_primary_key_WITHOUT_ROWID() throws {
#if GRDBCUSTOMSQLITE || SQLITE_HAS_CODEC
guard Database.sqliteLibVersionNumber >= 3035000 else {
throw XCTSkip("UPSERT is not available")
}
#else
guard #available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) else {
throw XCTSkip("UPSERT is not available")
}
#endif
struct MyRecord: Codable, FetchableRecord, PersistableRecord {
var id: Int64
var name: String
}
try makeDatabaseQueue().write { db in
try db.execute(sql: """
CREATE TABLE myRecord(
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
) WITHOUT ROWID;
""")
let record = MyRecord(id: 1, name: "foo")
let upserted = try record.upsertAndFetch(db)
// No rowid in the RETURNING clause: the table has none
XCTAssertEqual(lastSQLQuery, """
INSERT INTO "myRecord" ("id", "name") \
VALUES (1,'foo') \
ON CONFLICT DO UPDATE SET "name" = "excluded"."name" \
RETURNING *
""")
XCTAssertEqual(upserted.id, 1)
XCTAssertEqual(upserted.name, "foo")
}
}
func test_upsertAndFetch_do_update_set_where_with_default_strategy() throws {
#if GRDBCUSTOMSQLITE || SQLITE_HAS_CODEC
guard Database.sqliteLibVersionNumber >= 3035000 else {
@@ -882,6 +882,370 @@ class SharedValueObservationTests: GRDBTestCase {
break
}
}
// Regression test: a client can be cancelled from another thread while
// the shared observation is notifying a fresh value.
//
// Notifying clients and cancelling clients must not happen under the
// same lock. Otherwise, a notification that waits for a resource held by
// the cancelling thread deadlocks. This happens in practice when a client
// feeds an AsyncThrowingStream: `yield` waits for the consumer task's
// status record lock, while `swift_task_cancel` holds that lock and runs
// the stream's `onTermination` handler, which cancels the client.
func test_cancellation_from_other_thread_during_value_notification() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.write { db in
try db.create(table: "player") { t in
t.autoIncrementedPrimaryKey("id")
}
}
let sharedObservation = ValueObservation
.tracking(Table("player").fetchCount)
.shared(
in: dbQueue,
scheduling: .async(onQueue: DispatchQueue(label: "SharedValueObservationTests")),
extent: .observationLifetime)
let cancellableMutex: Mutex<AnyDatabaseCancellable?> = Mutex(nil)
let cancellationDidCompleteMutex: Mutex<Bool?> = Mutex(nil)
let exp = expectation(description: "")
let cancellable = sharedObservation.start(
onError: { XCTFail("Unexpected error \($0)") },
onChange: { value in
guard value == 1 else { return }
cancellationDidCompleteMutex.store(
Self.cancelFromOtherThreadAndWait(cancellableMutex.load()))
exp.fulfill()
})
cancellableMutex.store(cancellable)
try dbQueue.write { try $0.execute(sql: "INSERT INTO player DEFAULT VALUES") }
wait(for: [exp], timeout: 5)
XCTAssertEqual(cancellationDidCompleteMutex.load(), true)
}
// Regression test: a client can be cancelled from another thread while
// the shared observation is notifying an error.
//
// See test_cancellation_from_other_thread_during_value_notification.
func test_cancellation_from_other_thread_during_error_notification() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.write { db in
try db.create(table: "player") { t in
t.autoIncrementedPrimaryKey("id")
}
}
let fetchErrorMutex: Mutex<Error?> = Mutex(nil)
let sharedObservation = ValueObservation
.tracking { db -> Int in
try fetchErrorMutex.withLock { error in
if let error { throw error }
}
return try Table("player").fetchCount(db)
}
.shared(
in: dbQueue,
scheduling: .async(onQueue: DispatchQueue(label: "SharedValueObservationTests")),
extent: .observationLifetime)
let cancellableMutex: Mutex<AnyDatabaseCancellable?> = Mutex(nil)
let cancellationDidCompleteMutex: Mutex<Bool?> = Mutex(nil)
let initialValueExp = expectation(description: "initial value")
let errorExp = expectation(description: "error")
let cancellable = sharedObservation.start(
onError: { _ in
cancellationDidCompleteMutex.store(
Self.cancelFromOtherThreadAndWait(cancellableMutex.load()))
errorExp.fulfill()
},
onChange: { _ in
initialValueExp.fulfill()
})
cancellableMutex.store(cancellable)
wait(for: [initialValueExp], timeout: 5)
fetchErrorMutex.store(TestError())
try dbQueue.write { try $0.execute(sql: "INSERT INTO player DEFAULT VALUES") }
wait(for: [errorExp], timeout: 5)
XCTAssertEqual(cancellationDidCompleteMutex.load(), true)
}
// Regression test: a client cancelled from another thread is not
// notified of a value that is being notified to other clients.
func test_no_value_notification_after_cancellation_from_other_thread() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.write { db in
try db.create(table: "player") { t in
t.autoIncrementedPrimaryKey("id")
}
}
let queue = DispatchQueue(label: "SharedValueObservationTests")
let sharedObservation = ValueObservation
.tracking(Table("player").fetchCount)
.shared(
in: dbQueue,
scheduling: .async(onQueue: queue),
extent: .observationLifetime)
let initialValuesExp = expectation(description: "initial values")
initialValuesExp.expectedFulfillmentCount = 2
let notificationDidStartSemaphore = DispatchSemaphore(value: 0)
let cancellationDidCompleteSemaphore = DispatchSemaphore(value: 0)
// Client 1 blocks the notification of the second value until
// client 2 is cancelled.
let cancellable1 = sharedObservation.start(
onError: { XCTFail("Unexpected error \($0)") },
onChange: { value in
if value == 0 {
initialValuesExp.fulfill()
} else {
notificationDidStartSemaphore.signal()
_ = cancellationDidCompleteSemaphore.wait(timeout: .now() + 5)
}
})
let values2Mutex: Mutex<[Int]> = Mutex([])
let cancellable2 = sharedObservation.start(
onError: { XCTFail("Unexpected error \($0)") },
onChange: { value in
values2Mutex.withLock { $0.append(value) }
if value == 0 {
initialValuesExp.fulfill()
}
})
wait(for: [initialValuesExp], timeout: 5)
try dbQueue.write { try $0.execute(sql: "INSERT INTO player DEFAULT VALUES") }
XCTAssertEqual(notificationDidStartSemaphore.wait(timeout: .now() + 5), .success)
cancellable2.cancel()
cancellationDidCompleteSemaphore.signal()
queue.sync { }
XCTAssertEqual(values2Mutex.load(), [0])
withExtendedLifetime(cancellable1) { }
}
// Regression test: a client cancelled from another thread is not
// notified of an error that is being notified to other clients.
func test_no_error_notification_after_cancellation_from_other_thread() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.write { db in
try db.create(table: "player") { t in
t.autoIncrementedPrimaryKey("id")
}
}
let fetchErrorMutex: Mutex<Error?> = Mutex(nil)
let queue = DispatchQueue(label: "SharedValueObservationTests")
let sharedObservation = ValueObservation
.tracking { db -> Int in
try fetchErrorMutex.withLock { error in
if let error { throw error }
}
return try Table("player").fetchCount(db)
}
.shared(
in: dbQueue,
scheduling: .async(onQueue: queue),
extent: .observationLifetime)
let initialValuesExp = expectation(description: "initial values")
initialValuesExp.expectedFulfillmentCount = 2
let notificationDidStartSemaphore = DispatchSemaphore(value: 0)
let cancellationDidCompleteSemaphore = DispatchSemaphore(value: 0)
// Client 1 blocks the notification of the error until
// client 2 is cancelled.
let cancellable1 = sharedObservation.start(
onError: { _ in
notificationDidStartSemaphore.signal()
_ = cancellationDidCompleteSemaphore.wait(timeout: .now() + 5)
},
onChange: { _ in
initialValuesExp.fulfill()
})
let error2Mutex: Mutex<Error?> = Mutex(nil)
let cancellable2 = sharedObservation.start(
onError: { error in
error2Mutex.store(error)
},
onChange: { _ in
initialValuesExp.fulfill()
})
wait(for: [initialValuesExp], timeout: 5)
fetchErrorMutex.store(TestError())
try dbQueue.write { try $0.execute(sql: "INSERT INTO player DEFAULT VALUES") }
XCTAssertEqual(notificationDidStartSemaphore.wait(timeout: .now() + 5), .success)
cancellable2.cancel()
cancellationDidCompleteSemaphore.signal()
queue.sync { }
XCTAssertNil(error2Mutex.load())
withExtendedLifetime(cancellable1) { }
}
// Regression test: a client cancelled before the last value is
// notified as its initial value is not notified.
func test_no_initial_value_notification_after_cancellation() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.write { db in
try db.create(table: "player") { t in
t.autoIncrementedPrimaryKey("id")
}
}
let queue = DispatchQueue(label: "SharedValueObservationTests")
let sharedObservation = ValueObservation
.tracking(Table("player").fetchCount)
.shared(
in: dbQueue,
scheduling: .async(onQueue: queue),
extent: .observationLifetime)
let initialValueExp = expectation(description: "initial value")
let cancellable1 = sharedObservation.start(
onError: { XCTFail("Unexpected error \($0)") },
onChange: { _ in initialValueExp.fulfill() })
wait(for: [initialValueExp], timeout: 5)
// Block the queue until client 2 is cancelled.
let cancellationDidCompleteSemaphore = DispatchSemaphore(value: 0)
queue.async {
_ = cancellationDidCompleteSemaphore.wait(timeout: .now() + 5)
}
let values2Mutex: Mutex<[Int]> = Mutex([])
let cancellable2 = sharedObservation.start(
onError: { XCTFail("Unexpected error \($0)") },
onChange: { value in
values2Mutex.withLock { $0.append(value) }
})
cancellable2.cancel()
cancellationDidCompleteSemaphore.signal()
queue.sync { }
XCTAssertEqual(values2Mutex.load(), [])
withExtendedLifetime(cancellable1) { }
}
// Regression test: a shared observation that is restarted while the
// failed upstream observation is starting keeps on observing
// the database.
func test_restart_during_failed_start_whileObserved() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.write { db in
try db.create(table: "player") { t in
t.autoIncrementedPrimaryKey("id")
}
}
// The first fetch fails
let fetchCountMutex = Mutex(0)
let sharedObservation = ValueObservation
.tracking { db -> Int in
if fetchCountMutex.increment() == 1 {
throw TestError()
}
return try Table("player").fetchCount(db)
}
.shared(
in: dbQueue,
scheduling: .immediate,
extent: .whileObserved)
let cancellable2Mutex: Mutex<AnyDatabaseCancellable?> = Mutex(nil)
let values2Mutex: Mutex<[Int]> = Mutex([])
let value2Exp = expectation(description: "value")
let cancellable1 = sharedObservation.start(
onError: { _ in
// Restart the shared observation from the immediate
// error notification, before the failed upstream
// observation has returned its cancellable.
let cancellable2 = sharedObservation.start(
onError: { XCTFail("Unexpected error \($0)") },
onChange: { value in
values2Mutex.withLock { $0.append(value) }
if value == 1 {
value2Exp.fulfill()
}
})
cancellable2Mutex.store(cancellable2)
},
onChange: { _ in XCTFail("Unexpected value") })
XCTAssertEqual(values2Mutex.load(), [0])
try dbQueue.write { try $0.execute(sql: "INSERT INTO player DEFAULT VALUES") }
wait(for: [value2Exp], timeout: 5)
XCTAssertEqual(values2Mutex.load(), [0, 1])
withExtendedLifetime(cancellable1) { }
withExtendedLifetime(cancellable2Mutex) { }
}
// Regression test: the last value, notified as the initial value of a
// new client, is not notified after a fresher value.
func test_initial_value_is_not_notified_after_fresher_value() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.write { db in
try db.create(table: "player") { t in
t.autoIncrementedPrimaryKey("id")
}
}
let scheduler = ManualScheduler()
let sharedObservation = ValueObservation
.tracking(Table("player").fetchCount)
.shared(
in: dbQueue,
scheduling: scheduler,
extent: .observationLifetime)
// Client 1 makes 0 the last value.
let values1Mutex: Mutex<[Int]> = Mutex([])
let cancellable1 = sharedObservation.start(
onError: { XCTFail("Unexpected error \($0)") },
onChange: { value in values1Mutex.withLock { $0.append(value) } })
XCTAssertTrue(scheduler.waitForScheduledActions(count: 1))
scheduler.runScheduledActions()
XCTAssertEqual(values1Mutex.load(), [0])
// The fresh value 1 is scheduled, but not notified yet.
try dbQueue.write { try $0.execute(sql: "INSERT INTO player DEFAULT VALUES") }
XCTAssertTrue(scheduler.waitForScheduledActions(count: 1))
// Client 2 starts. The last value 0 is scheduled as its initial
// value, after the fresh value 1.
let values2Mutex: Mutex<[Int]> = Mutex([])
let cancellable2 = sharedObservation.start(
onError: { XCTFail("Unexpected error \($0)") },
onChange: { value in values2Mutex.withLock { $0.append(value) } })
XCTAssertTrue(scheduler.waitForScheduledActions(count: 2))
scheduler.runScheduledActions()
XCTAssertEqual(values1Mutex.load(), [0, 1])
XCTAssertEqual(values2Mutex.load(), [1])
withExtendedLifetime((cancellable1, cancellable2)) { }
}
/// Cancels `cancellable` from another thread, and returns whether the
/// cancellation completed before a timeout.
///
/// Waiting for the other thread models a notification callback that
/// waits for a resource held by a thread that is cancelling the client.
/// A timeout reveals a deadlock, without hanging the test suite.
private static func cancelFromOtherThreadAndWait(_ cancellable: AnyDatabaseCancellable?) -> Bool {
let semaphore = DispatchSemaphore(value: 0)
DispatchQueue.global().async {
cancellable?.cancel()
semaphore.signal()
}
return semaphore.wait(timeout: .now() + 1) == .success
}
}
private class Log: TextOutputStream {
@@ -905,3 +1269,41 @@ private class Log: TextOutputStream {
}
private struct TestError: Error { }
/// A scheduler that runs scheduled actions on demand.
private final class ManualScheduler: ValueObservationScheduler, @unchecked Sendable {
private let condition = NSCondition()
private var actions: [@Sendable () -> Void] = [] // protected by condition
func immediateInitialValue() -> Bool { false }
func schedule(_ action: @escaping @Sendable () -> Void) {
condition.lock()
actions.append(action)
condition.broadcast()
condition.unlock()
}
/// Waits until `count` actions are scheduled, and returns whether
/// they were scheduled before a timeout.
func waitForScheduledActions(count: Int) -> Bool {
let deadline = Date(timeIntervalSinceNow: 5)
condition.lock()
defer { condition.unlock() }
while actions.count < count {
if !condition.wait(until: deadline) { return false }
}
return true
}
/// Runs scheduled actions, in order.
func runScheduledActions() {
condition.lock()
let actions = self.actions
self.actions = []
condition.unlock()
for action in actions {
action()
}
}
}
@@ -0,0 +1,172 @@
import XCTest
import GRDB
/// Here we test the decoding of string columns. The other fetch tests in
/// this target use an all-INT model, and never decode a string.
class FetchStringTests: XCTestCase {
static let shortString = "foo"
static let longString = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus consectetur felis eget nibh aliquet ullamcorper. Nam sodales, tellus a cursus tincidunt, arcu purus suscipit elit, nec congue erat ipsum a purus."
static let expectedRowCount = 200_000
static let personRowCount = 100_000
static let recordFetchesPerIteration = 5
static let fetchesPerIteration = 10
private struct Person: Codable, FetchableRecord, PersistableRecord {
static let databaseTableName = "person"
var firstName: String
var lastName: String
var email: String
var address: String
var age: Int
var height: Double
}
func test_shortString_fetch_performance() throws {
try measureFetch(of: String.self, from: "short")
}
func test_longString_fetch_performance() throws {
try measureFetch(of: String.self, from: "long")
}
func test_shortString_databaseValue_performance() throws {
try measureFetch(of: DatabaseValue.self, from: "short")
}
func test_longString_databaseValue_performance() throws {
try measureFetch(of: DatabaseValue.self, from: "long")
}
func test_shortString_as_argument() throws {
let dbQueue = try DatabaseQueue()
try dbQueue.write { db in
let statement = try db.makeStatement(sql: "SELECT ? WHERE 0")
measure {
for _ in 0..<100_000 {
try! statement.execute(arguments: [FetchStringTests.shortString])
}
}
}
}
func test_longString_as_argument() throws {
let dbQueue = try DatabaseQueue()
try dbQueue.write { db in
let statement = try db.makeStatement(sql: "SELECT ? WHERE 0")
measure {
for _ in 0..<100_000 {
try! statement.execute(arguments: [FetchStringTests.longString])
}
}
}
}
func test_record_fetch_performance() throws {
let dbQueue = try makePersonDatabase()
try dbQueue.read { db in
measure {
for _ in 0..<Self.recordFetchesPerIteration {
let people = try! Person.fetchCursor(db)
var count = 0
while try! people.next() != nil {
count += 1
}
XCTAssertEqual(count, Self.personRowCount)
}
}
}
}
private func measureFetch<Value: DatabaseValueConvertible & StatementColumnConvertible>(
of type: Value.Type,
from column: String) throws
{
let dbQueue = try makeDatabaseQueue()
try dbQueue.read { db in
measure {
for _ in 0..<Self.fetchesPerIteration {
let values = try! Value.fetchCursor(db, sql: "SELECT \(column) FROM item")
var count = 0
while try! values.next() != nil {
count += 1
}
XCTAssertEqual(count, Self.expectedRowCount)
}
}
}
}
private func measureFetch<Value: DatabaseValueConvertible>(
of type: Value.Type,
from column: String) throws
{
let dbQueue = try makeDatabaseQueue()
try dbQueue.read { db in
measure {
for _ in 0..<Self.fetchesPerIteration {
let values = try! Value.fetchCursor(db, sql: "SELECT \(column) FROM item")
var count = 0
while try! values.next() != nil {
count += 1
}
XCTAssertEqual(count, Self.expectedRowCount)
}
}
}
}
private func makePersonDatabase() throws -> DatabaseQueue {
let url = URL(fileURLWithPath: NSTemporaryDirectory())
.appendingPathComponent("GRDBPerformancePeople.sqlite")
let dbQueue = try DatabaseQueue(path: url.path)
try dbQueue.write { db in
if try db.tableExists("person"),
try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM person") == Self.personRowCount
{
return
}
try db.execute(sql: "DROP TABLE IF EXISTS person")
try db.create(table: "person") { table in
table.column("firstName", .text)
table.column("lastName", .text)
table.column("email", .text)
table.column("address", .text)
table.column("age", .integer)
table.column("height", .double)
}
let statement = try db.makeStatement(sql: """
INSERT INTO person (firstName, lastName, email, address, age, height)
VALUES (?, ?, ?, ?, ?, ?)
""")
for index in 0..<Self.personRowCount {
try statement.execute(arguments: [
"Arthur", "Dubois", "arthur.dubois@example.com",
"12 Rue de la Paix, 75002 Paris", 20 + index % 60, 1.60 + Double(index % 40) / 100,
])
}
}
return dbQueue
}
private func makeDatabaseQueue() throws -> DatabaseQueue {
let url = URL(fileURLWithPath: NSTemporaryDirectory())
.appendingPathComponent("GRDBPerformanceStrings.sqlite")
let dbQueue = try DatabaseQueue(path: url.path)
try dbQueue.write { db in
if try db.tableExists("item"),
try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM item") == Self.expectedRowCount
{
return
}
try db.execute(sql: "DROP TABLE IF EXISTS item")
try db.execute(sql: "CREATE TABLE item (short TEXT, long TEXT)")
let statement = try db.makeStatement(sql: "INSERT INTO item (short, long) VALUES (?, ?)")
for _ in 0..<Self.expectedRowCount {
try statement.execute(arguments: [FetchStringTests.shortString, FetchStringTests.longString])
}
}
return dbQueue
}
}
@@ -0,0 +1,60 @@
import XCTest
import GRDB
/// Here we test the return of string values from a custom SQL function.
class FunctionResultTests: XCTestCase {
private let rowCount = 100_000
private let queriesPerIteration = 20
var dbDirectoryPath: String!
var dbQueue: DatabaseQueue!
static let echo = DatabaseFunction("echo", argumentCount: 1, pure: true) { dbValues in
String.fromDatabaseValue(dbValues[0])
}
override func setUpWithError() throws {
let dbDirectoryName = "FunctionResultTests-\(ProcessInfo.processInfo.globallyUniqueString)"
dbDirectoryPath = (NSTemporaryDirectory() as NSString).appendingPathComponent(dbDirectoryName)
try FileManager.default.createDirectory(atPath: dbDirectoryPath, withIntermediateDirectories: true)
let dbPath = (dbDirectoryPath as NSString).appendingPathComponent("db.sqlite")
var configuration = Configuration()
configuration.prepareDatabase { db in
db.add(function: Self.echo)
}
dbQueue = try DatabaseQueue(path: dbPath, configuration: configuration)
}
override func tearDownWithError() throws {
dbQueue = nil
try FileManager.default.removeItem(atPath: dbDirectoryPath)
}
func test_shortString_functionResult_performance() throws {
try measureFunctionResults(over: ArgumentsTests.shortString)
}
func test_longString_functionResult_performance() throws {
try measureFunctionResults(over: ArgumentsTests.longString)
}
private func measureFunctionResults(over string: String) throws {
try dbQueue.write { db in
try db.execute(sql: "CREATE TABLE t(a TEXT)")
let statement = try db.makeStatement(sql: "INSERT INTO t(a) VALUES (?)")
for _ in 0..<rowCount {
try statement.execute(arguments: [string])
}
}
measure {
try! dbQueue.read { db in
for _ in 0..<queriesPerIteration {
_ = try Int.fetchOne(db, sql: "SELECT COUNT(echo(a)) FROM t")
}
}
}
}
}
@@ -31,10 +31,14 @@
5690AFDC212058CB001530EA /* FetchRecordDecodableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5690AFDA212058CB001530EA /* FetchRecordDecodableTests.swift */; };
56A3FEAA27A85F4800B0292E /* RealmSwift in Frameworks */ = {isa = PBXBuildFile; productRef = 56A3FEA927A85F4800B0292E /* RealmSwift */; };
56A6F4AC29BBC8E200E22662 /* ArgumentsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56A6F4AB29BBC8E200E22662 /* ArgumentsTests.swift */; };
AA1F0001000000000000E001 /* FunctionResultTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA1F0002000000000000E002 /* FunctionResultTests.swift */; };
AA1F0003000000000000E003 /* FetchStringTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA1F0004000000000000E004 /* FetchStringTests.swift */; };
56B6D0E52618BF78003CC455 /* FetchRecordOptimizedTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B6D0E42618BF78003CC455 /* FetchRecordOptimizedTests.swift */; };
56B6D0E62618BF78003CC455 /* FetchRecordOptimizedTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B6D0E42618BF78003CC455 /* FetchRecordOptimizedTests.swift */; };
56B6D0EA2618C00C003CC455 /* InsertRecordOptimizedTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B6D0E92618C00C003CC455 /* InsertRecordOptimizedTests.swift */; };
56B6D0EB2618C00C003CC455 /* InsertRecordOptimizedTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B6D0E92618C00C003CC455 /* InsertRecordOptimizedTests.swift */; };
56B7DECA3063F1B50018462B /* StatementPreparationPerformanceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B7DEC83063F1B30018462B /* StatementPreparationPerformanceTests.swift */; };
56B7DECB3063F1B50018462B /* StatementPreparationPerformanceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56B7DEC83063F1B30018462B /* StatementPreparationPerformanceTests.swift */; };
56D3BE711F4EB1A00034C6D2 /* FetchRecordStructTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56D3BE701F4EB1900034C6D2 /* FetchRecordStructTests.swift */; };
56D3BE721F4EB1A00034C6D2 /* FetchRecordStructTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56D3BE701F4EB1900034C6D2 /* FetchRecordStructTests.swift */; };
56D507831F6D7B2E00AE1C5B /* InsertRecordStructTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56D507821F6D7A4500AE1C5B /* InsertRecordStructTests.swift */; };
@@ -98,8 +102,11 @@
5690AFD72120589A001530EA /* InsertRecordEncodableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InsertRecordEncodableTests.swift; sourceTree = "<group>"; };
5690AFDA212058CB001530EA /* FetchRecordDecodableTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchRecordDecodableTests.swift; sourceTree = "<group>"; };
56A6F4AB29BBC8E200E22662 /* ArgumentsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArgumentsTests.swift; sourceTree = "<group>"; };
AA1F0002000000000000E002 /* FunctionResultTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FunctionResultTests.swift; sourceTree = "<group>"; };
AA1F0004000000000000E004 /* FetchStringTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FetchStringTests.swift; sourceTree = "<group>"; };
56B6D0E42618BF78003CC455 /* FetchRecordOptimizedTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchRecordOptimizedTests.swift; sourceTree = "<group>"; };
56B6D0E92618C00C003CC455 /* InsertRecordOptimizedTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InsertRecordOptimizedTests.swift; sourceTree = "<group>"; };
56B7DEC83063F1B30018462B /* StatementPreparationPerformanceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatementPreparationPerformanceTests.swift; sourceTree = "<group>"; };
56BB86121BA9886D001F9168 /* InsertRecordClassTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InsertRecordClassTests.swift; sourceTree = "<group>"; };
56BB862D1BA98933001F9168 /* GRDBPerformanceComparisonTests-Bridging.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "GRDBPerformanceComparisonTests-Bridging.h"; sourceTree = "<group>"; };
56CA22211BB41565009A04C5 /* PerformanceTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PerformanceTests.swift; sourceTree = "<group>"; };
@@ -158,9 +165,12 @@
56BB86101BA9886D001F9168 /* Performance */ = {
isa = PBXGroup;
children = (
56B7DEC83063F1B30018462B /* StatementPreparationPerformanceTests.swift */,
56BB862D1BA98933001F9168 /* GRDBPerformanceComparisonTests-Bridging.h */,
56DE7B2D1C42B23B00861EB8 /* PerformanceModel.xcdatamodeld */,
56A6F4AB29BBC8E200E22662 /* ArgumentsTests.swift */,
AA1F0002000000000000E002 /* FunctionResultTests.swift */,
AA1F0004000000000000E004 /* FetchStringTests.swift */,
567071FA208A509C006AD95A /* DateParsingTests.swift */,
56DE7B271C41302500861EB8 /* FetchNamedValuesTests.swift */,
56DE7B291C4130AF00861EB8 /* FetchPositionalValuesTests.swift */,
@@ -338,6 +348,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
56B7DECA3063F1B50018462B /* StatementPreparationPerformanceTests.swift in Sources */,
56D3BE711F4EB1A00034C6D2 /* FetchRecordStructTests.swift in Sources */,
565BC5E82517645D00D2B53E /* Generation.swift in Sources */,
560C98231C0E23BB00BF8471 /* InsertRecordClassTests.swift in Sources */,
@@ -347,6 +358,8 @@
5690AFD82120589A001530EA /* InsertRecordEncodableTests.swift in Sources */,
56B6D0EA2618C00C003CC455 /* InsertRecordOptimizedTests.swift in Sources */,
56A6F4AC29BBC8E200E22662 /* ArgumentsTests.swift in Sources */,
AA1F0001000000000000E001 /* FunctionResultTests.swift in Sources */,
AA1F0003000000000000E003 /* FetchStringTests.swift in Sources */,
5690AFDB212058CB001530EA /* FetchRecordDecodableTests.swift in Sources */,
56D507831F6D7B2E00AE1C5B /* InsertRecordStructTests.swift in Sources */,
56DE7B241C412F7E00861EB8 /* InsertPositionalValuesTests.swift in Sources */,
@@ -365,6 +378,7 @@
565BC5E92517645D00D2B53E /* Generation.swift in Sources */,
56439B361F4CA1DC0066043F /* FetchRecordClassTests.swift in Sources */,
56439B381F4CA1DC0066043F /* FetchNamedValuesTests.swift in Sources */,
56B7DECB3063F1B50018462B /* StatementPreparationPerformanceTests.swift in Sources */,
56439B391F4CA1DC0066043F /* InsertPositionalValuesTests.swift in Sources */,
56D507841F6D7B2F00AE1C5B /* InsertRecordStructTests.swift in Sources */,
5690AFD92120589A001530EA /* InsertRecordEncodableTests.swift in Sources */,
@@ -0,0 +1,75 @@
import XCTest
import GRDB
// Inspired by <https://github.com/groue/GRDB.swift/pull/1873>
class StatementPreparationPerformanceTests: XCTestCase {
let iterationCount = 10_000
func testStatementPreparationPerformance_wide() throws {
let dbQueue = try makeTestDatabase()
dbQueue.inDatabase { db in
measure {
for _ in 0..<iterationCount {
_ = try! db.makeStatement(sql: "SELECT * FROM wide")
}
}
}
}
func testStatementPreparationPerformance_join() throws {
let dbQueue = try makeTestDatabase()
dbQueue.inDatabase { db in
measure {
for _ in 0..<iterationCount {
_ = try! db.makeStatement(sql: """
SELECT author.*, book.*, review.*
FROM author
JOIN book ON book.authorId = author.id
JOIN review ON review.bookId = book.id
""")
}
}
}
}
func testStatementPreparationPerformance_narrow() throws {
let dbQueue = try makeTestDatabase()
dbQueue.inDatabase { db in
measure {
for _ in 0..<iterationCount {
_ = try! db.makeStatement(sql: "SELECT id FROM wide")
}
}
}
}
private func makeTestDatabase() throws -> DatabaseQueue {
let dbQueue = try DatabaseQueue()
try dbQueue.write { db in
try db.execute(sql: createTableSQL(
table: "wide",
columns: ["id INTEGER PRIMARY KEY"] + integerColumns(count: 119)))
try db.execute(sql: createTableSQL(
table: "author",
columns: ["id INTEGER PRIMARY KEY"] + integerColumns(count: 39)))
try db.execute(sql: createTableSQL(
table: "book",
columns: ["id INTEGER PRIMARY KEY", "authorId INTEGER"]
+ integerColumns(count: 38)))
try db.execute(sql: createTableSQL(
table: "review",
columns: ["id INTEGER PRIMARY KEY", "bookId INTEGER"]
+ integerColumns(count: 38)))
}
return dbQueue
}
private func createTableSQL(table: String, columns: [String]) -> String {
"CREATE TABLE \(table) (\(columns.joined(separator: ", ")))"
}
private func integerColumns(count: Int) -> [String] {
(1...count).map { "column\($0) INTEGER" }
}
}