mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
Merge branch 'development'
This commit is contained in:
commit
5475dc98dc
10 files changed
+142
-34
No files matched your search
@@ -7,6 +7,7 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception:
|
||||
|
||||
#### 6.x Releases
|
||||
|
||||
- `6.12.x` Releases - [6.12.0](#6120)
|
||||
- `6.11.x` Releases - [6.11.0](#6110)
|
||||
- `6.10.x` Releases - [6.10.0](#6100) - [6.10.1](#6101) - [6.10.2](#6102)
|
||||
- `6.9.x` Releases - [6.9.0](#690) - [6.9.1](#691) - [6.9.2](#692)
|
||||
@@ -109,6 +110,13 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception:
|
||||
|
||||
---
|
||||
|
||||
## 6.12.0
|
||||
|
||||
Released April 29, 2023 • [diff](https://github.com/groue/GRDB.swift/compare/v6.11.0...v6.12.0)
|
||||
|
||||
- **Fixed**: [#1364](https://github.com/groue/GRDB.swift/pull/1364) Fix a regression introduced with version 6.10.0, where starting multiple observations while performing writes could create a deadlock.
|
||||
- **New**: `FetchableRecordDecoder` is an object that decodes `Decodable` fetchable records from database rows. You may want to use this decoder when you want to use the `Decodable` initializer from `init(row: Row)`.
|
||||
|
||||
## 6.11.0
|
||||
|
||||
Released April 17, 2023 • [diff](https://github.com/groue/GRDB.swift/compare/v6.10.2...v6.11.0)
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
Pod::Spec.new do |s|
|
||||
s.name = 'GRDB.swift'
|
||||
s.version = '6.11.0'
|
||||
s.version = '6.12.0'
|
||||
|
||||
s.license = { :type => 'MIT', :file => 'LICENSE' }
|
||||
s.summary = 'A toolkit for SQLite databases, with a focus on application development.'
|
||||
|
||||
@@ -94,6 +94,7 @@ let SQLITE_TRANSIENT = unsafeBitCast(OpaquePointer(bitPattern: -1), to: sqlite3_
|
||||
///
|
||||
/// ### Other Database Operations
|
||||
///
|
||||
/// - ``add(tokenizer:)``
|
||||
/// - ``backup(to:pagesPerStep:progress:)``
|
||||
/// - ``checkpoint(_:on:)``
|
||||
/// - ``clearSchemaCache()``
|
||||
|
||||
@@ -365,6 +365,7 @@ extension Team: TableRecord {
|
||||
- ``Database/create(virtualTable:ifNotExists:using:_:)``
|
||||
- ``Database/drop(table:)``
|
||||
- ``Database/dropFTS4SynchronizationTriggers(forTable:)``
|
||||
- ``Database/dropFTS5SynchronizationTriggers(forTable:)``
|
||||
- ``Database/rename(table:to:)``
|
||||
- ``Database/ColumnType``
|
||||
- ``Database/ConflictResolution``
|
||||
|
||||
@@ -61,7 +61,7 @@ extension Database {
|
||||
///
|
||||
/// class MyTokenizer : FTS5CustomTokenizer { ... }
|
||||
/// db.add(tokenizer: MyTokenizer.self)
|
||||
public func add<Tokenizer: FTS5CustomTokenizer>(tokenizer: Tokenizer.Type) {
|
||||
public func add(tokenizer: (some FTS5CustomTokenizer).Type) {
|
||||
let api = FTS5.api(self)
|
||||
|
||||
// Swift won't let the @convention(c) xCreate() function below create
|
||||
@@ -76,7 +76,7 @@ extension Database {
|
||||
return SQLITE_ERROR
|
||||
}
|
||||
do {
|
||||
let tokenizer = try Tokenizer(db: db, arguments: arguments)
|
||||
let tokenizer = try tokenizer.init(db: db, arguments: arguments)
|
||||
|
||||
// Tokenizer must remain alive until xDeleteTokenizer()
|
||||
// is called, as the xDelete member of xTokenizer
|
||||
@@ -158,7 +158,7 @@ extension Database {
|
||||
let code = withUnsafeMutablePointer(to: &xTokenizer) { xTokenizerPointer in
|
||||
api.pointee.xCreateTokenizer(
|
||||
UnsafeMutablePointer(mutating: api),
|
||||
Tokenizer.name,
|
||||
tokenizer.name,
|
||||
constructorPointer,
|
||||
xTokenizerPointer,
|
||||
deleteConstructor)
|
||||
|
||||
@@ -3,16 +3,71 @@ import Foundation
|
||||
extension FetchableRecord where Self: Decodable {
|
||||
/// Creates a record from `row`, using the `Decodable` conformance.
|
||||
public init(row: Row) throws {
|
||||
self = try RowDecoder().decode(from: row)
|
||||
self = try FetchableRecordDecoder().decode(Self.self, from: row)
|
||||
}
|
||||
}
|
||||
|
||||
// For testability. Not intended to become public as long as FetchableRecord has
|
||||
// a non-throwing row initializer, since this would open an undesired door.
|
||||
class RowDecoder {
|
||||
init() { }
|
||||
/// An object that decodes fetchable records from database rows.
|
||||
///
|
||||
/// The example below shows how to decode an instance of a simple `Player`
|
||||
/// type, that conforms to both ``FetchableRecord`` and `Decodable`, from a
|
||||
/// database row.
|
||||
///
|
||||
/// ```swift
|
||||
/// struct Player: FetchableRecord, Decodable {
|
||||
/// var id: Int64
|
||||
/// var name: String
|
||||
/// var score: Int
|
||||
/// }
|
||||
///
|
||||
/// try dbQueue.read { db in
|
||||
/// if let row = try Row.fetchOne(db, sql: "SELECT * FROM player WHERE id = 42") {
|
||||
/// let decoder = FetchableRecordDecoder()
|
||||
/// let player = try decoder.decode(Player.self, from: row)
|
||||
/// print(player.name)
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// You will generally not need to create an instance of
|
||||
/// `FetchableRecordDecoder`. The above sample code is correct, but you will
|
||||
/// generally write instead:
|
||||
///
|
||||
/// ```swift
|
||||
/// try dbQueue.read { db in
|
||||
/// // Prefer the init(row:) initializer:
|
||||
/// if let row = try Row.fetchOne(db, sql: "SELECT * FROM player WHERE id = 42") {
|
||||
/// let player = try Player(row: row)
|
||||
/// print(player.name)
|
||||
/// }
|
||||
///
|
||||
/// // OR just directly fetch a player:
|
||||
/// if let player = try Player.fetchOne(db, sql: "SELECT * FROM player WHERE id = 42") {
|
||||
/// print(player.name)
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// The behavior of the decoder depends on the decoded type. See:
|
||||
///
|
||||
/// - ``FetchableRecord/databaseColumnDecodingStrategy-6uefz``
|
||||
/// - ``FetchableRecord/databaseDateDecodingStrategy-78y03``
|
||||
/// - ``FetchableRecord/databaseDecodingUserInfo-77jim``
|
||||
/// - ``FetchableRecord/databaseJSONDecoder(for:)-7lmxd``
|
||||
public class FetchableRecordDecoder {
|
||||
/// Creates a decoder for fetchable records.
|
||||
public init() { }
|
||||
|
||||
func decode<T: FetchableRecord & Decodable>(_ type: T.Type = T.self, from row: Row) throws -> T {
|
||||
/// Returns a record of the type you specify, decoded from a
|
||||
/// database row.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - type: The type of the record to decode from the supplied
|
||||
/// database row.
|
||||
/// - row: The database row to decode.
|
||||
/// - Returns: An instance of the specified record type, if the decoder
|
||||
/// can parse the database row.
|
||||
public func decode<T: FetchableRecord & Decodable>(_ type: T.Type, from row: Row) throws -> T {
|
||||
let decoder = _RowDecoder<T>(row: row, codingPath: [], columnDecodingStrategy: T.databaseColumnDecodingStrategy)
|
||||
return try T(from: decoder)
|
||||
}
|
||||
|
||||
@@ -98,6 +98,7 @@ import Foundation
|
||||
/// ### Supporting Types
|
||||
///
|
||||
/// - ``RecordCursor``
|
||||
/// - ``FetchableRecordDecoder``
|
||||
public protocol FetchableRecord {
|
||||
|
||||
// MARK: - Row Decoding
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
<p align="center">
|
||||
<strong>A toolkit for SQLite databases, with a focus on application development</strong><br>
|
||||
Proudly serving the community since 2017
|
||||
Proudly serving the community since 2015
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
@@ -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**: April 10, 2023 • [version 6.11.0](https://github.com/groue/GRDB.swift/tree/v6.11.0) • [CHANGELOG](CHANGELOG.md) • [Migrating From GRDB 5 to GRDB 6](Documentation/GRDB6MigrationGuide.md)
|
||||
**Latest release**: April 29, 2023 • [version 6.12.0](https://github.com/groue/GRDB.swift/tree/v6.12.0) • [CHANGELOG](CHANGELOG.md) • [Migrating From GRDB 5 to GRDB 6](Documentation/GRDB6MigrationGuide.md)
|
||||
|
||||
**Requirements**: iOS 11.0+ / macOS 10.13+ / tvOS 11.0+ / watchOS 4.0+ • SQLite 3.19.3+ • Swift 5.7+ / Xcode 14+
|
||||
|
||||
|
||||
+1
-1
@@ -15,7 +15,7 @@
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>FMWK</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>6.11.0</string>
|
||||
<string>6.12.0</string>
|
||||
<key>CFBundleSignature</key>
|
||||
<string>????</string>
|
||||
<key>CFBundleVersion</key>
|
||||
|
||||
@@ -778,7 +778,7 @@ extension FetchableRecordDecodableTests {
|
||||
}
|
||||
|
||||
do {
|
||||
let record = try RowDecoder().decode(Record.self, from: [
|
||||
let record = try FetchableRecordDecoder().decode(Record.self, from: [
|
||||
"requiredId": 1,
|
||||
"optionalName": "test1",
|
||||
"requiredDates": "[128000]",
|
||||
@@ -794,7 +794,7 @@ extension FetchableRecordDecodableTests {
|
||||
}
|
||||
|
||||
do {
|
||||
let record = try RowDecoder().decode(Record.self, from: [
|
||||
let record = try FetchableRecordDecoder().decode(Record.self, from: [
|
||||
"REQUIREDID": 1,
|
||||
"OPTIONALNAME": "test1",
|
||||
"REQUIREDDATES": "[128000]",
|
||||
@@ -810,7 +810,7 @@ extension FetchableRecordDecodableTests {
|
||||
}
|
||||
|
||||
do {
|
||||
let record = try RowDecoder().decode(Record.self, from: [
|
||||
let record = try FetchableRecordDecoder().decode(Record.self, from: [
|
||||
"requiredId": 1,
|
||||
"optionalName": nil,
|
||||
"requiredDates": "[128000]",
|
||||
@@ -826,7 +826,7 @@ extension FetchableRecordDecodableTests {
|
||||
}
|
||||
|
||||
do {
|
||||
let record = try RowDecoder().decode(Record.self, from: [
|
||||
let record = try FetchableRecordDecoder().decode(Record.self, from: [
|
||||
"requiredId": 1,
|
||||
"requiredDates": "[128000]",
|
||||
"optionalDates": "[null, 128000]",
|
||||
@@ -840,7 +840,7 @@ extension FetchableRecordDecodableTests {
|
||||
}
|
||||
|
||||
do {
|
||||
_ = try RowDecoder().decode(Record.self, from: [
|
||||
_ = try FetchableRecordDecoder().decode(Record.self, from: [
|
||||
"required_id": 1,
|
||||
"optionalName": "test1",
|
||||
"requiredDates": "[128000]",
|
||||
@@ -874,7 +874,7 @@ extension FetchableRecordDecodableTests {
|
||||
}
|
||||
|
||||
do {
|
||||
let record = try RowDecoder().decode(Record.self, from: [
|
||||
let record = try FetchableRecordDecoder().decode(Record.self, from: [
|
||||
"required_id": 1,
|
||||
"optional_name": "test1",
|
||||
"required_dates": "[128000]",
|
||||
@@ -890,7 +890,7 @@ extension FetchableRecordDecodableTests {
|
||||
}
|
||||
|
||||
do {
|
||||
let record = try RowDecoder().decode(Record.self, from: [
|
||||
let record = try FetchableRecordDecoder().decode(Record.self, from: [
|
||||
"REQUIRED_ID": 1,
|
||||
"OPTIONAL_NAME": "test1",
|
||||
"REQUIRED_DATES": "[128000]",
|
||||
@@ -906,7 +906,7 @@ extension FetchableRecordDecodableTests {
|
||||
}
|
||||
|
||||
do {
|
||||
let record = try RowDecoder().decode(Record.self, from: [
|
||||
let record = try FetchableRecordDecoder().decode(Record.self, from: [
|
||||
"required_id": 1,
|
||||
"optional_name": nil,
|
||||
"required_dates": "[128000]",
|
||||
@@ -922,7 +922,7 @@ extension FetchableRecordDecodableTests {
|
||||
}
|
||||
|
||||
do {
|
||||
let record = try RowDecoder().decode(Record.self, from: [
|
||||
let record = try FetchableRecordDecoder().decode(Record.self, from: [
|
||||
"required_id": 1,
|
||||
"required_dates": "[128000]",
|
||||
"optional_dates": "[null, 128000]",
|
||||
@@ -937,7 +937,7 @@ extension FetchableRecordDecodableTests {
|
||||
|
||||
do {
|
||||
// Matches JSONDecoder.KeyDecodingStrategy.convertFromSnakeCase behavior
|
||||
let record = try RowDecoder().decode(Record.self, from: [
|
||||
let record = try FetchableRecordDecoder().decode(Record.self, from: [
|
||||
"requiredId": 1,
|
||||
"optionalName": "test1",
|
||||
"requiredDates": "[128000]",
|
||||
@@ -953,7 +953,7 @@ extension FetchableRecordDecodableTests {
|
||||
}
|
||||
|
||||
do {
|
||||
_ = try RowDecoder().decode(Record.self, from: [
|
||||
_ = try FetchableRecordDecoder().decode(Record.self, from: [
|
||||
"required_idx": 1,
|
||||
"optional_name": "test1",
|
||||
"required_dates": "[128000]",
|
||||
@@ -970,7 +970,7 @@ extension FetchableRecordDecodableTests {
|
||||
}
|
||||
|
||||
do {
|
||||
_ = try RowDecoder().decode(IncorrectRecord.self, from: [
|
||||
_ = try FetchableRecordDecoder().decode(IncorrectRecord.self, from: [
|
||||
"required_id": 1,
|
||||
"optional_name": "test1",
|
||||
"required_dates": "[128000]",
|
||||
@@ -1008,7 +1008,7 @@ extension FetchableRecordDecodableTests {
|
||||
}
|
||||
|
||||
do {
|
||||
let record = try RowDecoder().decode(Record.self, from: [
|
||||
let record = try FetchableRecordDecoder().decode(Record.self, from: [
|
||||
"_requiredId": 1,
|
||||
"_optionalName": "test1",
|
||||
"_requiredDates": "[128000]",
|
||||
@@ -1024,7 +1024,7 @@ extension FetchableRecordDecodableTests {
|
||||
}
|
||||
|
||||
do {
|
||||
let record = try RowDecoder().decode(Record.self, from: [
|
||||
let record = try FetchableRecordDecoder().decode(Record.self, from: [
|
||||
"_requiredId": 1,
|
||||
"_optionalName": nil,
|
||||
"_requiredDates": "[128000]",
|
||||
@@ -1040,7 +1040,7 @@ extension FetchableRecordDecodableTests {
|
||||
}
|
||||
|
||||
do {
|
||||
let record = try RowDecoder().decode(Record.self, from: [
|
||||
let record = try FetchableRecordDecoder().decode(Record.self, from: [
|
||||
"_requiredId": 1,
|
||||
"_requiredDates": "[128000]",
|
||||
"_optionalDates": "[null, 128000]",
|
||||
@@ -1054,7 +1054,7 @@ extension FetchableRecordDecodableTests {
|
||||
}
|
||||
|
||||
do {
|
||||
_ = try RowDecoder().decode(Record.self, from: [
|
||||
_ = try FetchableRecordDecoder().decode(Record.self, from: [
|
||||
"requiredId": 1,
|
||||
"_optionalName": "test1",
|
||||
"_requiredDates": "[128000]",
|
||||
@@ -1377,7 +1377,7 @@ extension FetchableRecordDecodableTests {
|
||||
// all optionals decode missing keys are nil. This is because GRDB
|
||||
// records accept rows with missing columns, and b and c may want to
|
||||
// decode columns.
|
||||
_ = try RowDecoder().decode(Composed.self, from: [:])
|
||||
_ = try FetchableRecordDecoder().decode(Composed.self, from: [:])
|
||||
}
|
||||
|
||||
// This is a regression test for https://github.com/groue/GRDB.swift/issues/664
|
||||
@@ -1389,7 +1389,7 @@ extension FetchableRecordDecodableTests {
|
||||
var b: B
|
||||
}
|
||||
do {
|
||||
_ = try RowDecoder().decode(Composed.self, from: [:])
|
||||
_ = try FetchableRecordDecoder().decode(Composed.self, from: [:])
|
||||
XCTFail("Expected error")
|
||||
} catch DecodingError.keyNotFound {
|
||||
// a or b can not be decoded because only one key is allowed to be missing
|
||||
@@ -1409,7 +1409,7 @@ extension FetchableRecordDecodableTests {
|
||||
// - a is present
|
||||
// - root is b and c is missing, or the opposite (two possible user intents)
|
||||
let row = try Row.fetchOne(db, sql: "SELECT NULL", adapter: ScopeAdapter(["a": EmptyRowAdapter()]))!
|
||||
_ = try RowDecoder().decode(Composed.self, from: row)
|
||||
_ = try FetchableRecordDecoder().decode(Composed.self, from: row)
|
||||
XCTFail("Expected error")
|
||||
} catch let DecodingError.keyNotFound(key, context) {
|
||||
XCTAssert(["b", "c"].contains(key.stringValue))
|
||||
@@ -1419,7 +1419,7 @@ extension FetchableRecordDecodableTests {
|
||||
// - b is present
|
||||
// - root is a and c is missing, or the opposite (two possible user intents)
|
||||
let row = try Row.fetchOne(db, sql: "SELECT NULL", adapter: ScopeAdapter(["b": EmptyRowAdapter()]))!
|
||||
_ = try RowDecoder().decode(Composed.self, from: row)
|
||||
_ = try FetchableRecordDecoder().decode(Composed.self, from: row)
|
||||
XCTFail("Expected error")
|
||||
} catch let DecodingError.keyNotFound(key, context) {
|
||||
XCTAssert(["a", "c"].contains(key.stringValue))
|
||||
@@ -1429,7 +1429,7 @@ extension FetchableRecordDecodableTests {
|
||||
// - c is present
|
||||
// - root is a and b is missing, or the opposite (two possible user intents)
|
||||
let row = try Row.fetchOne(db, sql: "SELECT NULL", adapter: ScopeAdapter(["c": EmptyRowAdapter()]))!
|
||||
_ = try RowDecoder().decode(Composed.self, from: row)
|
||||
_ = try FetchableRecordDecoder().decode(Composed.self, from: row)
|
||||
XCTFail("Expected error")
|
||||
} catch let DecodingError.keyNotFound(key, context) {
|
||||
XCTAssert(["a", "b"].contains(key.stringValue))
|
||||
@@ -1494,3 +1494,45 @@ extension FetchableRecordDecodableTests {
|
||||
fileprivate extension CodingUserInfoKey {
|
||||
static let testKey = CodingUserInfoKey(rawValue: "correct")!
|
||||
}
|
||||
|
||||
// MARK: - Decodable + custom init
|
||||
|
||||
extension FetchableRecordDecodableTests {
|
||||
// Test for <https://github.com/groue/GRDB.swift/issues/1366#issuecomment-1527280022>
|
||||
func testStructWithCustomizedRowInitializer() throws {
|
||||
struct Player: Decodable, FetchableRecord {
|
||||
var id: Int
|
||||
var name: String
|
||||
var isFetched: Bool = false
|
||||
|
||||
enum CodingKeys: String, CodingKey {
|
||||
case id, name
|
||||
}
|
||||
|
||||
init(id: Int, name: String) {
|
||||
self.id = id
|
||||
self.name = name
|
||||
self.isFetched = false
|
||||
}
|
||||
|
||||
init(row: Row) throws {
|
||||
self = try FetchableRecordDecoder().decode(Player.self, from: row)
|
||||
self.isFetched = true
|
||||
}
|
||||
}
|
||||
|
||||
do {
|
||||
let player = Player(id: 1, name: "Arthur")
|
||||
XCTAssertEqual(player.id, 1)
|
||||
XCTAssertEqual(player.name, "Arthur")
|
||||
XCTAssertEqual(player.isFetched, false)
|
||||
}
|
||||
|
||||
do {
|
||||
let player = try Player(row: ["id": 2, "name": "Barbara"])
|
||||
XCTAssertEqual(player.id, 2)
|
||||
XCTAssertEqual(player.name, "Barbara")
|
||||
XCTAssertEqual(player.isFetched, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user