diff --git a/GRDB Tests/Database/DatabaseTests.swift b/GRDB Tests/Database/DatabaseTests.swift index 46415a99e..9760a79bd 100644 --- a/GRDB Tests/Database/DatabaseTests.swift +++ b/GRDB Tests/Database/DatabaseTests.swift @@ -41,42 +41,6 @@ class DatabaseTests: GRDBTests { } } - func testUpdateStatementIndexedBinding() { - assertNoError { - try dbQueue.inDatabase { db in - try db.execute("CREATE TABLE persons (name TEXT, age INT)") - - let statement = try db.updateStatement("INSERT INTO persons (name, age) VALUES (?, ?)") - // The tested function: - statement.bind("Arthur", atIndex: 1) - statement.bind(41, atIndex: 2) - try statement.execute() - - let row = db.fetchOneRow("SELECT * FROM persons")! - XCTAssertEqual(row.value(atIndex: 0)! as String, "Arthur") - XCTAssertEqual(row.value(atIndex: 1)! as Int, 41) - } - } - } - - func testUpdateStatementKeyedBinding() { - assertNoError { - try dbQueue.inDatabase { db in - try db.execute("CREATE TABLE persons (name TEXT, age INT)") - - let statement = try db.updateStatement("INSERT INTO persons (name, age) VALUES (:name, :age)") - // The tested function: - statement.bind("Arthur", forKey: "name") - statement.bind(41, forKey: "age") - try statement.execute() - - let row = db.fetchOneRow("SELECT * FROM persons")! - XCTAssertEqual(row.value(atIndex: 0)! as String, "Arthur") - XCTAssertEqual(row.value(atIndex: 1)! as Int, 41) - } - } - } - func testUpdateStatementArrayBinding() { assertNoError { try dbQueue.inDatabase { db in @@ -84,7 +48,7 @@ class DatabaseTests: GRDBTests { let statement = try db.updateStatement("INSERT INTO persons (name, age) VALUES (?, ?)") // The tested function: - statement.bind(["Arthur", 41]) + statement.bindings = ["Arthur", 41] try statement.execute() let row = db.fetchOneRow("SELECT * FROM persons")! @@ -101,7 +65,7 @@ class DatabaseTests: GRDBTests { let statement = try db.updateStatement("INSERT INTO persons (name, age) VALUES (:name, :age)") // The tested function: - statement.bind(["name": "Arthur", "age": 41]) + statement.bindings = ["name": "Arthur", "age": 41] try statement.execute() let row = db.fetchOneRow("SELECT * FROM persons")! @@ -204,40 +168,6 @@ class DatabaseTests: GRDBTests { } } - func testSelectStatementIndexedBinding() { - assertNoError { - 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)", bindings: ["name": "Arthur", "age": 41]) - try db.execute("INSERT INTO persons (name, age) VALUES (:name, :age)", bindings: ["name": "Barbara"]) - - let statement = try db.selectStatement("SELECT * FROM persons WHERE name = ?") - // The tested function: - statement.bind("Arthur", atIndex: 1) - - let rows = statement.fetchAllRows() - XCTAssertEqual(rows.count, 1) - } - } - } - - func testSelectStatementKeyedBinding() { - assertNoError { - 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)", bindings: ["name": "Arthur", "age": 41]) - try db.execute("INSERT INTO persons (name, age) VALUES (:name, :age)", bindings: ["name": "Barbara"]) - - let statement = try db.selectStatement("SELECT * FROM persons WHERE name = :name") - // The tested function: - statement.bind("Arthur", forKey: "name") - - let rows = statement.fetchAllRows() - XCTAssertEqual(rows.count, 1) - } - } - } - func testSelectStatementArrayBinding() { assertNoError { try dbQueue.inDatabase { db in @@ -247,7 +177,7 @@ class DatabaseTests: GRDBTests { let statement = try db.selectStatement("SELECT * FROM persons WHERE name = ?") // The tested function: - statement.bind(["Arthur"]) + statement.bindings = ["Arthur"] let rows = statement.fetchAllRows() XCTAssertEqual(rows.count, 1) @@ -264,7 +194,7 @@ class DatabaseTests: GRDBTests { let statement = try db.selectStatement("SELECT * FROM persons WHERE name = :name") // The tested function: - statement.bind(["name": "Arthur"]) + statement.bindings = ["name": "Arthur"] let rows = statement.fetchAllRows() XCTAssertEqual(rows.count, 1) diff --git a/GRDB Tests/Statement/SelectStatementTests.swift b/GRDB Tests/Statement/SelectStatementTests.swift index c9069a177..515c8818c 100644 --- a/GRDB Tests/Statement/SelectStatementTests.swift +++ b/GRDB Tests/Statement/SelectStatementTests.swift @@ -43,9 +43,7 @@ class SelectStatementTests: GRDBTests { let statement = try db.selectStatement("SELECT COUNT(*) FROM persons WHERE age < ?") let ages = [20, 30, 40, 50] let counts = ages.map { age -> Int in - statement.reset() - statement.clearBindings() - statement.bind([age]) + statement.bindings = [age] return statement.fetchOne(Int.self)! } XCTAssertEqual(counts, [1,2,2,3]) @@ -60,11 +58,9 @@ class SelectStatementTests: GRDBTests { let statement = try db.selectStatement("SELECT COUNT(*) FROM persons WHERE age < :age") // TODO: why is this explicit type declaration required? - let ages: [[String: DatabaseValueType?]] = [["age": 20], ["age": 30], ["age": 40], ["age": 50]] - let counts = ages.map { age -> Int in - statement.reset() - statement.clearBindings() - statement.bind(Bindings(age)) + let ageDicts: [[String: DatabaseValueType?]] = [["age": 20], ["age": 30], ["age": 40], ["age": 50]] + let counts = ageDicts.map { ageDict -> Int in + statement.bindings = Bindings(ageDict) return statement.fetchOne(Int.self)! } XCTAssertEqual(counts, [1,2,2,3]) diff --git a/GRDB Tests/Statement/UpdateStatementTests.swift b/GRDB Tests/Statement/UpdateStatementTests.swift index db8afea4f..68b86bc22 100644 --- a/GRDB Tests/Statement/UpdateStatementTests.swift +++ b/GRDB Tests/Statement/UpdateStatementTests.swift @@ -42,9 +42,7 @@ class UpdateStatementTests: GRDBTests { ["Barbara"], ] for person in persons { - statement.reset() - statement.clearBindings() - statement.bind(Bindings(person)) + statement.bindings = Bindings(person) try statement.execute() } @@ -73,9 +71,7 @@ class UpdateStatementTests: GRDBTests { ["name": "Barbara"], ] for person in persons { - statement.reset() - statement.clearBindings() - statement.bind(Bindings(person)) + statement.bindings = Bindings(person) try statement.execute() } diff --git a/GRDB/SelectStatement.swift b/GRDB/SelectStatement.swift index 69d976f1d..7c41ed0c4 100644 --- a/GRDB/SelectStatement.swift +++ b/GRDB/SelectStatement.swift @@ -38,10 +38,7 @@ public final class SelectStatement : Statement { extension SelectStatement { - // TODO: document the reset() public func fetchRows() -> AnySequence { - reset() - return AnySequence { () -> AnyGenerator in var logSQL = self.database.configuration.verbose return anyGenerator { () -> Row? in diff --git a/GRDB/Statement.swift b/GRDB/Statement.swift index 700464e95..e4831d733 100644 --- a/GRDB/Statement.swift +++ b/GRDB/Statement.swift @@ -11,6 +11,15 @@ public class Statement { let sqliteStatement = SQLiteStatement() let databaseQueueID: DatabaseQueueID public lazy var sql: String = String.fromCString(UnsafePointer(sqlite3_sql(self.sqliteStatement)))! + public var bindings: Bindings? { + didSet { + reset() // necessary before applying new bindings + clearBindings() + if let bindings = bindings { + bindings.bindInStatement(self) + } + } + } init(database: Database, sql: String, bindings: Bindings?) throws { // See https://www.sqlite.org/c3ref/prepare.html @@ -18,7 +27,13 @@ public class Statement { self.databaseQueueID = dispatch_get_specific(DatabaseQueue.databaseQueueIDKey) let code = sqlite3_prepare_v2(database.sqliteConnection, sql, -1, &sqliteStatement, nil) try SQLiteError.checkCResultCode(code, sqliteConnection: database.sqliteConnection, sql: sql) - bind(bindings) + + // Set bingins. Duplicate the didSet property observer since it is not + // called during initialization. + self.bindings = bindings + if let bindings = bindings { + bindings.bindInStatement(self) + } } deinit { @@ -27,7 +42,8 @@ public class Statement { } } - public final func bind(value: DatabaseValueType?, atIndex index: Int) { + // Exposed for Bindings. Don't make this one public unless we keep the bindings property in sync. + final func bind(value: DatabaseValueType?, atIndex index: Int) { let code: Int32 if let value = value { code = value.bindInSQLiteStatement(sqliteStatement, atIndex: index) @@ -42,7 +58,8 @@ public class Statement { } // TODO: document that we only support the colon prefix (like FMDB). - public final func bind(value: DatabaseValueType?, forKey key: String) { + // Exposed for Bindings. Don't make this one public unless we keep the bindings property in sync. + final func bind(value: DatabaseValueType?, forKey key: String) { let index = Int(sqlite3_bind_parameter_index(sqliteStatement, ":\(key)")) guard index > 0 else { fatalError("Key not found in SQLite statement: `:\(key)`") @@ -50,13 +67,9 @@ public class Statement { bind(value, atIndex: index) } - public final func bind(bindings: Bindings?) { - if let bindings = bindings { - bindings.bindInStatement(self) - } - } - - public final func reset() { + // Not public until a need for it. + // Today the only place where a statement is reset is in the bindings didSet observer. + final func reset() { let code = sqlite3_reset(sqliteStatement) if code != SQLITE_OK { failOnError { () -> Void in @@ -65,7 +78,8 @@ public class Statement { } } - public final func clearBindings() { + // Don't make this one public or internal unless we keep the bindings property in sync. + private func clearBindings() { let code = sqlite3_clear_bindings(sqliteStatement) if code != SQLITE_OK { failOnError { () -> Void in diff --git a/README.md b/README.md index 9c9c32a95..6c25895a7 100644 --- a/README.md +++ b/README.md @@ -228,14 +228,7 @@ try dbQueue.inTransaction { db in ] for person in persons { - // Ready for next execution - statement.reset() - - // Forget previously bound values - statement.clearBindings() - - // Execute with new values - statement.bind(Bindings(person)) + statement.bindings = Bindings(person) try statement.execute() }