mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
RowAdapter refactoring
This commit is contained in:
1 parent
f7fddbc162
commit
4e2f396b4a
6 files changed
+215
-154
No files matched your search
@@ -329,6 +329,10 @@
|
||||
5672DE6A1CDB751D0022BA81 /* DatabasePoolBackupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5672DE661CDB751D0022BA81 /* DatabasePoolBackupTests.swift */; };
|
||||
5672DE6B1CDB751D0022BA81 /* DatabasePoolBackupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5672DE661CDB751D0022BA81 /* DatabasePoolBackupTests.swift */; };
|
||||
5672DE6C1CDB751D0022BA81 /* DatabasePoolBackupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5672DE661CDB751D0022BA81 /* DatabasePoolBackupTests.swift */; };
|
||||
567404881CEF84C8003ED5CC /* RowAdapter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567404871CEF84C8003ED5CC /* RowAdapter.swift */; };
|
||||
567404891CEF84C8003ED5CC /* RowAdapter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567404871CEF84C8003ED5CC /* RowAdapter.swift */; };
|
||||
5674048A1CEF84C8003ED5CC /* RowAdapter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567404871CEF84C8003ED5CC /* RowAdapter.swift */; };
|
||||
5674048B1CEF84C8003ED5CC /* RowAdapter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 567404871CEF84C8003ED5CC /* RowAdapter.swift */; };
|
||||
5683C2691B4D445E00296494 /* libsqlite3.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 5683C2681B4D445E00296494 /* libsqlite3.dylib */; };
|
||||
5687358C1CEDC334009B9116 /* FetchRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5687358B1CEDC334009B9116 /* FetchRequest.swift */; };
|
||||
5687358D1CEDC334009B9116 /* FetchRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5687358B1CEDC334009B9116 /* FetchRequest.swift */; };
|
||||
@@ -910,6 +914,7 @@
|
||||
567156701CB18050007DC145 /* EncryptionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EncryptionTests.swift; sourceTree = "<group>"; };
|
||||
5672DE581CDB72520022BA81 /* DatabaseQueueBackupTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseQueueBackupTests.swift; sourceTree = "<group>"; };
|
||||
5672DE661CDB751D0022BA81 /* DatabasePoolBackupTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabasePoolBackupTests.swift; sourceTree = "<group>"; };
|
||||
567404871CEF84C8003ED5CC /* RowAdapter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowAdapter.swift; sourceTree = "<group>"; };
|
||||
5683C2681B4D445E00296494 /* libsqlite3.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libsqlite3.dylib; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.4.sdk/usr/lib/libsqlite3.dylib; sourceTree = DEVELOPER_DIR; };
|
||||
5687358B1CEDC334009B9116 /* FetchRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchRequest.swift; sourceTree = "<group>"; };
|
||||
5687359E1CEDE16C009B9116 /* Betty.jpeg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = Betty.jpeg; sourceTree = "<group>"; };
|
||||
@@ -1492,6 +1497,7 @@
|
||||
560D923E1C672C3E00F4F92B /* DatabaseValueConvertible.swift */,
|
||||
563363C31C942C37000BE133 /* DatabaseWriter.swift */,
|
||||
56A238761B9C75030082EB20 /* Row.swift */,
|
||||
567404871CEF84C8003ED5CC /* RowAdapter.swift */,
|
||||
560A37A61C8FF6E500949E71 /* SerializedDatabase.swift */,
|
||||
56A238781B9C75030082EB20 /* Statement.swift */,
|
||||
560D923F1C672C3E00F4F92B /* StatementColumnConvertible.swift */,
|
||||
@@ -2236,6 +2242,7 @@
|
||||
560FC5211CB003810014AA8E /* SQLFunction.swift in Sources */,
|
||||
560FC5221CB003810014AA8E /* RawRepresentable.swift in Sources */,
|
||||
560FC5231CB003810014AA8E /* Configuration.swift in Sources */,
|
||||
567404891CEF84C8003ED5CC /* RowAdapter.swift in Sources */,
|
||||
560FC5241CB003810014AA8E /* DatabaseWriter.swift in Sources */,
|
||||
560FC5251CB003810014AA8E /* DatabaseStore.swift in Sources */,
|
||||
560FC5261CB003810014AA8E /* SerializedDatabase.swift in Sources */,
|
||||
@@ -2465,6 +2472,7 @@
|
||||
56AFC9F61CB1A8BB00F48B96 /* SQLFunction.swift in Sources */,
|
||||
56AFC9F71CB1A8BB00F48B96 /* RawRepresentable.swift in Sources */,
|
||||
56AFC9F81CB1A8BB00F48B96 /* Utils.swift in Sources */,
|
||||
5674048B1CEF84C8003ED5CC /* RowAdapter.swift in Sources */,
|
||||
56AFC9F91CB1A8BB00F48B96 /* DatabaseWriter.swift in Sources */,
|
||||
56AFC9FA1CB1A8BB00F48B96 /* DatabaseStore.swift in Sources */,
|
||||
56AFC9FB1CB1A8BB00F48B96 /* SerializedDatabase.swift in Sources */,
|
||||
@@ -2680,6 +2688,7 @@
|
||||
5605F1901C6B1A8700235C62 /* SQLFunction.swift in Sources */,
|
||||
5605F1721C672E4000235C62 /* RawRepresentable.swift in Sources */,
|
||||
563445061C4A7FD7003D3DC6 /* Utils.swift in Sources */,
|
||||
5674048A1CEF84C8003ED5CC /* RowAdapter.swift in Sources */,
|
||||
563363C51C942C37000BE133 /* DatabaseWriter.swift in Sources */,
|
||||
563363D91C95787F000BE133 /* DatabaseStore.swift in Sources */,
|
||||
560A37A81C8FF6E500949E71 /* SerializedDatabase.swift in Sources */,
|
||||
@@ -2892,6 +2901,7 @@
|
||||
5605F18F1C6B1A8700235C62 /* SQLFunction.swift in Sources */,
|
||||
5605F1711C672E4000235C62 /* RawRepresentable.swift in Sources */,
|
||||
56A2387B1B9C75030082EB20 /* Configuration.swift in Sources */,
|
||||
567404881CEF84C8003ED5CC /* RowAdapter.swift in Sources */,
|
||||
563363C41C942C37000BE133 /* DatabaseWriter.swift in Sources */,
|
||||
563363D81C95787F000BE133 /* DatabaseStore.swift in Sources */,
|
||||
560A37A71C8FF6E500949E71 /* SerializedDatabase.swift in Sources */,
|
||||
|
||||
+16
-139
@@ -18,17 +18,13 @@ public final class Row {
|
||||
// MARK: - Building rows
|
||||
|
||||
/// Builds an empty row.
|
||||
public init() {
|
||||
statementRef = nil
|
||||
sqliteStatement = nil
|
||||
impl = EmptyRowImpl()
|
||||
public convenience init() {
|
||||
self.init(impl: EmptyRowImpl())
|
||||
}
|
||||
|
||||
/// Builds a row from a dictionary of values.
|
||||
public init(_ dictionary: [String: DatabaseValueConvertible?]) {
|
||||
statementRef = nil
|
||||
sqliteStatement = nil
|
||||
impl = DictionaryRowImpl(dictionary: dictionary)
|
||||
public convenience init(_ dictionary: [String: DatabaseValueConvertible?]) {
|
||||
self.init(impl: DictionaryRowImpl(dictionary: dictionary))
|
||||
}
|
||||
|
||||
/// Returns a copy of the row.
|
||||
@@ -80,10 +76,8 @@ public final class Row {
|
||||
/// The row is implemented on top of StatementCopyRowImpl, which *copies*
|
||||
/// the values from the SQLite statement so that further iteration of the
|
||||
/// statement does not modify the row.
|
||||
init(copiedFromSQLiteStatement sqliteStatement: SQLiteStatement, statementRef: Unmanaged<SelectStatement>) {
|
||||
self.statementRef = nil
|
||||
self.sqliteStatement = nil
|
||||
self.impl = StatementCopyRowImpl(sqliteStatement: sqliteStatement, columnNames: statementRef.takeUnretainedValue().columnNames)
|
||||
convenience init(copiedFromSQLiteStatement sqliteStatement: SQLiteStatement, statementRef: Unmanaged<SelectStatement>) {
|
||||
self.init(impl: StatementCopyRowImpl(sqliteStatement: sqliteStatement, columnNames: statementRef.takeUnretainedValue().columnNames))
|
||||
}
|
||||
|
||||
/// Builds a row from the *current state* of the SQLite statement.
|
||||
@@ -91,17 +85,14 @@ public final class Row {
|
||||
/// The row is implemented on top of StatementCopyRowImpl, which *copies*
|
||||
/// the values from the SQLite statement so that further iteration of the
|
||||
/// statement does not modify the row.
|
||||
init(copiedFromSQLiteStatement sqliteStatement: SQLiteStatement, columnNames: [String]) {
|
||||
self.statementRef = nil
|
||||
self.sqliteStatement = nil
|
||||
self.impl = StatementCopyRowImpl(sqliteStatement: sqliteStatement, columnNames: columnNames)
|
||||
convenience init(copiedFromSQLiteStatement sqliteStatement: SQLiteStatement, columnNames: [String]) {
|
||||
self.init(impl: StatementCopyRowImpl(sqliteStatement: sqliteStatement, columnNames: columnNames))
|
||||
}
|
||||
|
||||
/// Builds a row from a base row and column mappings
|
||||
init(baseRow: Row, columnMapping: ColumnMapping, subrowColumnMappings: [String: ColumnMapping]) {
|
||||
init(impl: RowImpl) {
|
||||
self.impl = impl
|
||||
self.statementRef = nil
|
||||
self.sqliteStatement = nil
|
||||
self.impl = MappedRowImpl(baseRow: baseRow, columnMapping: columnMapping, subrowColumnMappings: subrowColumnMappings)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -452,8 +443,8 @@ extension Row {
|
||||
// Metal rows can be reused. And reusing them yields better performance.
|
||||
let row: Row
|
||||
if let adapter = adapter {
|
||||
let (columnMapping, subrowColumnMappings) = try! adapter.columnMappings(statement)
|
||||
row = Row(baseRow: Row(statement: statement), columnMapping: columnMapping, subrowColumnMappings: subrowColumnMappings)
|
||||
let boundRowAdapter = try! adapter.boundRowAdapter(with: statement)
|
||||
row = Row(baseRow: Row(statement: statement), boundRowAdapter: boundRowAdapter)
|
||||
} else {
|
||||
row = Row(statement: statement)
|
||||
}
|
||||
@@ -478,9 +469,9 @@ extension Row {
|
||||
let columnNames = statement.columnNames
|
||||
let sequence: DatabaseSequence<Row>
|
||||
if let adapter = adapter {
|
||||
let (columnMapping, subrowColumnMappings) = try! adapter.columnMappings(statement)
|
||||
let boundRowAdapter = try! adapter.boundRowAdapter(with: statement)
|
||||
sequence = statement.fetchSequence(arguments: arguments) {
|
||||
Row(baseRow: Row(copiedFromSQLiteStatement: sqliteStatement, columnNames: columnNames), columnMapping: columnMapping, subrowColumnMappings: subrowColumnMappings)
|
||||
Row(baseRow: Row(copiedFromSQLiteStatement: sqliteStatement, columnNames: columnNames), boundRowAdapter: boundRowAdapter)
|
||||
}
|
||||
} else {
|
||||
sequence = statement.fetchSequence(arguments: arguments) {
|
||||
@@ -511,8 +502,8 @@ extension Row {
|
||||
return nil
|
||||
}
|
||||
if let adapter = adapter {
|
||||
let (columnMapping, subrowColumnMappings) = try! adapter.columnMappings(statement)
|
||||
return Row(baseRow: row, columnMapping: columnMapping, subrowColumnMappings: subrowColumnMappings)
|
||||
let boundRowAdapter = try! adapter.boundRowAdapter(with: statement)
|
||||
return Row(baseRow: row, boundRowAdapter: boundRowAdapter)
|
||||
} else {
|
||||
return row
|
||||
}
|
||||
@@ -712,71 +703,6 @@ public func ==(lhs: RowIndex, rhs: RowIndex) -> Bool {
|
||||
}
|
||||
|
||||
|
||||
// MARK: - RowAdapter
|
||||
|
||||
public struct RowAdapter {
|
||||
let mapping: [String: String]
|
||||
let subrowMappings: [String: [String: String]]
|
||||
public init(mapping: [String: String], subrowMappings: [String: [String: String]] = [:]) {
|
||||
self.mapping = mapping
|
||||
self.subrowMappings = subrowMappings
|
||||
}
|
||||
}
|
||||
|
||||
struct ColumnMapping {
|
||||
let orderedMapping: [(String, String)] // [(mappedColumn, baseColumn), ...]
|
||||
let lowercaseColumnIndexes: [String: Int]
|
||||
|
||||
init(orderedMapping: [(String, String)]) {
|
||||
self.orderedMapping = orderedMapping
|
||||
self.lowercaseColumnIndexes = Dictionary(keyValueSequence: orderedMapping.enumerate().map { ($1.0.lowercaseString, $0) })
|
||||
}
|
||||
|
||||
var count: Int {
|
||||
return orderedMapping.count
|
||||
}
|
||||
|
||||
func baseColumName(atIndex index: Int) -> String {
|
||||
return orderedMapping[index].1
|
||||
}
|
||||
|
||||
func columnName(atIndex index: Int) -> String {
|
||||
return orderedMapping[index].0
|
||||
}
|
||||
|
||||
func indexOfColumn(named name: String) -> Int? {
|
||||
if let index = lowercaseColumnIndexes[name] {
|
||||
return index
|
||||
}
|
||||
return lowercaseColumnIndexes[name.lowercaseString]
|
||||
}
|
||||
}
|
||||
|
||||
extension RowAdapter {
|
||||
func columnMappings(statement: SelectStatement) throws -> (ColumnMapping, [String: ColumnMapping]) {
|
||||
let columnMapping = try ColumnMapping(orderedMapping: validatedOrderedMapping(statement: statement, mapping: mapping))
|
||||
let subrowColumnMappings = try Dictionary(keyValueSequence: subrowMappings.map { (identifier, mapping) in
|
||||
(identifier, try ColumnMapping(orderedMapping: validatedOrderedMapping(statement: statement, mapping: mapping)))
|
||||
})
|
||||
return (columnMapping, subrowColumnMappings)
|
||||
}
|
||||
|
||||
// Turns a dictionary [mappedColumn:baseColumn, ...] into an array
|
||||
// [(mappedColumn, baseColumn), ...] ordered like the satement columns.
|
||||
private func validatedOrderedMapping(statement statement: SelectStatement, mapping: [String: String]) throws -> [(String, String)] {
|
||||
return try mapping
|
||||
.map { (mappedColumn, baseColumn) -> (Int, (String, String)) in
|
||||
guard let index = statement.indexOfColumn(named: baseColumn) else {
|
||||
throw DatabaseError(code: SQLITE_MISUSE, message: "Mapping references missing column \(baseColumn). Valid column names are: \(statement.columnNames.joinWithSeparator(", ")).")
|
||||
}
|
||||
return (index, (mappedColumn, baseColumn))
|
||||
}
|
||||
.sort { return $0.0 < $1.0 }
|
||||
.map { $0.1 }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - RowImpl
|
||||
|
||||
// The protocol for Row underlying implementation
|
||||
@@ -987,52 +913,3 @@ private struct EmptyRowImpl : RowImpl {
|
||||
return row
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// See Row.init(baseRow:columnMapping:subrowColumnMappings:)
|
||||
private struct MappedRowImpl : RowImpl {
|
||||
let baseRow: Row
|
||||
let columnMapping: ColumnMapping
|
||||
let subrowColumnMappings: [String: ColumnMapping]
|
||||
|
||||
init(baseRow: Row, columnMapping: ColumnMapping, subrowColumnMappings: [String: ColumnMapping]) {
|
||||
self.baseRow = baseRow
|
||||
self.columnMapping = columnMapping
|
||||
self.subrowColumnMappings = subrowColumnMappings
|
||||
}
|
||||
|
||||
var count: Int {
|
||||
return columnMapping.count
|
||||
}
|
||||
|
||||
func databaseValue(atIndex index: Int) -> DatabaseValue {
|
||||
return baseRow.databaseValue(named: columnMapping.baseColumName(atIndex: index))!
|
||||
}
|
||||
|
||||
func dataNoCopy(atIndex index:Int) -> NSData? {
|
||||
return baseRow.dataNoCopy(named: columnMapping.baseColumName(atIndex: index))
|
||||
}
|
||||
|
||||
func columnName(atIndex index: Int) -> String {
|
||||
return columnMapping.columnName(atIndex: index)
|
||||
}
|
||||
|
||||
func indexOfColumn(named name: String) -> Int? {
|
||||
return columnMapping.indexOfColumn(named: name)
|
||||
}
|
||||
|
||||
func subrow(named name: String) -> Row? {
|
||||
guard let columnMapping = subrowColumnMappings[name] else {
|
||||
return nil
|
||||
}
|
||||
return Row(baseRow: baseRow, columnMapping: columnMapping, subrowColumnMappings: [:])
|
||||
}
|
||||
|
||||
var subrowNames: Set<String> {
|
||||
return Set(subrowColumnMappings.keys)
|
||||
}
|
||||
|
||||
func copy(row: Row) -> Row {
|
||||
return Row(baseRow: baseRow.copy(), columnMapping: columnMapping, subrowColumnMappings: subrowColumnMappings)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
#if !SQLITE_HAS_CODEC
|
||||
#if os(OSX)
|
||||
import SQLiteMacOSX
|
||||
#elseif os(iOS)
|
||||
#if (arch(i386) || arch(x86_64))
|
||||
import SQLiteiPhoneSimulator
|
||||
#else
|
||||
import SQLiteiPhoneOS
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
public struct RowAdapter {
|
||||
let impl: RowAdapterImpl
|
||||
|
||||
public init(_ mainAdapter: RowAdapter, subrowAdapters: [String: RowAdapter]) {
|
||||
impl = NestedRowAdapterImpl(mainAdapter: mainAdapter, subrowAdapters: subrowAdapters)
|
||||
}
|
||||
|
||||
public init(_ dictionary: [String: String]) {
|
||||
self.init(impl: DictionaryRowAdapterImpl(dictionary: dictionary))
|
||||
}
|
||||
|
||||
init(impl: RowAdapterImpl) {
|
||||
self.impl = impl
|
||||
}
|
||||
|
||||
func boundRowAdapter(with statement: SelectStatement) throws -> BoundRowAdapter {
|
||||
return try impl.boundRowAdapter(with: statement)
|
||||
}
|
||||
|
||||
func orderedMapping(statement statement: SelectStatement) throws -> [(String, String)] {
|
||||
return try impl.orderedMapping(statement: statement)
|
||||
}
|
||||
}
|
||||
|
||||
extension RowAdapter : DictionaryLiteralConvertible {
|
||||
public init(dictionaryLiteral elements: (String, String)...) {
|
||||
self.init(impl: DictionaryRowAdapterImpl(dictionary: Dictionary(keyValueSequence: elements)))
|
||||
}
|
||||
}
|
||||
|
||||
protocol RowAdapterImpl {
|
||||
func boundRowAdapter(with statement: SelectStatement) throws -> BoundRowAdapter
|
||||
func orderedMapping(statement statement: SelectStatement) throws -> [(String, String)]
|
||||
}
|
||||
|
||||
struct DictionaryRowAdapterImpl: RowAdapterImpl {
|
||||
let dictionary: [String: String]
|
||||
|
||||
func boundRowAdapter(with statement: SelectStatement) throws -> BoundRowAdapter {
|
||||
let mainColumnsAdapter = try ColumnsAdapter(orderedMapping: orderedMapping(statement: statement))
|
||||
return BoundRowAdapter(mainColumnsAdapter: mainColumnsAdapter, subrowBoundRowAdapters: [:])
|
||||
}
|
||||
|
||||
// Turns a dictionary [mappedColumn:baseColumn, ...] into an array
|
||||
// [(mappedColumn, baseColumn), ...] ordered like the satement columns.
|
||||
func orderedMapping(statement statement: SelectStatement) throws -> [(String, String)] {
|
||||
return try dictionary
|
||||
.map { (mappedColumn, baseColumn) -> (Int, (String, String)) in
|
||||
guard let index = statement.indexOfColumn(named: baseColumn) else {
|
||||
throw DatabaseError(code: SQLITE_MISUSE, message: "Mapping references missing column \(baseColumn). Valid column names are: \(statement.columnNames.joinWithSeparator(", ")).")
|
||||
}
|
||||
return (index, (mappedColumn, baseColumn))
|
||||
}
|
||||
.sort { return $0.0 < $1.0 }
|
||||
.map { $0.1 }
|
||||
}
|
||||
}
|
||||
|
||||
struct NestedRowAdapterImpl: RowAdapterImpl {
|
||||
let mainAdapter: RowAdapter
|
||||
let subrowAdapters: [String: RowAdapter]
|
||||
|
||||
func boundRowAdapter(with statement: SelectStatement) throws -> BoundRowAdapter {
|
||||
let mainColumnsAdapter = try ColumnsAdapter(orderedMapping: orderedMapping(statement: statement))
|
||||
let subrowBoundRowAdapters = try Dictionary(keyValueSequence: subrowAdapters.map { (identifier, adapter) in
|
||||
(identifier, try BoundRowAdapter(mainColumnsAdapter: ColumnsAdapter(orderedMapping: adapter.orderedMapping(statement: statement)), subrowBoundRowAdapters: [:]))
|
||||
})
|
||||
return BoundRowAdapter(mainColumnsAdapter: mainColumnsAdapter, subrowBoundRowAdapters: subrowBoundRowAdapters)
|
||||
}
|
||||
|
||||
func orderedMapping(statement statement: SelectStatement) throws -> [(String, String)] {
|
||||
return try mainAdapter.orderedMapping(statement: statement)
|
||||
}
|
||||
}
|
||||
|
||||
struct BoundRowAdapter {
|
||||
let mainColumnsAdapter: ColumnsAdapter
|
||||
let subrowBoundRowAdapters: [String: BoundRowAdapter]
|
||||
}
|
||||
|
||||
struct ColumnsAdapter {
|
||||
let orderedMapping: [(String, String)] // [(mappedColumn, baseColumn), ...]
|
||||
let lowercaseColumnIndexes: [String: Int]
|
||||
|
||||
init(orderedMapping: [(String, String)]) {
|
||||
self.orderedMapping = orderedMapping
|
||||
self.lowercaseColumnIndexes = Dictionary(keyValueSequence: orderedMapping.enumerate().map { ($1.0.lowercaseString, $0) })
|
||||
}
|
||||
|
||||
var count: Int {
|
||||
return orderedMapping.count
|
||||
}
|
||||
|
||||
func baseColumName(atIndex index: Int) -> String {
|
||||
return orderedMapping[index].1
|
||||
}
|
||||
|
||||
func columnName(atIndex index: Int) -> String {
|
||||
return orderedMapping[index].0
|
||||
}
|
||||
|
||||
func indexOfColumn(named name: String) -> Int? {
|
||||
if let index = lowercaseColumnIndexes[name] {
|
||||
return index
|
||||
}
|
||||
return lowercaseColumnIndexes[name.lowercaseString]
|
||||
}
|
||||
}
|
||||
|
||||
extension Row {
|
||||
/// Builds a row from a base row and column mappings
|
||||
convenience init(baseRow: Row, boundRowAdapter: BoundRowAdapter) {
|
||||
self.init(impl: AdaptedRowImpl(baseRow: baseRow, boundRowAdapter: boundRowAdapter))
|
||||
}
|
||||
}
|
||||
|
||||
// See Row.init(baseRow:boundRowAdapter:)
|
||||
private struct AdaptedRowImpl : RowImpl {
|
||||
let baseRow: Row
|
||||
let boundRowAdapter: BoundRowAdapter
|
||||
var mainAdapter: ColumnsAdapter { return boundRowAdapter.mainColumnsAdapter }
|
||||
|
||||
init(baseRow: Row, boundRowAdapter: BoundRowAdapter) {
|
||||
self.baseRow = baseRow
|
||||
self.boundRowAdapter = boundRowAdapter
|
||||
}
|
||||
|
||||
var count: Int {
|
||||
return mainAdapter.count
|
||||
}
|
||||
|
||||
func databaseValue(atIndex index: Int) -> DatabaseValue {
|
||||
return baseRow.databaseValue(named: mainAdapter.baseColumName(atIndex: index))!
|
||||
}
|
||||
|
||||
func dataNoCopy(atIndex index:Int) -> NSData? {
|
||||
return baseRow.dataNoCopy(named: mainAdapter.baseColumName(atIndex: index))
|
||||
}
|
||||
|
||||
func columnName(atIndex index: Int) -> String {
|
||||
return mainAdapter.columnName(atIndex: index)
|
||||
}
|
||||
|
||||
func indexOfColumn(named name: String) -> Int? {
|
||||
return mainAdapter.indexOfColumn(named: name)
|
||||
}
|
||||
|
||||
func subrow(named name: String) -> Row? {
|
||||
guard let adapter = boundRowAdapter.subrowBoundRowAdapters[name] else {
|
||||
return nil
|
||||
}
|
||||
return Row(baseRow: baseRow, boundRowAdapter: adapter)
|
||||
}
|
||||
|
||||
var subrowNames: Set<String> {
|
||||
return Set(boundRowAdapter.subrowBoundRowAdapters.keys)
|
||||
}
|
||||
|
||||
func copy(row: Row) -> Row {
|
||||
return Row(baseRow: baseRow.copy(), boundRowAdapter: boundRowAdapter)
|
||||
}
|
||||
}
|
||||
@@ -69,8 +69,8 @@ extension RowConvertible {
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) -> DatabaseSequence<Self> {
|
||||
let row: Row
|
||||
if let adapter = adapter {
|
||||
let (columnMapping, subrowColumnMappings) = try! adapter.columnMappings(statement)
|
||||
row = Row(baseRow: Row(statement: statement), columnMapping: columnMapping, subrowColumnMappings: subrowColumnMappings)
|
||||
let boundRowAdapter = try! adapter.boundRowAdapter(with: statement)
|
||||
row = Row(baseRow: Row(statement: statement), boundRowAdapter: boundRowAdapter)
|
||||
} else {
|
||||
row = Row(statement: statement)
|
||||
}
|
||||
|
||||
@@ -111,8 +111,8 @@ class RowConvertibleTests: GRDBTestCase {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
dbQueue.inDatabase { db in
|
||||
let adapter = RowAdapter(
|
||||
mapping: ["firstName": "firstName1", "lastName": "lastName1"],
|
||||
subrowMappings: ["bestFriend": ["firstName": "firstName2", "lastName": "lastName2"]])
|
||||
["firstName": "firstName1", "lastName": "lastName1"],
|
||||
subrowAdapters: ["bestFriend": ["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)
|
||||
@@ -132,8 +132,8 @@ class RowConvertibleTests: GRDBTestCase {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
dbQueue.inDatabase { db in
|
||||
let adapter = RowAdapter(
|
||||
mapping: ["firstName": "firstName1", "lastName": "lastName1"],
|
||||
subrowMappings: ["bestFriend": ["firstName": "firstName2", "lastName": "lastName2"]])
|
||||
["firstName": "firstName1", "lastName": "lastName1"],
|
||||
subrowAdapters: ["bestFriend": ["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,8 +153,8 @@ class RowConvertibleTests: GRDBTestCase {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
dbQueue.inDatabase { db in
|
||||
let adapter = RowAdapter(
|
||||
mapping: ["firstName": "firstName1", "lastName": "lastName1"],
|
||||
subrowMappings: ["bestFriend": ["firstName": "firstName2", "lastName": "lastName2"]])
|
||||
["firstName": "firstName1", "lastName": "lastName1"],
|
||||
subrowAdapters: ["bestFriend": ["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)!
|
||||
@@ -220,8 +220,8 @@ class RowConvertibleTests: GRDBTestCase {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
let adapter = RowAdapter(
|
||||
mapping: ["firstName": "firstName1", "lastName": "lastName1"],
|
||||
subrowMappings: ["bestFriend": ["firstName": "firstName2", "lastName": "lastName2"]])
|
||||
["firstName": "firstName1", "lastName": "lastName1"],
|
||||
subrowAdapters: ["bestFriend": ["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)
|
||||
@@ -242,8 +242,8 @@ class RowConvertibleTests: GRDBTestCase {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
let adapter = RowAdapter(
|
||||
mapping: ["firstName": "firstName1", "lastName": "lastName1"],
|
||||
subrowMappings: ["bestFriend": ["firstName": "firstName2", "lastName": "lastName2"]])
|
||||
["firstName": "firstName1", "lastName": "lastName1"],
|
||||
subrowAdapters: ["bestFriend": ["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)
|
||||
@@ -264,8 +264,8 @@ class RowConvertibleTests: GRDBTestCase {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
let adapter = RowAdapter(
|
||||
mapping: ["firstName": "firstName1", "lastName": "lastName1"],
|
||||
subrowMappings: ["bestFriend": ["firstName": "firstName2", "lastName": "lastName2"]])
|
||||
["firstName": "firstName1", "lastName": "lastName1"],
|
||||
subrowAdapters: ["bestFriend": ["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)
|
||||
|
||||
@@ -128,7 +128,7 @@ class FetchedRecordsControllerTests: GRDBTestCase {
|
||||
return cervantes.id!
|
||||
}
|
||||
|
||||
let adapter = RowAdapter(mapping: ["id": "_id", "authorId": "_authorId", "title": "_title"])
|
||||
let adapter = RowAdapter(["id": "_id", "authorId": "_authorId", "title": "_title"])
|
||||
let controller = FetchedRecordsController<Book>(dbQueue, sql: "SELECT id AS _id, authorId AS _authorId, title AS _title FROM books WHERE authorID = ?", arguments: [authorId], adapter: adapter)
|
||||
controller.performFetch()
|
||||
XCTAssertEqual(controller.fetchedRecords!.count, 1)
|
||||
|
||||
Reference in new issue
Block a user