mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
SelectStatement.fetch(RowModel, bindings:)
This commit is contained in:
1 parent
2441686368
commit
933a472da7
4 files changed
+93
-11
No files matched your search
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// RowModelSelectStatementTests.swift
|
||||
// GRDB
|
||||
//
|
||||
// Created by Gwendal Roué on 04/07/2015.
|
||||
// Copyright © 2015 Gwendal Roué. All rights reserved.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
|
||||
class RowModelSelectStatementTests: RowModelTests {
|
||||
|
||||
func testBlah() {
|
||||
assertNoError {
|
||||
try dbQueue.inTransaction { db in
|
||||
try Person(name: "Arthur", age: 41).insert(db)
|
||||
try Person(name: "Barbara", age: 37).insert(db)
|
||||
return .Commit
|
||||
}
|
||||
|
||||
try dbQueue.inDatabase { db in
|
||||
let statement = try db.selectStatement("SELECT * FROM persons WHERE name = ?")
|
||||
|
||||
for name in ["Arthur", "Barbara"] {
|
||||
let person = statement.fetchOne(Person.self, bindings: [name])!
|
||||
XCTAssertEqual(person.name!, name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,7 @@
|
||||
56CC3C991B445DDB00A9A534 /* GRDBTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3C981B445DDB00A9A534 /* GRDBTests.swift */; };
|
||||
56CC3C9B1B4464AE00A9A534 /* RowModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3C9A1B4464AE00A9A534 /* RowModel.swift */; };
|
||||
56CC3CA31B47D19F00A9A534 /* SQLiteValueConvertibleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3CA21B47D19F00A9A534 /* SQLiteValueConvertibleTests.swift */; };
|
||||
56CC3CA51B48289200A9A534 /* RowModelSelectStatementTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56CC3CA41B48289200A9A534 /* RowModelSelectStatementTests.swift */; };
|
||||
56E4BB7F1B452A6200FD324B /* SQLite.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56E4BB7E1B452A6200FD324B /* SQLite.swift */; };
|
||||
56E4BB811B45373600FD324B /* Bindings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 56E4BB801B45373600FD324B /* Bindings.swift */; };
|
||||
DC2393C81ABE35F8003FF113 /* GRDB-Bridging.h in Headers */ = {isa = PBXBuildFile; fileRef = DC2393C61ABE35F8003FF113 /* GRDB-Bridging.h */; settings = {ATTRIBUTES = (Public, ); }; };
|
||||
@@ -73,6 +74,7 @@
|
||||
56CC3C981B445DDB00A9A534 /* GRDBTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GRDBTests.swift; sourceTree = "<group>"; };
|
||||
56CC3C9A1B4464AE00A9A534 /* RowModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowModel.swift; sourceTree = "<group>"; };
|
||||
56CC3CA21B47D19F00A9A534 /* SQLiteValueConvertibleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLiteValueConvertibleTests.swift; sourceTree = SOURCE_ROOT; };
|
||||
56CC3CA41B48289200A9A534 /* RowModelSelectStatementTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowModelSelectStatementTests.swift; sourceTree = "<group>"; };
|
||||
56E1524B1B43FD5B00EE27AA /* TODO.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = TODO.md; sourceTree = "<group>"; };
|
||||
56E4BB7E1B452A6200FD324B /* SQLite.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLite.swift; sourceTree = "<group>"; };
|
||||
56E4BB801B45373600FD324B /* Bindings.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Bindings.swift; sourceTree = "<group>"; };
|
||||
@@ -142,6 +144,7 @@
|
||||
56C9E6551B468EB200DDC541 /* PrimaryKeySingleTests.swift */,
|
||||
56C9E6561B468EB200DDC541 /* PrimaryKeySQLiteRowIDTests.swift */,
|
||||
56C9E6571B468EB200DDC541 /* RowModelSubClassTests.swift */,
|
||||
56CC3CA41B48289200A9A534 /* RowModelSelectStatementTests.swift */,
|
||||
56C9E6581B468EB200DDC541 /* RowModelTests.swift */,
|
||||
);
|
||||
path = RowModel;
|
||||
@@ -375,6 +378,7 @@
|
||||
56C9E6611B468EB200DDC541 /* PrimaryKeySQLiteRowIDTests.swift in Sources */,
|
||||
56C9E65F1B468EB200DDC541 /* PrimaryKeyNoneTests.swift in Sources */,
|
||||
56C9E6681B4696E100DDC541 /* SelectStatementTests.swift in Sources */,
|
||||
56CC3CA51B48289200A9A534 /* RowModelSelectStatementTests.swift in Sources */,
|
||||
56CC3CA31B47D19F00A9A534 /* SQLiteValueConvertibleTests.swift in Sources */,
|
||||
56CC3C991B445DDB00A9A534 /* GRDBTests.swift in Sources */,
|
||||
56C9E6601B468EB200DDC541 /* PrimaryKeySingleTests.swift in Sources */,
|
||||
|
||||
@@ -331,3 +331,37 @@ extension Database {
|
||||
}
|
||||
}
|
||||
|
||||
extension SelectStatement {
|
||||
|
||||
// let persons = statement.fetch(Person.self, bindings: ...)
|
||||
public func fetch<RowModel: GRDB.RowModel>(type: RowModel.Type, bindings: Bindings? = nil) -> AnySequence<RowModel> {
|
||||
let rowSequence = fetchRows(bindings: bindings)
|
||||
return AnySequence { () -> AnyGenerator<RowModel> in
|
||||
let rowGenerator = rowSequence.generate()
|
||||
return anyGenerator { () -> RowModel? in
|
||||
if let row = rowGenerator.next() {
|
||||
return RowModel.init(row: row)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// let persons = statement.fetchAll(Person.self, bindings: ...)
|
||||
public func fetchAll<RowModel: GRDB.RowModel>(type: RowModel.Type, bindings: Bindings? = nil) -> [RowModel] {
|
||||
return Array(fetch(type, bindings: bindings))
|
||||
}
|
||||
|
||||
// let person = statement.fetchOne(Person.self, bindings: ...)
|
||||
public func fetchOne<RowModel: GRDB.RowModel>(type: RowModel.Type, bindings: Bindings? = nil) -> RowModel? {
|
||||
if let first = fetch(type, bindings: bindings).generate().next() {
|
||||
// one row containing an optional value
|
||||
return first
|
||||
} else {
|
||||
// no row
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,10 +170,9 @@ You can load **lazy sequences** of values, **arrays**, or a **single** value:
|
||||
|
||||
```swift
|
||||
dbQueue.inDatabase { db in
|
||||
|
||||
db.fetch(String.self, "SELECT ...", bindings: ...) // AnySequence<String?>
|
||||
db.fetchAll(String.self, "SELECT ...", bindings: ...) // [String?]
|
||||
db.fetchOne(String.self, "SELECT ...", bindings: ...) // String?
|
||||
db.fetch(Int.self, "SELECT ...", bindings: ...) // AnySequence<Int?>
|
||||
db.fetchAll(Int.self, "SELECT ...", bindings: ...) // [Int?]
|
||||
db.fetchOne(Int.self, "SELECT ...", bindings: ...) // Int?
|
||||
}
|
||||
|
||||
|
||||
@@ -317,7 +316,7 @@ try dbQueue.inTransaction { db in
|
||||
}
|
||||
```
|
||||
|
||||
Select statements:
|
||||
Select statements can load rows and values:
|
||||
|
||||
```swift
|
||||
try dbQueue.inDatabase { db in
|
||||
@@ -325,8 +324,13 @@ try dbQueue.inDatabase { db in
|
||||
let statement = try db.selectStatement("SELECT COUNT(*) FROM persons " +
|
||||
"WHERE age < ?")
|
||||
|
||||
let lessThanTwentyCount = statement.fetchOne(Int.self, bindings: [20])!
|
||||
let lessThanFortyCount = statement.fetchOne(Int.self, bindings: [40])!
|
||||
statement.fetchRows(bindings: ...) // AnySequence<Row>
|
||||
statement.fetchAllRows(bindings: ...) // [Row]
|
||||
statement.fetchOneRow(bindings: ...) // Row?
|
||||
|
||||
statement.fetch(Int.self, bindings: ...) // AnySequence<Int?>
|
||||
statement.fetchAll(Int.self, bindings: ...) // [Int?]
|
||||
statement.fetchOne(Int.self, bindings: ...) // Int?
|
||||
}
|
||||
```
|
||||
|
||||
@@ -422,8 +426,17 @@ class Person : RowModel {
|
||||
}
|
||||
}
|
||||
|
||||
let persons = dbQueue.inDatabase { db in
|
||||
db.fetchAll(Person.self, "SELECT * FROM persons")
|
||||
dbQueue.inDatabase { db in
|
||||
|
||||
db.fetch(Person.self, "SELECT ...", bindings:...) // AnySequence<Person>
|
||||
db.fetchAll(Person.self, "SELECT ...", bindings:...) // [Person]
|
||||
db.fetchOne(Person.self, "SELECT ...", bindings:...) // Person?
|
||||
|
||||
let statement = db.selectStatement("SELECT ...")
|
||||
|
||||
statement.fetch(Person.self, bindings:...) // AnySequence<Person>
|
||||
statement.fetchAll(Person.self, bindings:...) // [Person]
|
||||
statement.fetchOne(Person.self, bindings:...) // Person?
|
||||
}
|
||||
```
|
||||
|
||||
@@ -439,8 +452,8 @@ class Person : RowModel {
|
||||
}
|
||||
}
|
||||
|
||||
let person = dbQueue.inDatabase { db in
|
||||
db.fetchOne(Person.self, primaryKey: 123)
|
||||
dbQueue.inDatabase { db in
|
||||
db.fetchOne(Person.self, primaryKey: 123) // Person?
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Reference in new issue
Block a user