mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
Rename statement argument methods
This commit is contained in:
1 parent
db546bf478
commit
ee64f8ff9d
6 files changed
+279
-69
No files matched your search
@@ -201,7 +201,7 @@ extension Database {
|
||||
// Extract statement arguments
|
||||
let bindings = try arguments.extractBindings(forStatement: statement, allowingRemainingValues: true)
|
||||
// unsafe is OK because we just extracted the correct number of arguments
|
||||
statement.unsafeSetArguments(StatementArguments(bindings))
|
||||
statement.setUncheckedArguments(StatementArguments(bindings))
|
||||
|
||||
// Execute
|
||||
try statement.execute()
|
||||
|
||||
@@ -157,7 +157,7 @@ public struct SQLRequest<T>: FetchRequest {
|
||||
case .internal?:
|
||||
statement = try db.internalCachedSelectStatement(sql: sql)
|
||||
}
|
||||
try statement.setArgumentsWithValidation(context.arguments)
|
||||
try statement.setArguments(context.arguments)
|
||||
return PreparedRequest(statement: statement, adapter: adapter)
|
||||
}
|
||||
}
|
||||
|
||||
+107
-10
@@ -128,19 +128,74 @@ public class Statement {
|
||||
set {
|
||||
// Force arguments validity: it is a programmer error to provide
|
||||
// arguments that do not match the statement.
|
||||
try! setArgumentsWithValidation(newValue)
|
||||
try! setArguments(newValue)
|
||||
}
|
||||
}
|
||||
|
||||
/// Throws a DatabaseError of code SQLITE_ERROR if arguments don't fill all
|
||||
/// statement arguments.
|
||||
public func validate(arguments: StatementArguments) throws {
|
||||
///
|
||||
/// For example:
|
||||
///
|
||||
/// let statement = try db.makeUpdateArgument(sql: """
|
||||
/// INSERT INTO player (id, name) VALUES (?, ?)
|
||||
/// """)
|
||||
///
|
||||
/// // OK
|
||||
/// statement.validateArguments([1, "Arthur"])
|
||||
///
|
||||
/// // Throws
|
||||
/// statement.validateArguments([1])
|
||||
///
|
||||
/// See also setArguments(_:)
|
||||
public func validateArguments(_ arguments: StatementArguments) throws {
|
||||
var arguments = arguments
|
||||
_ = try arguments.extractBindings(forStatement: self, allowingRemainingValues: false)
|
||||
}
|
||||
|
||||
/// Throws a DatabaseError of code SQLITE_ERROR if arguments don't fill all
|
||||
/// statement arguments.
|
||||
///
|
||||
/// For example:
|
||||
///
|
||||
/// let statement = try db.makeUpdateArgument(sql: """
|
||||
/// INSERT INTO player (id, name) VALUES (?, ?)
|
||||
/// """)
|
||||
///
|
||||
/// // OK
|
||||
/// statement.validate([1, "Arthur"])
|
||||
///
|
||||
/// // Throws
|
||||
/// statement.validate([1])
|
||||
///
|
||||
/// See also setArguments(_:)
|
||||
@available(*, deprecated, renamed: "validateArguments(_:)")
|
||||
public func validate(arguments: StatementArguments) throws {
|
||||
try validateArguments(arguments)
|
||||
}
|
||||
|
||||
/// Set arguments without any validation. Trades safety for performance.
|
||||
public func unsafeSetArguments(_ arguments: StatementArguments) {
|
||||
///
|
||||
/// Only call this method if you are sure input arguments match all expected
|
||||
/// arguments of the statement.
|
||||
///
|
||||
/// For example:
|
||||
///
|
||||
/// let statement = try db.makeUpdateArgument(sql: """
|
||||
/// INSERT INTO player (id, name) VALUES (?, ?)
|
||||
/// """)
|
||||
///
|
||||
/// // OK
|
||||
/// statement.setUncheckedArguments([1, "Arthur"])
|
||||
///
|
||||
/// // OK
|
||||
/// let arguments: StatementArguments = ... // some untrusted arguments
|
||||
/// try statement.validateArguments(arguments)
|
||||
/// statement.setUncheckedArguments(arguments)
|
||||
///
|
||||
/// // NOT OK
|
||||
/// statement.setUncheckedArguments([1])
|
||||
public func setUncheckedArguments(_ arguments: StatementArguments) {
|
||||
_arguments = arguments
|
||||
argumentsNeedValidation = false
|
||||
|
||||
@@ -159,14 +214,56 @@ public class Statement {
|
||||
}
|
||||
}
|
||||
|
||||
func setArgumentsWithValidation(_ arguments: StatementArguments) throws {
|
||||
/// Set arguments without any validation. Trades safety for performance.
|
||||
///
|
||||
/// Only call this method if you are sure input arguments match all expected
|
||||
/// arguments of the statement.
|
||||
///
|
||||
/// For example:
|
||||
///
|
||||
/// let statement = try db.makeUpdateArgument(sql: """
|
||||
/// INSERT INTO player (id, name) VALUES (?, ?)
|
||||
/// """)
|
||||
///
|
||||
/// // OK
|
||||
/// statement.unsafeSetArguments([1, "Arthur"])
|
||||
///
|
||||
/// // OK
|
||||
/// let arguments: StatementArguments = ... // some untrusted arguments
|
||||
/// try statement.validateArguments(arguments)
|
||||
/// statement.unsafeSetArguments(arguments)
|
||||
///
|
||||
/// // NOT OK
|
||||
/// statement.unsafeSetArguments([1])
|
||||
///
|
||||
/// See also setArguments(_:)
|
||||
@available(*, deprecated, renamed: "setUncheckedArguments(_:)")
|
||||
public func unsafeSetArguments(_ arguments: StatementArguments) {
|
||||
setUncheckedArguments(arguments)
|
||||
}
|
||||
|
||||
/// Set the statement arguments, or throws a DatabaseError of code
|
||||
/// SQLITE_ERROR if arguments don't fill all statement arguments.
|
||||
///
|
||||
/// For example:
|
||||
///
|
||||
/// let statement = try db.makeUpdateArgument(sql: """
|
||||
/// INSERT INTO player (id, name) VALUES (?, ?)
|
||||
/// """)
|
||||
///
|
||||
/// // OK
|
||||
/// try statement.setArguments([1, "Arthur"])
|
||||
///
|
||||
/// // Throws an error
|
||||
/// try statement.setArguments([1])
|
||||
public func setArguments(_ arguments: StatementArguments) throws {
|
||||
// Validate
|
||||
_arguments = arguments
|
||||
var arguments = arguments
|
||||
let bindings = try arguments.extractBindings(forStatement: self, allowingRemainingValues: false)
|
||||
argumentsNeedValidation = false
|
||||
var consumedArguments = arguments
|
||||
let bindings = try consumedArguments.extractBindings(forStatement: self, allowingRemainingValues: false)
|
||||
|
||||
// Apply
|
||||
_arguments = arguments
|
||||
argumentsNeedValidation = false
|
||||
try reset()
|
||||
clearBindings()
|
||||
for (index, dbValue) in zip(Int32(1)..., bindings) {
|
||||
@@ -220,9 +317,9 @@ public class Statement {
|
||||
// Force arguments validity: it is a programmer error to provide
|
||||
// arguments that do not match the statement.
|
||||
if let arguments = arguments {
|
||||
try! setArgumentsWithValidation(arguments)
|
||||
try! setArguments(arguments)
|
||||
} else if argumentsNeedValidation {
|
||||
try! validate(arguments: self.arguments)
|
||||
try! validateArguments(self.arguments)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -859,7 +859,7 @@ final class DAO<Record: MutablePersistableRecord> {
|
||||
tableName: databaseTableName,
|
||||
insertedColumns: persistenceContainer.columns)
|
||||
let statement = try db.internalCachedUpdateStatement(sql: query.sql)
|
||||
statement.unsafeSetArguments(StatementArguments(persistenceContainer.values))
|
||||
statement.setUncheckedArguments(StatementArguments(persistenceContainer.values))
|
||||
return statement
|
||||
}
|
||||
|
||||
@@ -909,7 +909,7 @@ final class DAO<Record: MutablePersistableRecord> {
|
||||
updatedColumns: updatedColumns,
|
||||
conditionColumns: primaryKeyColumns)
|
||||
let statement = try db.internalCachedUpdateStatement(sql: query.sql)
|
||||
statement.unsafeSetArguments(StatementArguments(updatedValues + primaryKeyValues))
|
||||
statement.setUncheckedArguments(StatementArguments(updatedValues + primaryKeyValues))
|
||||
return statement
|
||||
}
|
||||
|
||||
@@ -929,7 +929,7 @@ final class DAO<Record: MutablePersistableRecord> {
|
||||
tableName: databaseTableName,
|
||||
conditionColumns: primaryKeyColumns)
|
||||
let statement = try db.internalCachedUpdateStatement(sql: query.sql)
|
||||
statement.unsafeSetArguments(StatementArguments(primaryKeyValues))
|
||||
statement.setUncheckedArguments(StatementArguments(primaryKeyValues))
|
||||
return statement
|
||||
}
|
||||
|
||||
@@ -949,7 +949,7 @@ final class DAO<Record: MutablePersistableRecord> {
|
||||
tableName: databaseTableName,
|
||||
conditionColumns: primaryKeyColumns)
|
||||
let statement = try db.internalCachedSelectStatement(sql: query.sql)
|
||||
statement.unsafeSetArguments(StatementArguments(primaryKeyValues))
|
||||
statement.setUncheckedArguments(StatementArguments(primaryKeyValues))
|
||||
return statement
|
||||
}
|
||||
|
||||
|
||||
@@ -7019,8 +7019,7 @@ In such a situation, you can still avoid fatal errors by exposing and handling e
|
||||
// Untrusted arguments
|
||||
if let arguments = StatementArguments(arguments) {
|
||||
let statement = try db.makeSelectStatement(sql: sql)
|
||||
try statement.validate(arguments: arguments)
|
||||
statement.unsafeSetArguments(arguments)
|
||||
try statement.setArguments(arguments)
|
||||
|
||||
var cursor = try Row.fetchCursor(statement)
|
||||
while let row = try iterator.next() {
|
||||
@@ -7981,12 +7980,12 @@ for player in players {
|
||||
}
|
||||
|
||||
// Prepared statement
|
||||
let insertStatement = db.prepareStatement("INSERT INTO player (name, email) VALUES (?, ?)")
|
||||
let insertStatement = db.makeUpdateStatement(sql: "INSERT INTO player (name, email) VALUES (?, ?)")
|
||||
for player in players {
|
||||
// Only use the unsafe arguments setter if you are sure that you provide
|
||||
// Only use the unchecked arguments setter if you are sure that you provide
|
||||
// all statement arguments. A mistake can store unexpected values in
|
||||
// the database.
|
||||
insertStatement.unsafeSetArguments([player.name, player.email])
|
||||
insertStatement.setUncheckedArguments([player.name, player.email])
|
||||
try insertStatement.execute()
|
||||
}
|
||||
```
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import XCTest
|
||||
#if GRDBCUSTOMSQLITE
|
||||
import GRDBCustomSQLite
|
||||
import GRDBCustomSQLite
|
||||
#else
|
||||
import GRDB
|
||||
import GRDB
|
||||
#endif
|
||||
|
||||
class StatementArgumentsTests: GRDBTestCase {
|
||||
|
||||
|
||||
override func setup(_ dbWriter: DatabaseWriter) throws {
|
||||
var migrator = DatabaseMigrator()
|
||||
migrator.registerMigration("createPersons") { db in
|
||||
@@ -28,14 +28,14 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
|
||||
do {
|
||||
// Correct number of arguments
|
||||
try statement.validate(arguments: ["foo", 1])
|
||||
try statement.validateArguments(["foo", 1])
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
|
||||
do {
|
||||
// Missing arguments
|
||||
try statement.validate(arguments: [])
|
||||
try statement.validateArguments([])
|
||||
XCTFail("Expected error")
|
||||
} catch is DatabaseError {
|
||||
} catch {
|
||||
@@ -44,7 +44,7 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
|
||||
do {
|
||||
// Two few arguments
|
||||
try statement.validate(arguments: ["foo"])
|
||||
try statement.validateArguments(["foo"])
|
||||
XCTFail("Expected error")
|
||||
} catch is DatabaseError {
|
||||
} catch {
|
||||
@@ -53,7 +53,7 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
|
||||
do {
|
||||
// Two many arguments
|
||||
try statement.validate(arguments: ["foo", 1, "bar"])
|
||||
try statement.validateArguments(["foo", 1, "bar"])
|
||||
XCTFail("Expected error")
|
||||
} catch is DatabaseError {
|
||||
} catch {
|
||||
@@ -62,7 +62,7 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
|
||||
do {
|
||||
// Missing arguments
|
||||
try statement.validate(arguments: [:])
|
||||
try statement.validateArguments([:])
|
||||
XCTFail("Expected error")
|
||||
} catch is DatabaseError {
|
||||
} catch {
|
||||
@@ -71,7 +71,7 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
|
||||
do {
|
||||
// Unmappable arguments
|
||||
try statement.validate(arguments: ["firstName": "foo", "age": 1])
|
||||
try statement.validateArguments(["firstName": "foo", "age": 1])
|
||||
XCTFail("Expected error")
|
||||
} catch is DatabaseError {
|
||||
} catch {
|
||||
@@ -79,7 +79,7 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func testPositionalStatementArguments() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
@@ -99,8 +99,8 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
XCTAssertEqual(row["age"] as Int, age)
|
||||
}
|
||||
}
|
||||
|
||||
func testUnsafePositionalStatementArguments() throws {
|
||||
|
||||
func testCheckedPositionalStatementArguments() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
let name = "Arthur"
|
||||
@@ -108,18 +108,56 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
let arguments = StatementArguments([name, age] as [DatabaseValueConvertible?])
|
||||
|
||||
let updateStatement = try db.makeUpdateStatement(sql: "INSERT INTO persons (firstName, age) VALUES (?, ?)")
|
||||
updateStatement.unsafeSetArguments(arguments)
|
||||
try updateStatement.setArguments(arguments)
|
||||
try updateStatement.execute()
|
||||
|
||||
let selectStatement = try db.makeSelectStatement(sql: "SELECT * FROM persons WHERE firstName = ? AND age = ?")
|
||||
selectStatement.unsafeSetArguments(arguments)
|
||||
try selectStatement.setArguments(arguments)
|
||||
let row = try Row.fetchOne(selectStatement)!
|
||||
|
||||
XCTAssertEqual(row["firstName"] as String, name)
|
||||
XCTAssertEqual(row["age"] as Int, age)
|
||||
|
||||
do {
|
||||
try updateStatement.setArguments([1])
|
||||
XCTFail("Expected error")
|
||||
} catch is DatabaseError {
|
||||
XCTAssertEqual(updateStatement.arguments, arguments)
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
|
||||
do {
|
||||
try selectStatement.setArguments([1])
|
||||
XCTFail("Expected error")
|
||||
} catch is DatabaseError {
|
||||
XCTAssertEqual(selectStatement.arguments, arguments)
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testUncheckedPositionalStatementArguments() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
let name = "Arthur"
|
||||
let age = 42
|
||||
let arguments = StatementArguments([name, age] as [DatabaseValueConvertible?])
|
||||
|
||||
let updateStatement = try db.makeUpdateStatement(sql: "INSERT INTO persons (firstName, age) VALUES (?, ?)")
|
||||
updateStatement.setUncheckedArguments(arguments)
|
||||
try updateStatement.execute()
|
||||
|
||||
let selectStatement = try db.makeSelectStatement(sql: "SELECT * FROM persons WHERE firstName = ? AND age = ?")
|
||||
selectStatement.setUncheckedArguments(arguments)
|
||||
let row = try Row.fetchOne(selectStatement)!
|
||||
|
||||
XCTAssertEqual(row["firstName"] as String, name)
|
||||
XCTAssertEqual(row["age"] as Int, age)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func testNamedStatementArgumentsValidation() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
@@ -127,28 +165,28 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
|
||||
do {
|
||||
// Correct number of arguments
|
||||
try statement.validate(arguments: ["foo", 1])
|
||||
try statement.validateArguments(["foo", 1])
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
|
||||
do {
|
||||
// All arguments are mapped
|
||||
try statement.validate(arguments: ["firstName": "foo", "age": 1])
|
||||
try statement.validateArguments(["firstName": "foo", "age": 1])
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
|
||||
do {
|
||||
// All arguments are mapped
|
||||
try statement.validate(arguments: ["firstName": "foo", "age": 1, "bar": "baz"])
|
||||
try statement.validateArguments(["firstName": "foo", "age": 1, "bar": "baz"])
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
|
||||
do {
|
||||
// Missing arguments
|
||||
try statement.validate(arguments: [])
|
||||
try statement.validateArguments([])
|
||||
XCTFail("Expected error")
|
||||
} catch is DatabaseError {
|
||||
} catch {
|
||||
@@ -157,7 +195,7 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
|
||||
do {
|
||||
// Missing arguments
|
||||
try statement.validate(arguments: ["foo"])
|
||||
try statement.validateArguments(["foo"])
|
||||
XCTFail("Expected error")
|
||||
} catch is DatabaseError {
|
||||
} catch {
|
||||
@@ -166,7 +204,7 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
|
||||
do {
|
||||
// Too many arguments
|
||||
try statement.validate(arguments: ["foo", 1, "baz"])
|
||||
try statement.validateArguments(["foo", 1, "baz"])
|
||||
XCTFail("Expected error")
|
||||
} catch is DatabaseError {
|
||||
} catch {
|
||||
@@ -175,7 +213,7 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
|
||||
do {
|
||||
// Missing arguments
|
||||
try statement.validate(arguments: [:])
|
||||
try statement.validateArguments([:])
|
||||
XCTFail("Expected error")
|
||||
} catch is DatabaseError {
|
||||
} catch {
|
||||
@@ -184,7 +222,7 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
|
||||
do {
|
||||
// Missing arguments
|
||||
try statement.validate(arguments: ["firstName": "foo"])
|
||||
try statement.validateArguments(["firstName": "foo"])
|
||||
XCTFail("Expected error")
|
||||
} catch is DatabaseError {
|
||||
} catch {
|
||||
@@ -192,7 +230,7 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func testNamedStatementArguments() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
@@ -212,8 +250,8 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
XCTAssertEqual(row["age"] as Int, age)
|
||||
}
|
||||
}
|
||||
|
||||
func testUnsafeNamedStatementArguments() throws {
|
||||
|
||||
func testCheckedNamedStatementArguments() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
let name = "Arthur"
|
||||
@@ -221,18 +259,56 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
let arguments = StatementArguments(["name": name, "age": age] as [String: DatabaseValueConvertible?])
|
||||
|
||||
let updateStatement = try db.makeUpdateStatement(sql: "INSERT INTO persons (firstName, age) VALUES (:name, :age)")
|
||||
updateStatement.unsafeSetArguments(arguments)
|
||||
try updateStatement.setArguments(arguments)
|
||||
try updateStatement.execute()
|
||||
|
||||
let selectStatement = try db.makeSelectStatement(sql: "SELECT * FROM persons WHERE firstName = :name AND age = :age")
|
||||
selectStatement.unsafeSetArguments(arguments)
|
||||
try selectStatement.setArguments(arguments)
|
||||
let row = try Row.fetchOne(selectStatement)!
|
||||
|
||||
XCTAssertEqual(row["firstName"] as String, name)
|
||||
XCTAssertEqual(row["age"] as Int, age)
|
||||
|
||||
do {
|
||||
try updateStatement.setArguments(["name": name])
|
||||
XCTFail("Expected error")
|
||||
} catch is DatabaseError {
|
||||
XCTAssertEqual(updateStatement.arguments, arguments)
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
|
||||
do {
|
||||
try selectStatement.setArguments(["name": name])
|
||||
XCTFail("Expected error")
|
||||
} catch is DatabaseError {
|
||||
XCTAssertEqual(selectStatement.arguments, arguments)
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testUncheckedNamedStatementArguments() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
let name = "Arthur"
|
||||
let age = 42
|
||||
let arguments = StatementArguments(["name": name, "age": age] as [String: DatabaseValueConvertible?])
|
||||
|
||||
let updateStatement = try db.makeUpdateStatement(sql: "INSERT INTO persons (firstName, age) VALUES (:name, :age)")
|
||||
updateStatement.setUncheckedArguments(arguments)
|
||||
try updateStatement.execute()
|
||||
|
||||
let selectStatement = try db.makeSelectStatement(sql: "SELECT * FROM persons WHERE firstName = :name AND age = :age")
|
||||
selectStatement.setUncheckedArguments(arguments)
|
||||
let row = try Row.fetchOne(selectStatement)!
|
||||
|
||||
XCTAssertEqual(row["firstName"] as String, name)
|
||||
XCTAssertEqual(row["age"] as Int, age)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func testReusedNamedStatementArgumentsValidation() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
@@ -248,28 +324,28 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
|
||||
do {
|
||||
// Correct number of arguments
|
||||
try statement.validate(arguments: ["foo", 1])
|
||||
try statement.validateArguments(["foo", 1])
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
|
||||
do {
|
||||
// All arguments are mapped
|
||||
try statement.validate(arguments: ["name": "foo", "age": 1])
|
||||
try statement.validateArguments(["name": "foo", "age": 1])
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
|
||||
do {
|
||||
// All arguments are mapped
|
||||
try statement.validate(arguments: ["name": "foo", "age": 1, "bar": "baz"])
|
||||
try statement.validateArguments(["name": "foo", "age": 1, "bar": "baz"])
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
|
||||
do {
|
||||
// Missing arguments
|
||||
try statement.validate(arguments: [])
|
||||
try statement.validateArguments([])
|
||||
XCTFail("Expected error")
|
||||
} catch is DatabaseError {
|
||||
} catch {
|
||||
@@ -278,7 +354,7 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
|
||||
do {
|
||||
// Missing arguments
|
||||
try statement.validate(arguments: ["foo"])
|
||||
try statement.validateArguments(["foo"])
|
||||
XCTFail("Expected error")
|
||||
} catch is DatabaseError {
|
||||
} catch {
|
||||
@@ -287,7 +363,7 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
|
||||
do {
|
||||
// Too many arguments
|
||||
try statement.validate(arguments: ["foo", 1, "baz"])
|
||||
try statement.validateArguments(["foo", 1, "baz"])
|
||||
XCTFail("Expected error")
|
||||
} catch is DatabaseError {
|
||||
} catch {
|
||||
@@ -296,7 +372,7 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
|
||||
do {
|
||||
// Missing arguments
|
||||
try statement.validate(arguments: [:])
|
||||
try statement.validateArguments([:])
|
||||
XCTFail("Expected error")
|
||||
} catch is DatabaseError {
|
||||
} catch {
|
||||
@@ -305,7 +381,7 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
|
||||
do {
|
||||
// Missing arguments
|
||||
try statement.validate(arguments: ["name": "foo"])
|
||||
try statement.validateArguments(["name": "foo"])
|
||||
XCTFail("Expected error")
|
||||
} catch is DatabaseError {
|
||||
} catch {
|
||||
@@ -313,8 +389,8 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
func testReusedNamedStatementArguments() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
@@ -334,8 +410,8 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
XCTAssertEqual(row["age"] as Int, age)
|
||||
}
|
||||
}
|
||||
|
||||
func testUnsafeReusedNamedStatementArguments() throws {
|
||||
|
||||
func testCheckedReusedNamedStatementArguments() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
let name = "Arthur"
|
||||
@@ -343,18 +419,56 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
let arguments = StatementArguments(["name": name, "age": age] as [String: DatabaseValueConvertible?])
|
||||
|
||||
let updateStatement = try db.makeUpdateStatement(sql: "INSERT INTO persons (firstName, lastName, age) VALUES (:name, :name, :age)")
|
||||
updateStatement.unsafeSetArguments(arguments)
|
||||
try updateStatement.setArguments(arguments)
|
||||
try updateStatement.execute()
|
||||
|
||||
let selectStatement = try db.makeSelectStatement(sql: "SELECT * FROM persons WHERE firstName = :name AND lastName = :name AND age = :age")
|
||||
selectStatement.unsafeSetArguments(arguments)
|
||||
try selectStatement.setArguments(arguments)
|
||||
let row = try Row.fetchOne(selectStatement)!
|
||||
|
||||
XCTAssertEqual(row["firstName"] as String, name)
|
||||
XCTAssertEqual(row["age"] as Int, age)
|
||||
|
||||
do {
|
||||
try updateStatement.setArguments(["name": name])
|
||||
XCTFail("Expected error")
|
||||
} catch is DatabaseError {
|
||||
XCTAssertEqual(updateStatement.arguments, arguments)
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
|
||||
do {
|
||||
try selectStatement.setArguments(["name": name])
|
||||
XCTFail("Expected error")
|
||||
} catch is DatabaseError {
|
||||
XCTAssertEqual(selectStatement.arguments, arguments)
|
||||
} catch {
|
||||
XCTFail("Unexpected error: \(error)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testUncheckedReusedNamedStatementArguments() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
let name = "Arthur"
|
||||
let age = 42
|
||||
let arguments = StatementArguments(["name": name, "age": age] as [String: DatabaseValueConvertible?])
|
||||
|
||||
let updateStatement = try db.makeUpdateStatement(sql: "INSERT INTO persons (firstName, lastName, age) VALUES (:name, :name, :age)")
|
||||
updateStatement.setUncheckedArguments(arguments)
|
||||
try updateStatement.execute()
|
||||
|
||||
let selectStatement = try db.makeSelectStatement(sql: "SELECT * FROM persons WHERE firstName = :name AND lastName = :name AND age = :age")
|
||||
selectStatement.setUncheckedArguments(arguments)
|
||||
let row = try Row.fetchOne(selectStatement)!
|
||||
|
||||
XCTAssertEqual(row["firstName"] as String, name)
|
||||
XCTAssertEqual(row["age"] as Int, age)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func testMixedArguments() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
@@ -363,7 +477,7 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
XCTAssertEqual(row, ["two": 2, "foo": "foo", "one": 1, "foo2": "foo", "bar": "bar"])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func testAppendContentsOf() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
@@ -395,7 +509,7 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func testPlusOperator() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
@@ -419,7 +533,7 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func testOverflowPlusOperator() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
@@ -443,7 +557,7 @@ class StatementArgumentsTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func testPlusEqualOperator() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.inDatabase { db in
|
||||
|
||||
Reference in new issue
Block a user