mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-29 04:24:21 +00:00
Use precondition() when validating user input
This commit is contained in:
1 parent
2bd9bdbf27
commit
f8ad2f2f6a
8 files changed
+21
-46
No files matched your search
@@ -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
|
||||
@@ -458,7 +458,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 = [:]
|
||||
}
|
||||
|
||||
@@ -614,7 +614,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
|
||||
@@ -649,10 +649,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 ") + ")")
|
||||
}
|
||||
|
||||
+5
-15
@@ -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
|
||||
|
||||
@@ -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