Row.asDictionary

This commit is contained in:
Gwendal Roué committed 2015-06-30 18:34:14 +02:00
1 parent 627938a807
commit ced9a43ee3
3 files changed
+45 -18

No files matched your search

+1
View File
@@ -58,6 +58,7 @@ class DatabaseTests: XCTestCase {
let name = row.stringAtIndex(0)
let age = row.intAtIndex(1)
print("\(name): \(age)")
print("\(row.asDictionary)")
}
} catch let error as GRDB.Error {
print("Error \(error._code), \(error.message)")
+37 -11
View File
@@ -7,46 +7,72 @@
//
public struct Row {
let statement: Statement
let rowSequence: RowSequence
init(statement: Statement) {
self.statement = statement
init(rowSequence: RowSequence) {
self.rowSequence = rowSequence
}
public func intAtIndex(index: Int) -> Int? {
switch sqlite3_column_type(statement.cStatement, Int32(index)) {
switch sqlite3_column_type(rowSequence.cStatement, Int32(index)) {
case SQLITE_NULL:
return nil
default:
return Int(sqlite3_column_int(statement.cStatement, Int32(index)))
return Int(sqlite3_column_int(rowSequence.cStatement, Int32(index)))
}
}
public func int64AtIndex(index: Int) -> Int64? {
switch sqlite3_column_type(statement.cStatement, Int32(index)) {
switch sqlite3_column_type(rowSequence.cStatement, Int32(index)) {
case SQLITE_NULL:
return nil
default:
return sqlite3_column_int64(statement.cStatement, Int32(index))
return sqlite3_column_int64(rowSequence.cStatement, Int32(index))
}
}
public func doubleAtIndex(index: Int) -> Double? {
switch sqlite3_column_type(statement.cStatement, Int32(index)) {
switch sqlite3_column_type(rowSequence.cStatement, Int32(index)) {
case SQLITE_NULL:
return nil;
default:
return sqlite3_column_double(statement.cStatement, Int32(index))
return sqlite3_column_double(rowSequence.cStatement, Int32(index))
}
}
public func stringAtIndex(index: Int) -> String? {
switch sqlite3_column_type(statement.cStatement, Int32(index)) {
switch sqlite3_column_type(rowSequence.cStatement, Int32(index)) {
case SQLITE_NULL:
return nil;
default:
let cString = UnsafePointer<Int8>(sqlite3_column_text(statement.cStatement, Int32(index)))
let cString = UnsafePointer<Int8>(sqlite3_column_text(rowSequence.cStatement, Int32(index)))
return String.fromCString(cString)!
}
}
public func valueAtIndex(index: Int) -> DBValue? {
switch sqlite3_column_type(rowSequence.cStatement, Int32(index)) {
case SQLITE_NULL:
return nil;
case SQLITE_INTEGER:
return sqlite3_column_int64(rowSequence.cStatement, Int32(index))
case SQLITE_FLOAT:
return sqlite3_column_double(rowSequence.cStatement, Int32(index))
case SQLITE_TEXT:
let cString = UnsafePointer<Int8>(sqlite3_column_text(rowSequence.cStatement, Int32(index)))
return String.fromCString(cString)!
default:
fatalError("Not implemented")
}
}
public var asDictionary: [String: DBValue?] {
var dictionary = [String: DBValue?]()
for index in 0..<rowSequence.columnCount {
let cString = sqlite3_column_name(rowSequence.cStatement, Int32(index))
let key = String.fromCString(cString)!
dictionary[key] = valueAtIndex(index)
}
return dictionary
}
}
+7 -7
View File
@@ -15,7 +15,7 @@ public class RowSequence : Statement, SequenceType {
if let _rowGenerator = _rowGenerator {
return _rowGenerator
}
_rowGenerator = RowGenerator(statement: self)
_rowGenerator = RowGenerator(rowSequence: self)
return _rowGenerator!
}
@@ -25,23 +25,23 @@ public class RowSequence : Statement, SequenceType {
}
public class RowGenerator : GeneratorType {
let statement: Statement
let rowSequence: RowSequence
init(statement: Statement) {
self.statement = statement
init(rowSequence: RowSequence) {
self.rowSequence = rowSequence
}
public func next() -> Row? {
let code = sqlite3_step(statement.cStatement)
let code = sqlite3_step(rowSequence.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(rowSequence: rowSequence)
default:
try! Error.checkCResultCode(code, cConnection: statement.cConnection)
try! Error.checkCResultCode(code, cConnection: rowSequence.cConnection)
return nil
}
}