diff --git a/GRDB Tests/Public/Row/DictionaryRowTests.swift b/GRDB Tests/Public/Row/DictionaryRowTests.swift index 105cb62fc..74a369797 100644 --- a/GRDB Tests/Public/Row/DictionaryRowTests.swift +++ b/GRDB Tests/Public/Row/DictionaryRowTests.swift @@ -43,4 +43,19 @@ class DictionaryRowTests: GRDBTestCase { // row.value(named: "foo") // row.value(named: "foo") as Int? } + + func testRowCount() { + let row = Row(dictionary: ["a": 0, "b": 1, "c": 2]) + XCTAssertEqual(row.count, 3) + } + + func testRowColumns() { + let row = Row(dictionary: ["a": 0, "b": 1, "c": 2]) + XCTAssertEqual(Array(row.columns).sort(), ["a", "b", "c"]) + } + + func testRowDatabaseValues() { + let row = Row(dictionary: ["a": 0, "b": 1, "c": 2]) + XCTAssertEqual(row.databaseValues.sort { ($0.value() as Int!) < ($1.value() as Int!) }, [0.databaseValue, 1.databaseValue, 2.databaseValue]) + } } diff --git a/GRDB Tests/Public/Row/FetchedRowTests.swift b/GRDB Tests/Public/Row/FetchedRowTests.swift index be39b6a32..4fe2af4bd 100644 --- a/GRDB Tests/Public/Row/FetchedRowTests.swift +++ b/GRDB Tests/Public/Row/FetchedRowTests.swift @@ -73,4 +73,43 @@ class FetchedRowTests: GRDBTestCase { } } } + + func testRowCount() { + assertNoError { + let dbQueue = DatabaseQueue() + try dbQueue.inDatabase { db in + try db.execute("CREATE TABLE ints (a INTEGER, b INTEGER, c INTEGER)") + try db.execute("INSERT INTO ints (a,b,c) VALUES (0, 1, 2)") + let row = Row.fetchOne(db, "SELECT * FROM ints")! + + XCTAssertEqual(row.count, 3) + } + } + } + + func testRowColumns() { + assertNoError { + let dbQueue = DatabaseQueue() + try dbQueue.inDatabase { db in + try db.execute("CREATE TABLE ints (a INTEGER, b INTEGER, c INTEGER)") + try db.execute("INSERT INTO ints (a,b,c) VALUES (0, 1, 2)") + let row = Row.fetchOne(db, "SELECT a, b, c FROM ints")! + + XCTAssertEqual(Array(row.columns), ["a", "b", "c"]) + } + } + } + + func testRowDatabaseValues() { + assertNoError { + let dbQueue = DatabaseQueue() + try dbQueue.inDatabase { db in + try db.execute("CREATE TABLE ints (a INTEGER, b INTEGER, c INTEGER)") + try db.execute("INSERT INTO ints (a,b,c) VALUES (0, 1, 2)") + let row = Row.fetchOne(db, "SELECT a, b, c FROM ints")! + + XCTAssertEqual(Array(row.databaseValues), [0.databaseValue, 1.databaseValue, 2.databaseValue]) + } + } + } } diff --git a/GRDB/Row.swift b/GRDB/Row.swift index 6258d88b9..a21ae73c8 100644 --- a/GRDB/Row.swift +++ b/GRDB/Row.swift @@ -182,6 +182,27 @@ public struct Row: CollectionType { // MARK: - Row as a Collection of (ColumnName, DatabaseValue) Pairs + /// The number of columns in the row. + public var count: Int { + return impl.count + } + + /// The names of columns in the row. + /// + /// Columns appear in the same order as they occur as the `.0` member + /// of column-value pairs in `self`. + public var columns: LazyMapCollection { + return LazyMapCollection(self) { $0.0 } + } + + /// The database values in the row. + /// + /// Values appear in the same order as they occur as the `.1` member + /// of column-value pairs in `self`. + public var databaseValues: LazyMapCollection { + return LazyMapCollection(self) { $0.1 } + } + /// Returns a *generator* over (ColumnName, DatabaseValue) pairs, from left /// to right. public func generate() -> IndexingGenerator { @@ -196,7 +217,7 @@ public struct Row: CollectionType { /// The "past-the-end" index, successor of the index of the last /// (ColumnName, DatabaseValue) pair. public var endIndex: RowIndex { - return Index(impl.columnCount) + return Index(impl.count) } /// Returns the (ColumnName, DatabaseValue) pair at given index. @@ -319,7 +340,7 @@ public struct Row: CollectionType { private struct DictionaryRowImpl : RowImpl { let databaseDictionary: [String: DatabaseValue] - var columnCount: Int { + var count: Int { return databaseDictionary.count } @@ -364,7 +385,7 @@ public struct Row: CollectionType { self.databaseDictionary = databaseDictionary } - var columnCount: Int { + var count: Int { return columnNames.count } @@ -396,7 +417,7 @@ extension Row: CustomStringConvertible { // The protocol for Row underlying implementation protocol RowImpl { - var columnCount: Int { get } + var count: Int { get } func databaseValue(atIndex index: Int) -> DatabaseValue func columnName(atIndex index: Int) -> String func indexForColumn(named name: String) -> Int? diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 7e2834eaf..585f1e6ff 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,6 +1,12 @@ Release Notes ============= +## Next Release + +- `Row.columns` returns the names of columns in the row. +- `Row.databaseValues` returns the database values in the row. + + ## 0.11.0 Released September 4, 2015