mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +00:00
Merge branch 'master' of https://github.com/groue/GRDB.swift into FetchedRecordsController
This commit is contained in:
commit
67f00e70a3
11 files changed
+70
-106
No files matched your search
+3
-3
@@ -19,18 +19,18 @@ DatabasePersistable:
|
||||
|
||||
Row:
|
||||
|
||||
- `Row.value(named:)` returns nil if no such column exists in the row. It used to crash with a fatal error ([documentation](https://github.com/groue/GRDB.swift#column-values)).
|
||||
- `Row.value(named:)` and `Row.dataNoCopy(named:)` returns nil if no such column exists in the row. It used to crash with a fatal error ([documentation](https://github.com/groue/GRDB.swift#column-values)).
|
||||
|
||||
DatabaseValue:
|
||||
|
||||
- `DatabaseValue` has no public initializers. To create one, use `DatabaseValue.Null`, or the fact that Int, String, etc. adopt the protocol: `1.databaseValue`, `"foo".databaseValue` ([documentation](https://github.com/groue/GRDB.swift#custom-value-types)).
|
||||
- `DatabaseValue` has no public initializers. To create one, use `DatabaseValue.Null`, or the fact that Int, String, etc. adopt the DatabaseValueConvertible protocol: `1.databaseValue`, `"foo".databaseValue` ([documentation](https://github.com/groue/GRDB.swift#custom-value-types)).
|
||||
|
||||
Record ([documentation](https://github.com/groue/GRDB.swift#record)):
|
||||
|
||||
- `Record.reload()` has been removed. You have to provide your own implementation, should you need reloading.
|
||||
- `Record.init(row: Row)` has been renamed `Record.init(_ row: Row)` (unlabelled row argument).
|
||||
- `Record.updateFromRow()` has been removed. Override `init(_ row: Row)` instead.
|
||||
- `Record.didInsertWithRowID(_:forColumn:)` should be overriden by Record subclasses that are interested with their row ids.
|
||||
- `Record.didInsertWithRowID(_:forColumn:)` should be overriden by Record subclasses that are interested in their row ids.
|
||||
- `Record.databaseEdited` has been renamed `hasPersistentChangedValues`.
|
||||
- `Record.databaseChanges` has been renamed `persistentChangedValues` and now returns `[String: DatabaseValue?]`, the dictionary of old values for changed columns.
|
||||
|
||||
|
||||
@@ -23,67 +23,59 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
||||
}
|
||||
|
||||
func testValueAtIndexPerformance() {
|
||||
for _ in 0..<10 {
|
||||
dbQueue.inDatabase { db in
|
||||
for row in Row.fetch(db, "SELECT * FROM items") {
|
||||
let _: Int = row.value(atIndex: 0)
|
||||
let _: Int = row.value(atIndex: 1)
|
||||
let _: Int = row.value(atIndex: 2)
|
||||
let _: Int = row.value(atIndex: 3)
|
||||
let _: Int = row.value(atIndex: 4)
|
||||
let _: Int = row.value(atIndex: 5)
|
||||
let _: Int = row.value(atIndex: 6)
|
||||
let _: Int = row.value(atIndex: 7)
|
||||
let _: Int = row.value(atIndex: 8)
|
||||
let _: Int = row.value(atIndex: 9)
|
||||
}
|
||||
dbQueue.inDatabase { db in
|
||||
for row in Row.fetch(db, "SELECT * FROM items") {
|
||||
let _: Int = row.value(atIndex: 0)
|
||||
let _: Int = row.value(atIndex: 1)
|
||||
let _: Int = row.value(atIndex: 2)
|
||||
let _: Int = row.value(atIndex: 3)
|
||||
let _: Int = row.value(atIndex: 4)
|
||||
let _: Int = row.value(atIndex: 5)
|
||||
let _: Int = row.value(atIndex: 6)
|
||||
let _: Int = row.value(atIndex: 7)
|
||||
let _: Int = row.value(atIndex: 8)
|
||||
let _: Int = row.value(atIndex: 9)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testValueNamedPerformance() {
|
||||
for _ in 0..<10 {
|
||||
dbQueue.inDatabase { db in
|
||||
for row in Row.fetch(db, "SELECT * FROM items") {
|
||||
let _: Int = row.value(named: "i0")
|
||||
let _: Int = row.value(named: "i1")
|
||||
let _: Int = row.value(named: "i2")
|
||||
let _: Int = row.value(named: "i3")
|
||||
let _: Int = row.value(named: "i4")
|
||||
let _: Int = row.value(named: "i5")
|
||||
let _: Int = row.value(named: "i6")
|
||||
let _: Int = row.value(named: "i7")
|
||||
let _: Int = row.value(named: "i8")
|
||||
let _: Int = row.value(named: "i9")
|
||||
}
|
||||
dbQueue.inDatabase { db in
|
||||
for row in Row.fetch(db, "SELECT * FROM items") {
|
||||
let _: Int = row.value(named: "i0")
|
||||
let _: Int = row.value(named: "i1")
|
||||
let _: Int = row.value(named: "i2")
|
||||
let _: Int = row.value(named: "i3")
|
||||
let _: Int = row.value(named: "i4")
|
||||
let _: Int = row.value(named: "i5")
|
||||
let _: Int = row.value(named: "i6")
|
||||
let _: Int = row.value(named: "i7")
|
||||
let _: Int = row.value(named: "i8")
|
||||
let _: Int = row.value(named: "i9")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testRecordPerformance() {
|
||||
for _ in 0..<10 {
|
||||
let records = dbQueue.inDatabase { db in
|
||||
PerformanceRecord.fetchAll(db, "SELECT * FROM items")
|
||||
}
|
||||
precondition(records[4].i2 == 1)
|
||||
precondition(records[4].i3 == 0)
|
||||
precondition(records[5].i2 == 2)
|
||||
precondition(records[5].i3 == 1)
|
||||
let records = dbQueue.inDatabase { db in
|
||||
PerformanceRecord.fetchAll(db, "SELECT * FROM items")
|
||||
}
|
||||
assert(records[4].i2 == 1)
|
||||
assert(records[4].i3 == 0)
|
||||
assert(records[5].i2 == 2)
|
||||
assert(records[5].i3 == 1)
|
||||
}
|
||||
|
||||
func testKeyValueCodingPerformance() {
|
||||
for _ in 0..<10 {
|
||||
let records = dbQueue.inDatabase { db in
|
||||
Row.fetch(db, "SELECT * FROM items").map { row in
|
||||
PerformanceObjCRecord(dictionary: row.toNSDictionary())
|
||||
}
|
||||
let records = dbQueue.inDatabase { db in
|
||||
Row.fetch(db, "SELECT * FROM items").map { row in
|
||||
PerformanceObjCRecord(dictionary: row.toNSDictionary())
|
||||
}
|
||||
precondition(records[4].i2!.intValue == 1)
|
||||
precondition(records[4].i3!.intValue == 0)
|
||||
precondition(records[5].i2!.intValue == 2)
|
||||
precondition(records[5].i3!.intValue == 1)
|
||||
}
|
||||
assert(records[4].i2!.intValue == 1)
|
||||
assert(records[4].i3!.intValue == 0)
|
||||
assert(records[5].i2!.intValue == 2)
|
||||
assert(records[5].i3!.intValue == 1)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
buildImplicitDependencies = "YES">
|
||||
</BuildAction>
|
||||
<TestAction
|
||||
buildConfiguration = "Debug"
|
||||
buildConfiguration = "Release"
|
||||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
|
||||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
|
||||
shouldUseLaunchSchemeArgsEnv = "YES">
|
||||
|
||||
@@ -78,7 +78,7 @@ public final class Database {
|
||||
/// - returns: A DatabaseChanges. Note that insertedRowID will always be nil.
|
||||
/// - throws: A DatabaseError whenever a SQLite error occurs.
|
||||
public func executeMultiStatement(sql: String) throws -> DatabaseChanges {
|
||||
assertValidQueue()
|
||||
preconditionValidQueue()
|
||||
|
||||
let changedRowsBefore = sqlite3_total_changes(sqliteConnection)
|
||||
|
||||
@@ -117,7 +117,7 @@ public final class Database {
|
||||
/// either .Commit or .Rollback.
|
||||
/// - throws: The error thrown by the block.
|
||||
public func inTransaction(kind: TransactionKind? = nil, block: () throws -> TransactionCompletion) throws {
|
||||
assertValidQueue()
|
||||
preconditionValidQueue()
|
||||
|
||||
var completion: TransactionCompletion = .Rollback
|
||||
var blockError: ErrorType? = nil
|
||||
@@ -485,7 +485,7 @@ public final class Database {
|
||||
/// You may need to clear the cache if you modify the database schema
|
||||
/// outside of a migration (see DatabaseMigrator).
|
||||
public func clearSchemaCache() {
|
||||
assertValidQueue()
|
||||
preconditionValidQueue()
|
||||
columnInfosCache = [:]
|
||||
}
|
||||
|
||||
@@ -641,7 +641,7 @@ public final class Database {
|
||||
// =========================================================================
|
||||
// MARK: - Initialization
|
||||
|
||||
/// The queue from which the database can be used. See assertValidQueue().
|
||||
/// The queue from which the database can be used. See preconditionValidQueue().
|
||||
/// Design note: this is not very clean. A delegation pattern may be a
|
||||
/// better fit.
|
||||
var databaseQueueID: DatabaseQueueID = nil
|
||||
@@ -676,10 +676,8 @@ public final class Database {
|
||||
// =========================================================================
|
||||
// MARK: - Misc
|
||||
|
||||
func assertValidQueue() {
|
||||
guard databaseQueueID == nil || databaseQueueID == dispatch_get_specific(DatabaseQueue.databaseQueueIDKey) else {
|
||||
fatalError("Database was not used on the correct thread: execute your statements inside DatabaseQueue.inDatabase() or DatabaseQueue.inTransaction(). If you get this error while iterating the result of a fetch() method, use fetchAll() instead: it returns an Array that can be iterated on any thread.")
|
||||
}
|
||||
func preconditionValidQueue() {
|
||||
precondition(databaseQueueID == nil || databaseQueueID == dispatch_get_specific(DatabaseQueue.databaseQueueIDKey), "Database was not used on the correct thread: execute your statements inside DatabaseQueue.inDatabase() or DatabaseQueue.inTransaction(). If you get this error while iterating the result of a fetch() method, use fetchAll() instead: it returns an Array that can be iterated on any thread.")
|
||||
}
|
||||
|
||||
func setupForeignKeys() throws {
|
||||
|
||||
@@ -197,9 +197,7 @@ public final class DatabaseQueue {
|
||||
//
|
||||
// I try not to ship half-baked solutions, so until a complete solution
|
||||
// is found to this problem, I prefer totally disabling reentrancy.
|
||||
guard databaseQueueID != dispatch_get_specific(DatabaseQueue.databaseQueueIDKey) else {
|
||||
fatalError("DatabaseQueue.inDatabase(_:) or DatabaseQueue.inTransaction(_:) was called reentrantly, which would lead to a deadlock.")
|
||||
}
|
||||
precondition(databaseQueueID != dispatch_get_specific(DatabaseQueue.databaseQueueIDKey), "DatabaseQueue.inDatabase(_:) or DatabaseQueue.inTransaction(_:) was called reentrantly, which would lead to a deadlock.")
|
||||
return try DatabaseQueue.dispatchSync(queue, block: block)
|
||||
}
|
||||
|
||||
|
||||
@@ -515,9 +515,7 @@ final class DataMapper {
|
||||
|
||||
// Fail early if persistentDictionary is empty
|
||||
let persistentDictionary = persistable.persistentDictionary
|
||||
guard persistentDictionary.count > 0 else {
|
||||
fatalError("Invalid empty dictionary returned from \(persistable.dynamicType).persistentDictionary")
|
||||
}
|
||||
precondition(persistentDictionary.count > 0, "Invalid empty dictionary returned from \(persistable.dynamicType).persistentDictionary")
|
||||
|
||||
self.db = db
|
||||
self.persistable = persistable
|
||||
|
||||
@@ -76,9 +76,7 @@ extension RowConvertible where Self: DatabaseTableMapping {
|
||||
|
||||
// Fail early if database table has not one column in its primary key
|
||||
let columns = primaryKey.columns
|
||||
guard columns.count == 1 else {
|
||||
fatalError("Primary key of table \(databaseTableName.quotedDatabaseIdentifier) is not made of a single column. See \(self).databaseTableName()")
|
||||
}
|
||||
precondition(columns.count == 1, "Primary key of table \(databaseTableName.quotedDatabaseIdentifier) is not made of a single column. See \(self).databaseTableName()")
|
||||
|
||||
let keys = keys.map { $0 as DatabaseValueConvertible? }
|
||||
|
||||
@@ -160,10 +158,7 @@ extension RowConvertible where Self: DatabaseTableMapping {
|
||||
var arguments: [DatabaseValueConvertible?] = []
|
||||
var whereClauses: [String] = []
|
||||
for dictionary in keys {
|
||||
guard dictionary.count > 0 else {
|
||||
fatalError("Invalid empty key")
|
||||
}
|
||||
|
||||
precondition(dictionary.count > 0, "Invalid empty key dictionary")
|
||||
arguments.appendContentsOf(dictionary.values)
|
||||
whereClauses.append("(" + dictionary.keys.map { "\($0.quotedDatabaseIdentifier) = ?" }.joinWithSeparator(" AND ") + ")")
|
||||
}
|
||||
|
||||
+12
-25
@@ -65,9 +65,7 @@ public final class Row: CollectionType {
|
||||
/// - parameter index: A column index.
|
||||
/// - returns: An Int64, Double, String, NSData or nil.
|
||||
public func value(atIndex index: Int) -> DatabaseValueConvertible? {
|
||||
guard index >= 0 && index < count else {
|
||||
fatalError("Row index out of range")
|
||||
}
|
||||
precondition(index >= 0 && index < count, "Row index out of range")
|
||||
return impl
|
||||
.databaseValue(atIndex: index)
|
||||
.value()
|
||||
@@ -93,9 +91,7 @@ public final class Row: CollectionType {
|
||||
/// - parameter index: A column index.
|
||||
/// - returns: An optional *Value*.
|
||||
public func value<Value: DatabaseValueConvertible>(atIndex index: Int) -> Value? {
|
||||
guard index >= 0 && index < count else {
|
||||
fatalError("Row index out of range")
|
||||
}
|
||||
precondition(index >= 0 && index < count, "Row index out of range")
|
||||
return impl
|
||||
.databaseValue(atIndex: index)
|
||||
.value()
|
||||
@@ -126,9 +122,7 @@ public final class Row: CollectionType {
|
||||
/// - parameter index: A column index.
|
||||
/// - returns: An optional *Value*.
|
||||
public func value<Value: protocol<DatabaseValueConvertible, SQLiteStatementConvertible>>(atIndex index: Int) -> Value? {
|
||||
guard index >= 0 && index < count else {
|
||||
fatalError("Row index out of range")
|
||||
}
|
||||
precondition(index >= 0 && index < count, "Row index out of range")
|
||||
let sqliteStatement = self.sqliteStatement
|
||||
if sqliteStatement != nil {
|
||||
// Metal row
|
||||
@@ -163,9 +157,7 @@ public final class Row: CollectionType {
|
||||
/// - parameter index: A column index.
|
||||
/// - returns: A *Value*.
|
||||
public func value<Value: DatabaseValueConvertible>(atIndex index: Int) -> Value {
|
||||
guard index >= 0 && index < count else {
|
||||
fatalError("Row index out of range")
|
||||
}
|
||||
precondition(index >= 0 && index < count, "Row index out of range")
|
||||
return impl
|
||||
.databaseValue(atIndex: index)
|
||||
.value()
|
||||
@@ -196,9 +188,7 @@ public final class Row: CollectionType {
|
||||
/// - parameter index: A column index.
|
||||
/// - returns: A *Value*.
|
||||
public func value<Value: protocol<DatabaseValueConvertible, SQLiteStatementConvertible>>(atIndex index: Int) -> Value {
|
||||
guard index >= 0 && index < count else {
|
||||
fatalError("Row index out of range")
|
||||
}
|
||||
precondition(index >= 0 && index < count, "Row index out of range")
|
||||
let sqliteStatement = self.sqliteStatement
|
||||
if sqliteStatement != nil {
|
||||
// Metal row
|
||||
@@ -360,17 +350,16 @@ public final class Row: CollectionType {
|
||||
///
|
||||
/// - parameter index: A column index.
|
||||
/// - returns: An optional NSData.
|
||||
public func dataNoCopy(atIndex index:Int) -> NSData? {
|
||||
public func dataNoCopy(atIndex index: Int) -> NSData? {
|
||||
precondition(index >= 0 && index < count, "Row index out of range")
|
||||
return impl.dataNoCopy(atIndex: index)
|
||||
}
|
||||
|
||||
/// Returns the optional `NSData` at given column.
|
||||
///
|
||||
/// Column name is case-insensitive. If the row does not contain the column,
|
||||
/// a fatal error is raised.
|
||||
///
|
||||
/// The result is nil if the fetched SQLite value is NULL, or if the SQLite
|
||||
/// value is not a blob.
|
||||
/// Column name is case-insensitive. The result is nil if the row does not
|
||||
/// contain the column, or if the fetched SQLite value is NULL, or if the
|
||||
/// SQLite value can not be converted to NSData.
|
||||
///
|
||||
/// Otherwise, the returned data does not owns its bytes: it must not be
|
||||
/// used longer than the row's lifetime.
|
||||
@@ -379,7 +368,7 @@ public final class Row: CollectionType {
|
||||
/// - returns: An optional NSData.
|
||||
public func dataNoCopy(named columnName: String) -> NSData? {
|
||||
guard let index = impl.indexForColumn(named: columnName) else {
|
||||
fatalError("No such column: \(String(reflecting: columnName))")
|
||||
return nil
|
||||
}
|
||||
return dataNoCopy(atIndex: index)
|
||||
}
|
||||
@@ -410,9 +399,7 @@ public final class 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
|
||||
}
|
||||
public lazy var count: Int = { self.impl.count }()
|
||||
|
||||
/// The names of columns in the row.
|
||||
///
|
||||
|
||||
@@ -34,7 +34,7 @@ public final class SelectStatement : Statement {
|
||||
|
||||
/// The column index, case insensitive.
|
||||
func indexForColumn(named name: String) -> Int? {
|
||||
return lowercaseColumnIndexes[name.lowercaseString]
|
||||
return lowercaseColumnIndexes[name] ?? lowercaseColumnIndexes[name.lowercaseString]
|
||||
}
|
||||
|
||||
/// Support for indexForColumn(named:)
|
||||
@@ -57,18 +57,18 @@ public struct DatabaseSequence<T>: SequenceType {
|
||||
|
||||
private init(statement: SelectStatement, yield: () -> T) {
|
||||
generateImpl = {
|
||||
let assertValidQueue = statement.database.assertValidQueue
|
||||
let preconditionValidQueue = statement.database.preconditionValidQueue
|
||||
let sqliteStatement = statement.sqliteStatement
|
||||
|
||||
// Check that sequence is built on a valid queue.
|
||||
assertValidQueue()
|
||||
preconditionValidQueue()
|
||||
|
||||
// DatabaseSequence can be restarted:
|
||||
statement.reset()
|
||||
|
||||
return DatabaseGenerator {
|
||||
// Check that generator is used on a valid queue.
|
||||
assertValidQueue()
|
||||
preconditionValidQueue()
|
||||
|
||||
let code = sqlite3_step(sqliteStatement)
|
||||
switch code {
|
||||
|
||||
@@ -31,7 +31,7 @@ public class Statement {
|
||||
let database: Database
|
||||
|
||||
init(database: Database, sql: String) throws {
|
||||
database.assertValidQueue()
|
||||
database.preconditionValidQueue()
|
||||
|
||||
// See https://www.sqlite.org/c3ref/prepare.html
|
||||
|
||||
@@ -149,9 +149,7 @@ public class Statement {
|
||||
final func setArgument(value: DatabaseValueConvertible?, forKey key: String) {
|
||||
let argumentName = ":\(key)"
|
||||
let index = Int(sqlite3_bind_parameter_index(sqliteStatement, argumentName))
|
||||
guard index > 0 else {
|
||||
fatalError("Argument not found in SQLite statement: `\(argumentName)`")
|
||||
}
|
||||
precondition(index > 0, "Argument not found in SQLite statement: `\(argumentName)`")
|
||||
setArgument(value, atIndex: index)
|
||||
}
|
||||
|
||||
|
||||
@@ -92,9 +92,7 @@ public struct DatabaseMigrator {
|
||||
private var migrations: [Migration] = []
|
||||
|
||||
private mutating func registerMigration(migration: Migration) {
|
||||
guard migrations.map({ $0.identifier }).indexOf(migration.identifier) == nil else {
|
||||
fatalError("Already registered migration: \"\(migration.identifier)\"")
|
||||
}
|
||||
precondition(migrations.map({ $0.identifier }).indexOf(migration.identifier) == nil, "Already registered migration: \"\(migration.identifier)\"")
|
||||
migrations.append(migration)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user