mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
Statement.arguments is a non-optional StatementArguments. Remove StatementArguments.Default.
This commit is contained in:
1 parent
32bb03f0eb
commit
0345d482b2
11 files changed
+107
-148
No files matched your search
@@ -1,6 +1,13 @@
|
||||
Release Notes
|
||||
=============
|
||||
|
||||
## Next Release
|
||||
|
||||
**Breaking Changes**
|
||||
|
||||
- `Statement.arguments` in now a non-optional `StatementArguments`.
|
||||
|
||||
|
||||
## 0.37.1
|
||||
|
||||
Released January 7, 2016
|
||||
|
||||
@@ -45,7 +45,7 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
|
||||
do {
|
||||
// Missing arguments
|
||||
try statement.validateArguments(nil)
|
||||
try statement.validateArguments([])
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
print(error)
|
||||
@@ -124,7 +124,7 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
|
||||
do {
|
||||
// Missing arguments
|
||||
try statement.validateArguments(nil)
|
||||
try statement.validateArguments([])
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
print(error)
|
||||
@@ -211,7 +211,7 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
|
||||
do {
|
||||
// Missing arguments
|
||||
try statement.validateArguments(nil)
|
||||
try statement.validateArguments([])
|
||||
XCTFail("Expected error")
|
||||
} catch let error as DatabaseError {
|
||||
print(error)
|
||||
|
||||
@@ -65,12 +65,19 @@ public final class Database {
|
||||
/// This method may throw a DatabaseError.
|
||||
///
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - returns: A DatabaseChanges.
|
||||
/// - throws: A DatabaseError whenever a SQLite error occurs.
|
||||
public func execute(sql: String, arguments: StatementArguments = StatementArguments.Default) throws -> DatabaseChanges {
|
||||
public func execute(sql: String, arguments: StatementArguments? = nil) throws -> DatabaseChanges {
|
||||
preconditionValidQueue()
|
||||
|
||||
let usedArguments: StatementArguments
|
||||
if let arguments = arguments {
|
||||
usedArguments = arguments
|
||||
} else {
|
||||
usedArguments = []
|
||||
}
|
||||
|
||||
// The tricky part is to consume arguments as statements are executed.
|
||||
//
|
||||
// Here we return two functions:
|
||||
@@ -78,7 +85,7 @@ public final class Database {
|
||||
// - one that validates the remaining arguments, in the same way as
|
||||
// Statement.validateArguments()
|
||||
let (consumeArguments, validateRemainingArguments) = { () -> (UpdateStatement -> StatementArguments, () throws -> ()) in
|
||||
switch arguments.kind {
|
||||
switch usedArguments.kind {
|
||||
case .Array(let values):
|
||||
// Extract as many values as needed, statement after statement:
|
||||
var remainingValues = values
|
||||
@@ -100,9 +107,9 @@ public final class Database {
|
||||
}
|
||||
}
|
||||
return (consumeArguments, validateRemainingArguments)
|
||||
case .Dictionary, .Default:
|
||||
case .Dictionary:
|
||||
// Reuse the dictionary argument for all statements:
|
||||
let consumeArguments = { (_: UpdateStatement) -> StatementArguments in return arguments }
|
||||
let consumeArguments = { (_: UpdateStatement) -> StatementArguments in return usedArguments }
|
||||
let validateRemainingArguments = { () in }
|
||||
return (consumeArguments, validateRemainingArguments)
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ extension DatabaseError: CustomStringConvertible {
|
||||
if let sql = sql {
|
||||
description += " with statement `\(sql)`"
|
||||
}
|
||||
if let arguments = arguments {
|
||||
if let arguments = arguments where !arguments.isEmpty {
|
||||
description += " arguments \(arguments)"
|
||||
}
|
||||
if let message = message {
|
||||
|
||||
@@ -65,9 +65,9 @@ public extension DatabaseValueConvertible {
|
||||
/// remaining elements are undefined.
|
||||
///
|
||||
/// - parameter statement: The statement to run.
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - returns: A sequence.
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments = StatementArguments.Default) -> DatabaseSequence<Self> {
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> DatabaseSequence<Self> {
|
||||
let sqliteStatement = statement.sqliteStatement
|
||||
return statement.fetch(arguments: arguments) {
|
||||
DatabaseValue(sqliteStatement: sqliteStatement, index: 0).value()
|
||||
@@ -80,9 +80,9 @@ public extension DatabaseValueConvertible {
|
||||
/// let names = String.fetchAll(statement) // [String]
|
||||
///
|
||||
/// - parameter statement: The statement to run.
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - returns: An array.
|
||||
public static func fetchAll(statement: SelectStatement, arguments: StatementArguments = StatementArguments.Default) -> [Self] {
|
||||
public static func fetchAll(statement: SelectStatement, arguments: StatementArguments? = nil) -> [Self] {
|
||||
return Array(fetch(statement, arguments: arguments))
|
||||
}
|
||||
|
||||
@@ -95,9 +95,9 @@ public extension DatabaseValueConvertible {
|
||||
/// let name = String.fetchOne(statement) // String?
|
||||
///
|
||||
/// - parameter statement: The statement to run.
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - returns: An optional value.
|
||||
public static func fetchOne(statement: SelectStatement, arguments: StatementArguments = StatementArguments.Default) -> Self? {
|
||||
public static func fetchOne(statement: SelectStatement, arguments: StatementArguments? = nil) -> Self? {
|
||||
let sequence: DatabaseSequence<Self?> = statement.fetch(arguments: arguments) {
|
||||
fromDatabaseValue(DatabaseValue(sqliteStatement: statement.sqliteStatement, index: 0))
|
||||
}
|
||||
@@ -128,9 +128,9 @@ public extension DatabaseValueConvertible {
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - returns: A sequence.
|
||||
public static func fetch(db: Database, _ sql: String, arguments: StatementArguments = StatementArguments.Default) -> DatabaseSequence<Self> {
|
||||
public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> DatabaseSequence<Self> {
|
||||
return fetch(try! db.selectStatement(sql), arguments: arguments)
|
||||
}
|
||||
|
||||
@@ -140,9 +140,9 @@ public extension DatabaseValueConvertible {
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - returns: An array.
|
||||
public static func fetchAll(db: Database, _ sql: String, arguments: StatementArguments = StatementArguments.Default) -> [Self] {
|
||||
public static func fetchAll(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> [Self] {
|
||||
return fetchAll(try! db.selectStatement(sql), arguments: arguments)
|
||||
}
|
||||
|
||||
@@ -155,9 +155,9 @@ public extension DatabaseValueConvertible {
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - returns: An optional value.
|
||||
public static func fetchOne(db: Database, _ sql: String, arguments: StatementArguments = StatementArguments.Default) -> Self? {
|
||||
public static func fetchOne(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> Self? {
|
||||
return fetchOne(try! db.selectStatement(sql), arguments: arguments)
|
||||
}
|
||||
}
|
||||
@@ -198,9 +198,9 @@ public extension Optional where Wrapped: DatabaseValueConvertible {
|
||||
/// remaining elements are undefined.
|
||||
///
|
||||
/// - parameter statement: The statement to run.
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - returns: A sequence of optional values.
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments = StatementArguments.Default) -> DatabaseSequence<Wrapped?> {
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> DatabaseSequence<Wrapped?> {
|
||||
let sqliteStatement = statement.sqliteStatement
|
||||
return statement.fetch(arguments: arguments) {
|
||||
Wrapped.fromDatabaseValue(DatabaseValue(sqliteStatement: sqliteStatement, index: 0))
|
||||
@@ -213,9 +213,9 @@ public extension Optional where Wrapped: DatabaseValueConvertible {
|
||||
/// let names = Optional<String>.fetchAll(statement) // [String?]
|
||||
///
|
||||
/// - parameter statement: The statement to run.
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - returns: An array of optional values.
|
||||
public static func fetchAll(statement: SelectStatement, arguments: StatementArguments = StatementArguments.Default) -> [Wrapped?] {
|
||||
public static func fetchAll(statement: SelectStatement, arguments: StatementArguments? = nil) -> [Wrapped?] {
|
||||
return Array(fetch(statement, arguments: arguments))
|
||||
}
|
||||
|
||||
@@ -240,9 +240,9 @@ public extension Optional where Wrapped: DatabaseValueConvertible {
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - returns: A sequence of optional values.
|
||||
public static func fetch(db: Database, _ sql: String, arguments: StatementArguments = StatementArguments.Default) -> DatabaseSequence<Wrapped?> {
|
||||
public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> DatabaseSequence<Wrapped?> {
|
||||
return fetch(try! db.selectStatement(sql), arguments: arguments)
|
||||
}
|
||||
|
||||
@@ -252,9 +252,9 @@ public extension Optional where Wrapped: DatabaseValueConvertible {
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - returns: An array of optional values.
|
||||
public static func fetchAll(db: Database, _ sql: String, arguments: StatementArguments = StatementArguments.Default) -> [Wrapped?] {
|
||||
public static func fetchAll(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> [Wrapped?] {
|
||||
return fetchAll(try! db.selectStatement(sql), arguments: arguments)
|
||||
}
|
||||
}
|
||||
@@ -62,9 +62,9 @@ extension RowConvertible {
|
||||
/// remaining elements are undefined.
|
||||
///
|
||||
/// - parameter statement: The statement to run.
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - returns: A sequence.
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments = StatementArguments.Default) -> DatabaseSequence<Self> {
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> DatabaseSequence<Self> {
|
||||
// Metal rows can be reused. And reusing them yields better performance.
|
||||
let row = Row(metalStatement: statement)
|
||||
return statement.fetch(arguments: arguments) {
|
||||
@@ -80,9 +80,9 @@ extension RowConvertible {
|
||||
/// let persons = Person.fetchAll(statement) // [Person]
|
||||
///
|
||||
/// - parameter statement: The statement to run.
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - returns: An array.
|
||||
public static func fetchAll(statement: SelectStatement, arguments: StatementArguments = StatementArguments.Default) -> [Self] {
|
||||
public static func fetchAll(statement: SelectStatement, arguments: StatementArguments? = nil) -> [Self] {
|
||||
return Array(fetch(statement, arguments: arguments))
|
||||
}
|
||||
|
||||
@@ -92,9 +92,9 @@ extension RowConvertible {
|
||||
/// let persons = Person.fetchOne(statement) // Person?
|
||||
///
|
||||
/// - parameter statement: The statement to run.
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - returns: An optional value.
|
||||
public static func fetchOne(statement: SelectStatement, arguments: StatementArguments = StatementArguments.Default) -> Self? {
|
||||
public static func fetchOne(statement: SelectStatement, arguments: StatementArguments? = nil) -> Self? {
|
||||
return fetch(statement, arguments: arguments).generate().next()
|
||||
}
|
||||
|
||||
@@ -119,9 +119,9 @@ extension RowConvertible {
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - returns: A sequence.
|
||||
public static func fetch(db: Database, _ sql: String, arguments: StatementArguments = StatementArguments.Default) -> DatabaseSequence<Self> {
|
||||
public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> DatabaseSequence<Self> {
|
||||
return fetch(try! db.selectStatement(sql), arguments: arguments)
|
||||
}
|
||||
|
||||
@@ -131,9 +131,9 @@ extension RowConvertible {
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - returns: An array.
|
||||
public static func fetchAll(db: Database, _ sql: String, arguments: StatementArguments = StatementArguments.Default) -> [Self] {
|
||||
public static func fetchAll(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> [Self] {
|
||||
return fetchAll(try! db.selectStatement(sql), arguments: arguments)
|
||||
}
|
||||
|
||||
@@ -143,9 +143,9 @@ extension RowConvertible {
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - returns: An optional value.
|
||||
public static func fetchOne(db: Database, _ sql: String, arguments: StatementArguments = StatementArguments.Default) -> Self? {
|
||||
public static func fetchOne(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> Self? {
|
||||
return fetchOne(try! db.selectStatement(sql), arguments: arguments)
|
||||
}
|
||||
}
|
||||
@@ -52,9 +52,9 @@ public extension DatabaseValueConvertible where Self: SQLiteStatementConvertible
|
||||
/// remaining elements are undefined.
|
||||
///
|
||||
/// - parameter statement: The statement to run.
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - returns: A sequence of values.
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments = StatementArguments.Default) -> DatabaseSequence<Self> {
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> DatabaseSequence<Self> {
|
||||
let sqliteStatement = statement.sqliteStatement
|
||||
return statement.fetch(arguments: arguments) {
|
||||
if sqlite3_column_type(sqliteStatement, 0) == SQLITE_NULL {
|
||||
@@ -71,9 +71,9 @@ public extension DatabaseValueConvertible where Self: SQLiteStatementConvertible
|
||||
/// let names = String.fetchAll(statement) // [String]
|
||||
///
|
||||
/// - parameter statement: The statement to run.
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - returns: An array of values.
|
||||
public static func fetchAll(statement: SelectStatement, arguments: StatementArguments = StatementArguments.Default) -> [Self] {
|
||||
public static func fetchAll(statement: SelectStatement, arguments: StatementArguments? = nil) -> [Self] {
|
||||
return Array(fetch(statement, arguments: arguments))
|
||||
}
|
||||
|
||||
@@ -83,9 +83,9 @@ public extension DatabaseValueConvertible where Self: SQLiteStatementConvertible
|
||||
/// let name = String.fetchOne(statement) // String?
|
||||
///
|
||||
/// - parameter statement: The statement to run.
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - returns: An optional value.
|
||||
public static func fetchOne(statement: SelectStatement, arguments: StatementArguments = StatementArguments.Default) -> Self? {
|
||||
public static func fetchOne(statement: SelectStatement, arguments: StatementArguments? = nil) -> Self? {
|
||||
let generator = statement.fetch(arguments: arguments, yield: { }).generate()
|
||||
guard generator.next() != nil else {
|
||||
return nil
|
||||
@@ -118,9 +118,9 @@ public extension DatabaseValueConvertible where Self: SQLiteStatementConvertible
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - returns: A sequence of values.
|
||||
public static func fetch(db: Database, _ sql: String, arguments: StatementArguments = StatementArguments.Default) -> DatabaseSequence<Self> {
|
||||
public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> DatabaseSequence<Self> {
|
||||
return fetch(try! db.selectStatement(sql), arguments: arguments)
|
||||
}
|
||||
|
||||
@@ -130,9 +130,9 @@ public extension DatabaseValueConvertible where Self: SQLiteStatementConvertible
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - returns: An array of values.
|
||||
public static func fetchAll(db: Database, _ sql: String, arguments: StatementArguments = StatementArguments.Default) -> [Self] {
|
||||
public static func fetchAll(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> [Self] {
|
||||
return fetchAll(try! db.selectStatement(sql), arguments: arguments)
|
||||
}
|
||||
|
||||
@@ -142,9 +142,9 @@ public extension DatabaseValueConvertible where Self: SQLiteStatementConvertible
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - returns: An optional value.
|
||||
public static func fetchOne(db: Database, _ sql: String, arguments: StatementArguments = StatementArguments.Default) -> Self? {
|
||||
public static func fetchOne(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> Self? {
|
||||
return fetchOne(try! db.selectStatement(sql), arguments: arguments)
|
||||
}
|
||||
}
|
||||
+12
-12
@@ -473,9 +473,9 @@ public final class Row: CollectionType {
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - returns: A sequence of rows.
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments = StatementArguments.Default) -> DatabaseSequence<Row> {
|
||||
public static func fetch(statement: SelectStatement, arguments: StatementArguments? = nil) -> DatabaseSequence<Row> {
|
||||
// Metal rows can be reused. And reusing them yields better performance.
|
||||
let row = Row(metalStatement: statement)
|
||||
return statement.fetch(arguments: arguments) { row }
|
||||
@@ -487,9 +487,9 @@ public final class Row: CollectionType {
|
||||
/// let rows = Row.fetchAll(statement)
|
||||
///
|
||||
/// - parameter statement: The statement to run.
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - returns: An array of rows.
|
||||
public static func fetchAll(statement: SelectStatement, arguments: StatementArguments = StatementArguments.Default) -> [Row] {
|
||||
public static func fetchAll(statement: SelectStatement, arguments: StatementArguments? = nil) -> [Row] {
|
||||
let sequence = statement.fetch(arguments: arguments) {
|
||||
Row(detachedStatement: statement)
|
||||
}
|
||||
@@ -502,9 +502,9 @@ public final class Row: CollectionType {
|
||||
/// let row = Row.fetchOne(statement)
|
||||
///
|
||||
/// - parameter statement: The statement to run.
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - returns: An optional row.
|
||||
public static func fetchOne(statement: SelectStatement, arguments: StatementArguments = StatementArguments.Default) -> Row? {
|
||||
public static func fetchOne(statement: SelectStatement, arguments: StatementArguments? = nil) -> Row? {
|
||||
let sequence = statement.fetch(arguments: arguments) {
|
||||
Row(detachedStatement: statement)
|
||||
}
|
||||
@@ -543,9 +543,9 @@ public final class Row: CollectionType {
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - returns: A sequence of rows.
|
||||
public static func fetch(db: Database, _ sql: String, arguments: StatementArguments = StatementArguments.Default) -> DatabaseSequence<Row> {
|
||||
public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> DatabaseSequence<Row> {
|
||||
return fetch(try! db.selectStatement(sql), arguments: arguments)
|
||||
}
|
||||
|
||||
@@ -555,9 +555,9 @@ public final class Row: CollectionType {
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - returns: An array of rows.
|
||||
public static func fetchAll(db: Database, _ sql: String, arguments: StatementArguments = StatementArguments.Default) -> [Row] {
|
||||
public static func fetchAll(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> [Row] {
|
||||
return fetchAll(try! db.selectStatement(sql), arguments: arguments)
|
||||
}
|
||||
|
||||
@@ -567,9 +567,9 @@ public final class Row: CollectionType {
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - parameter sql: An SQL query.
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - parameter arguments: Optional statement arguments.
|
||||
/// - returns: An optional row.
|
||||
public static func fetchOne(db: Database, _ sql: String, arguments: StatementArguments = StatementArguments.Default) -> Row? {
|
||||
public static func fetchOne(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> Row? {
|
||||
return fetchOne(try! db.selectStatement(sql), arguments: arguments)
|
||||
}
|
||||
|
||||
|
||||
+24
-72
@@ -14,9 +14,9 @@ public class Statement {
|
||||
public let sql: String
|
||||
|
||||
/// The query arguments
|
||||
public var arguments: StatementArguments? {
|
||||
public var arguments: StatementArguments {
|
||||
get { return _arguments }
|
||||
set { try! setArguments(newValue) }
|
||||
set { try! setArgumentsWithValidation(newValue) }
|
||||
}
|
||||
|
||||
// MARK: Not public
|
||||
@@ -76,7 +76,7 @@ public class Statement {
|
||||
|
||||
// MARK: Arguments
|
||||
|
||||
var _arguments: StatementArguments?
|
||||
var _arguments: StatementArguments = []
|
||||
|
||||
lazy var sqliteArgumentCount: Int = {
|
||||
Int(sqlite3_bind_parameter_count(self.sqliteStatement))
|
||||
@@ -95,9 +95,9 @@ public class Statement {
|
||||
}
|
||||
}()
|
||||
|
||||
private func setArguments(arguments: StatementArguments?) throws {
|
||||
private func setArgumentsWithValidation(arguments: StatementArguments) throws {
|
||||
// Validate
|
||||
let bindings = try self.bindings(arguments)
|
||||
let bindings = try validatedBindings(arguments)
|
||||
_arguments = arguments
|
||||
|
||||
// Apply
|
||||
@@ -127,13 +127,14 @@ public class Statement {
|
||||
|
||||
/// Throws a DatabaseError of code SQLITE_ERROR if arguments don't fill all
|
||||
/// statement arguments.
|
||||
public func validateArguments(arguments: StatementArguments?) throws {
|
||||
try bindings(arguments)
|
||||
public func validateArguments(arguments: StatementArguments) throws {
|
||||
let _ = try validatedBindings(arguments)
|
||||
}
|
||||
|
||||
// Returns a validated array of as many DatabaseValue as there are
|
||||
// parameters in the statement.
|
||||
private func bindings(arguments: StatementArguments?) throws -> [DatabaseValue] {
|
||||
@warn_unused_result
|
||||
private func validatedBindings(arguments: StatementArguments) throws -> [DatabaseValue] {
|
||||
// An array of (key, value) pairs.
|
||||
//
|
||||
// The key is not nil if the statement has a named parameter at given index.
|
||||
@@ -145,20 +146,8 @@ public class Statement {
|
||||
// then we have extra arguments.
|
||||
//
|
||||
// If one of the values is nil, then we have a missing argument.
|
||||
let keyValueBindings: [(String?, DatabaseValue?)] = try {
|
||||
guard let arguments = arguments else {
|
||||
return sqliteArgumentNames.map { argumentName in
|
||||
if let argumentName = argumentName {
|
||||
return (argumentName, nil)
|
||||
}
|
||||
return (nil, nil)
|
||||
}
|
||||
}
|
||||
|
||||
let keyValueBindings: [(String?, DatabaseValue?)] = {
|
||||
switch arguments.kind {
|
||||
case .Default:
|
||||
throw DatabaseError(code: SQLITE_ERROR, message: "Invalid StatementArguments.Default arguments.", sql: sql, arguments: nil)
|
||||
|
||||
case .Array(let array):
|
||||
var keyValueBindings: [(String?, DatabaseValue?)] = []
|
||||
var argumentNameGen = sqliteArgumentNames.generate()
|
||||
@@ -230,9 +219,9 @@ public class Statement {
|
||||
}
|
||||
}
|
||||
|
||||
private func prepareWithArguments(arguments: StatementArguments) throws {
|
||||
if !arguments.kind.isDefault {
|
||||
try setArguments(arguments)
|
||||
private func prepareWithArguments(arguments: StatementArguments?) throws {
|
||||
if let arguments = arguments {
|
||||
try setArgumentsWithValidation(arguments)
|
||||
} else {
|
||||
try validateArguments(self.arguments)
|
||||
}
|
||||
@@ -267,7 +256,7 @@ public final class SelectStatement : Statement {
|
||||
// MARK: Not public
|
||||
|
||||
/// The DatabaseSequence builder.
|
||||
func fetch<T>(arguments arguments: StatementArguments, yield: () -> T) -> DatabaseSequence<T> {
|
||||
func fetch<T>(arguments arguments: StatementArguments?, yield: () -> T) -> DatabaseSequence<T> {
|
||||
try! prepareWithArguments(arguments)
|
||||
return DatabaseSequence(statement: self, yield: yield)
|
||||
}
|
||||
@@ -362,7 +351,7 @@ public final class UpdateStatement : Statement {
|
||||
/// - parameter arguments: Statement arguments.
|
||||
/// - returns: A DatabaseChanges.
|
||||
/// - throws: A DatabaseError whenever a SQLite error occurs.
|
||||
public func execute(arguments arguments: StatementArguments = StatementArguments.Default) throws -> DatabaseChanges {
|
||||
public func execute(arguments arguments: StatementArguments? = nil) throws -> DatabaseChanges {
|
||||
try prepareWithArguments(arguments)
|
||||
reset()
|
||||
|
||||
@@ -455,9 +444,7 @@ public struct DatabaseChanges {
|
||||
///
|
||||
/// db.execute("INSERT ... (:name, :age)", arguments: ["name": "Arthur", "age": 41])
|
||||
///
|
||||
/// GRDB.swift only supports colon-prefixed named arguments, even though SQLite
|
||||
/// supports other syntaxes. See https://www.sqlite.org/lang_expr.html#varparam
|
||||
/// for more information.
|
||||
/// See https://www.sqlite.org/lang_expr.html#varparam for more information.
|
||||
public struct StatementArguments {
|
||||
|
||||
// MARK: Positional Arguments
|
||||
@@ -493,53 +480,21 @@ public struct StatementArguments {
|
||||
kind = .Dictionary(dictionary)
|
||||
}
|
||||
|
||||
|
||||
// MARK: Default Arguments
|
||||
|
||||
/// Whenever you need to write a method with optional statement arguments,
|
||||
/// do not use nil as a sentinel. This is because StatementArguments has
|
||||
/// failable initializers, and you do not want such a failed initializer
|
||||
/// have your method behave as if no arguments was given.
|
||||
///
|
||||
/// Instead, use a non-optional StatementArguments parameter type, and use
|
||||
/// StatementArguments.Default as its default value.
|
||||
///
|
||||
/// Compare:
|
||||
///
|
||||
/// func bad(arguments: StatementArguments? = nil)
|
||||
/// func good(arguments: StatementArguments = StatementArguments.Default)
|
||||
///
|
||||
/// let badDict: NSDictionary = ["foo": NSObject()] // can't be used as arguments
|
||||
/// let arguments = StatementArguments(badDict) // nil, actually
|
||||
///
|
||||
/// // Bad function swallows nil. Bad, bad function!
|
||||
/// bad(arguments: arguments)
|
||||
///
|
||||
/// // Good function forces the user to handle the invalid input case:
|
||||
/// good(arguments: arguments) // won't compile
|
||||
/// if let arguments = arguments {
|
||||
/// good(arguments: arguments)
|
||||
/// } else {
|
||||
/// // handle wrong dictionary
|
||||
/// }
|
||||
public static var Default = StatementArguments(kind: .Default)
|
||||
public var isEmpty: Bool {
|
||||
switch kind {
|
||||
case .Array(let array):
|
||||
return array.isEmpty
|
||||
case .Dictionary(let dictionary):
|
||||
return dictionary.isEmpty
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: Not Public
|
||||
|
||||
enum Kind {
|
||||
case Default
|
||||
case Array([DatabaseValueConvertible?])
|
||||
case Dictionary([String: DatabaseValueConvertible?])
|
||||
|
||||
var isDefault: Bool {
|
||||
switch self {
|
||||
case .Default:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let kind: Kind
|
||||
@@ -575,9 +530,6 @@ extension StatementArguments : CustomStringConvertible {
|
||||
/// A textual representation of `self`.
|
||||
public var description: String {
|
||||
switch kind {
|
||||
case .Default:
|
||||
return "StatementArguments.Default"
|
||||
|
||||
case .Array(let values):
|
||||
return "["
|
||||
+ values
|
||||
|
||||
@@ -966,16 +966,9 @@ The following code is dangerous for your application, because it has many opport
|
||||
|
||||
```swift
|
||||
func fetchUserQuery(db: Database, sql: String, arguments: NSDictionary) throws -> [Row] {
|
||||
// Dictionary arguments may contain invalid values
|
||||
guard let arguments = StatementArguments(arguments) else {
|
||||
throw NSError(
|
||||
domain: "MyDomain",
|
||||
code: 0,
|
||||
userInfo: [NSLocalizedDescriptionKey: "Invalid arguments"])
|
||||
}
|
||||
|
||||
// Crashes if sql is invalid, or if arguments don't fit the SQL query:
|
||||
return Row.fetchAll(db, sql, arguments: arguments)
|
||||
// Crashes if sql is invalid, if dictionary arguments contains invalid
|
||||
// values, or if arguments don't fit the SQL query:
|
||||
return Row.fetchAll(db, sql, arguments: StatementArguments(arguments))
|
||||
}
|
||||
|
||||
// fatal error: no such table: foo
|
||||
|
||||
Reference in new issue
Block a user