From 933a472da7c2c58e013fb72dc08b4faf647d7063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 4 Jul 2015 16:57:01 +0200 Subject: [PATCH] SelectStatement.fetch(RowModel, bindings:) --- .../RowModelSelectStatementTests.swift | 31 ++++++++++++++++ GRDB.xcodeproj/project.pbxproj | 4 +++ GRDB/RowModel.swift | 34 ++++++++++++++++++ README.md | 35 +++++++++++++------ 4 files changed, 93 insertions(+), 11 deletions(-) create mode 100644 GRDB Tests/RowModel/RowModelSelectStatementTests.swift diff --git a/GRDB Tests/RowModel/RowModelSelectStatementTests.swift b/GRDB Tests/RowModel/RowModelSelectStatementTests.swift new file mode 100644 index 000000000..5750df2da --- /dev/null +++ b/GRDB Tests/RowModel/RowModelSelectStatementTests.swift @@ -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) + } + } + } + } +} diff --git a/GRDB.xcodeproj/project.pbxproj b/GRDB.xcodeproj/project.pbxproj index 9154712c2..0495c5c4c 100644 --- a/GRDB.xcodeproj/project.pbxproj +++ b/GRDB.xcodeproj/project.pbxproj @@ -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 = ""; }; 56CC3C9A1B4464AE00A9A534 /* RowModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowModel.swift; sourceTree = ""; }; 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 = ""; }; 56E1524B1B43FD5B00EE27AA /* TODO.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = TODO.md; sourceTree = ""; }; 56E4BB7E1B452A6200FD324B /* SQLite.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLite.swift; sourceTree = ""; }; 56E4BB801B45373600FD324B /* Bindings.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Bindings.swift; sourceTree = ""; }; @@ -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 */, diff --git a/GRDB/RowModel.swift b/GRDB/RowModel.swift index 9c6b93347..c5f53f940 100644 --- a/GRDB/RowModel.swift +++ b/GRDB/RowModel.swift @@ -331,3 +331,37 @@ extension Database { } } +extension SelectStatement { + + // let persons = statement.fetch(Person.self, bindings: ...) + public func fetch(type: RowModel.Type, bindings: Bindings? = nil) -> AnySequence { + let rowSequence = fetchRows(bindings: bindings) + return AnySequence { () -> AnyGenerator 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(type: RowModel.Type, bindings: Bindings? = nil) -> [RowModel] { + return Array(fetch(type, bindings: bindings)) + } + + // let person = statement.fetchOne(Person.self, bindings: ...) + public func fetchOne(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 + } + } +} + diff --git a/README.md b/README.md index 16294c153..b2780d767 100644 --- a/README.md +++ b/README.md @@ -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 - db.fetchAll(String.self, "SELECT ...", bindings: ...) // [String?] - db.fetchOne(String.self, "SELECT ...", bindings: ...) // String? + db.fetch(Int.self, "SELECT ...", bindings: ...) // AnySequence + 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 + statement.fetchAllRows(bindings: ...) // [Row] + statement.fetchOneRow(bindings: ...) // Row? + + statement.fetch(Int.self, bindings: ...) // AnySequence + 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 + 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 + 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? } ```