mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
Merge branch 'master' of github.com:groue/GRDB.swift into Swift3
Conflicts: GRDB/Core/Database.swift GRDB/Core/RowAdapter.swift README.md Tests/Public/Core/Row/AdapterRowTests.swift Tests/Public/Core/Row/RowConvertibleTests.swift
This commit is contained in:
commit
a288c8d7fb
13 files changed
+309
-210
No files matched your search
@@ -1,6 +1,52 @@
|
||||
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
|
||||
|
||||
**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
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
Pod::Spec.new do |s|
|
||||
s.name = 'GRDB.swift'
|
||||
s.version = '0.74.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'
|
||||
|
||||
@@ -1097,7 +1097,17 @@ final class StatementCompilationObserver {
|
||||
observer.databaseEventKinds.append(.delete(tableName: String(cString: CString1!)))
|
||||
case SQLITE_UPDATE:
|
||||
let observer = unsafeBitCast(observerPointer, to: StatementCompilationObserver.self)
|
||||
observer.insertUpdateEventKind(tableName: String(cString: CString1!), columnName: String(cString: CString2!))
|
||||
let tableName = String(cString: 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(cString: CString2!))
|
||||
}
|
||||
case SQLITE_SAVEPOINT:
|
||||
let observer = unsafeBitCast(observerPointer, to: StatementCompilationObserver.self)
|
||||
let name = String(cString: CString2!)
|
||||
|
||||
+43
-20
@@ -380,10 +380,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 defines this scope.
|
||||
///
|
||||
/// // Two adapters
|
||||
/// let fooAdapter = ColumnMapping(["value": "foo"])
|
||||
/// let barAdapter = ColumnMapping(["value": "bar"])
|
||||
///
|
||||
/// // Define scopes
|
||||
/// let adapter = ScopeAdapter([
|
||||
/// "foo": fooAdapter,
|
||||
/// "bar": barAdapter])
|
||||
///
|
||||
/// // Fetch
|
||||
/// let sql = "SELECT 'foo' AS foo, 'bar' AS bar"
|
||||
/// let row = Row.fetchOne(db, sql, adapter: adapter)!
|
||||
///
|
||||
/// // 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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -707,16 +730,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
|
||||
}
|
||||
}
|
||||
@@ -768,9 +791,9 @@ protocol RowImpl {
|
||||
// leftmost column that matches *name*.
|
||||
func index(ofColumn 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
|
||||
@@ -813,11 +836,11 @@ private struct DictionaryRowImpl : RowImpl {
|
||||
return dictionary.distance(from: dictionary.startIndex, to: index)
|
||||
}
|
||||
|
||||
func variant(named name: String) -> Row? {
|
||||
func scoped(on name: String) -> Row? {
|
||||
return nil
|
||||
}
|
||||
|
||||
var variantNames: Set<String> {
|
||||
var scopeNames: Set<String> {
|
||||
return []
|
||||
}
|
||||
|
||||
@@ -861,11 +884,11 @@ private struct StatementCopyRowImpl : RowImpl {
|
||||
return columnNames.index { $0.lowercased() == lowercaseName }
|
||||
}
|
||||
|
||||
func variant(named name: String) -> Row? {
|
||||
func scoped(on name: String) -> Row? {
|
||||
return nil
|
||||
}
|
||||
|
||||
var variantNames: Set<String> {
|
||||
var scopeNames: Set<String> {
|
||||
return []
|
||||
}
|
||||
|
||||
@@ -921,11 +944,11 @@ private struct StatementRowImpl : RowImpl {
|
||||
return lowercaseColumnIndexes[name.lowercased()]
|
||||
}
|
||||
|
||||
func variant(named name: String) -> Row? {
|
||||
func scoped(on name: String) -> Row? {
|
||||
return nil
|
||||
}
|
||||
|
||||
var variantNames: Set<String> {
|
||||
var scopeNames: Set<String> {
|
||||
return []
|
||||
}
|
||||
|
||||
@@ -955,11 +978,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 []
|
||||
}
|
||||
|
||||
|
||||
+71
-72
@@ -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 foo:2 bar: 3>
|
||||
/// Row.fetchOne(db, "SELECT 1, 2, 3", adapter: FooBarAdapter())
|
||||
/// // <Row foo:"foo" bar: "bar">
|
||||
/// Row.fetchOne(db, "SELECT NULL, 'foo', 'bar'", adapter: FooBarAdapter())
|
||||
public init(columns: [(Int, String)]) {
|
||||
self.columns = columns
|
||||
self.lowercaseColumnIndexes = Dictionary(keyValueSequence: columns.enumerated().map { ($1.1.lowercased(), $0) }.reversed())
|
||||
@@ -62,37 +62,33 @@ 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
|
||||
}
|
||||
|
||||
/// Part of the ConcreteRowAdapter protocol; returns the empty dictionary.
|
||||
public var variants: [String: ConcreteRowAdapter] {
|
||||
public var scopes: [String: ConcreteRowAdapter] {
|
||||
return [:]
|
||||
}
|
||||
}
|
||||
|
||||
struct ConcreteVariantRowAdapter : ConcreteRowAdapter {
|
||||
let adaptedColumnsDescription: AdaptedColumnsDescription
|
||||
let variants: [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 variant names.
|
||||
var variants: [String: ConcreteRowAdapter] { get }
|
||||
/// A dictionary of scopes
|
||||
var scopes: [String: ConcreteRowAdapter] { get }
|
||||
}
|
||||
|
||||
/// RowAdapter is a protocol that helps two incompatible row interfaces working
|
||||
@@ -102,8 +98,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.
|
||||
@@ -121,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:
|
||||
///
|
||||
@@ -130,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")])
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
@@ -140,15 +135,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 adapter(withVariants variants: [String: RowAdapter]) -> RowAdapter {
|
||||
return VariantRowAdapter(mainAdapter: self, variants: variants)
|
||||
public func addingScopes(_ scopes: [String: RowAdapter]) -> RowAdapter {
|
||||
return ScopeAdapter(mainAdapter: self, scopes: scopes)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,7 +174,7 @@ public struct ColumnMapping : RowAdapter {
|
||||
return (index, mappedColumn)
|
||||
}
|
||||
.sorted { $0.0 < $1.0 }
|
||||
return AdaptedColumnsDescription(columns: columns)
|
||||
return ConcreteColumnMapping(columns: columns)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -206,69 +201,73 @@ 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.suffix(from: index).enumerated().map { ($0 + index, $1) })
|
||||
return ConcreteColumnMapping(columns: statement.columnNames.suffix(from: index).enumerated().map { ($0 + index, $1) })
|
||||
}
|
||||
}
|
||||
|
||||
/// VariantRowAdapter is a row adapter that lets you add adapted variants to
|
||||
/// fetched 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 variants
|
||||
/// let variants: [String: RowAdapter] = [
|
||||
/// // Define scopes
|
||||
/// let adapter = ScopeAdapter([
|
||||
/// "foo": fooAdapter,
|
||||
/// "bar": barAdapter])
|
||||
/// let adapter = VariantRowAdapter(variants: variants)
|
||||
///
|
||||
/// // Fetch a row
|
||||
/// // Fetch
|
||||
/// 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") {
|
||||
/// // 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]) {
|
||||
self.mainAdapter = SuffixRowAdapter(fromIndex: 0)
|
||||
self.variants = variants
|
||||
public init(_ scopes: [String: RowAdapter]) {
|
||||
self.mainAdapter = SuffixRowAdapter(fromIndex: 0) // Use SuffixRowAdapter(fromIndex: 0) as the identity adapter
|
||||
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(
|
||||
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) {
|
||||
@@ -287,43 +286,43 @@ 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(atUncheckedIndex index: Int) -> DatabaseValue {
|
||||
return baseRow.databaseValue(atIndex: adaptedColumnsDescription.baseColumIndex(adaptedIndex: index))
|
||||
return baseRow.databaseValue(atIndex: concreteColumnMapping.baseColumIndex(adaptedIndex: index))
|
||||
}
|
||||
|
||||
func dataNoCopy(atUncheckedIndex index:Int) -> Data? {
|
||||
return baseRow.dataNoCopy(atIndex: adaptedColumnsDescription.baseColumIndex(adaptedIndex: index))
|
||||
return baseRow.dataNoCopy(atIndex: concreteColumnMapping.baseColumIndex(adaptedIndex: index))
|
||||
}
|
||||
|
||||
func columnName(atUncheckedIndex index: Int) -> String {
|
||||
return adaptedColumnsDescription.columnName(adaptedIndex: index)
|
||||
return concreteColumnMapping.columnName(adaptedIndex: index)
|
||||
}
|
||||
|
||||
func index(ofColumn name: String) -> Int? {
|
||||
return adaptedColumnsDescription.adaptedIndexOfColumn(named: name)
|
||||
return concreteColumnMapping.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 {
|
||||
|
||||
@@ -25,7 +25,7 @@ You should give it a try.
|
||||
|
||||
---
|
||||
|
||||
The Swift3 branch has no version. It is currently synced with v0.74.0 of the Swift 2.2 [main branch](https://github.com/groue/GRDB.swift).
|
||||
The Swift3 branch has no version. It is currently synced with v0.75.1 of the Swift 2.2 [main branch](https://github.com/groue/GRDB.swift).
|
||||
|
||||
Follow [@groue](http://twitter.com/groue) on Twitter for release announcements and usage tips.
|
||||
|
||||
@@ -280,7 +280,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.1/Structs/Configuration.html) for more details.
|
||||
|
||||
|
||||
## Database Pools
|
||||
@@ -360,7 +360,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.1/Structs/Configuration.html) for more details.
|
||||
|
||||
|
||||
Database pools are more memory-hungry than database queues. See [Memory Management](#memory-management) for more information.
|
||||
@@ -1355,7 +1355,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:
|
||||
|
||||
@@ -1366,14 +1366,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) {
|
||||
@@ -1383,15 +1383,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) {
|
||||
@@ -1408,7 +1408,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, " +
|
||||
@@ -1418,21 +1418,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.adapter(withVariants: ["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: row) consumes the "bestFriend" variant:
|
||||
// Assuming Person.init(row: row) consumes the "bestFriend" scope:
|
||||
for person in Person.fetch(db, sql, adapter: adapter) {
|
||||
person.name // Arthur
|
||||
person.bestFriend?.name // Barbara
|
||||
@@ -1442,10 +1442,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
|
||||
- [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
|
||||
- [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
|
||||
@@ -2359,7 +2359,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<Person>
|
||||
let request = Person.filter(...)... // QueryInterfaceRequest<Person>
|
||||
|
||||
// Fetch persons:
|
||||
request.fetch(db) // DatabaseSequence<Person>
|
||||
@@ -3340,7 +3340,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.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:
|
||||
|
||||
@@ -3448,6 +3448,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.
|
||||
+1
-1
@@ -15,7 +15,7 @@
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>FMWK</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>0.74.0</string>
|
||||
<string>0.75.1</string>
|
||||
<key>CFBundleSignature</key>
|
||||
<string>????</string>
|
||||
<key>CFBundleVersion</key>
|
||||
|
||||
@@ -434,4 +434,26 @@ 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 {
|
||||
// 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")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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"])
|
||||
.adapter(withVariants: [
|
||||
.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.adapter(withVariants: ["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"])
|
||||
.adapter(withVariants: [
|
||||
.addingScopes([
|
||||
"sub1": ColumnMapping(["id": "id1", "val": "val1"])
|
||||
.adapter(withVariants: [
|
||||
.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)
|
||||
.adapter(withVariants: [
|
||||
"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"])
|
||||
.adapter(withVariants: ["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"])
|
||||
.adapter(withVariants: ["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"])
|
||||
.adapter(withVariants: ["sub": ColumnMapping(["b": "baseb2"])])
|
||||
.addingScopes(["sub": ColumnMapping(["b": "baseb2"])])
|
||||
let adapter4 = ColumnMapping(["a": "basea", "b": "baseb", "c": "basec"])
|
||||
.adapter(withVariants: ["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"])
|
||||
.adapter(withVariants: ["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(row: variant)
|
||||
if let bestFriendRow = row.scoped(on: "bestFriend") {
|
||||
bestFriend = Person(row: bestFriendRow)
|
||||
}
|
||||
fetched = false
|
||||
}
|
||||
|
||||
func awakeFromFetch(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"])
|
||||
.adapter(withVariants: ["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"])
|
||||
.adapter(withVariants: ["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"])
|
||||
.adapter(withVariants: ["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"])
|
||||
.adapter(withVariants: ["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.makeSelectStatement(sql)
|
||||
@@ -240,7 +240,7 @@ class RowConvertibleTests: GRDBTestCase {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
let adapter = ColumnMapping(["firstName": "firstName1", "lastName": "lastName1"])
|
||||
.adapter(withVariants: ["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.makeSelectStatement(sql)
|
||||
@@ -261,7 +261,7 @@ class RowConvertibleTests: GRDBTestCase {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
let adapter = ColumnMapping(["firstName": "firstName1", "lastName": "lastName1"])
|
||||
.adapter(withVariants: ["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.makeSelectStatement(sql)
|
||||
|
||||
Reference in new issue
Block a user