mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
Row adapters: rename "variants" to "scopes"
This commit is contained in:
1 parent
a658599406
commit
358c3ab0c3
8 files changed
+186
-167
No files matched your search
+43
-20
@@ -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<String> { get }
|
||||
var scopeNames: Set<String> { 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<String> {
|
||||
var scopeNames: Set<String> {
|
||||
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<String> {
|
||||
var scopeNames: Set<String> {
|
||||
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<String> {
|
||||
var scopeNames: Set<String> {
|
||||
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<String> {
|
||||
var scopeNames: Set<String> {
|
||||
return []
|
||||
}
|
||||
|
||||
|
||||
+38
-41
@@ -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<String> {
|
||||
return Set(concreteRowAdapter.variants.keys)
|
||||
var scopeNames: Set<String> {
|
||||
return Set(concreteRowAdapter.scopes.keys)
|
||||
}
|
||||
|
||||
func copy(row: Row) -> Row {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user