mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
Merge branch 'master' of https://github.com/groue/GRDB.swift into FetchedRecordsController
This commit is contained in:
commit
3c28277f15
10 files changed
+131
-81
No files matched your search
@@ -1,6 +1,16 @@
|
||||
Release Notes
|
||||
=============
|
||||
|
||||
|
||||
## 0.42.1
|
||||
|
||||
Released January 29, 2016
|
||||
|
||||
**Fixed**
|
||||
|
||||
- Improved consistency of the [query interface](https://github.com/groue/GRDB.swift/#the-query-interface).
|
||||
|
||||
|
||||
## 0.42.0
|
||||
|
||||
Released January 28, 2016
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
Pod::Spec.new do |s|
|
||||
s.name = 'GRDB.swift'
|
||||
s.version = '0.42.0'
|
||||
s.version = '0.42.1'
|
||||
s.license = { :type => 'MIT', :file => 'LICENSE' }
|
||||
s.summary = 'Versatile SQLite toolkit for Swift.'
|
||||
s.homepage = 'https://github.com/groue/GRDB.swift'
|
||||
|
||||
@@ -88,7 +88,7 @@ extension RowConvertible {
|
||||
/// Returns a single value fetched from a prepared statement.
|
||||
///
|
||||
/// let statement = db.selectStatement("SELECT * FROM persons")
|
||||
/// let persons = Person.fetchOne(statement) // Person?
|
||||
/// let person = Person.fetchOne(statement) // Person?
|
||||
///
|
||||
/// - parameter statement: The statement to run.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
|
||||
@@ -146,24 +146,7 @@ extension FetchRequest {
|
||||
/// Returns the number of rows matched by the request.
|
||||
@warn_unused_result
|
||||
public func fetchCount(db: Database) -> Int {
|
||||
return fetchAggregate(db, _SQLExpression.Count(_SQLResultColumn.Star(nil)))!
|
||||
}
|
||||
|
||||
/// Returns the number of non-NULL values of *counted*.
|
||||
@warn_unused_result
|
||||
public func fetchCount(db: Database, _ counted: _SQLSelectable) -> Int {
|
||||
return fetchAggregate(db, _SQLExpression.Count(counted ?? _SQLResultColumn.Star(nil)))!
|
||||
}
|
||||
|
||||
/// Returns the number of distinct values of *counted*.
|
||||
@warn_unused_result
|
||||
public func fetchCount(db: Database, distinct counted: _SQLExpressionType) -> Int {
|
||||
return fetchAggregate(db, count(distinct: counted.SQLExpression))!
|
||||
}
|
||||
|
||||
@warn_unused_result
|
||||
private func fetchAggregate<T: DatabaseValueConvertible>(db: Database, _ selection: _SQLSelectable) -> T? {
|
||||
return T.fetchOne(db, FetchRequest<Void>(query.select([selection])))
|
||||
return Int.fetchOne(db, select([_SQLExpression.Count(_SQLResultColumn.Star(nil))]))!
|
||||
}
|
||||
}
|
||||
|
||||
@@ -196,7 +179,7 @@ extension FetchRequest where T: RowConvertible {
|
||||
/// Returns an array of values fetched from a fetch request.
|
||||
///
|
||||
/// let request = Person.order(name)
|
||||
/// let persons = request.fetchAll(db) // DatabaseSequence<Person>
|
||||
/// let persons = request.fetchAll(db) // [Person]
|
||||
@warn_unused_result
|
||||
public func fetchAll(db: Database) -> [T] {
|
||||
return Array(fetch(db))
|
||||
@@ -205,7 +188,7 @@ extension FetchRequest where T: RowConvertible {
|
||||
/// Returns a single value fetched from a fetch request.
|
||||
///
|
||||
/// let request = Person.order(name)
|
||||
/// let persons = request.fetchOne(db) // Person?
|
||||
/// let person = request.fetchOne(db) // Person?
|
||||
@warn_unused_result
|
||||
public func fetchOne(db: Database) -> T? {
|
||||
return fetch(db).generate().next()
|
||||
@@ -273,6 +256,12 @@ extension TableMapping {
|
||||
return all().order(sql: sql)
|
||||
}
|
||||
|
||||
/// Returns a FetchRequest sorted in reversed order.
|
||||
@warn_unused_result
|
||||
public static func reverse() -> FetchRequest<Self> {
|
||||
return all().reverse()
|
||||
}
|
||||
|
||||
/// Returns a FetchRequest which fetches *limit* rows, starting at
|
||||
/// *offset*.
|
||||
@warn_unused_result
|
||||
@@ -291,18 +280,6 @@ extension TableMapping {
|
||||
public static func fetchCount(db: Database) -> Int {
|
||||
return all().fetchCount(db)
|
||||
}
|
||||
|
||||
/// Returns the number of non-NULL values of *counted*.
|
||||
@warn_unused_result
|
||||
public static func fetchCount(db: Database, _ counted: _SQLSelectable) -> Int {
|
||||
return all().fetchCount(db, counted)
|
||||
}
|
||||
|
||||
/// Returns the number of distinct values of *counted*.
|
||||
@warn_unused_result
|
||||
public static func fetchCount(db: Database, distinct counted: _SQLExpressionType) -> Int {
|
||||
return all().fetchCount(db, distinct: counted)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -466,7 +443,7 @@ extension RowConvertible {
|
||||
/// Returns an array of values fetched from a fetch request.
|
||||
///
|
||||
/// let request = Person.order(name)
|
||||
/// let persons = Person.fetchAll(db, request) // DatabaseSequence<Person>
|
||||
/// let persons = Person.fetchAll(db, request) // [Person]
|
||||
@warn_unused_result
|
||||
public static func fetchAll<T>(db: Database, _ request: FetchRequest<T>) -> [Self] {
|
||||
return try! fetchAll(request.selectStatement(db))
|
||||
@@ -475,13 +452,54 @@ extension RowConvertible {
|
||||
/// Returns a single value fetched from a fetch request.
|
||||
///
|
||||
/// let request = Person.order(name)
|
||||
/// let persons = Person.fetchOne(db, request) // Person?
|
||||
/// let person = Person.fetchOne(db, request) // Person?
|
||||
@warn_unused_result
|
||||
public static func fetchOne<T>(db: Database, _ request: FetchRequest<T>) -> Self? {
|
||||
return try! fetchOne(request.selectStatement(db))
|
||||
}
|
||||
}
|
||||
|
||||
extension RowConvertible where Self: TableMapping {
|
||||
|
||||
// MARK: Fetching All
|
||||
|
||||
/// Returns a sequence of all values fetched from the database.
|
||||
///
|
||||
/// let persons = Person.fetch(db) // DatabaseSequence<Person>
|
||||
///
|
||||
/// The returned sequence can be consumed several times, but it may yield
|
||||
/// different results, should database changes have occurred between two
|
||||
/// generations:
|
||||
///
|
||||
/// let persons = Person.fetch(db)
|
||||
/// Array(persons).count // 3
|
||||
/// db.execute("DELETE ...")
|
||||
/// Array(persons).count // 2
|
||||
///
|
||||
/// If the database is modified while the sequence is iterating, the
|
||||
/// remaining elements are undefined.
|
||||
@warn_unused_result
|
||||
public static func fetch(db: Database) -> DatabaseSequence<Self> {
|
||||
return all().fetch(db)
|
||||
}
|
||||
|
||||
/// Returns an array of all values fetched from the database.
|
||||
///
|
||||
/// let persons = Person.fetchAll(db) // [Person]
|
||||
@warn_unused_result
|
||||
public static func fetchAll(db: Database) -> [Self] {
|
||||
return all().fetchAll(db)
|
||||
}
|
||||
|
||||
/// Returns the first value fetched from a fetch request.
|
||||
///
|
||||
/// let person = Person.fetchOne(db) // Person?
|
||||
@warn_unused_result
|
||||
public static func fetchOne(db: Database) -> Self? {
|
||||
return all().fetchOne(db)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension Row {
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ GRDB.swift is an SQLite toolkit for Swift 2.
|
||||
|
||||
It provides an SQL API and application tools.
|
||||
|
||||
**January 28, 2016: GRDB.swift 0.42.0 is out** ([changelog](CHANGELOG.md)). Follow [@groue](http://twitter.com/groue) on Twitter for release announcements and usage tips.
|
||||
**January 29, 2016: GRDB.swift 0.42.1 is out** ([changelog](CHANGELOG.md)). Follow [@groue](http://twitter.com/groue) on Twitter for release announcements and usage tips.
|
||||
|
||||
**Requirements**: iOS 7.0+ / OSX 10.9+, Xcode 7+
|
||||
|
||||
@@ -91,7 +91,7 @@ dbQueue.inDatabase { db in
|
||||
|
||||
### Documentation
|
||||
|
||||
- [GRDB Reference](http://cocoadocs.org/docsets/GRDB.swift/0.42.0/index.html) (on cocoadocs.org)
|
||||
- [GRDB Reference](http://cocoadocs.org/docsets/GRDB.swift/0.42.1/index.html) (on cocoadocs.org)
|
||||
- [Installation](#installation)
|
||||
- [SQLite API](#sqlite-api): SQL & SQLite
|
||||
- [Records](#records): Fetching and persistence methods for your custom structs and class hierarchies.
|
||||
@@ -118,7 +118,7 @@ To use GRDB.swift with Cocoapods, specify in your Podfile:
|
||||
source 'https://github.com/CocoaPods/Specs.git'
|
||||
use_frameworks!
|
||||
|
||||
pod 'GRDB.swift', '~> 0.42.0'
|
||||
pod 'GRDB.swift', '~> 0.42.1'
|
||||
```
|
||||
|
||||
|
||||
@@ -129,7 +129,7 @@ pod 'GRDB.swift', '~> 0.42.0'
|
||||
To use GRDB.swift with Carthage, specify in your Cartfile:
|
||||
|
||||
```
|
||||
github "groue/GRDB.swift" ~> 0.42.0
|
||||
github "groue/GRDB.swift" ~> 0.42.1
|
||||
```
|
||||
|
||||
|
||||
@@ -249,7 +249,7 @@ let dbQueue = try DatabaseQueue(
|
||||
configuration: config)
|
||||
```
|
||||
|
||||
See [Configuration](http://cocoadocs.org/docsets/GRDB.swift/0.42.0/Structs/Configuration.html) and [Concurrency](#concurrency) for more details.
|
||||
See [Configuration](http://cocoadocs.org/docsets/GRDB.swift/0.42.1/Structs/Configuration.html) and [Concurrency](#concurrency) for more details.
|
||||
|
||||
> :bowtie: **Tip**: see [DemoApps/GRDBDemoiOS/Database.swift](DemoApps/GRDBDemoiOS/GRDBDemoiOS/Database.swift) for a sample code that sets up a GRDB database.
|
||||
|
||||
@@ -1367,7 +1367,6 @@ let personWithEmailCount = dbQueue.inDatabase { db in
|
||||
}
|
||||
```
|
||||
|
||||
You can generally fetch the average, count, min, max and sum of any column. See the [query interface](#the-query-interface) for more details.
|
||||
|
||||
You can now jump to:
|
||||
|
||||
@@ -1748,7 +1747,7 @@ Please bear in mind that the query interface can not generate all possible SQL q
|
||||
- [SQL Operators](#sql-operators)
|
||||
- [SQL Functions](#sql-functions)
|
||||
- [Fetching from Requests](#fetching-from-requests)
|
||||
- [Counting with Requests](#counting-with-requests)
|
||||
- [Fetching Aggregated Values](#fetching-aggregated-values)
|
||||
|
||||
|
||||
### Requests
|
||||
@@ -2053,7 +2052,7 @@ dbQueue.inDatabase { db in
|
||||
For example:
|
||||
|
||||
```swift
|
||||
let persons = Person.all().fetchAll(db) // [Persons]
|
||||
let persons = Person.fetchAll(db) // [Persons]
|
||||
```
|
||||
|
||||
Both `fetch` and `fetchAll` let you iterate the full list of fetched objects. The differences are:
|
||||
@@ -2117,31 +2116,33 @@ Country.fetchAll(db, keys: ["FR", "US"])!
|
||||
```
|
||||
|
||||
|
||||
### Counting with Requests
|
||||
### Fetching Aggregated Values
|
||||
|
||||
**Requests can count:**
|
||||
|
||||
```swift
|
||||
dbQueue.inDatabase { db in
|
||||
// SELECT COUNT(*) FROM "persons"
|
||||
let count = Person.fetchCount(db) // Int
|
||||
|
||||
// SELECT COUNT(*) FROM "persons" ...
|
||||
request.fetchCount(db) // Int
|
||||
|
||||
// SELECT COUNT(name) FROM "persons" ...
|
||||
request.fetchCount(db, Col.name) // Int
|
||||
|
||||
// SELECT COUNT(DISTINCT name) FROM "persons" ...
|
||||
request.fetchCount(db, distinct: Col.name) // Int
|
||||
// SELECT COUNT(*) FROM "persons" WHERE "email" IS NOT NULL
|
||||
let count = Person.filter(Col.email != nil).fetchCount(db) // Int
|
||||
}
|
||||
```
|
||||
|
||||
You can also fetch other aggregated values (average, sum, min, max, etc.) For example:
|
||||
Other aggregated values can also be selected and fetched (see [SQL Functions](#sql-functions)):
|
||||
|
||||
```swift
|
||||
dbQueue.inDatabase { db in
|
||||
// SELECT MIN("age") FROM "persons"
|
||||
let request = Person.select(min(Col.age))
|
||||
let minAge = Int.fetchOne(db, request) // Int?
|
||||
|
||||
// SELECT MIN("height"), MAX("height") FROM "persons"
|
||||
let request = Person.select(min(Col.height), max(Col.height))
|
||||
let row = Row.fetchOne(db, request)!
|
||||
let minHeight = row.value(atIndex: 0) as Int?
|
||||
let maxHeight = row.value(atIndex: 1) as Int?
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
+1
-1
@@ -15,7 +15,7 @@
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>FMWK</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>0.42.0</string>
|
||||
<string>0.42.1</string>
|
||||
<key>CFBundleSignature</key>
|
||||
<string>????</string>
|
||||
<key>CFBundleVersion</key>
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
- [ ] How to have robust single writer/several readers?
|
||||
- [ ] protocol DatabaseEquatable, with default implementation for MutablePersistable
|
||||
- [ ] Query builder
|
||||
- [ ] Question the utility of request.fetchCount(db, Col.name) and request.fetchCount(db, distinct: Col.name)
|
||||
- [ ] Check for gross pollution (`1.SQLexpression`)
|
||||
- [ ] SELECT readers.*, books.* FROM ... JOIN ...
|
||||
- [ ] date functions
|
||||
|
||||
@@ -101,18 +101,6 @@ class FetchRequestTests: GRDBTestCase {
|
||||
|
||||
XCTAssertEqual(tableRequest.filter(Col.age == 42).fetchCount(db), 3)
|
||||
XCTAssertEqual(self.lastSQLQuery, "SELECT COUNT(*) FROM \"readers\" WHERE (\"age\" = 42)")
|
||||
|
||||
XCTAssertEqual(tableRequest.fetchCount(db, Col.age), 4)
|
||||
XCTAssertEqual(self.lastSQLQuery, "SELECT COUNT(\"age\") FROM \"readers\"")
|
||||
|
||||
XCTAssertEqual(tableRequest.fetchCount(db, Col.age ?? 0), 5)
|
||||
XCTAssertEqual(self.lastSQLQuery, "SELECT COUNT(IFNULL(\"age\", 0)) FROM \"readers\"")
|
||||
|
||||
XCTAssertEqual(tableRequest.fetchCount(db, distinct: Col.age), 2)
|
||||
XCTAssertEqual(self.lastSQLQuery, "SELECT COUNT(DISTINCT \"age\") FROM \"readers\"")
|
||||
|
||||
XCTAssertEqual(tableRequest.fetchCount(db, distinct: Col.age / Col.age), 1)
|
||||
XCTAssertEqual(self.lastSQLQuery, "SELECT COUNT(DISTINCT (\"age\" / \"age\")) FROM \"readers\"")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ class RowConvertibleFetchRequestTests: GRDBTestCase {
|
||||
|
||||
// MARK: - Fetch RowConvertible
|
||||
|
||||
func testFetch() {
|
||||
func testAll() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
var arthur = Reader(id: nil, name: "Arthur", age: 42)
|
||||
@@ -99,6 +99,43 @@ class RowConvertibleFetchRequestTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testFetch() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
var arthur = Reader(id: nil, name: "Arthur", age: 42)
|
||||
try arthur.insert(db)
|
||||
var barbara = Reader(id: nil, name: "Barbara", age: 36)
|
||||
try barbara.insert(db)
|
||||
|
||||
do {
|
||||
let readers = Reader.fetchAll(db)
|
||||
XCTAssertEqual(self.lastSQLQuery, "SELECT * FROM \"readers\"")
|
||||
XCTAssertEqual(readers.count, 2)
|
||||
XCTAssertEqual(readers[0].id!, arthur.id!)
|
||||
XCTAssertEqual(readers[0].name, arthur.name)
|
||||
XCTAssertEqual(readers[0].age, arthur.age)
|
||||
XCTAssertEqual(readers[1].id!, barbara.id!)
|
||||
XCTAssertEqual(readers[1].name, barbara.name)
|
||||
XCTAssertEqual(readers[1].age, barbara.age)
|
||||
}
|
||||
|
||||
do {
|
||||
let reader = Reader.fetchOne(db)!
|
||||
XCTAssertEqual(self.lastSQLQuery, "SELECT * FROM \"readers\"")
|
||||
XCTAssertEqual(reader.id!, arthur.id!)
|
||||
XCTAssertEqual(reader.name, arthur.name)
|
||||
XCTAssertEqual(reader.age, arthur.age)
|
||||
}
|
||||
|
||||
do {
|
||||
let names = Reader.fetch(db).map { $0.name }
|
||||
XCTAssertEqual(self.lastSQLQuery, "SELECT * FROM \"readers\"")
|
||||
XCTAssertEqual(names, [arthur.name, barbara.name])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testAlternativeFetch() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
|
||||
@@ -56,18 +56,6 @@ class TableMappingFetchRequestTests: GRDBTestCase {
|
||||
|
||||
XCTAssertEqual(Reader.filter(Col.age == 42).fetchCount(db), 3)
|
||||
XCTAssertEqual(self.lastSQLQuery, "SELECT COUNT(*) FROM \"readers\" WHERE (\"age\" = 42)")
|
||||
|
||||
XCTAssertEqual(Reader.fetchCount(db, Col.age), 4)
|
||||
XCTAssertEqual(self.lastSQLQuery, "SELECT COUNT(\"age\") FROM \"readers\"")
|
||||
|
||||
XCTAssertEqual(Reader.fetchCount(db, Col.age ?? 0), 5)
|
||||
XCTAssertEqual(self.lastSQLQuery, "SELECT COUNT(IFNULL(\"age\", 0)) FROM \"readers\"")
|
||||
|
||||
XCTAssertEqual(Reader.fetchCount(db, distinct: Col.age), 2)
|
||||
XCTAssertEqual(self.lastSQLQuery, "SELECT COUNT(DISTINCT \"age\") FROM \"readers\"")
|
||||
|
||||
XCTAssertEqual(Reader.fetchCount(db, distinct: Col.age / Col.age), 1)
|
||||
XCTAssertEqual(self.lastSQLQuery, "SELECT COUNT(DISTINCT (\"age\" / \"age\")) FROM \"readers\"")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -172,6 +160,15 @@ class TableMappingFetchRequestTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Reverse
|
||||
|
||||
func testReverse() {
|
||||
XCTAssertEqual(
|
||||
sql(Reader.reverse()),
|
||||
"SELECT * FROM \"readers\" ORDER BY \"id\" DESC")
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Limit
|
||||
|
||||
func testLimit() {
|
||||
|
||||
Reference in new issue
Block a user