Row.columns, Row.databaseValues

This commit is contained in:
Gwendal Roué committed 2015-09-05 12:37:46 +02:00
1 parent 8fc4927eb7
commit df78541430
4 files changed
+85 -4

No files matched your search

@@ -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])
}
}
@@ -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])
}
}
}
}
+25 -4
View File
@@ -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<Row, String> {
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<Row, DatabaseValue> {
return LazyMapCollection(self) { $0.1 }
}
/// Returns a *generator* over (ColumnName, DatabaseValue) pairs, from left
/// to right.
public func generate() -> IndexingGenerator<Row> {
@@ -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?
+6
View File
@@ -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