Statement arguments mismatch is always a programming error that crashes.

This commit is contained in:
Gwendal Roué committed 2016-01-10 19:03:06 +01:00
1 parent dd2f11988f
commit f6f991a2cb
4 files changed
+104 -92

No files matched your search

@@ -193,49 +193,6 @@ class UpdateStatementTests : GRDBTestCase {
XCTAssertEqual(Int.fetchAll(db, "SELECT age FROM persons ORDER BY age"), [32, 41])
return .Rollback
}
try dbQueue.inTransaction { db in
do {
// Too few arguments
try db.execute(
"INSERT INTO persons (name, age) VALUES ('Arthur', :age1);" +
"INSERT INTO persons (name, age) VALUES ('Arthur', :age2);")
XCTFail("Expected Error")
} catch {
XCTAssertTrue(Int.fetchAll(db, "SELECT age FROM persons ORDER BY age").isEmpty)
}
return .Rollback
}
try dbQueue.inTransaction { db in
do {
// Too few arguments
try db.execute(
"INSERT INTO persons (name, age) VALUES ('Arthur', :age1);" +
"INSERT INTO persons (name, age) VALUES ('Arthur', :age2);",
arguments: ["age1": 41])
XCTFail("Expected Error")
} catch {
// Partial fail
XCTAssertEqual(Int.fetchAll(db, "SELECT age FROM persons ORDER BY age"), [41])
}
return .Rollback
}
try dbQueue.inTransaction { db in
do {
// Too many arguments
try db.execute(
"INSERT INTO persons (name, age) VALUES ('Arthur', :age1);" +
"INSERT INTO persons (name, age) VALUES ('Arthur', :age2);",
arguments: [41, 32, 17])
XCTFail("Expected Error")
} catch {
// Partial fail
XCTAssertEqual(Int.fetchAll(db, "SELECT age FROM persons ORDER BY age"), [32, 41])
}
return .Rollback
}
}
}
@@ -275,51 +232,6 @@ class UpdateStatementTests : GRDBTestCase {
XCTAssertEqual(Int.fetchAll(db, "SELECT age FROM persons ORDER BY age"), [32, 41])
return .Rollback
}
try dbQueue.inTransaction { db in
do {
// Too few arguments
try db.execute(
"INSERT INTO persons (name, age) VALUES ('Arthur', ?);" +
"INSERT INTO persons (name, age) VALUES ('Arthur', ?);")
XCTFail("Expected Error")
} catch {
XCTAssertTrue(Int.fetchAll(db, "SELECT age FROM persons ORDER BY age").isEmpty)
}
return .Rollback
}
try dbQueue.inTransaction { db in
do {
// Too few arguments
try db.execute(
"INSERT INTO persons (name, age) VALUES ('Arthur', ?);" +
"INSERT INTO persons (name, age) VALUES ('Arthur', ?);",
arguments: [41])
XCTFail("Expected Error")
} catch {
// Partial fail
XCTAssertEqual(Int.fetchAll(db, "SELECT age FROM persons ORDER BY age"), [41])
}
return .Rollback
}
try dbQueue.inTransaction { db in
do {
// Too many arguments
try db.execute(
"INSERT INTO persons (name, age) VALUES ('Arthur', ?);" +
"INSERT INTO persons (name, age) VALUES ('Arthur', ?);",
arguments: [41, 32, 17])
XCTFail("Expected Error")
} catch {
// Partial fail
XCTAssertEqual(Int.fetchAll(db, "SELECT age FROM persons ORDER BY age"), [32, 41])
}
return .Rollback
}
}
}
+102 -2
View File
@@ -167,15 +167,115 @@ class CrashTests: GRDBTestCase {
// =========================================================================
// MARK: - Statements
func testInvalidNamedBinding() {
func testInvalidStatementArguments() {
assertCrash("SQLite error 1 with statement `INSERT INTO persons (name, age) VALUES (:name, :age)`: missing statement argument(s): age") {
try dbQueue.inDatabase { db in
try db.execute("CREATE TABLE persons (name TEXT, age INT)")
try! db.execute("INSERT INTO persons (name, age) VALUES (:name, :age)", arguments: ["name": "Arthur"])
}
}
assertCrash("SQLite error 21 with statement `INSERT INTO persons (name, age) VALUES ('Arthur', ?);`: wrong number of statement arguments: 0") {
try dbQueue.inTransaction { db in
do {
try db.execute("CREATE TABLE persons (name TEXT, age INT)")
try db.execute(
"INSERT INTO persons (name, age) VALUES ('Arthur', ?);" +
"INSERT INTO persons (name, age) VALUES ('Arthur', ?);")
XCTFail("Expected Error")
} catch {
XCTAssertTrue(Int.fetchAll(db, "SELECT age FROM persons ORDER BY age").isEmpty)
}
return .Rollback
}
}
assertCrash("SQLite error 21 with statement `INSERT INTO persons (name, age) VALUES ('Arthur', ?);`: wrong number of statement arguments: 0") {
try dbQueue.inTransaction { db in
do {
try db.execute("CREATE TABLE persons (name TEXT, age INT)")
try db.execute(
"INSERT INTO persons (name, age) VALUES ('Arthur', ?);" +
"INSERT INTO persons (name, age) VALUES ('Arthur', ?);",
arguments: [41])
XCTFail("Expected Error")
} catch {
// Partial fail
XCTAssertEqual(Int.fetchAll(db, "SELECT age FROM persons ORDER BY age"), [41])
}
return .Rollback
}
}
assertCrash("SQLite error 21 with statement `INSERT INTO persons (name, age) VALUES ('Arthur', :age1);`: missing statement argument(s): age1") {
try dbQueue.inTransaction { db in
do {
try db.execute("CREATE TABLE persons (name TEXT, age INT)")
try db.execute(
"INSERT INTO persons (name, age) VALUES ('Arthur', :age1);" +
"INSERT INTO persons (name, age) VALUES ('Arthur', :age2);")
XCTFail("Expected Error")
} catch {
XCTAssertTrue(Int.fetchAll(db, "SELECT age FROM persons ORDER BY age").isEmpty)
}
return .Rollback
}
}
assertCrash("SQLite error 21 with statement `INSERT INTO persons (name, age) VALUES ('Arthur', :age2);`: missing statement argument(s): age2") {
try dbQueue.inTransaction { db in
do {
try db.execute("CREATE TABLE persons (name TEXT, age INT)")
try db.execute(
"INSERT INTO persons (name, age) VALUES ('Arthur', :age1);" +
"INSERT INTO persons (name, age) VALUES ('Arthur', :age2);",
arguments: ["age1": 41])
XCTFail("Expected Error")
} catch {
// Partial fail
XCTAssertEqual(Int.fetchAll(db, "SELECT age FROM persons ORDER BY age"), [41])
}
return .Rollback
}
}
assertCrash("wrong number of statement arguments: 3") {
try dbQueue.inTransaction { db in
do {
try db.execute("CREATE TABLE persons (name TEXT, age INT)")
try db.execute(
"INSERT INTO persons (name, age) VALUES ('Arthur', :age1);" +
"INSERT INTO persons (name, age) VALUES ('Arthur', :age2);",
arguments: [41, 32, 17])
XCTFail("Expected Error")
} catch {
// Partial fail
XCTAssertEqual(Int.fetchAll(db, "SELECT age FROM persons ORDER BY age"), [32, 41])
}
return .Rollback
}
}
assertCrash("wrong number of statement arguments: 3") {
try dbQueue.inTransaction { db in
do {
try db.execute("CREATE TABLE persons (name TEXT, age INT)")
try db.execute(
"INSERT INTO persons (name, age) VALUES ('Arthur', ?);" +
"INSERT INTO persons (name, age) VALUES ('Arthur', ?);",
arguments: [41, 32, 17])
XCTFail("Expected Error")
} catch {
// Partial fail
XCTAssertEqual(Int.fetchAll(db, "SELECT age FROM persons ORDER BY age"), [32, 41])
}
return .Rollback
}
}
}
// =========================================================================
// MARK: - RecordWithoutDatabaseTableName
+1 -1
View File
@@ -123,7 +123,7 @@ public final class Database {
// It's not OK if there remains unused arguments:
let validateRemainingArguments = {
if !remainingValues.isEmpty {
throw DatabaseError(code: SQLITE_MISUSE, message: "Statement arguments mismatch: got \(values.count) argument(s) instead of \(values.count - remainingValues.count).", sql: sql, arguments: nil)
fatalError("wrong number of statement arguments: \(values.count)")
}
}
return (consumeArguments, validateRemainingArguments)
+1 -1
View File
@@ -381,7 +381,7 @@ public final class UpdateStatement : Statement {
/// - returns: A DatabaseChanges.
/// - throws: A DatabaseError whenever a SQLite error occurs.
public func execute(arguments arguments: StatementArguments? = nil) throws -> DatabaseChanges {
try prepareWithArguments(arguments)
try! prepareWithArguments(arguments)
reset()
let changes: DatabaseChanges