diff --git a/GRDB Tests/DatabaseTests.swift b/GRDB Tests/DatabaseTests.swift index 05cc64af5..240132d62 100644 --- a/GRDB Tests/DatabaseTests.swift +++ b/GRDB Tests/DatabaseTests.swift @@ -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)") diff --git a/GRDB/Row.swift b/GRDB/Row.swift index 81578cd82..802100f45 100644 --- a/GRDB/Row.swift +++ b/GRDB/Row.swift @@ -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(sqlite3_column_text(statement.cStatement, Int32(index))) + let cString = UnsafePointer(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(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.. 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 } }