From d0b1bde521e16da63b2ec514b5264d111960a18e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 7 Jul 2016 09:09:53 +0200 Subject: [PATCH 1/9] Add Cristian Filipov to the thanks section --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a647a0f0f..3d78ff198 100644 --- a/README.md +++ b/README.md @@ -3483,6 +3483,6 @@ Sample Code **Thanks** - [Pierlis](http://pierlis.com), where we write great software. -- [Vladimir Babin](https://github.com/Chiliec), [Pascal Edmond](https://github.com/pakko972), [@peter-ss](https://github.com/peter-ss), [Pierre-Loïc Raynaud](https://github.com/pierlo), [Steven Schveighoffer](https://github.com/schveiguy) and [@swiftlyfalling](https://github.com/swiftlyfalling) for their contributions, help, and feedback on GRDB. +- [Vladimir Babin](https://github.com/Chiliec), [Pascal Edmond](https://github.com/pakko972), [Cristian Filipov](https://github.com/cfilipov), [@peter-ss](https://github.com/peter-ss), [Pierre-Loïc Raynaud](https://github.com/pierlo), [Steven Schveighoffer](https://github.com/schveiguy) and [@swiftlyfalling](https://github.com/swiftlyfalling) for their contributions, help, and feedback on GRDB. - [@aymerick](https://github.com/aymerick) and [Mathieu "Kali" Poumeyrol](https://github.com/kali) because SQL. - [ccgus/fmdb](https://github.com/ccgus/fmdb) for its excellency. From a65859940674e86f9a81a9206fccbbdec3f15270 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 7 Jul 2016 09:15:24 +0200 Subject: [PATCH 2/9] Documentation fix --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3d78ff198..02a6adf6f 100644 --- a/README.md +++ b/README.md @@ -2394,7 +2394,7 @@ Once you have a request, you can fetch the records at the origin of the request: ```swift // Some request based on `Person` -let request = Person.filter(...)... // FetchRequest +let request = Person.filter(...)... // QueryInterfaceRequest // Fetch persons: request.fetch(db) // DatabaseSequence From 358c3ab0c339dae68809bfff64dbf0d75d32a430 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 8 Jul 2016 08:44:41 +0200 Subject: [PATCH 3/9] Row adapters: rename "variants" to "scopes" --- GRDB/Core/Row.swift | 63 ++++--- GRDB/Core/RowAdapter.swift | 79 +++++---- README.md | 26 +-- Tests/Public/Core/Row/AdapterRowTests.swift | 159 +++++++++--------- Tests/Public/Core/Row/DetachedRowTests.swift | 2 +- .../Public/Core/Row/DictionaryRowTests.swift | 2 +- Tests/Public/Core/Row/MetalRowTests.swift | 2 +- .../Public/Core/Row/RowConvertibleTests.swift | 20 +-- 8 files changed, 186 insertions(+), 167 deletions(-) diff --git a/GRDB/Core/Row.swift b/GRDB/Core/Row.swift index 16420355f..8f7569333 100644 --- a/GRDB/Core/Row.swift +++ b/GRDB/Core/Row.swift @@ -393,10 +393,33 @@ extension Row { extension Row { - // MARK: - Variants + // MARK: - Scopes - public func variant(named name: String) -> Row? { - return impl.variant(named: name) + /// Returns a scoped row, if the row was fetched along with a row adapter + /// that defined this scope. + /// + /// // Two adapters + /// let fooAdapter = ColumnMapping(["value": "foo"]) + /// let barAdapter = ColumnMapping(["value": "bar"]) + /// + /// // An adapter with scopes + /// let adapter = ScopeAdapter([ + /// "foo": fooAdapter, + /// "bar": barAdapter]) + /// + /// // Fetch a row + /// let sql = "SELECT 'foo' AS foo, 'bar' AS bar" + /// let row = Row.fetchOne(db, sql, adapter: adapter)! + /// + /// // Two scoped rows: + /// if let fooRow = row.scoped(on: "foo") { + /// fooRow.value(named: "value") // "foo" + /// } + /// if let barRow = row.scopeed(on: "bar") { + /// barRow.value(named: "value") // "bar" + /// } + public func scoped(on name: String) -> Row? { + return impl.scoped(on: name) } } @@ -705,16 +728,16 @@ public func ==(lhs: Row, rhs: Row) -> Bool { } } - let lvariantNames = lhs.impl.variantNames - let rvariantNames = rhs.impl.variantNames - guard lvariantNames == rvariantNames else { + let lscopeNames = lhs.impl.scopeNames + let rscopeNames = rhs.impl.scopeNames + guard lscopeNames == rscopeNames else { return false } - for name in lvariantNames { - let lvariant = lhs.variant(named: name) - let rvariant = rhs.variant(named: name) - guard lvariant == rvariant else { + for name in lscopeNames { + let lscope = lhs.scoped(on: name) + let rscope = rhs.scoped(on: name) + guard lscope == rscope else { return false } } @@ -775,9 +798,9 @@ protocol RowImpl { // leftmost column that matches *name*. func indexOfColumn(named name: String) -> Int? - func variant(named name: String) -> Row? + func scoped(on name: String) -> Row? - var variantNames: Set { get } + var scopeNames: Set { get } // row.impl is guaranteed to be self. func copy(row: Row) -> Row @@ -818,11 +841,11 @@ private struct DictionaryRowImpl : RowImpl { return dictionary.startIndex.distanceTo(index) } - func variant(named name: String) -> Row? { + func scoped(on name: String) -> Row? { return nil } - var variantNames: Set { + var scopeNames: Set { return [] } @@ -867,11 +890,11 @@ private struct StatementCopyRowImpl : RowImpl { return columnNames.indexOf { $0.lowercaseString == lowercaseName } } - func variant(named name: String) -> Row? { + func scoped(on name: String) -> Row? { return nil } - var variantNames: Set { + var scopeNames: Set { return [] } @@ -926,11 +949,11 @@ private struct StatementRowImpl : RowImpl { return lowercaseColumnIndexes[name.lowercaseString] } - func variant(named name: String) -> Row? { + func scoped(on name: String) -> Row? { return nil } - var variantNames: Set { + var scopeNames: Set { return [] } @@ -960,11 +983,11 @@ private struct EmptyRowImpl : RowImpl { return nil } - func variant(named name: String) -> Row? { + func scoped(on name: String) -> Row? { return nil } - var variantNames: Set { + var scopeNames: Set { return [] } diff --git a/GRDB/Core/RowAdapter.swift b/GRDB/Core/RowAdapter.swift index 59a4dc4d1..ba29aedb2 100644 --- a/GRDB/Core/RowAdapter.swift +++ b/GRDB/Core/RowAdapter.swift @@ -70,14 +70,14 @@ extension AdaptedColumnsDescription : ConcreteRowAdapter { } /// Part of the ConcreteRowAdapter protocol; returns the empty dictionary. - public var variants: [String: ConcreteRowAdapter] { + public var scopes: [String: ConcreteRowAdapter] { return [:] } } -struct ConcreteVariantRowAdapter : ConcreteRowAdapter { +struct ConcreteScopeAdapter : ConcreteRowAdapter { let adaptedColumnsDescription: AdaptedColumnsDescription - let variants: [String: ConcreteRowAdapter] + let scopes: [String: ConcreteRowAdapter] } /// ConcreteRowAdapter is a protocol that supports the RowAdapter protocol. @@ -91,8 +91,8 @@ public protocol ConcreteRowAdapter { // An AdaptedColumnsDescription var adaptedColumnsDescription: AdaptedColumnsDescription { get } - /// A dictionary whose keys are variant names. - var variants: [String: ConcreteRowAdapter] { get } + /// A dictionary whose keys are scope names. + var scopes: [String: ConcreteRowAdapter] { get } } /// RowAdapter is a protocol that helps two incompatible row interfaces working @@ -102,8 +102,7 @@ public protocol ConcreteRowAdapter { /// /// - ColumnMapping: renames row columns /// - SuffixRowAdapter: hides the first columns of a row -/// - VariantRowAdapter: groups several adapters together to define named row -/// variants. +/// - ScopeAdapter: groups several adapters together to define named scopes /// /// If the built-in adapters don't fit your needs, you can implement your own /// type that adopts RowAdapter. @@ -140,15 +139,15 @@ public protocol RowAdapter { } extension RowAdapter { - /// Returns an adapter based on self, with added variants. + /// Returns an adapter based on self, with added scopes. /// - /// If self already defines variants, the added variants replace - /// eventual existing variants with the same name. + /// If self already defines scopes, the added scopes replace + /// eventual existing scopes with the same name. /// - /// - parameter variants: A dictionary that maps variant names to + /// - parameter scopes: A dictionary that maps scope names to /// row adapters. - public func adapterWithVariants(variants: [String: RowAdapter]) -> RowAdapter { - return VariantRowAdapter(mainAdapter: self, variants: variants) + public func addingScopes(scopes: [String: RowAdapter]) -> RowAdapter { + return ScopeAdapter(mainAdapter: self, scopes: scopes) } } @@ -210,62 +209,60 @@ public struct SuffixRowAdapter : RowAdapter { } } -/// VariantRowAdapter is a row adapter that lets you add adapted variants to -/// fetched rows. +/// ScopeAdapter is a row adapter that lets you add scoped adaptations to rows. /// /// // Two adapters /// let fooAdapter = ColumnMapping(["value": "foo"]) /// let barAdapter = ColumnMapping(["value": "bar"]) /// -/// // An adapter with named variants -/// let variants: [String: RowAdapter] = [ +/// // An adapter with named scopes +/// let adapter = ScopeAdapter([ /// "foo": fooAdapter, /// "bar": barAdapter]) -/// let adapter = VariantRowAdapter(variants: variants) /// /// // Fetch a row /// let sql = "SELECT 'foo' AS foo, 'bar' AS bar" /// let row = Row.fetchOne(db, sql, adapter: adapter)! /// -/// // Two variants of the fetched row: -/// if let fooRow = row.variant(named: "foo") { +/// // Two scoped rows: +/// if let fooRow = row.scoped(on: "foo") { /// fooRow.value(named: "value") // "foo" /// } -/// if let barRow = row.variant(named: "bar") { +/// if let barRow = row.scopeed(on: "bar") { /// barRow.value(named: "value") // "bar" /// } -public struct VariantRowAdapter : RowAdapter { +public struct ScopeAdapter : RowAdapter { /// The main adapter let mainAdapter: RowAdapter - /// The variant adapters - let variants: [String: RowAdapter] + /// The scope adapters + let scopes: [String: RowAdapter] - /// Creates a variant adapter. + /// Creates a scoped adapter. /// - /// - parameter variants: A dictionary that maps variant names to + /// - parameter scopes: A dictionary that maps scope names to /// row adapters. - public init(variants: [String: RowAdapter]) { + public init(_ scopes: [String: RowAdapter]) { self.mainAdapter = SuffixRowAdapter(fromIndex: 0) - self.variants = variants + self.scopes = scopes } - init(mainAdapter: RowAdapter, variants: [String: RowAdapter]) { + init(mainAdapter: RowAdapter, scopes: [String: RowAdapter]) { self.mainAdapter = mainAdapter - self.variants = variants + self.scopes = scopes } /// Part of the RowAdapter protocol public func concreteRowAdapter(with statement: SelectStatement) throws -> ConcreteRowAdapter { - let mainConcreteRowAdapter = try mainAdapter.concreteRowAdapter(with: statement) - var variantConcreteRowAdapters = mainConcreteRowAdapter.variants - for (name, adapter) in variants { - try variantConcreteRowAdapters[name] = adapter.concreteRowAdapter(with: statement) + let mainConcreteAdapter = try mainAdapter.concreteRowAdapter(with: statement) + var concreteAdapterScopes = mainConcreteAdapter.scopes + for (name, adapter) in scopes { + try concreteAdapterScopes[name] = adapter.concreteRowAdapter(with: statement) } - return ConcreteVariantRowAdapter( - adaptedColumnsDescription: mainConcreteRowAdapter.adaptedColumnsDescription, - variants: variantConcreteRowAdapters) + return ConcreteScopeAdapter( + adaptedColumnsDescription: mainConcreteAdapter.adaptedColumnsDescription, + scopes: concreteAdapterScopes) } } @@ -315,15 +312,15 @@ struct AdapterRowImpl : RowImpl { return adaptedColumnsDescription.adaptedIndexOfColumn(named: name) } - func variant(named name: String) -> Row? { - guard let concreteRowAdapter = concreteRowAdapter.variants[name] else { + func scoped(on name: String) -> Row? { + guard let concreteRowAdapter = concreteRowAdapter.scopes[name] else { return nil } return Row(baseRow: baseRow, concreteRowAdapter: concreteRowAdapter) } - var variantNames: Set { - return Set(concreteRowAdapter.variants.keys) + var scopeNames: Set { + return Set(concreteRowAdapter.scopes.keys) } func copy(row: Row) -> Row { diff --git a/README.md b/README.md index 02a6adf6f..cc7848006 100644 --- a/README.md +++ b/README.md @@ -1390,7 +1390,7 @@ row.value(named: "consumed") // "Hello" ``` -**Row adapters can also define row variants.** Variants help several consumers feed on a single row and can reveal useful with joined queries. +**Row adapters can also define row "scopes".** Scopes help several consumers feed on a single row and can reveal useful with joined queries. For example, let's build a query which loads books along with their author: @@ -1401,14 +1401,14 @@ let sql = "SELECT books.id, books.title, " + "JOIN persons ON books.authorID = persons.id" ``` -The author columns are "authorID" and "authorName". Let's say that we prefer to consume them as "id" and "name". For that we build an adapter which defines a variant named "author": +The author columns are "authorID" and "authorName". Let's say that we prefer to consume them as "id" and "name". For that we define a scope named "author": ```swift let authorMapping = ColumnMapping(["id": "authorID", "name": "authorName"]) -let adapter = VariantRowAdapter(variants: ["author": authorMapping]) +let adapter = ScopeAdapter(["author": authorMapping]) ``` -Use the `Row.variant(named:)` method to load the "author" variant: +Use the `Row.scoped(on:)` method to access the "author" scope: ```swift for row in Row.fetch(db, sql, adapter: adapter) { @@ -1418,15 +1418,15 @@ for row in Row.fetch(db, sql, adapter: adapter) { row.value(named: "authorID") // 10 row.value(named: "authorName") // Melville - // The "author" variant, with mapped columns: - if let authorRow = row.variant(named: "author") { + // The "author" scope, with mapped columns: + if let authorRow = row.scoped(on: "author") { authorRow.value(named: "id") // 10 authorRow.value(named: "name") // Melville } } ``` -> :bowtie: **Tip**: now that we have nice "id" and "name" columns, we can leverage [RowConvertible](#rowconvertible-protocol) types such as [Record](#record-class) subclasses. For example, assuming the Book type consumes the "author" variant in its row initializer and builds a Person from it, the same row can be consumed by both the Book and Person types: +> :bowtie: **Tip**: now that we have nice "id" and "name" columns, we can leverage [RowConvertible](#rowconvertible-protocol) types such as [Record](#record-class) subclasses. For example, assuming the Book type consumes the "author" scope in its row initializer and builds a Person from it, the same row can be consumed by both the Book and Person types: > > ```swift > for book in Book.fetch(db, sql, adapter: adapter) { @@ -1443,7 +1443,7 @@ for row in Row.fetch(db, sql, adapter: adapter) { > ``` -**You can mix a main adapter with variant adapters:** +**You can mix a main adapter with scopes:** ```swift let sql = "SELECT main.id AS mainID, main.name AS mainName, " + @@ -1453,21 +1453,21 @@ let sql = "SELECT main.id AS mainID, main.name AS mainName, " + let mainAdapter = ColumnMapping(["id": "mainID", "name": "mainName"]) let bestFriendAdapter = ColumnMapping(["id": "friendID", "name": "friendName"]) -let adapter = mainAdapter.adapterWithVariants(["bestFriend": bestFriendAdapter]) +let adapter = mainAdapter.addingScopes(["bestFriend": bestFriendAdapter]) for row in Row.fetch(db, sql, adapter: adapter) { // The fetched row, adapted with mainAdapter: row.value(named: "id") // 1 row.value(named: "name") // Arthur - // The "bestFriend" variant, with bestFriendAdapter: - if let bestFriendRow = row.variant(named: "bestFriend") { + // The "bestFriend" scope, with bestFriendAdapter: + if let bestFriendRow = row.scoped(on: "bestFriend") { bestFriendRow.value(named: "id") // 2 bestFriendRow.value(named: "name") // Barbara } } -// Assuming Person.init(row) consumes the "bestFriend" variant: +// Assuming Person.init(row) consumes the "bestFriend" scope: for person in Person.fetch(db, sql, adapter: adapter) { person.name // Arthur person.bestFriend?.name // Barbara @@ -1480,7 +1480,7 @@ For more information about row adapters, see the documentation of: - [RowAdapter](http://cocoadocs.org/docsets/GRDB.swift/0.74.0/Protocols/RowAdapter.html): the protocol that lets you define your custom row adapters - [ColumnMapping](http://cocoadocs.org/docsets/GRDB.swift/0.74.0/Structs/ColumnMapping.html): a row adapter that renames row columns - [SuffixRowAdapter](http://cocoadocs.org/docsets/GRDB.swift/0.74.0/Structs/SuffixRowAdapter.html): a row adapter that hides the first columns of a row -- [VariantRowAdapter](http://cocoadocs.org/docsets/GRDB.swift/0.74.0/Structs/VariantRowAdapter.html): the row adapter that groups several adapters together to define named variants +- [ScopeAdapter](http://cocoadocs.org/docsets/GRDB.swift/0.74.0/Structs/ScopeAdapter.html): the row adapter that groups several adapters together to define scopes ## Raw SQLite Pointers diff --git a/Tests/Public/Core/Row/AdapterRowTests.swift b/Tests/Public/Core/Row/AdapterRowTests.swift index bf6238594..990885e08 100644 --- a/Tests/Public/Core/Row/AdapterRowTests.swift +++ b/Tests/Public/Core/Row/AdapterRowTests.swift @@ -270,10 +270,10 @@ class AdapterRowTests: GRDBTestCase { } } - func testVariants() { + func testScopes() { let dbQueue = DatabaseQueue() dbQueue.inDatabase { db in - let adapter = VariantRowAdapter(variants: [ + let adapter = ScopeAdapter([ "sub1": ColumnMapping(["id": "id1", "val": "val1"]), "sub2": ColumnMapping(["id": "id2", "val": "val2"])]) let row = Row.fetchOne(db, "SELECT 1 AS id1, 'foo1' AS val1, 2 as id2, 'foo2' AS val2", adapter: adapter)! @@ -284,32 +284,32 @@ class AdapterRowTests: GRDBTestCase { XCTAssertEqual(row.value(named: "id2") as Int, 2) XCTAssertEqual(row.value(named: "val2") as String, "foo2") - if let variant = row.variant(named: "sub1") { - XCTAssertEqual(variant.count, 2) - XCTAssertEqual(variant.value(named: "id") as Int, 1) - XCTAssertEqual(variant.value(named: "val") as String, "foo1") + if let scope = row.scoped(on: "sub1") { + XCTAssertEqual(scope.count, 2) + XCTAssertEqual(scope.value(named: "id") as Int, 1) + XCTAssertEqual(scope.value(named: "val") as String, "foo1") } else { XCTFail() } - if let variant = row.variant(named: "sub2") { - XCTAssertEqual(variant.count, 2) - XCTAssertEqual(variant.value(named: "id") as Int, 2) - XCTAssertEqual(variant.value(named: "val") as String, "foo2") + if let scope = row.scoped(on: "sub2") { + XCTAssertEqual(scope.count, 2) + XCTAssertEqual(scope.value(named: "id") as Int, 2) + XCTAssertEqual(scope.value(named: "val") as String, "foo2") } else { XCTFail() } - XCTAssertTrue(row.variant(named: "SUB1") == nil) // case-insensitivity is not really required here, and case-sensitivity helps the implementation because it allows the use of a dictionary. So let's enforce this with a public test. - XCTAssertTrue(row.variant(named: "missing") == nil) + XCTAssertTrue(row.scoped(on: "SUB1") == nil) // case-insensitivity is not really required here, and case-sensitivity helps the implementation because it allows the use of a dictionary. So let's enforce this with a public test. + XCTAssertTrue(row.scoped(on: "missing") == nil) } } - func testVariantsWithMainMapping() { + func testScopesWithMainMapping() { let dbQueue = DatabaseQueue() dbQueue.inDatabase { db in let adapter = ColumnMapping(["id": "id0", "val": "val0"]) - .adapterWithVariants([ + .addingScopes([ "sub1": ColumnMapping(["id": "id1", "val": "val1"]), "sub2": ColumnMapping(["id": "id2", "val": "val2"])]) let row = Row.fetchOne(db, "SELECT 0 AS id0, 'foo0' AS val0, 1 AS id1, 'foo1' AS val1, 2 as id2, 'foo2' AS val2", adapter: adapter)! @@ -318,75 +318,75 @@ class AdapterRowTests: GRDBTestCase { XCTAssertEqual(row.value(named: "id") as Int, 0) XCTAssertEqual(row.value(named: "val") as String, "foo0") - if let variant = row.variant(named: "sub1") { - XCTAssertEqual(variant.count, 2) - XCTAssertEqual(variant.value(named: "id") as Int, 1) - XCTAssertEqual(variant.value(named: "val") as String, "foo1") + if let scope = row.scoped(on: "sub1") { + XCTAssertEqual(scope.count, 2) + XCTAssertEqual(scope.value(named: "id") as Int, 1) + XCTAssertEqual(scope.value(named: "val") as String, "foo1") } else { XCTFail() } - if let variant = row.variant(named: "sub2") { - XCTAssertEqual(variant.count, 2) - XCTAssertEqual(variant.value(named: "id") as Int, 2) - XCTAssertEqual(variant.value(named: "val") as String, "foo2") + if let scope = row.scoped(on: "sub2") { + XCTAssertEqual(scope.count, 2) + XCTAssertEqual(scope.value(named: "id") as Int, 2) + XCTAssertEqual(scope.value(named: "val") as String, "foo2") } else { XCTFail() } - XCTAssertTrue(row.variant(named: "SUB1") == nil) // case-insensitivity is not really required here, and case-sensitivity helps the implementation because it allows the use of a dictionary. So let's enforce this with a public test. - XCTAssertTrue(row.variant(named: "missing") == nil) + XCTAssertTrue(row.scoped(on: "SUB1") == nil) // case-insensitivity is not really required here, and case-sensitivity helps the implementation because it allows the use of a dictionary. So let's enforce this with a public test. + XCTAssertTrue(row.scoped(on: "missing") == nil) } } - func testMergeVariants() { + func testMergeScopes() { let dbQueue = DatabaseQueue() dbQueue.inDatabase { db in let adapter0 = ColumnMapping(["id": "id0", "val": "val0"]) let adapter1 = ColumnMapping(["id": "id1", "val": "val1"]) let adapter2 = ColumnMapping(["id": "id2", "val": "val2"]) - let mainAdapter = VariantRowAdapter(variants: ["sub0": adapter0, "sub1": adapter2]) - let adapter = mainAdapter.adapterWithVariants(["sub1": adapter1, "sub2": adapter2]) + let mainAdapter = ScopeAdapter(["sub0": adapter0, "sub1": adapter2]) + let adapter = mainAdapter.addingScopes(["sub1": adapter1, "sub2": adapter2]) let row = Row.fetchOne(db, "SELECT 0 AS id0, 'foo0' AS val0, 1 AS id1, 'foo1' AS val1, 2 as id2, 'foo2' AS val2", adapter: adapter)! - // sub0 is defined in the the first variant adapter - if let variant = row.variant(named: "sub0") { - XCTAssertEqual(variant.count, 2) - XCTAssertEqual(variant.value(named: "id") as Int, 0) - XCTAssertEqual(variant.value(named: "val") as String, "foo0") + // sub0 is defined in the the first scoped adapter + if let scope = row.scoped(on: "sub0") { + XCTAssertEqual(scope.count, 2) + XCTAssertEqual(scope.value(named: "id") as Int, 0) + XCTAssertEqual(scope.value(named: "val") as String, "foo0") } else { XCTFail() } - // sub1 is defined in the the first variant adapter, and then + // sub1 is defined in the the first scoped adapter, and then // redefined in the second - if let variant = row.variant(named: "sub1") { - XCTAssertEqual(variant.count, 2) - XCTAssertEqual(variant.value(named: "id") as Int, 1) - XCTAssertEqual(variant.value(named: "val") as String, "foo1") + if let scope = row.scoped(on: "sub1") { + XCTAssertEqual(scope.count, 2) + XCTAssertEqual(scope.value(named: "id") as Int, 1) + XCTAssertEqual(scope.value(named: "val") as String, "foo1") } else { XCTFail() } - // sub2 is defined in the the second variant adapter - if let variant = row.variant(named: "sub2") { - XCTAssertEqual(variant.count, 2) - XCTAssertEqual(variant.value(named: "id") as Int, 2) - XCTAssertEqual(variant.value(named: "val") as String, "foo2") + // sub2 is defined in the the second scoped adapter + if let scope = row.scoped(on: "sub2") { + XCTAssertEqual(scope.count, 2) + XCTAssertEqual(scope.value(named: "id") as Int, 2) + XCTAssertEqual(scope.value(named: "val") as String, "foo2") } else { XCTFail() } } } - func testThreeLevelVariants() { + func testThreeLevelScopes() { let dbQueue = DatabaseQueue() dbQueue.inDatabase { db in let adapter = ColumnMapping(["id": "id0", "val": "val0"]) - .adapterWithVariants([ + .addingScopes([ "sub1": ColumnMapping(["id": "id1", "val": "val1"]) - .adapterWithVariants([ + .addingScopes([ "sub2": ColumnMapping(["id": "id2", "val": "val2"])])]) let row = Row.fetchOne(db, "SELECT 0 AS id0, 'foo0' AS val0, 1 AS id1, 'foo1' AS val1, 2 as id2, 'foo2' AS val2", adapter: adapter)! @@ -394,15 +394,15 @@ class AdapterRowTests: GRDBTestCase { XCTAssertEqual(row.value(named: "id") as Int, 0) XCTAssertEqual(row.value(named: "val") as String, "foo0") - if let variant = row.variant(named: "sub1") { - XCTAssertEqual(variant.count, 2) - XCTAssertEqual(variant.value(named: "id") as Int, 1) - XCTAssertEqual(variant.value(named: "val") as String, "foo1") + if let scope = row.scoped(on: "sub1") { + XCTAssertEqual(scope.count, 2) + XCTAssertEqual(scope.value(named: "id") as Int, 1) + XCTAssertEqual(scope.value(named: "val") as String, "foo1") - if let variant = variant.variant(named: "sub2") { - XCTAssertEqual(variant.count, 2) - XCTAssertEqual(variant.value(named: "id") as Int, 2) - XCTAssertEqual(variant.value(named: "val") as String, "foo2") + if let scope = scope.scoped(on: "sub2") { + XCTAssertEqual(scope.count, 2) + XCTAssertEqual(scope.value(named: "id") as Int, 2) + XCTAssertEqual(scope.value(named: "val") as String, "foo2") } else { XCTFail() } @@ -411,33 +411,32 @@ class AdapterRowTests: GRDBTestCase { } // sub2 is only defined in sub1 - XCTAssertTrue(row.variant(named: "sub2") == nil) + XCTAssertTrue(row.scoped(on: "sub2") == nil) } } func testSuffixAdapter() { let dbQueue = DatabaseQueue() dbQueue.inDatabase { db in - let adapter = VariantRowAdapter( - variants: [ - "sub1": SuffixRowAdapter(fromIndex: 2) - .adapterWithVariants([ - "sub2": SuffixRowAdapter(fromIndex: 4)])]) + let adapter = ScopeAdapter([ + "sub1": SuffixRowAdapter(fromIndex: 2) + .addingScopes([ + "sub2": SuffixRowAdapter(fromIndex: 4)])]) let row = Row.fetchOne(db, "SELECT 0 AS id, 'foo0' AS val, 1 AS id, 'foo1' AS val, 2 as id, 'foo2' AS val", adapter: adapter)! XCTAssertEqual(row.count, 6) XCTAssertEqual(row.value(named: "id") as Int, 0) XCTAssertEqual(row.value(named: "val") as String, "foo0") - if let variant = row.variant(named: "sub1") { - XCTAssertEqual(variant.count, 4) - XCTAssertEqual(variant.value(named: "id") as Int, 1) - XCTAssertEqual(variant.value(named: "val") as String, "foo1") + if let scope = row.scoped(on: "sub1") { + XCTAssertEqual(scope.count, 4) + XCTAssertEqual(scope.value(named: "id") as Int, 1) + XCTAssertEqual(scope.value(named: "val") as String, "foo1") - if let variant = variant.variant(named: "sub2") { - XCTAssertEqual(variant.count, 2) - XCTAssertEqual(variant.value(named: "id") as Int, 2) - XCTAssertEqual(variant.value(named: "val") as String, "foo2") + if let scope = scope.scoped(on: "sub2") { + XCTAssertEqual(scope.count, 2) + XCTAssertEqual(scope.value(named: "id") as Int, 2) + XCTAssertEqual(scope.value(named: "val") as String, "foo2") } else { XCTFail() } @@ -446,7 +445,7 @@ class AdapterRowTests: GRDBTestCase { } // sub2 is only defined in sub1 - XCTAssertTrue(row.variant(named: "sub2") == nil) + XCTAssertTrue(row.scoped(on: "sub2") == nil) } } @@ -454,7 +453,7 @@ class AdapterRowTests: GRDBTestCase { let dbQueue = DatabaseQueue() dbQueue.inDatabase { db in let adapter = ColumnMapping(["a": "basea", "b": "baseb", "c": "basec"]) - .adapterWithVariants(["sub": ColumnMapping(["a": "baseb"])]) + .addingScopes(["sub": ColumnMapping(["a": "baseb"])]) var copiedRow: Row? = nil for baseRow in Row.fetch(db, "SELECT 0 AS basea, 'XXX' AS extra, 1 AS baseb, 2 as basec", adapter: adapter) { copiedRow = baseRow.copy() @@ -465,9 +464,9 @@ class AdapterRowTests: GRDBTestCase { XCTAssertEqual(copiedRow.value(named: "a") as Int, 0) XCTAssertEqual(copiedRow.value(named: "b") as Int, 1) XCTAssertEqual(copiedRow.value(named: "c") as Int, 2) - if let variant = copiedRow.variant(named: "sub") { - XCTAssertEqual(variant.count, 1) - XCTAssertEqual(variant.value(named: "a") as Int, 1) + if let scope = copiedRow.scoped(on: "sub") { + XCTAssertEqual(scope.count, 1) + XCTAssertEqual(scope.value(named: "a") as Int, 1) } } else { XCTFail() @@ -493,18 +492,18 @@ class AdapterRowTests: GRDBTestCase { } } - func testEqualityComparesVariants() { + func testEqualityComparesScopes() { let dbQueue = DatabaseQueue() dbQueue.inDatabase { db in let adapter1 = ColumnMapping(["a": "basea", "b": "baseb", "c": "basec"]) - .adapterWithVariants(["sub": ColumnMapping(["b": "baseb"])]) + .addingScopes(["sub": ColumnMapping(["b": "baseb"])]) let adapter2 = ColumnMapping(["a": "basea", "b": "baseb2", "c": "basec"]) let adapter3 = ColumnMapping(["a": "basea", "b": "baseb2", "c": "basec"]) - .adapterWithVariants(["sub": ColumnMapping(["b": "baseb2"])]) + .addingScopes(["sub": ColumnMapping(["b": "baseb2"])]) let adapter4 = ColumnMapping(["a": "basea", "b": "baseb", "c": "basec"]) - .adapterWithVariants(["sub": ColumnMapping(["b": "baseb"]), "altSub": ColumnMapping(["a": "baseb2"])]) + .addingScopes(["sub": ColumnMapping(["b": "baseb"]), "altSub": ColumnMapping(["a": "baseb2"])]) let adapter5 = ColumnMapping(["a": "basea", "b": "baseb", "c": "basec"]) - .adapterWithVariants(["sub": ColumnMapping(["b": "baseb", "c": "basec"])]) + .addingScopes(["sub": ColumnMapping(["b": "baseb", "c": "basec"])]) let row1 = Row.fetchOne(db, "SELECT 0 AS basea, 'XXX' AS extra, 1 AS baseb, 1 AS baseb2, 2 as basec", adapter: adapter1)! let row2 = Row.fetchOne(db, "SELECT 0 AS basea, 'XXX' AS extra, 1 AS baseb, 1 AS baseb2, 2 as basec", adapter: adapter2)! let row3 = Row.fetchOne(db, "SELECT 0 AS basea, 'XXX' AS extra, 1 AS baseb, 1 AS baseb2, 2 as basec", adapter: adapter3)! @@ -516,9 +515,9 @@ class AdapterRowTests: GRDBTestCase { (row1, row3, true), (row1, row4, false), (row1, row5, false), - (row1.variant(named: "sub"), row3.variant(named: "sub"), true), - (row1.variant(named: "sub"), row4.variant(named: "sub"), true), - (row1.variant(named: "sub"), row5.variant(named: "sub"), false)] + (row1.scoped(on: "sub"), row3.scoped(on: "sub"), true), + (row1.scoped(on: "sub"), row4.scoped(on: "sub"), true), + (row1.scoped(on: "sub"), row5.scoped(on: "sub"), false)] for (lrow, rrow, equal) in tests { print(lrow) print(rrow) diff --git a/Tests/Public/Core/Row/DetachedRowTests.swift b/Tests/Public/Core/Row/DetachedRowTests.swift index b6e238884..b7f873642 100644 --- a/Tests/Public/Core/Row/DetachedRowTests.swift +++ b/Tests/Public/Core/Row/DetachedRowTests.swift @@ -256,7 +256,7 @@ class DetachedRowTests: GRDBTestCase { let dbQueue = DatabaseQueue() dbQueue.inDatabase { db in let row = Row.fetchOne(db, "SELECT 'foo' AS nAmE, 1 AS foo")! - XCTAssertTrue(row.variant(named: "missing") == nil) + XCTAssertTrue(row.scoped(on: "missing") == nil) } } diff --git a/Tests/Public/Core/Row/DictionaryRowTests.swift b/Tests/Public/Core/Row/DictionaryRowTests.swift index 8b4010e02..9cf4f6062 100644 --- a/Tests/Public/Core/Row/DictionaryRowTests.swift +++ b/Tests/Public/Core/Row/DictionaryRowTests.swift @@ -185,7 +185,7 @@ class DictionaryRowTests: GRDBTestCase { func testSubRows() { let row = Row(["a": 0, "b": 1, "c": 2]) - XCTAssertTrue(row.variant(named: "missing") == nil) + XCTAssertTrue(row.scoped(on: "missing") == nil) } func testCopy() { diff --git a/Tests/Public/Core/Row/MetalRowTests.swift b/Tests/Public/Core/Row/MetalRowTests.swift index e353fac55..ba8b7f1de 100644 --- a/Tests/Public/Core/Row/MetalRowTests.swift +++ b/Tests/Public/Core/Row/MetalRowTests.swift @@ -299,7 +299,7 @@ class MetalRowTests: GRDBTestCase { var rowFetched = false for row in Row.fetch(db, "SELECT 'foo' AS nAmE, 1 AS foo") { rowFetched = true - XCTAssertTrue(row.variant(named: "missing") == nil) + XCTAssertTrue(row.scoped(on: "missing") == nil) } XCTAssertTrue(rowFetched) } diff --git a/Tests/Public/Core/Row/RowConvertibleTests.swift b/Tests/Public/Core/Row/RowConvertibleTests.swift index 33d9cd2d1..cdf6442c1 100644 --- a/Tests/Public/Core/Row/RowConvertibleTests.swift +++ b/Tests/Public/Core/Row/RowConvertibleTests.swift @@ -34,16 +34,16 @@ private class Person : RowConvertible { required init(_ row: Row) { firstName = row.value(named: "firstName") lastName = row.value(named: "lastName") - if let variant = row.variant(named: "bestFriend") { - bestFriend = Person(variant) + if let bestFriendRow = row.scoped(on: "bestFriend") { + bestFriend = Person(bestFriendRow) } fetched = false } func awakeFromFetch(row row: Row) { fetched = true - if let bestFriend = bestFriend, let variant = row.variant(named: "bestFriend") { - bestFriend.awakeFromFetch(row: variant) + if let bestFriend = bestFriend, let bestFriendRow = row.scoped(on: "bestFriend") { + bestFriend.awakeFromFetch(row: bestFriendRow) } } } @@ -113,7 +113,7 @@ class RowConvertibleTests: GRDBTestCase { let dbQueue = try makeDatabaseQueue() dbQueue.inDatabase { db in let adapter = ColumnMapping(["firstName": "firstName1", "lastName": "lastName1"]) - .adapterWithVariants(["bestFriend": ColumnMapping(["firstName": "firstName2", "lastName": "lastName2"])]) + .addingScopes(["bestFriend": ColumnMapping(["firstName": "firstName2", "lastName": "lastName2"])]) let sql = "SELECT ? AS firstName1, ? AS lastName1, ? AS firstName2, ? AS lastName2" let arguments = StatementArguments(["Stan", "Laurel", "Oliver", "Hardy"]) let ss = Person.fetch(db, sql, arguments: arguments, adapter: adapter) @@ -133,7 +133,7 @@ class RowConvertibleTests: GRDBTestCase { let dbQueue = try makeDatabaseQueue() dbQueue.inDatabase { db in let adapter = ColumnMapping(["firstName": "firstName1", "lastName": "lastName1"]) - .adapterWithVariants(["bestFriend": ColumnMapping(["firstName": "firstName2", "lastName": "lastName2"])]) + .addingScopes(["bestFriend": ColumnMapping(["firstName": "firstName2", "lastName": "lastName2"])]) let sql = "SELECT ? AS firstName1, ? AS lastName1, ? AS firstName2, ? AS lastName2" let arguments = StatementArguments(["Stan", "Laurel", "Oliver", "Hardy"]) let ss = Person.fetchAll(db, sql, arguments: arguments, adapter: adapter) @@ -153,7 +153,7 @@ class RowConvertibleTests: GRDBTestCase { let dbQueue = try makeDatabaseQueue() dbQueue.inDatabase { db in let adapter = ColumnMapping(["firstName": "firstName1", "lastName": "lastName1"]) - .adapterWithVariants(["bestFriend": ColumnMapping(["firstName": "firstName2", "lastName": "lastName2"])]) + .addingScopes(["bestFriend": ColumnMapping(["firstName": "firstName2", "lastName": "lastName2"])]) let sql = "SELECT ? AS firstName1, ? AS lastName1, ? AS firstName2, ? AS lastName2" let arguments = StatementArguments(["Stan", "Laurel", "Oliver", "Hardy"]) let s = Person.fetchOne(db, sql, arguments: arguments, adapter: adapter)! @@ -219,7 +219,7 @@ class RowConvertibleTests: GRDBTestCase { let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in let adapter = ColumnMapping(["firstName": "firstName1", "lastName": "lastName1"]) - .adapterWithVariants(["bestFriend": ColumnMapping(["firstName": "firstName2", "lastName": "lastName2"])]) + .addingScopes(["bestFriend": ColumnMapping(["firstName": "firstName2", "lastName": "lastName2"])]) let sql = "SELECT ? AS firstName1, ? AS lastName1, ? AS firstName2, ? AS lastName2" let arguments = StatementArguments(["Stan", "Laurel", "Oliver", "Hardy"]) let statement = try db.selectStatement(sql) @@ -240,7 +240,7 @@ class RowConvertibleTests: GRDBTestCase { let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in let adapter = ColumnMapping(["firstName": "firstName1", "lastName": "lastName1"]) - .adapterWithVariants(["bestFriend": ColumnMapping(["firstName": "firstName2", "lastName": "lastName2"])]) + .addingScopes(["bestFriend": ColumnMapping(["firstName": "firstName2", "lastName": "lastName2"])]) let sql = "SELECT ? AS firstName1, ? AS lastName1, ? AS firstName2, ? AS lastName2" let arguments = StatementArguments(["Stan", "Laurel", "Oliver", "Hardy"]) let statement = try db.selectStatement(sql) @@ -261,7 +261,7 @@ class RowConvertibleTests: GRDBTestCase { let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in let adapter = ColumnMapping(["firstName": "firstName1", "lastName": "lastName1"]) - .adapterWithVariants(["bestFriend": ColumnMapping(["firstName": "firstName2", "lastName": "lastName2"])]) + .addingScopes(["bestFriend": ColumnMapping(["firstName": "firstName2", "lastName": "lastName2"])]) let sql = "SELECT ? AS firstName1, ? AS lastName1, ? AS firstName2, ? AS lastName2" let arguments = StatementArguments(["Stan", "Laurel", "Oliver", "Hardy"]) let statement = try db.selectStatement(sql) From 0260a33678f1b0a3b2b8a9263f2e34529cf29f18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 8 Jul 2016 08:46:55 +0200 Subject: [PATCH 4/9] v0.75.0 --- CHANGELOG.md | 37 +++++++++++++++++++++++++++++++++++++ GRDB.swift.podspec | 2 +- README.md | 22 +++++++++++----------- Support/Info.plist | 2 +- 4 files changed, 50 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b80de5af9..eb9573d17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,43 @@ Release Notes ============= +## 0.75.0 + +Released July 8, 2016 + +**Breaking change** + +- Row adapters have been refactored ([documentation](https://github.com/groue/GRDB.swift#row-adapters)). + + ```diff + // Row "variants" have been renamed row "scopes": + struct Row { + - func variant(named name: String) -> Row? + + func scoped(on name: String) -> Row? + } + + // Scope definition: VariantRowAdapter has been renamed ScopeAdapter: + -struct VariantRowAdapter : RowAdapter { + - init(variants: [String: RowAdapter]) + -} + +struct ScopeAdapter : RowAdapter { + + init(_ scopes: [String: RowAdapter]) + +} + + // Adding scopes to an existing adapter: + extension RowAdapter { + - func adapterWithVariants(variants: [String: RowAdapter]) -> RowAdapter + + func addingScopes(scopes: [String: RowAdapter]) -> RowAdapter + } + + // Implementing custom adapters + protocol ConcreteRowAdapter { + - var variants: [String: ConcreteRowAdapter] { get } + + var scopes: [String: ConcreteRowAdapter] { get } + } + ``` + + ## 0.74.0 Released July 6, 2016 diff --git a/GRDB.swift.podspec b/GRDB.swift.podspec index 7c6934d73..02832896a 100644 --- a/GRDB.swift.podspec +++ b/GRDB.swift.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'GRDB.swift' - s.version = '0.74.0' + s.version = '0.75.0' s.license = { :type => 'MIT', :file => 'LICENSE' } s.summary = 'A versatile SQLite toolkit for Swift, with WAL mode support.' s.homepage = 'https://github.com/groue/GRDB.swift' diff --git a/README.md b/README.md index cc7848006..33cad7dc8 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ You should give it a try. --- -**July 6, 2016: GRDB.swift 0.74.0 is out** ([changelog](CHANGELOG.md)). Follow [@groue](http://twitter.com/groue) on Twitter for release announcements and usage tips. +**July 8, 2016: GRDB.swift 0.75.0 is out** ([changelog](CHANGELOG.md)). Follow [@groue](http://twitter.com/groue) on Twitter for release announcements and usage tips. **Requirements**: iOS 8.0+ / OSX 10.9+, Xcode 7.3+ @@ -143,7 +143,7 @@ Documentation **Reference** -- [GRDB Reference](http://cocoadocs.org/docsets/GRDB.swift/0.74.0/index.html) (on cocoadocs.org) +- [GRDB Reference](http://cocoadocs.org/docsets/GRDB.swift/0.75.0/index.html) (on cocoadocs.org) **Getting started** @@ -189,7 +189,7 @@ To use GRDB with CocoaPods, specify in your Podfile: source 'https://github.com/CocoaPods/Specs.git' use_frameworks! -pod 'GRDB.swift', '~> 0.74.0' +pod 'GRDB.swift', '~> 0.75.0' ``` > :point_up: **Note**: [SQLCipher](#encryption) and [custom SQLite builds](#custom-sqlite-builds) are not available via CocoaPods. @@ -202,7 +202,7 @@ pod 'GRDB.swift', '~> 0.74.0' To use GRDB with Carthage, specify in your Cartfile: ``` -github "groue/GRDB.swift" ~> 0.74.0 +github "groue/GRDB.swift" ~> 0.75.0 ``` > :point_up: **Note**: [custom SQLite builds](#custom-sqlite-builds) are not available via Carthage. @@ -311,7 +311,7 @@ let dbQueue = try DatabaseQueue( configuration: config) ``` -See [Configuration](http://cocoadocs.org/docsets/GRDB.swift/0.74.0/Structs/Configuration.html) for more details. +See [Configuration](http://cocoadocs.org/docsets/GRDB.swift/0.75.0/Structs/Configuration.html) for more details. ## Database Pools @@ -391,7 +391,7 @@ let dbPool = try DatabasePool( configuration: config) ``` -See [Configuration](http://cocoadocs.org/docsets/GRDB.swift/0.74.0/Structs/Configuration.html) for more details. +See [Configuration](http://cocoadocs.org/docsets/GRDB.swift/0.75.0/Structs/Configuration.html) for more details. Database pools are more memory-hungry than database queues. See [Memory Management](#memory-management) for more information. @@ -1477,10 +1477,10 @@ for person in Person.fetch(db, sql, adapter: adapter) { For more information about row adapters, see the documentation of: -- [RowAdapter](http://cocoadocs.org/docsets/GRDB.swift/0.74.0/Protocols/RowAdapter.html): the protocol that lets you define your custom row adapters -- [ColumnMapping](http://cocoadocs.org/docsets/GRDB.swift/0.74.0/Structs/ColumnMapping.html): a row adapter that renames row columns -- [SuffixRowAdapter](http://cocoadocs.org/docsets/GRDB.swift/0.74.0/Structs/SuffixRowAdapter.html): a row adapter that hides the first columns of a row -- [ScopeAdapter](http://cocoadocs.org/docsets/GRDB.swift/0.74.0/Structs/ScopeAdapter.html): the row adapter that groups several adapters together to define scopes +- [RowAdapter](http://cocoadocs.org/docsets/GRDB.swift/0.75.0/Protocols/RowAdapter.html): the protocol that lets you define your custom row adapters +- [ColumnMapping](http://cocoadocs.org/docsets/GRDB.swift/0.75.0/Structs/ColumnMapping.html): a row adapter that renames row columns +- [SuffixRowAdapter](http://cocoadocs.org/docsets/GRDB.swift/0.75.0/Structs/SuffixRowAdapter.html): a row adapter that hides the first columns of a row +- [ScopeAdapter](http://cocoadocs.org/docsets/GRDB.swift/0.75.0/Structs/ScopeAdapter.html): the row adapter that groups several adapters together to define scopes ## Raw SQLite Pointers @@ -3375,7 +3375,7 @@ let count2 = dbQueue.inDatabase { db in SQLite concurrency is a wiiide topic. -First have a detailed look at the full API of [DatabaseQueue](http://cocoadocs.org/docsets/GRDB.swift/0.74.0/Classes/DatabaseQueue.html) and [DatabasePool](http://cocoadocs.org/docsets/GRDB.swift/0.74.0/Classes/DatabasePool.html). Both adopt the [DatabaseReader](http://cocoadocs.org/docsets/GRDB.swift/0.74.0/Protocols/DatabaseReader.html) and [DatabaseWriter](http://cocoadocs.org/docsets/GRDB.swift/0.74.0/Protocols/DatabaseWriter.html) protocols, so that you can write code that targets both classes. +First have a detailed look at the full API of [DatabaseQueue](http://cocoadocs.org/docsets/GRDB.swift/0.75.0/Classes/DatabaseQueue.html) and [DatabasePool](http://cocoadocs.org/docsets/GRDB.swift/0.75.0/Classes/DatabasePool.html). Both adopt the [DatabaseReader](http://cocoadocs.org/docsets/GRDB.swift/0.75.0/Protocols/DatabaseReader.html) and [DatabaseWriter](http://cocoadocs.org/docsets/GRDB.swift/0.75.0/Protocols/DatabaseWriter.html) protocols, so that you can write code that targets both classes. If the built-in queues and pools do not fit your needs, or if you can not guarantee that a single queue or pool is accessing your database file, you may have a look at: diff --git a/Support/Info.plist b/Support/Info.plist index 2dcaabc32..5fe056557 100644 --- a/Support/Info.plist +++ b/Support/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 0.74.0 + 0.75.0 CFBundleSignature ???? CFBundleVersion From 4ddc8f950eee8275715318789f94fd1409501b40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 8 Jul 2016 09:52:04 +0200 Subject: [PATCH 5/9] Row adapters: rename AdaptedColumnsDescription to ConcreteColumnMapping --- GRDB/Core/RowAdapter.swift | 68 ++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 33 deletions(-) diff --git a/GRDB/Core/RowAdapter.swift b/GRDB/Core/RowAdapter.swift index ba29aedb2..3b041299e 100644 --- a/GRDB/Core/RowAdapter.swift +++ b/GRDB/Core/RowAdapter.swift @@ -10,33 +10,33 @@ #endif #endif -/// AdaptedColumnsDescription is a type that supports the RowAdapter protocol. -public struct AdaptedColumnsDescription { +/// ConcreteColumnMapping is a type that supports the RowAdapter protocol. +public struct ConcreteColumnMapping { let columns: [(Int, String)] // [(baseRowIndex, adaptedColumn), ...] let lowercaseColumnIndexes: [String: Int] // [adaptedColumn: adaptedRowIndex] - /// Creates an AdaptedColumnsDescription from an array of (index, name) + /// Creates an ConcreteColumnMapping from an array of (index, name) /// pairs. In each pair: /// /// - index is the index of a column in an original row /// - name is the name of the column in an adapted row /// - /// For example, the following AdaptedColumnsDescription defines two + /// For example, the following ConcreteColumnMapping defines two /// columns, "foo" and "bar", that load from the original columns at /// indexes 1 and 2: /// - /// AdaptedColumnsDescription([(1, "foo"), (2, "bar")]) + /// ConcreteColumnMapping([(1, "foo"), (2, "bar")]) /// /// Use it in your custom RowAdapter type: /// /// struct FooBarAdapter : RowAdapter { /// func concreteRowAdapter(with statement: SelectStatement) throws -> ConcreteRowAdapter { - /// return AdaptedColumnsDescription([(1, "foo"), (2, "bar")]) + /// return ConcreteColumnMapping([(1, "foo"), (2, "bar")]) /// } /// } /// - /// // - /// Row.fetchOne(db, "SELECT 1, 2, 3", adapter: FooBarAdapter()) + /// // + /// Row.fetchOne(db, "SELECT NULL, 'foo', 'bar'", adapter: FooBarAdapter()) public init(columns: [(Int, String)]) { self.columns = columns self.lowercaseColumnIndexes = Dictionary(keyValueSequence: columns.enumerate().map { ($1.1.lowercaseString, $0) }.reverse()) @@ -62,10 +62,10 @@ public struct AdaptedColumnsDescription { } } -/// AdaptedColumnsDescription adopts ConcreteRowAdapter -extension AdaptedColumnsDescription : ConcreteRowAdapter { +/// ConcreteColumnMapping adopts ConcreteRowAdapter +extension ConcreteColumnMapping : ConcreteRowAdapter { /// Part of the ConcreteRowAdapter protocol; returns self. - public var adaptedColumnsDescription: AdaptedColumnsDescription { + public var concreteColumnMapping: ConcreteColumnMapping { return self } @@ -75,23 +75,19 @@ extension AdaptedColumnsDescription : ConcreteRowAdapter { } } -struct ConcreteScopeAdapter : ConcreteRowAdapter { - let adaptedColumnsDescription: AdaptedColumnsDescription - let scopes: [String: ConcreteRowAdapter] -} - /// ConcreteRowAdapter is a protocol that supports the RowAdapter protocol. /// -/// GRBD ships with a concrete type that adopts the ConcreteRowAdapter protocol: -/// AdaptedColumnsDescription. +/// GRBD ships with a ready-made type that adopts this protocol: +/// ConcreteColumnMapping. /// /// It is unlikely that you need to write your custom type that adopts /// this protocol. public protocol ConcreteRowAdapter { - // An AdaptedColumnsDescription - var adaptedColumnsDescription: AdaptedColumnsDescription { get } + /// A ConcreteColumnMapping that defines how to map a column name to a + /// column in an original row. + var concreteColumnMapping: ConcreteColumnMapping { get } - /// A dictionary whose keys are scope names. + /// A dictionary of scopes var scopes: [String: ConcreteRowAdapter] { get } } @@ -120,7 +116,7 @@ public protocol RowAdapter { /// adapter has to be applied. /// /// The result is a value that adopts ConcreteRowAdapter, such as - /// AdaptedColumnsDescription. + /// ConcreteColumnMapping. /// /// For example: /// @@ -129,7 +125,7 @@ public protocol RowAdapter { /// // original row. /// struct FirstColumnAdapter : RowAdapter { /// func concreteRowAdapter(with statement: SelectStatement) throws -> ConcreteRowAdapter { - /// return AdaptedColumnsDescription(columns: [(0, "foo")]) + /// return ConcreteColumnMapping(columns: [(0, "foo")]) /// } /// } /// @@ -178,7 +174,7 @@ public struct ColumnMapping : RowAdapter { return (index, mappedColumn) } .sort { $0.0 < $1.0 } - return AdaptedColumnsDescription(columns: columns) + return ConcreteColumnMapping(columns: columns) } } @@ -205,7 +201,7 @@ public struct SuffixRowAdapter : RowAdapter { /// Part of the RowAdapter protocol public func concreteRowAdapter(with statement: SelectStatement) throws -> ConcreteRowAdapter { GRDBPrecondition(index <= statement.columnCount, "Column index is out of range") - return AdaptedColumnsDescription(columns: statement.columnNames.suffixFrom(index).enumerate().map { ($0 + index, $1) }) + return ConcreteColumnMapping(columns: statement.columnNames.suffixFrom(index).enumerate().map { ($0 + index, $1) }) } } @@ -261,11 +257,17 @@ public struct ScopeAdapter : RowAdapter { try concreteAdapterScopes[name] = adapter.concreteRowAdapter(with: statement) } return ConcreteScopeAdapter( - adaptedColumnsDescription: mainConcreteAdapter.adaptedColumnsDescription, + concreteColumnMapping: mainConcreteAdapter.concreteColumnMapping, scopes: concreteAdapterScopes) } } +/// The concrete row adapter for ScopeAdapter +struct ConcreteScopeAdapter : ConcreteRowAdapter { + let concreteColumnMapping: ConcreteColumnMapping + let scopes: [String: ConcreteRowAdapter] +} + extension Row { /// Builds a row from a base row and a statement adapter convenience init(baseRow: Row, concreteRowAdapter: ConcreteRowAdapter) { @@ -284,32 +286,32 @@ extension Row { struct AdapterRowImpl : RowImpl { let baseRow: Row let concreteRowAdapter: ConcreteRowAdapter - let adaptedColumnsDescription: AdaptedColumnsDescription + let concreteColumnMapping: ConcreteColumnMapping init(baseRow: Row, concreteRowAdapter: ConcreteRowAdapter) { self.baseRow = baseRow self.concreteRowAdapter = concreteRowAdapter - self.adaptedColumnsDescription = concreteRowAdapter.adaptedColumnsDescription + self.concreteColumnMapping = concreteRowAdapter.concreteColumnMapping } var count: Int { - return adaptedColumnsDescription.count + return concreteColumnMapping.count } func databaseValue(atIndex index: Int) -> DatabaseValue { - return baseRow.databaseValue(atIndex: adaptedColumnsDescription.baseColumIndex(adaptedIndex: index)) + return baseRow.databaseValue(atIndex: concreteColumnMapping.baseColumIndex(adaptedIndex: index)) } func dataNoCopy(atIndex index:Int) -> NSData? { - return baseRow.dataNoCopy(atIndex: adaptedColumnsDescription.baseColumIndex(adaptedIndex: index)) + return baseRow.dataNoCopy(atIndex: concreteColumnMapping.baseColumIndex(adaptedIndex: index)) } func columnName(atIndex index: Int) -> String { - return adaptedColumnsDescription.columnName(adaptedIndex: index) + return concreteColumnMapping.columnName(adaptedIndex: index) } func indexOfColumn(named name: String) -> Int? { - return adaptedColumnsDescription.adaptedIndexOfColumn(named: name) + return concreteColumnMapping.adaptedIndexOfColumn(named: name) } func scoped(on name: String) -> Row? { From b78c5b32c6f6eb13761964a085d33aee4b507ab2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 8 Jul 2016 10:01:23 +0200 Subject: [PATCH 6/9] Documentation --- GRDB/Core/Row.swift | 8 ++++---- GRDB/Core/RowAdapter.swift | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/GRDB/Core/Row.swift b/GRDB/Core/Row.swift index 8f7569333..b04417f1e 100644 --- a/GRDB/Core/Row.swift +++ b/GRDB/Core/Row.swift @@ -396,22 +396,22 @@ extension Row { // MARK: - Scopes /// Returns a scoped row, if the row was fetched along with a row adapter - /// that defined this scope. + /// that defines this scope. /// /// // Two adapters /// let fooAdapter = ColumnMapping(["value": "foo"]) /// let barAdapter = ColumnMapping(["value": "bar"]) /// - /// // An adapter with scopes + /// // Define scopes /// let adapter = ScopeAdapter([ /// "foo": fooAdapter, /// "bar": barAdapter]) /// - /// // Fetch a row + /// // Fetch /// let sql = "SELECT 'foo' AS foo, 'bar' AS bar" /// let row = Row.fetchOne(db, sql, adapter: adapter)! /// - /// // Two scoped rows: + /// // Scoped rows: /// if let fooRow = row.scoped(on: "foo") { /// fooRow.value(named: "value") // "foo" /// } diff --git a/GRDB/Core/RowAdapter.swift b/GRDB/Core/RowAdapter.swift index 3b041299e..18bf051f7 100644 --- a/GRDB/Core/RowAdapter.swift +++ b/GRDB/Core/RowAdapter.swift @@ -205,22 +205,22 @@ public struct SuffixRowAdapter : RowAdapter { } } -/// ScopeAdapter is a row adapter that lets you add scoped adaptations to rows. +/// ScopeAdapter is a row adapter that lets you define scopes on rows. /// /// // Two adapters /// let fooAdapter = ColumnMapping(["value": "foo"]) /// let barAdapter = ColumnMapping(["value": "bar"]) /// -/// // An adapter with named scopes +/// // Define scopes /// let adapter = ScopeAdapter([ /// "foo": fooAdapter, /// "bar": barAdapter]) /// -/// // Fetch a row +/// // Fetch /// let sql = "SELECT 'foo' AS foo, 'bar' AS bar" /// let row = Row.fetchOne(db, sql, adapter: adapter)! /// -/// // Two scoped rows: +/// // Scoped rows: /// if let fooRow = row.scoped(on: "foo") { /// fooRow.value(named: "value") // "foo" /// } @@ -240,7 +240,7 @@ public struct ScopeAdapter : RowAdapter { /// - parameter scopes: A dictionary that maps scope names to /// row adapters. public init(_ scopes: [String: RowAdapter]) { - self.mainAdapter = SuffixRowAdapter(fromIndex: 0) + self.mainAdapter = SuffixRowAdapter(fromIndex: 0) // Use SuffixRowAdapter(fromIndex: 0) as the identity adapter self.scopes = scopes } From e664ab18ee1a76460f13a944caeb9708a242db1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 8 Jul 2016 14:30:45 +0200 Subject: [PATCH 7/9] Fixes #80 --- GRDB/Core/Database.swift | 12 ++++++++- .../DatabasePoolConcurrencyTests.swift | 26 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index 7df1bb0aa..71ba341de 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -1100,7 +1100,17 @@ final class StatementCompilationObserver { observer.databaseEventKinds.append(.Delete(tableName: String.fromCString(CString1)!)) case SQLITE_UPDATE: let observer = unsafeBitCast(observerPointer, StatementCompilationObserver.self) - observer.insertUpdateEventKind(tableName: String.fromCString(CString1)!, columnName: String.fromCString(CString2)!) + let tableName = String.fromCString(CString1)! + + // Ignore all changes to sqlite_master. + // + // The schema changes are already processed by the SQLITE_DROP_TABLE, SQLITE_CREATE_INDEX, etc. (see above). + // + // Plus SQLite will sometimes announce that SELECT statements + // perform changes to sqlite_master. See DatabasePoolConcurrencyTests.testIssue80() + if tableName != "sqlite_master" { + observer.insertUpdateEventKind(tableName: tableName, columnName: String.fromCString(CString2)!) + } case SQLITE_SAVEPOINT: let observer = unsafeBitCast(observerPointer, StatementCompilationObserver.self) let name = String.fromCString(CString2)! diff --git a/Tests/Public/Core/DatabasePool/DatabasePoolConcurrencyTests.swift b/Tests/Public/Core/DatabasePool/DatabasePoolConcurrencyTests.swift index 6a5c49d93..bbca5fa2c 100644 --- a/Tests/Public/Core/DatabasePool/DatabasePoolConcurrencyTests.swift +++ b/Tests/Public/Core/DatabasePool/DatabasePoolConcurrencyTests.swift @@ -434,4 +434,30 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { } } } + + func testIssue80() { + // See https://github.com/groue/GRDB.swift/issues/80 + // + // Here we test that `SELECT * FROM search`, which is announced by + // SQLite has a statement that modifies the sqlite_master table, is + // still allowed as a regular select statement. + assertNoError { + do { + // This test uses a database pool, a connection for creating the + // search virtual table, and another conncetion for reading. + // + // This is the tested setup: don't change it. + + let dbPool = try makeDatabasePool() + dbPool.write { db in + try! db.execute("CREATE VIRTUAL TABLE search USING fts3(title, tokenize = unicode61)") + } + + let rows = dbPool.read { db -> [Row] in + return Row.fetchAll(db, "SELECT * FROM search") + } + print(rows) + } + } + } } From 252ec6fe59b6b77c9d324397b67002ad8ff33122 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 8 Jul 2016 14:33:24 +0200 Subject: [PATCH 8/9] v0.75.1 --- CHANGELOG.md | 9 +++++++++ GRDB.swift.podspec | 2 +- README.md | 22 +++++++++++----------- Support/Info.plist | 2 +- 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb9573d17..cf2dd8ada 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,15 @@ Release Notes ============= +## 0.75.1 + +Released July 8, 2016 + +**Fixed** + +- Fixed a crash that would happen when performing a full text search in a DatabasePool (fixes [#80](https://github.com/groue/GRDB.swift/issues/80)) + + ## 0.75.0 Released July 8, 2016 diff --git a/GRDB.swift.podspec b/GRDB.swift.podspec index 02832896a..81ecef495 100644 --- a/GRDB.swift.podspec +++ b/GRDB.swift.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'GRDB.swift' - s.version = '0.75.0' + s.version = '0.75.1' s.license = { :type => 'MIT', :file => 'LICENSE' } s.summary = 'A versatile SQLite toolkit for Swift, with WAL mode support.' s.homepage = 'https://github.com/groue/GRDB.swift' diff --git a/README.md b/README.md index 33cad7dc8..588670bf2 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ You should give it a try. --- -**July 8, 2016: GRDB.swift 0.75.0 is out** ([changelog](CHANGELOG.md)). Follow [@groue](http://twitter.com/groue) on Twitter for release announcements and usage tips. +**July 8, 2016: GRDB.swift 0.75.1 is out** ([changelog](CHANGELOG.md)). Follow [@groue](http://twitter.com/groue) on Twitter for release announcements and usage tips. **Requirements**: iOS 8.0+ / OSX 10.9+, Xcode 7.3+ @@ -143,7 +143,7 @@ Documentation **Reference** -- [GRDB Reference](http://cocoadocs.org/docsets/GRDB.swift/0.75.0/index.html) (on cocoadocs.org) +- [GRDB Reference](http://cocoadocs.org/docsets/GRDB.swift/0.75.1/index.html) (on cocoadocs.org) **Getting started** @@ -189,7 +189,7 @@ To use GRDB with CocoaPods, specify in your Podfile: source 'https://github.com/CocoaPods/Specs.git' use_frameworks! -pod 'GRDB.swift', '~> 0.75.0' +pod 'GRDB.swift', '~> 0.75.1' ``` > :point_up: **Note**: [SQLCipher](#encryption) and [custom SQLite builds](#custom-sqlite-builds) are not available via CocoaPods. @@ -202,7 +202,7 @@ pod 'GRDB.swift', '~> 0.75.0' To use GRDB with Carthage, specify in your Cartfile: ``` -github "groue/GRDB.swift" ~> 0.75.0 +github "groue/GRDB.swift" ~> 0.75.1 ``` > :point_up: **Note**: [custom SQLite builds](#custom-sqlite-builds) are not available via Carthage. @@ -311,7 +311,7 @@ let dbQueue = try DatabaseQueue( configuration: config) ``` -See [Configuration](http://cocoadocs.org/docsets/GRDB.swift/0.75.0/Structs/Configuration.html) for more details. +See [Configuration](http://cocoadocs.org/docsets/GRDB.swift/0.75.1/Structs/Configuration.html) for more details. ## Database Pools @@ -391,7 +391,7 @@ let dbPool = try DatabasePool( configuration: config) ``` -See [Configuration](http://cocoadocs.org/docsets/GRDB.swift/0.75.0/Structs/Configuration.html) for more details. +See [Configuration](http://cocoadocs.org/docsets/GRDB.swift/0.75.1/Structs/Configuration.html) for more details. Database pools are more memory-hungry than database queues. See [Memory Management](#memory-management) for more information. @@ -1477,10 +1477,10 @@ for person in Person.fetch(db, sql, adapter: adapter) { For more information about row adapters, see the documentation of: -- [RowAdapter](http://cocoadocs.org/docsets/GRDB.swift/0.75.0/Protocols/RowAdapter.html): the protocol that lets you define your custom row adapters -- [ColumnMapping](http://cocoadocs.org/docsets/GRDB.swift/0.75.0/Structs/ColumnMapping.html): a row adapter that renames row columns -- [SuffixRowAdapter](http://cocoadocs.org/docsets/GRDB.swift/0.75.0/Structs/SuffixRowAdapter.html): a row adapter that hides the first columns of a row -- [ScopeAdapter](http://cocoadocs.org/docsets/GRDB.swift/0.75.0/Structs/ScopeAdapter.html): the row adapter that groups several adapters together to define scopes +- [RowAdapter](http://cocoadocs.org/docsets/GRDB.swift/0.75.1/Protocols/RowAdapter.html): the protocol that lets you define your custom row adapters +- [ColumnMapping](http://cocoadocs.org/docsets/GRDB.swift/0.75.1/Structs/ColumnMapping.html): a row adapter that renames row columns +- [SuffixRowAdapter](http://cocoadocs.org/docsets/GRDB.swift/0.75.1/Structs/SuffixRowAdapter.html): a row adapter that hides the first columns of a row +- [ScopeAdapter](http://cocoadocs.org/docsets/GRDB.swift/0.75.1/Structs/ScopeAdapter.html): the row adapter that groups several adapters together to define scopes ## Raw SQLite Pointers @@ -3375,7 +3375,7 @@ let count2 = dbQueue.inDatabase { db in SQLite concurrency is a wiiide topic. -First have a detailed look at the full API of [DatabaseQueue](http://cocoadocs.org/docsets/GRDB.swift/0.75.0/Classes/DatabaseQueue.html) and [DatabasePool](http://cocoadocs.org/docsets/GRDB.swift/0.75.0/Classes/DatabasePool.html). Both adopt the [DatabaseReader](http://cocoadocs.org/docsets/GRDB.swift/0.75.0/Protocols/DatabaseReader.html) and [DatabaseWriter](http://cocoadocs.org/docsets/GRDB.swift/0.75.0/Protocols/DatabaseWriter.html) protocols, so that you can write code that targets both classes. +First have a detailed look at the full API of [DatabaseQueue](http://cocoadocs.org/docsets/GRDB.swift/0.75.1/Classes/DatabaseQueue.html) and [DatabasePool](http://cocoadocs.org/docsets/GRDB.swift/0.75.1/Classes/DatabasePool.html). Both adopt the [DatabaseReader](http://cocoadocs.org/docsets/GRDB.swift/0.75.1/Protocols/DatabaseReader.html) and [DatabaseWriter](http://cocoadocs.org/docsets/GRDB.swift/0.75.1/Protocols/DatabaseWriter.html) protocols, so that you can write code that targets both classes. If the built-in queues and pools do not fit your needs, or if you can not guarantee that a single queue or pool is accessing your database file, you may have a look at: diff --git a/Support/Info.plist b/Support/Info.plist index 5fe056557..eb90c95cc 100644 --- a/Support/Info.plist +++ b/Support/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 0.75.0 + 0.75.1 CFBundleSignature ???? CFBundleVersion From 88e8c2ba332fde3c19397a8abd6e810450795c42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 8 Jul 2016 16:10:38 +0200 Subject: [PATCH 9/9] #80 cleanup --- .../DatabasePoolConcurrencyTests.swift | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/Tests/Public/Core/DatabasePool/DatabasePoolConcurrencyTests.swift b/Tests/Public/Core/DatabasePool/DatabasePoolConcurrencyTests.swift index bbca5fa2c..8e5bdd74b 100644 --- a/Tests/Public/Core/DatabasePool/DatabasePoolConcurrencyTests.swift +++ b/Tests/Public/Core/DatabasePool/DatabasePoolConcurrencyTests.swift @@ -442,21 +442,17 @@ class DatabasePoolConcurrencyTests: GRDBTestCase { // SQLite has a statement that modifies the sqlite_master table, is // still allowed as a regular select statement. assertNoError { - do { - // This test uses a database pool, a connection for creating the - // search virtual table, and another conncetion for reading. - // - // This is the tested setup: don't change it. - - let dbPool = try makeDatabasePool() - dbPool.write { db in - try! db.execute("CREATE VIRTUAL TABLE search USING fts3(title, tokenize = unicode61)") - } - - let rows = dbPool.read { db -> [Row] in - return Row.fetchAll(db, "SELECT * FROM search") - } - print(rows) + // This test uses a database pool, a connection for creating the + // search virtual table, and another connection for reading. + // + // This is the tested setup: don't change it. + + let dbPool = try makeDatabasePool() + try dbPool.write { db in + try db.execute("CREATE VIRTUAL TABLE search USING fts3(title, tokenize = unicode61)") + } + dbPool.read { db in + _ = Row.fetchAll(db, "SELECT * FROM search") } } }