Replace the Statement methods bind(), reset() and clearBindings() with the bindings property.

This commit is contained in:
Gwendal Roué committed 2015-07-03 20:12:16 +02:00
1 parent 35a50d610d
commit 526840bca1
6 files changed
+36 -110

No files matched your search

+4 -74
View File
@@ -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)
@@ -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])
@@ -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()
}
-3
View File
@@ -38,10 +38,7 @@ public final class SelectStatement : Statement {
extension SelectStatement {
// TODO: document the reset()
public func fetchRows() -> AnySequence<Row> {
reset()
return AnySequence { () -> AnyGenerator<Row> in
var logSQL = self.database.configuration.verbose
return anyGenerator { () -> Row? in
+25 -11
View File
@@ -11,6 +11,15 @@ public class Statement {
let sqliteStatement = SQLiteStatement()
let databaseQueueID: DatabaseQueueID
public lazy var sql: String = String.fromCString(UnsafePointer<Int8>(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
+1 -8
View File
@@ -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()
}