mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-29 04:24:21 +00:00
Bind values
This commit is contained in:
1 parent
e82453a266
commit
7f3d1f516e
2 files changed
+46
-7
No files matched your search
@@ -25,11 +25,33 @@ class DatabaseTests: XCTestCase {
|
||||
insert1Stmt.bind("Arthur", atIndex: 1)
|
||||
insert1Stmt.bind(36, atIndex: 2)
|
||||
try insert1Stmt.executeUpdate()
|
||||
insert1Stmt.reset()
|
||||
insert1Stmt.bind("Arthur no age", atIndex: 1)
|
||||
insert1Stmt.bind(nil, atIndex: 2)
|
||||
try insert1Stmt.executeUpdate()
|
||||
|
||||
let insert2Stmt = try database.updateStatementWithQuery("INSERT INTO persons (name, age) VALUES (:name, :age)")
|
||||
insert2Stmt.bind("Beate", forKey: ":name")
|
||||
insert2Stmt.bind(37, forKey: ":age")
|
||||
let insert2Stmt = try database.updateStatementWithQuery("INSERT INTO persons (name, age) VALUES (?, ?)")
|
||||
insert2Stmt.bind(["Zoe", 65])
|
||||
try insert2Stmt.executeUpdate()
|
||||
insert2Stmt.reset()
|
||||
insert2Stmt.bind(["Zoe no age", nil])
|
||||
try insert2Stmt.executeUpdate()
|
||||
|
||||
let insert3Stmt = try database.updateStatementWithQuery("INSERT INTO persons (name, age) VALUES (:name, :age)")
|
||||
insert3Stmt.bind("Beate", forKey: ":name")
|
||||
insert3Stmt.bind(37, forKey: ":age")
|
||||
try insert3Stmt.executeUpdate()
|
||||
insert3Stmt.reset()
|
||||
insert3Stmt.bind("Beate no age", forKey: ":name")
|
||||
insert3Stmt.bind(nil, forKey: ":age")
|
||||
try insert3Stmt.executeUpdate()
|
||||
|
||||
let insert4Stmt = try database.updateStatementWithQuery("INSERT INTO persons (name, age) VALUES (:name, :age)")
|
||||
insert4Stmt.bind([":name": "Crystel", ":age": 16])
|
||||
try insert4Stmt.executeUpdate()
|
||||
insert4Stmt.reset()
|
||||
insert4Stmt.bind([":name": "Crystel no age", ":age": nil])
|
||||
try insert4Stmt.executeUpdate()
|
||||
|
||||
let rows = try database.rowSequenceWithQuery("SELECT * FROM persons")
|
||||
for row in rows {
|
||||
|
||||
+21
-4
@@ -32,18 +32,35 @@ public class Statement {
|
||||
public lazy var columnCount: Int = Int(sqlite3_column_count(self.cStatement))
|
||||
public lazy var bindParameterCount: Int = Int(sqlite3_bind_parameter_count(self.cStatement))
|
||||
|
||||
public func bind(value: DBValue, atIndex index: Int) {
|
||||
value.bindInStatement(self, atIndex: index)
|
||||
public func bind(value: DBValue?, atIndex index: Int) {
|
||||
if let value = value {
|
||||
value.bindInStatement(self, atIndex: index)
|
||||
} else {
|
||||
let code = sqlite3_bind_null(cStatement, Int32(index))
|
||||
assert(code == SQLITE_OK)
|
||||
}
|
||||
}
|
||||
|
||||
public func bind(value: DBValue, forKey key: String) {
|
||||
public func bind(value: DBValue?, forKey key: String) {
|
||||
let index = key.nulTerminatedUTF8.withUnsafeBufferPointer { codeUnits in
|
||||
Int(sqlite3_bind_parameter_index(cStatement, UnsafePointer<Int8>(codeUnits.baseAddress)))
|
||||
}
|
||||
guard index > 0 else {
|
||||
fatalError("Key not found: \(key)")
|
||||
}
|
||||
value.bindInStatement(self, atIndex: index)
|
||||
bind(value, atIndex: index)
|
||||
}
|
||||
|
||||
public func bind(dictionary: [String: DBValue?]) {
|
||||
for (key, value) in dictionary {
|
||||
bind(value, forKey: key)
|
||||
}
|
||||
}
|
||||
|
||||
public func bind(values: [DBValue?]) {
|
||||
for (index, value) in values.enumerate() {
|
||||
bind(value, atIndex: index + 1)
|
||||
}
|
||||
}
|
||||
|
||||
public func reset() {
|
||||
|
||||
Reference in new issue
Block a user