mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
Prevent statement argument mismatch.
This commit is contained in:
1 parent
ff795bd719
commit
6db9ae464c
5 files changed
+87
-40
No files matched your search
@@ -146,7 +146,7 @@ class DatabaseTests : GRDBTestCase {
|
||||
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", "age": 41])
|
||||
try db.execute("INSERT INTO persons (name, age) VALUES (:name, :age)", arguments: ["name": "Barbara"])
|
||||
try db.execute("INSERT INTO persons (name, age) VALUES (:name, :age)", arguments: ["name": "Barbara", "age": nil])
|
||||
|
||||
let statement = db.selectStatement("SELECT * FROM persons")
|
||||
let rows = Row.fetchAll(statement)
|
||||
@@ -160,7 +160,7 @@ class DatabaseTests : GRDBTestCase {
|
||||
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", "age": 41])
|
||||
try db.execute("INSERT INTO persons (name, age) VALUES (:name, :age)", arguments: ["name": "Barbara"])
|
||||
try db.execute("INSERT INTO persons (name, age) VALUES (:name, :age)", arguments: ["name": "Barbara", "age": nil])
|
||||
|
||||
let statement = db.selectStatement("SELECT * FROM persons WHERE name = ?")
|
||||
let rows = Row.fetchAll(statement, arguments: ["Arthur"])
|
||||
@@ -174,7 +174,7 @@ class DatabaseTests : GRDBTestCase {
|
||||
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", "age": 41])
|
||||
try db.execute("INSERT INTO persons (name, age) VALUES (:name, :age)", arguments: ["name": "Barbara"])
|
||||
try db.execute("INSERT INTO persons (name, age) VALUES (:name, :age)", arguments: ["name": "Barbara", "age": nil])
|
||||
|
||||
let statement = db.selectStatement("SELECT * FROM persons WHERE name = :name")
|
||||
let rows = Row.fetchAll(statement, arguments: ["name": "Arthur"])
|
||||
@@ -188,7 +188,7 @@ class DatabaseTests : GRDBTestCase {
|
||||
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", "age": 41])
|
||||
try db.execute("INSERT INTO persons (name, age) VALUES (:name, :age)", arguments: ["name": "Barbara"])
|
||||
try db.execute("INSERT INTO persons (name, age) VALUES (:name, :age)", arguments: ["name": "Barbara", "age": nil])
|
||||
|
||||
var names: [String?] = []
|
||||
var ages: [Int?] = []
|
||||
@@ -214,7 +214,7 @@ class DatabaseTests : GRDBTestCase {
|
||||
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", "age": 41])
|
||||
try db.execute("INSERT INTO persons (name, age) VALUES (:name, :age)", arguments: ["name": "Barbara"])
|
||||
try db.execute("INSERT INTO persons (name, age) VALUES (:name, :age)", arguments: ["name": "Barbara", "age": nil])
|
||||
|
||||
var names: [String?] = []
|
||||
var ages: [Int?] = []
|
||||
|
||||
@@ -27,9 +27,9 @@ class UpdateStatementTests : GRDBTestCase {
|
||||
assertNoError {
|
||||
try dbQueue.inTransaction { db in
|
||||
try db.updateStatement("INSERT INTO persons (name, age) VALUES ('Arthur', ?)").execute()
|
||||
try db.updateStatement("INSERT INTO persons (name, age) VALUES ('', ?)").execute()
|
||||
try db.updateStatement("INSERT INTO persons (name, age) VALUES ('Barbara', ?)").execute()
|
||||
try db.updateStatement("INSERT INTO persons (name, age) VALUES ('Arthur', ?);").execute()
|
||||
try db.updateStatement("INSERT INTO persons (name, age) VALUES ('', ?);").execute()
|
||||
try db.updateStatement("INSERT INTO persons (name, age) VALUES ('Barbara', ?);").execute()
|
||||
return .Commit
|
||||
}
|
||||
}
|
||||
@@ -39,8 +39,8 @@ class UpdateStatementTests : GRDBTestCase {
|
||||
XCTAssertEqual(rows.count, 4)
|
||||
XCTAssertEqual(rows[0].value(named: "name")! as String, "Arthur")
|
||||
XCTAssertEqual(rows[1].value(named: "name")! as String, "Arthur")
|
||||
XCTAssertEqual(rows[2].value(named: "name")! as String, "")
|
||||
XCTAssertEqual(rows[3].value(named: "name")! as String, "")
|
||||
XCTAssertEqual(rows[2].value(named: "name")! as String, "Barbara")
|
||||
XCTAssertEqual(rows[3].value(named: "name")! as String, "Barbara")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,9 +50,9 @@ class UpdateStatementTests : GRDBTestCase {
|
||||
try dbQueue.inTransaction { db in
|
||||
|
||||
let statement = db.updateStatement("INSERT INTO persons (name, age) VALUES (?, ?)")
|
||||
let persons = [
|
||||
let persons: [[DatabaseValueConvertible?]] = [
|
||||
["Arthur", 41],
|
||||
[""],
|
||||
["Barbara", nil],
|
||||
]
|
||||
for person in persons {
|
||||
try statement.execute(arguments: StatementArguments(person))
|
||||
@@ -66,7 +66,7 @@ class UpdateStatementTests : GRDBTestCase {
|
||||
XCTAssertEqual(rows.count, 2)
|
||||
XCTAssertEqual(rows[0].value(named: "name")! as String, "Arthur")
|
||||
XCTAssertEqual(rows[0].value(named: "age")! as Int, 41)
|
||||
XCTAssertEqual(rows[1].value(named: "name")! as String, "")
|
||||
XCTAssertEqual(rows[1].value(named: "name")! as String, "Barbara")
|
||||
XCTAssertTrue(rows[1].value(named: "age") == nil)
|
||||
}
|
||||
}
|
||||
@@ -78,9 +78,9 @@ class UpdateStatementTests : GRDBTestCase {
|
||||
try dbQueue.inTransaction { db in
|
||||
|
||||
let statement = db.updateStatement("INSERT INTO persons (name, age) VALUES (?, ?)")
|
||||
let persons = [
|
||||
let persons: [[DatabaseValueConvertible?]] = [
|
||||
["Arthur", 41],
|
||||
[""],
|
||||
["Barbara", nil],
|
||||
]
|
||||
for person in persons {
|
||||
statement.arguments = StatementArguments(person)
|
||||
@@ -95,7 +95,7 @@ class UpdateStatementTests : GRDBTestCase {
|
||||
XCTAssertEqual(rows.count, 2)
|
||||
XCTAssertEqual(rows[0].value(named: "name")! as String, "Arthur")
|
||||
XCTAssertEqual(rows[0].value(named: "age")! as Int, 41)
|
||||
XCTAssertEqual(rows[1].value(named: "name")! as String, "")
|
||||
XCTAssertEqual(rows[1].value(named: "name")! as String, "Barbara")
|
||||
XCTAssertTrue(rows[1].value(named: "age") == nil)
|
||||
}
|
||||
}
|
||||
@@ -107,9 +107,9 @@ class UpdateStatementTests : GRDBTestCase {
|
||||
try dbQueue.inTransaction { db in
|
||||
|
||||
let statement = db.updateStatement("INSERT INTO persons (name, age) VALUES (:name, :age)")
|
||||
let persons = [
|
||||
let persons: [[String: DatabaseValueConvertible?]] = [
|
||||
["name": "Arthur", "age": 41],
|
||||
["name": ""],
|
||||
["name": "Barbara", "age": nil],
|
||||
]
|
||||
for person in persons {
|
||||
try statement.execute(arguments: StatementArguments(person))
|
||||
@@ -123,7 +123,7 @@ class UpdateStatementTests : GRDBTestCase {
|
||||
XCTAssertEqual(rows.count, 2)
|
||||
XCTAssertEqual(rows[0].value(named: "name")! as String, "Arthur")
|
||||
XCTAssertEqual(rows[0].value(named: "age")! as Int, 41)
|
||||
XCTAssertEqual(rows[1].value(named: "name")! as String, "")
|
||||
XCTAssertEqual(rows[1].value(named: "name")! as String, "Barbara")
|
||||
XCTAssertTrue(rows[1].value(named: "age") == nil)
|
||||
}
|
||||
}
|
||||
@@ -135,9 +135,9 @@ class UpdateStatementTests : GRDBTestCase {
|
||||
try dbQueue.inTransaction { db in
|
||||
|
||||
let statement = db.updateStatement("INSERT INTO persons (name, age) VALUES (:name, :age)")
|
||||
let persons = [
|
||||
let persons: [[String: DatabaseValueConvertible?]] = [
|
||||
["name": "Arthur", "age": 41],
|
||||
["name": ""],
|
||||
["name": "Barbara", "age": nil],
|
||||
]
|
||||
for person in persons {
|
||||
statement.arguments = StatementArguments(person)
|
||||
@@ -152,7 +152,7 @@ class UpdateStatementTests : GRDBTestCase {
|
||||
XCTAssertEqual(rows.count, 2)
|
||||
XCTAssertEqual(rows[0].value(named: "name")! as String, "Arthur")
|
||||
XCTAssertEqual(rows[0].value(named: "age")! as Int, 41)
|
||||
XCTAssertEqual(rows[1].value(named: "name")! as String, "")
|
||||
XCTAssertEqual(rows[1].value(named: "name")! as String, "Barbara")
|
||||
XCTAssertTrue(rows[1].value(named: "age") == nil)
|
||||
}
|
||||
}
|
||||
|
||||
+62
-16
@@ -69,8 +69,60 @@ public class Statement {
|
||||
}
|
||||
}
|
||||
|
||||
// Exposed for StatementArguments. Don't make this one public unless we keep the arguments property in sync.
|
||||
final func bind(value: DatabaseValueConvertible?, atIndex index: Int) {
|
||||
// Not public until a need for it.
|
||||
final func reset() {
|
||||
let code = sqlite3_reset(sqliteStatement)
|
||||
if code != SQLITE_OK {
|
||||
fatalError(DatabaseError(code: code, message: database.lastErrorMessage, sql: sql).description)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Arguments
|
||||
|
||||
private var argumentCount: Int {
|
||||
return Int(sqlite3_bind_parameter_count(sqliteStatement))
|
||||
}
|
||||
|
||||
private lazy var sqliteArgumentNames: Set<String> = { [unowned self] in
|
||||
let sqliteStatement = self.sqliteStatement
|
||||
var argumentNames = Set<String>()
|
||||
for i in 1...self.argumentCount {
|
||||
if let name = String.fromCString(sqlite3_bind_parameter_name(sqliteStatement, Int32(i))) {
|
||||
argumentNames.insert(name)
|
||||
}
|
||||
}
|
||||
return argumentNames
|
||||
}()
|
||||
|
||||
// Exposed for StatementArguments.
|
||||
func validateArgumentCount(count: Int) {
|
||||
guard count == argumentCount else {
|
||||
fatalError("SQLite statement arguments mismatch: got \(count) argument(s) instead of \(argumentCount).")
|
||||
}
|
||||
}
|
||||
|
||||
// Exposed for StatementArguments.
|
||||
func validateCoveringArgumentKeys(keys: [String]) {
|
||||
let inputArgumentNames = keys.map { ":\($0)" }
|
||||
guard Set(inputArgumentNames) == sqliteArgumentNames else {
|
||||
func caseInsensitiveSort(strings: [String]) -> [String] {
|
||||
return strings
|
||||
.map { ($0.lowercaseString, $0) }
|
||||
.sort { $0.0 < $1.0 }
|
||||
.map { $0.1 }
|
||||
}
|
||||
let input = caseInsensitiveSort(inputArgumentNames).joinWithSeparator(",")
|
||||
let expected = caseInsensitiveSort(Array(sqliteArgumentNames)).joinWithSeparator(",")
|
||||
fatalError("SQLite statement argument names mismatch: got [\(input)] instead of [\(expected)].")
|
||||
}
|
||||
}
|
||||
|
||||
// Exposed for StatementArguments. Don't make this one public unless we keep
|
||||
// the arguments property in sync.
|
||||
//
|
||||
// As in sqlite3_bind_xxx methods, index is one-based.
|
||||
final func setArgument(value: DatabaseValueConvertible?, atIndex index: Int) {
|
||||
let databaseValue = value?.databaseValue ?? .Null
|
||||
let code: Int32
|
||||
|
||||
@@ -92,24 +144,18 @@ public class Statement {
|
||||
}
|
||||
}
|
||||
|
||||
// Exposed for StatementArguments. Don't make this one public unless we keep the arguments property in sync.
|
||||
final func bind(value: DatabaseValueConvertible?, forKey key: String) {
|
||||
let index = Int(sqlite3_bind_parameter_index(sqliteStatement, ":\(key)"))
|
||||
// Exposed for StatementArguments. Don't make this one public unless we keep
|
||||
// the arguments property in sync.
|
||||
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("Key not found in SQLite statement: `:\(key)`")
|
||||
fatalError("Argument not found in SQLite statement: `\(argumentName)`")
|
||||
}
|
||||
bind(value, atIndex: index)
|
||||
setArgument(value, atIndex: index)
|
||||
}
|
||||
|
||||
// Not public until a need for it.
|
||||
final func reset() {
|
||||
let code = sqlite3_reset(sqliteStatement)
|
||||
if code != SQLITE_OK {
|
||||
fatalError(DatabaseError(code: code, message: database.lastErrorMessage, sql: sql).description)
|
||||
}
|
||||
}
|
||||
|
||||
// Don't make this one public or internal unless we keep the arguments property in sync.
|
||||
// Don't make this one public unless we keep the arguments property in sync.
|
||||
private func clearArguments() {
|
||||
let code = sqlite3_clear_bindings(sqliteStatement)
|
||||
if code != SQLITE_OK {
|
||||
|
||||
@@ -88,8 +88,9 @@ public struct StatementArguments {
|
||||
}
|
||||
|
||||
func bindInStatement(statement: Statement) {
|
||||
statement.validateArgumentCount(values.count)
|
||||
for (index, value) in values.enumerate() {
|
||||
statement.bind(value, atIndex: index + 1)
|
||||
statement.setArgument(value, atIndex: index + 1)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,8 +121,9 @@ public struct StatementArguments {
|
||||
}
|
||||
|
||||
func bindInStatement(statement: Statement) {
|
||||
statement.validateCoveringArgumentKeys(Array(dictionary.keys))
|
||||
for (key, value) in dictionary {
|
||||
statement.bind(value, forKey: key)
|
||||
statement.setArgument(value, forKey: key) // crash if key is not found
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
- [ ] Metal: How does the code look like when one iterates metal row and extracts NSData without copy?
|
||||
- [ ] Update or delete the "Value Extraction in Details" paragraph in README.md
|
||||
- [ ] Talk about SQLiteStatementConvertible. Maybe in a special "Performance" section.
|
||||
- [ ] Statement arguments: crash when there are missing arguments. This will prevent another https://github.com/stephencelis/SQLite.swift/issues/232, because one argument was missing in the arguments array.
|
||||
|
||||
Not sure:
|
||||
|
||||
|
||||
Reference in new issue
Block a user