mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
SelectStatement is no longer a Row sequence
This commit is contained in:
1 parent
cf58123725
commit
b7d5c40595
5 files changed
+36
-45
No files matched your search
@@ -25,7 +25,7 @@ class DatabaseTests: XCTestCase {
|
||||
insert1Stmt.bind("Arthur", atIndex: 1)
|
||||
insert1Stmt.bind(36, atIndex: 2)
|
||||
try insert1Stmt.execute()
|
||||
insert1Stmt.reset()
|
||||
try insert1Stmt.reset()
|
||||
insert1Stmt.bind("Arthur no age", atIndex: 1)
|
||||
insert1Stmt.bind(nil, atIndex: 2)
|
||||
try insert1Stmt.execute()
|
||||
@@ -33,7 +33,7 @@ class DatabaseTests: XCTestCase {
|
||||
let insert2Stmt = try database.updateStatement("INSERT INTO persons (name, age) VALUES (?, ?)")
|
||||
insert2Stmt.bind(["Zoe", 65])
|
||||
try insert2Stmt.execute()
|
||||
insert2Stmt.reset()
|
||||
try insert2Stmt.reset()
|
||||
insert2Stmt.bind(["Zoe no age", nil])
|
||||
try insert2Stmt.execute()
|
||||
|
||||
@@ -41,7 +41,7 @@ class DatabaseTests: XCTestCase {
|
||||
insert3Stmt.bind("Beate", forKey: ":name")
|
||||
insert3Stmt.bind(37, forKey: ":age")
|
||||
try insert3Stmt.execute()
|
||||
insert3Stmt.reset()
|
||||
try insert3Stmt.reset()
|
||||
insert3Stmt.bind("Beate no age", forKey: ":name")
|
||||
insert3Stmt.bind(nil, forKey: ":age")
|
||||
try insert3Stmt.execute()
|
||||
@@ -49,12 +49,18 @@ class DatabaseTests: XCTestCase {
|
||||
let insert4Stmt = try database.updateStatement("INSERT INTO persons (name, age) VALUES (:name, :age)")
|
||||
insert4Stmt.bind([":name": "Crystel", ":age": 16])
|
||||
try insert4Stmt.execute()
|
||||
insert4Stmt.reset()
|
||||
try insert4Stmt.reset()
|
||||
insert4Stmt.bind([":name": "Crystel no age", ":age": nil])
|
||||
try insert4Stmt.execute()
|
||||
|
||||
let rows = try database.selectStatement("SELECT * FROM persons")
|
||||
for row in rows {
|
||||
let selectStmt = try database.selectStatement("SELECT * FROM persons")
|
||||
for row in selectStmt.rows {
|
||||
let name = row.stringAtIndex(0)
|
||||
let age = row.intAtIndex(1)
|
||||
print("\(name): \(age)")
|
||||
print("\(row.asDictionary)")
|
||||
}
|
||||
for row in selectStmt.rows {
|
||||
let name = row.stringAtIndex(0)
|
||||
let age = row.intAtIndex(1)
|
||||
print("\(name): \(age)")
|
||||
|
||||
+4
-4
@@ -25,11 +25,11 @@ public class Database {
|
||||
}
|
||||
}
|
||||
|
||||
public func selectStatement(query: String) throws -> SelectStatement {
|
||||
return try SelectStatement(cConnection: cConnection, query: query)
|
||||
public func selectStatement(sql: String) throws -> SelectStatement {
|
||||
return try SelectStatement(database: self, sql: sql)
|
||||
}
|
||||
|
||||
public func updateStatement(query: String) throws -> UpdateStatement {
|
||||
return try UpdateStatement(cConnection: cConnection, query: query)
|
||||
public func updateStatement(sql: String) throws -> UpdateStatement {
|
||||
return try UpdateStatement(database: self, sql: sql)
|
||||
}
|
||||
}
|
||||
+11
-25
@@ -6,42 +6,28 @@
|
||||
// Copyright © 2015 Gwendal Roué. All rights reserved.
|
||||
//
|
||||
|
||||
public class SelectStatement : Statement, SequenceType {
|
||||
public class SelectStatement : Statement {
|
||||
public lazy var columnCount: Int = Int(sqlite3_column_count(self.cStatement))
|
||||
|
||||
private var _rowGenerator: RowGenerator?
|
||||
|
||||
public func generate() -> RowGenerator {
|
||||
if let _rowGenerator = _rowGenerator {
|
||||
return _rowGenerator
|
||||
// Document the reset performed on each generation
|
||||
public var rows: AnySequence<Row> {
|
||||
return AnySequence {
|
||||
return self.rowGenerator()
|
||||
}
|
||||
_rowGenerator = RowGenerator(statement: self)
|
||||
return _rowGenerator!
|
||||
}
|
||||
|
||||
override public func reset() {
|
||||
super.reset()
|
||||
_rowGenerator = nil
|
||||
}
|
||||
|
||||
public class RowGenerator : GeneratorType {
|
||||
let statement: SelectStatement
|
||||
private func rowGenerator() -> AnyGenerator<Row> {
|
||||
try! reset()
|
||||
|
||||
init(statement: SelectStatement) {
|
||||
self.statement = statement
|
||||
}
|
||||
|
||||
public func next() -> Row? {
|
||||
let code = sqlite3_step(statement.cStatement)
|
||||
return anyGenerator { () -> Row? in
|
||||
let code = sqlite3_step(self.cStatement)
|
||||
switch code {
|
||||
case SQLITE_DONE:
|
||||
// the statement has finished executing successfully
|
||||
return nil
|
||||
case SQLITE_ROW:
|
||||
// each time a new row of data is ready for processing by the caller.
|
||||
return Row(statement: statement)
|
||||
return Row(statement: self)
|
||||
default:
|
||||
try! Error.checkCResultCode(code, cConnection: statement.cConnection)
|
||||
try! Error.checkCResultCode(code, cConnection: self.database.cConnection)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,16 +9,16 @@
|
||||
typealias CStatement = COpaquePointer
|
||||
|
||||
public class Statement {
|
||||
let database: Database
|
||||
let cStatement = CStatement()
|
||||
let cConnection: CConnection
|
||||
|
||||
init(cConnection: CConnection, query: String) throws {
|
||||
init(database: Database, sql: String) throws {
|
||||
// See https://www.sqlite.org/c3ref/prepare.html
|
||||
self.cConnection = cConnection
|
||||
let code = query.nulTerminatedUTF8.withUnsafeBufferPointer { codeUnits in
|
||||
sqlite3_prepare_v2(cConnection, UnsafePointer<Int8>(codeUnits.baseAddress), -1, &cStatement, nil)
|
||||
self.database = database
|
||||
let code = sql.nulTerminatedUTF8.withUnsafeBufferPointer { codeUnits in
|
||||
sqlite3_prepare_v2(database.cConnection, UnsafePointer<Int8>(codeUnits.baseAddress), -1, &cStatement, nil)
|
||||
}
|
||||
try Error.checkCResultCode(code, cConnection: cConnection)
|
||||
try Error.checkCResultCode(code, cConnection: database.cConnection)
|
||||
}
|
||||
|
||||
deinit {
|
||||
@@ -58,9 +58,9 @@ public class Statement {
|
||||
}
|
||||
}
|
||||
|
||||
public func reset() {
|
||||
public func reset() throws {
|
||||
let code = sqlite3_reset(cStatement)
|
||||
assert(code == SQLITE_OK)
|
||||
try Error.checkCResultCode(code, cConnection: database.cConnection)
|
||||
}
|
||||
|
||||
public func clearBindings() {
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
//
|
||||
|
||||
public class UpdateStatement : Statement {
|
||||
|
||||
public lazy var lastInsertedRowID: Int64 = sqlite3_last_insert_rowid(self.cStatement)
|
||||
|
||||
public func execute() throws {
|
||||
@@ -17,7 +16,7 @@ public class UpdateStatement : Statement {
|
||||
// the statement has finished executing successfully
|
||||
break
|
||||
default:
|
||||
try Error.checkCResultCode(code, cConnection: cConnection)
|
||||
try Error.checkCResultCode(code, cConnection: database.cConnection)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user