mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-29 04:24:21 +00:00
RowAdapter documentation
This commit is contained in:
1 parent
390cf44a88
commit
a882a5f346
4 files changed
+158
-132
No files matched your search
+71
-28
@@ -12,10 +12,10 @@
|
||||
|
||||
/// AdaptedColumnsDescription is a type that supports the RowAdapter protocol.
|
||||
public struct AdaptedColumnsDescription {
|
||||
public let columns: [(Int, String)] // [(baseRowIndex, adaptedColumn), ...]
|
||||
let columns: [(Int, String)] // [(baseRowIndex, adaptedColumn), ...]
|
||||
let lowercaseColumnIndexes: [String: Int] // [adaptedColumn: adaptedRowIndex]
|
||||
|
||||
/// Creates a AdaptedColumnsDescription from an array of (index, name)
|
||||
/// Creates an AdaptedColumnsDescription from an array of (index, name)
|
||||
/// pairs. In each pair:
|
||||
///
|
||||
/// - index is the index of a column in an original row
|
||||
@@ -23,9 +23,9 @@ public struct AdaptedColumnsDescription {
|
||||
///
|
||||
/// For example, the following AdaptedColumnsDescription defines two
|
||||
/// columns, "foo" and "bar", that load from the original columns at
|
||||
/// indexes 0 and 1:
|
||||
/// indexes 1 and 2:
|
||||
///
|
||||
/// AdaptedColumnsDescription([(0, "foo"), (1, "bar")])
|
||||
/// AdaptedColumnsDescription([(1, "foo"), (2, "bar")])
|
||||
///
|
||||
/// Use it in your custom RowAdapter type:
|
||||
///
|
||||
@@ -34,11 +34,11 @@ public struct AdaptedColumnsDescription {
|
||||
/// // original row.
|
||||
/// struct FooBarAdapter : RowAdapter {
|
||||
/// func concreteRowAdapter(with statement: SelectStatement) throws -> ConcreteRowAdapter {
|
||||
/// return AdaptedColumnsDescription([(0, "foo"), (1, "bar")])
|
||||
/// return AdaptedColumnsDescription([(1, "foo"), (2, "bar")])
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// // <Row foo:1 bar: 2>
|
||||
/// // <Row foo:2 bar: 3>
|
||||
/// Row.fetchOne(db, "SELECT 1, 2, 3", adapter: FooBarAdapter())
|
||||
public init(columns: [(Int, String)]) {
|
||||
self.columns = columns
|
||||
@@ -65,6 +65,7 @@ public struct AdaptedColumnsDescription {
|
||||
}
|
||||
}
|
||||
|
||||
/// AdaptedColumnsDescription adopts ConcreteRowAdapter
|
||||
extension AdaptedColumnsDescription : ConcreteRowAdapter {
|
||||
/// Part of the ConcreteRowAdapter protocol; returns self.
|
||||
public var adaptedColumnsDescription: AdaptedColumnsDescription {
|
||||
@@ -78,12 +79,8 @@ extension AdaptedColumnsDescription : ConcreteRowAdapter {
|
||||
}
|
||||
|
||||
struct ConcreteVariantRowAdapter : ConcreteRowAdapter {
|
||||
let mainAdapter: ConcreteRowAdapter
|
||||
let adaptedColumnsDescription: AdaptedColumnsDescription
|
||||
let variants: [String: ConcreteRowAdapter]
|
||||
|
||||
var adaptedColumnsDescription: AdaptedColumnsDescription {
|
||||
return mainAdapter.adaptedColumnsDescription
|
||||
}
|
||||
}
|
||||
|
||||
/// ConcreteRowAdapter is a protocol that supports the RowAdapter protocol.
|
||||
@@ -94,18 +91,13 @@ struct ConcreteVariantRowAdapter : ConcreteRowAdapter {
|
||||
/// It is unlikely that you need to write your custom type that adopts
|
||||
/// this protocol.
|
||||
public protocol ConcreteRowAdapter {
|
||||
// A AdaptedColumnsDescription that defines the adapted columns.
|
||||
// An AdaptedColumnsDescription
|
||||
var adaptedColumnsDescription: AdaptedColumnsDescription { get }
|
||||
|
||||
/// Default implementation return the empty dictionary.
|
||||
/// A dictionary whose keys are variant names.
|
||||
var variants: [String: ConcreteRowAdapter] { get }
|
||||
}
|
||||
|
||||
extension ConcreteRowAdapter {
|
||||
/// Default implementation return the empty dictionary.
|
||||
var variants: [String: ConcreteRowAdapter] { return [:] }
|
||||
}
|
||||
|
||||
/// RowAdapter is a protocol that helps two incompatible row interfaces working
|
||||
/// together.
|
||||
///
|
||||
@@ -150,6 +142,19 @@ public protocol RowAdapter {
|
||||
func concreteRowAdapter(with statement: SelectStatement) throws -> ConcreteRowAdapter
|
||||
}
|
||||
|
||||
extension RowAdapter {
|
||||
/// Returns an adapter based on self, with added variants.
|
||||
///
|
||||
/// If self already defines variants, the added variants replace
|
||||
/// eventual existing variants with the same name.
|
||||
///
|
||||
/// - parameter variants: A dictionary that maps variant names to
|
||||
/// row adapters.
|
||||
public func adapterWithVariants(variants: [String: RowAdapter]) -> RowAdapter {
|
||||
return VariantRowAdapter(mainAdapter: self, variants: variants)
|
||||
}
|
||||
}
|
||||
|
||||
/// ColumnMapping is a row adapter that maps column names.
|
||||
///
|
||||
/// let adapter = ColumnMapping(["foo": "bar"])
|
||||
@@ -159,7 +164,7 @@ public protocol RowAdapter {
|
||||
/// Row.fetchOne(db, sql, adapter: adapter)
|
||||
public struct ColumnMapping : RowAdapter {
|
||||
/// The column names mapping, from adapted names to original names.
|
||||
public let mapping: [String: String]
|
||||
let mapping: [String: String]
|
||||
|
||||
/// Creates a ColumnMapping with a dictionary that maps adapted column names
|
||||
/// to original column names.
|
||||
@@ -176,7 +181,7 @@ public struct ColumnMapping : RowAdapter {
|
||||
}
|
||||
return (index, mappedColumn)
|
||||
}
|
||||
.sort { return $0.0 < $1.0 }
|
||||
.sort { $0.0 < $1.0 }
|
||||
return AdaptedColumnsDescription(columns: columns)
|
||||
}
|
||||
}
|
||||
@@ -190,7 +195,7 @@ public struct ColumnMapping : RowAdapter {
|
||||
/// Row.fetchOne(db, sql, adapter: adapter)
|
||||
public struct SuffixRowAdapter : RowAdapter {
|
||||
/// The suffix index
|
||||
public let index: Int
|
||||
let index: Int
|
||||
|
||||
/// Creates a SuffixRowAdapter that hides all columns before the
|
||||
/// provided index.
|
||||
@@ -206,15 +211,54 @@ public struct SuffixRowAdapter : RowAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
/// VariantRowAdapter is a row adapter that lets you add adapted variants to
|
||||
/// fetched rows.
|
||||
///
|
||||
/// // Two adapters
|
||||
/// let fooAdapter = ColumnMapping(["value": "foo"])
|
||||
/// let barAdapter = ColumnMapping(["value": "bar"])
|
||||
///
|
||||
/// // An adapter with named variants
|
||||
/// let variants: [String: RowAdapter] = [
|
||||
/// "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") {
|
||||
/// fooRow.value(named: "value") // "foo"
|
||||
/// }
|
||||
/// if let barRow = row.variant(named: "bar") {
|
||||
/// barRow.value(named: "value") // "bar"
|
||||
/// }
|
||||
public struct VariantRowAdapter : RowAdapter {
|
||||
public let mainAdapter: RowAdapter
|
||||
public let variants: [String: RowAdapter]
|
||||
|
||||
public init(_ mainAdapter: RowAdapter? = nil, variants: [String: RowAdapter]) {
|
||||
self.mainAdapter = mainAdapter ?? SuffixRowAdapter(fromIndex: 0)
|
||||
/// The main adapter
|
||||
let mainAdapter: RowAdapter
|
||||
|
||||
/// The variant adapters
|
||||
let variants: [String: RowAdapter]
|
||||
|
||||
/// Creates a variant adapter.
|
||||
///
|
||||
/// - parameters:
|
||||
/// - mainAdapter: An eventual row adapter to be applied by default
|
||||
/// - variants: A dictionary that maps variant names to row adapters.
|
||||
public init(variants: [String: RowAdapter]) {
|
||||
self.mainAdapter = SuffixRowAdapter(fromIndex: 0)
|
||||
self.variants = variants
|
||||
}
|
||||
|
||||
|
||||
init(mainAdapter: RowAdapter, variants: [String: RowAdapter]) {
|
||||
self.mainAdapter = mainAdapter
|
||||
self.variants = variants
|
||||
}
|
||||
|
||||
/// Part of the RowAdapter protocol
|
||||
public func concreteRowAdapter(with statement: SelectStatement) throws -> ConcreteRowAdapter {
|
||||
let mainConcreteRowAdapter = try mainAdapter.concreteRowAdapter(with: statement)
|
||||
var variantConcreteRowAdapters = mainConcreteRowAdapter.variants
|
||||
@@ -222,7 +266,7 @@ public struct VariantRowAdapter : RowAdapter {
|
||||
try variantConcreteRowAdapters[name] = adapter.concreteRowAdapter(with: statement)
|
||||
}
|
||||
return ConcreteVariantRowAdapter(
|
||||
mainAdapter: mainConcreteRowAdapter,
|
||||
adaptedColumnsDescription: mainConcreteRowAdapter.adaptedColumnsDescription,
|
||||
variants: variantConcreteRowAdapters)
|
||||
}
|
||||
}
|
||||
@@ -243,7 +287,6 @@ extension Row {
|
||||
}
|
||||
|
||||
struct AdapterRowImpl : RowImpl {
|
||||
|
||||
let baseRow: Row
|
||||
let concreteRowAdapter: ConcreteRowAdapter
|
||||
let adaptedColumnsDescription: AdaptedColumnsDescription
|
||||
|
||||
@@ -1369,13 +1369,13 @@ citizenshipsPk.rowIDColumn // nil
|
||||
|
||||
## Row Adapters
|
||||
|
||||
**Row adapters let you map column names for easier row consumption.**
|
||||
**Row adapters let you transform rows for easier row consumption.**
|
||||
|
||||
They basically help two incompatible row interfaces to work together. For example, a row consumer expects a column named "consumed", but the produced column has a column named "produced":
|
||||
|
||||
```swift
|
||||
// An adapter that maps column 'produced' to column 'consumed':
|
||||
let adapter = RowAdapter(mapping: ["consumed": "produced"])
|
||||
let adapter = ColumnMapping(["consumed": "produced"])
|
||||
|
||||
// Fetch a column named 'produced', and apply adapter:
|
||||
let row = Row.fetchOne(db, "SELECT 'Hello' AS produced", adapter: adapter)!
|
||||
@@ -1384,76 +1384,61 @@ let row = Row.fetchOne(db, "SELECT 'Hello' AS produced", adapter: adapter)!
|
||||
row.value(named: "consumed") // "Hello"
|
||||
```
|
||||
|
||||
**Row adapters can also define "sub rows".** Sub rows help several consumers feed on a single row:
|
||||
|
||||
**Row adapters can also define variants of a same row.** Variants help several consumers feed on a single row and can reveal useful on joined queries.
|
||||
|
||||
For example, let's build a joined query which loads books along with their author:
|
||||
|
||||
```swift
|
||||
let sql = "SELECT books.id, books.title, books.authorID, " +
|
||||
" persons.name AS authorName " +
|
||||
let sql = "SELECT books.id, books.title, " +
|
||||
" books.authorID, persons.name AS authorName " +
|
||||
"FROM books " +
|
||||
"JOIN persons ON books.authorID = persons.id"
|
||||
```
|
||||
|
||||
let authorMapping = ["id": "authorID", "name": "authorName"]
|
||||
let adapter = RowAdapter(subrows: ["author": authorMapping])
|
||||
The raw author columns are "authorID" and "authorName". Let's say that we prefer to consume them as "id" and "name". For that we define a row variant named "author":
|
||||
|
||||
```swift
|
||||
let authorAdapter = ColumnMapping(["id": "authorID", "name": "authorName"])
|
||||
let adapter = VariantRowAdapter(variants: ["author": authorAdapter])
|
||||
```
|
||||
|
||||
Use the `Row.variant(named:)` method to load the "author" variant:
|
||||
|
||||
```swift
|
||||
for row in Row.fetch(db, sql, adapter: adapter) {
|
||||
// No mapping is applied to the fetched row:
|
||||
// <Row id:1 title:"Moby-Dick" authorID:10 authorName:"Melville">
|
||||
print(row)
|
||||
// The fetched row, without adaptation:
|
||||
row.value(named: "id") // 1
|
||||
row.value(named: "title") // Moby-Dick
|
||||
row.value(named: "authorID") // 10
|
||||
row.value(named: "authorName") // Melville
|
||||
|
||||
if let authorRow = row.subrow(named: "author") {
|
||||
// <Row id:10 name:"Melville">
|
||||
print(authorRow)
|
||||
// The "author" variant, with mapped columns:
|
||||
if let authorRow = row.variant(named: "author") {
|
||||
authorRow.value(named: "id") // 10
|
||||
authorRow.value(named: "name") // Melville
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
The last SQL and adapter can be very useful with [RowConvertible](#rowconvertible-protocol) types. For example:
|
||||
And 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:
|
||||
|
||||
```swift
|
||||
for book in Book.fetch(db, sql, adapter: adapter) {
|
||||
book.title // Moby-Dick
|
||||
book.author?.name // Melville
|
||||
book.title // Moby-Dick
|
||||
book.author?.name // Melville
|
||||
}
|
||||
```
|
||||
|
||||
All we need are two regular RowConvertible types:
|
||||
|
||||
```swift
|
||||
class Person : RowConvertible {
|
||||
var id: Int64?
|
||||
var name: String
|
||||
|
||||
init(_ row: Row) {
|
||||
id = row.value(named: "id")
|
||||
name = row.value(named: "name")
|
||||
}
|
||||
}
|
||||
|
||||
class Book : RowConvertible {
|
||||
var id: Int64?
|
||||
var title: String
|
||||
var author: Person?
|
||||
|
||||
init(_ row: Row) {
|
||||
id = row.value(named: "id")
|
||||
title = row.value(named: "title")
|
||||
|
||||
// Consume the subrow:
|
||||
if let authorRow = row.subrow(named: "author") {
|
||||
author = Person(authorRow)
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Note that the Person and Book types can still be fetched without row adapters:
|
||||
Note that Person and Book can still be fetched without row adapters:
|
||||
|
||||
```swift
|
||||
let books = Book.fetchAll(db, "SELECT * FROM books")
|
||||
let persons = Person.fetchAll(db, "SELECT * FROM persons")
|
||||
```
|
||||
|
||||
**You can mix a main mapping with subrows:**
|
||||
|
||||
**You can mix a main adapter with variant adapters:**
|
||||
|
||||
```swift
|
||||
let sql = "SELECT main.id AS mainID, main.name AS mainName, " +
|
||||
@@ -1461,20 +1446,23 @@ let sql = "SELECT main.id AS mainID, main.name AS mainName, " +
|
||||
"FROM persons main " +
|
||||
"LEFT JOIN persons friend ON p.bestFriendID = f.id"
|
||||
|
||||
let mainMapping = ["id": "mainID", "name": "mainName"]
|
||||
let bestFriendMapping = ["id": "friendID", "name": "friendName"]
|
||||
let adapter = RowAdapter(
|
||||
mapping: mainMapping,
|
||||
subrows: ["bestFriend": bestFriendMapping])
|
||||
let mainAdapter = ColumnMapping(["id": "mainID", "name": "mainName"])
|
||||
let bestFriendAdapter = ColumnMapping(["id": "friendID", "name": "friendName"])
|
||||
let adapter = mainAdapter.adapterWithVariants(["bestFriend": bestFriendAdapter])
|
||||
|
||||
for row in Row.fetch(db, sql, adapter: adapter) {
|
||||
// <Row id:1 name:"Arthur">
|
||||
print(row)
|
||||
// <Row id:2 name:"Barbara">
|
||||
print(row.subrow(named: "bestFriend"))
|
||||
// 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") {
|
||||
bestFriendRow.value(named: "id") // 2
|
||||
bestFriendRow.value(named: "name") // Barbara
|
||||
}
|
||||
}
|
||||
|
||||
// Assuming Person.init(row) consumes the "bestFriend" subrow:
|
||||
// Assuming Person.init(row) consumes the "bestFriend" variant:
|
||||
for person in Person.fetch(db, sql, adapter: adapter) {
|
||||
person.name // Arthur
|
||||
person.bestFriend?.name // Barbara
|
||||
@@ -1482,6 +1470,14 @@ 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.71.0/Protocols/RowAdapter.html)
|
||||
- [ColumnMapping](http://cocoadocs.org/docsets/GRDB.swift/0.71.0/Structs/ColumnMapping.html)
|
||||
- [SuffixRowAdapter](http://cocoadocs.org/docsets/GRDB.swift/0.71.0/Structs/SuffixRowAdapter.html)
|
||||
- [VariantRowAdapter](http://cocoadocs.org/docsets/GRDB.swift/0.71.0/Structs/VariantRowAdapter.html)
|
||||
|
||||
|
||||
## Raw SQLite Pointers
|
||||
|
||||
Not all SQLite APIs are exposed in GRDB.
|
||||
|
||||
@@ -308,9 +308,8 @@ class AdapterRowTests: GRDBTestCase {
|
||||
func testVariantsWithMainMapping() {
|
||||
let dbQueue = DatabaseQueue()
|
||||
dbQueue.inDatabase { db in
|
||||
let adapter = VariantRowAdapter(
|
||||
ColumnMapping(["id": "id0", "val": "val0"]),
|
||||
variants: [
|
||||
let adapter = ColumnMapping(["id": "id0", "val": "val0"])
|
||||
.adapterWithVariants([
|
||||
"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)!
|
||||
@@ -348,7 +347,7 @@ class AdapterRowTests: GRDBTestCase {
|
||||
let adapter2 = ColumnMapping(["id": "id2", "val": "val2"])
|
||||
|
||||
let mainAdapter = VariantRowAdapter(variants: ["sub0": adapter0, "sub1": adapter2])
|
||||
let adapter = VariantRowAdapter(mainAdapter, variants: ["sub1": adapter1, "sub2": adapter2])
|
||||
let adapter = mainAdapter.adapterWithVariants(["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
|
||||
@@ -384,12 +383,11 @@ class AdapterRowTests: GRDBTestCase {
|
||||
func testThreeLevelVariants() {
|
||||
let dbQueue = DatabaseQueue()
|
||||
dbQueue.inDatabase { db in
|
||||
let adapter = VariantRowAdapter(
|
||||
ColumnMapping(["id": "id0", "val": "val0"]),
|
||||
variants: [
|
||||
"sub1": VariantRowAdapter(
|
||||
ColumnMapping(["id": "id1", "val": "val1"]),
|
||||
variants: ["sub2": ColumnMapping(["id": "id2", "val": "val2"])])])
|
||||
let adapter = ColumnMapping(["id": "id0", "val": "val0"])
|
||||
.adapterWithVariants([
|
||||
"sub1": ColumnMapping(["id": "id1", "val": "val1"])
|
||||
.adapterWithVariants([
|
||||
"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)!
|
||||
|
||||
XCTAssertEqual(row.count, 2)
|
||||
@@ -422,9 +420,9 @@ class AdapterRowTests: GRDBTestCase {
|
||||
dbQueue.inDatabase { db in
|
||||
let adapter = VariantRowAdapter(
|
||||
variants: [
|
||||
"sub1": VariantRowAdapter(
|
||||
SuffixRowAdapter(fromIndex: 2),
|
||||
variants: ["sub2": SuffixRowAdapter(fromIndex: 4)])])
|
||||
"sub1": SuffixRowAdapter(fromIndex: 2)
|
||||
.adapterWithVariants([
|
||||
"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)
|
||||
@@ -455,9 +453,8 @@ class AdapterRowTests: GRDBTestCase {
|
||||
func testCopy() {
|
||||
let dbQueue = DatabaseQueue()
|
||||
dbQueue.inDatabase { db in
|
||||
let adapter = VariantRowAdapter(
|
||||
ColumnMapping(["a": "basea", "b": "baseb", "c": "basec"]),
|
||||
variants: ["sub": ColumnMapping(["a": "baseb"])])
|
||||
let adapter = ColumnMapping(["a": "basea", "b": "baseb", "c": "basec"])
|
||||
.adapterWithVariants(["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()
|
||||
@@ -499,19 +496,15 @@ class AdapterRowTests: GRDBTestCase {
|
||||
func testEqualityComparesVariants() {
|
||||
let dbQueue = DatabaseQueue()
|
||||
dbQueue.inDatabase { db in
|
||||
let adapter1 = VariantRowAdapter(
|
||||
ColumnMapping(["a": "basea", "b": "baseb", "c": "basec"]),
|
||||
variants: ["sub": ColumnMapping(["b": "baseb"])])
|
||||
let adapter1 = ColumnMapping(["a": "basea", "b": "baseb", "c": "basec"])
|
||||
.adapterWithVariants(["sub": ColumnMapping(["b": "baseb"])])
|
||||
let adapter2 = ColumnMapping(["a": "basea", "b": "baseb2", "c": "basec"])
|
||||
let adapter3 = VariantRowAdapter(
|
||||
ColumnMapping(["a": "basea", "b": "baseb2", "c": "basec"]),
|
||||
variants: ["sub": ColumnMapping(["b": "baseb2"])])
|
||||
let adapter4 = VariantRowAdapter(
|
||||
ColumnMapping(["a": "basea", "b": "baseb", "c": "basec"]),
|
||||
variants: ["sub": ColumnMapping(["b": "baseb"]), "altSub": ColumnMapping(["a": "baseb2"])])
|
||||
let adapter5 = VariantRowAdapter(
|
||||
ColumnMapping(["a": "basea", "b": "baseb", "c": "basec"]),
|
||||
variants: ["sub": ColumnMapping(["b": "baseb", "c": "basec"])])
|
||||
let adapter3 = ColumnMapping(["a": "basea", "b": "baseb2", "c": "basec"])
|
||||
.adapterWithVariants(["sub": ColumnMapping(["b": "baseb2"])])
|
||||
let adapter4 = ColumnMapping(["a": "basea", "b": "baseb", "c": "basec"])
|
||||
.adapterWithVariants(["sub": ColumnMapping(["b": "baseb"]), "altSub": ColumnMapping(["a": "baseb2"])])
|
||||
let adapter5 = ColumnMapping(["a": "basea", "b": "baseb", "c": "basec"])
|
||||
.adapterWithVariants(["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)!
|
||||
|
||||
@@ -112,9 +112,8 @@ class RowConvertibleTests: GRDBTestCase {
|
||||
assertNoError {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
dbQueue.inDatabase { db in
|
||||
let adapter = VariantRowAdapter(
|
||||
ColumnMapping(["firstName": "firstName1", "lastName": "lastName1"]),
|
||||
variants: ["bestFriend": ColumnMapping(["firstName": "firstName2", "lastName": "lastName2"])])
|
||||
let adapter = ColumnMapping(["firstName": "firstName1", "lastName": "lastName1"])
|
||||
.adapterWithVariants(["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,9 +132,8 @@ class RowConvertibleTests: GRDBTestCase {
|
||||
assertNoError {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
dbQueue.inDatabase { db in
|
||||
let adapter = VariantRowAdapter(
|
||||
ColumnMapping(["firstName": "firstName1", "lastName": "lastName1"]),
|
||||
variants: ["bestFriend": ColumnMapping(["firstName": "firstName2", "lastName": "lastName2"])])
|
||||
let adapter = ColumnMapping(["firstName": "firstName1", "lastName": "lastName1"])
|
||||
.adapterWithVariants(["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)
|
||||
@@ -154,9 +152,8 @@ class RowConvertibleTests: GRDBTestCase {
|
||||
assertNoError {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
dbQueue.inDatabase { db in
|
||||
let adapter = VariantRowAdapter(
|
||||
ColumnMapping(["firstName": "firstName1", "lastName": "lastName1"]),
|
||||
variants: ["bestFriend": ColumnMapping(["firstName": "firstName2", "lastName": "lastName2"])])
|
||||
let adapter = ColumnMapping(["firstName": "firstName1", "lastName": "lastName1"])
|
||||
.adapterWithVariants(["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)!
|
||||
@@ -221,9 +218,8 @@ class RowConvertibleTests: GRDBTestCase {
|
||||
assertNoError {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
let adapter = VariantRowAdapter(
|
||||
ColumnMapping(["firstName": "firstName1", "lastName": "lastName1"]),
|
||||
variants: ["bestFriend": ColumnMapping(["firstName": "firstName2", "lastName": "lastName2"])])
|
||||
let adapter = ColumnMapping(["firstName": "firstName1", "lastName": "lastName1"])
|
||||
.adapterWithVariants(["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)
|
||||
@@ -243,9 +239,8 @@ class RowConvertibleTests: GRDBTestCase {
|
||||
assertNoError {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
let adapter = VariantRowAdapter(
|
||||
ColumnMapping(["firstName": "firstName1", "lastName": "lastName1"]),
|
||||
variants: ["bestFriend": ColumnMapping(["firstName": "firstName2", "lastName": "lastName2"])])
|
||||
let adapter = ColumnMapping(["firstName": "firstName1", "lastName": "lastName1"])
|
||||
.adapterWithVariants(["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)
|
||||
@@ -265,9 +260,8 @@ class RowConvertibleTests: GRDBTestCase {
|
||||
assertNoError {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
let adapter = VariantRowAdapter(
|
||||
ColumnMapping(["firstName": "firstName1", "lastName": "lastName1"]),
|
||||
variants: ["bestFriend": ColumnMapping(["firstName": "firstName2", "lastName": "lastName2"])])
|
||||
let adapter = ColumnMapping(["firstName": "firstName1", "lastName": "lastName1"])
|
||||
.adapterWithVariants(["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