mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
Row.metalFetch(), the base for future Int.metalFetch() and Person.metalFetch()
This commit is contained in:
1 parent
1254f0e91a
commit
4b049eefda
3 files changed
+124
-7
No files matched your search
@@ -18,7 +18,7 @@ class FetchPerformanceTests: XCTestCase {
|
||||
//
|
||||
// }
|
||||
|
||||
func testMetalFetchPerformance() {
|
||||
func testMetalRowFetchPerformance() {
|
||||
let databasePath = NSBundle(forClass: self.dynamicType).pathForResource("FetchPerformanceTests", ofType: "sqlite")!
|
||||
let dbQueue = try! DatabaseQueue(path: databasePath)
|
||||
|
||||
@@ -38,6 +38,26 @@ class FetchPerformanceTests: XCTestCase {
|
||||
XCTAssertEqual(sum, 4999990)
|
||||
}
|
||||
|
||||
func testRowMetalFetchPerformance() {
|
||||
let databasePath = NSBundle(forClass: self.dynamicType).pathForResource("FetchPerformanceTests", ofType: "sqlite")!
|
||||
let dbQueue = try! DatabaseQueue(path: databasePath)
|
||||
|
||||
var sum: Int64 = 0
|
||||
self.measureBlock {
|
||||
dbQueue.inDatabase { db in
|
||||
for row in Row.metalFetch(db, "SELECT * FROM items") {
|
||||
let i0: Int64 = row.value(atIndex: 0)
|
||||
let i1: Int64 = row.value(atIndex: 1)
|
||||
let i2: Int64 = row.value(atIndex: 2)
|
||||
let i3: Int64 = row.value(atIndex: 3)
|
||||
let i4: Int64 = row.value(atIndex: 4)
|
||||
sum += i0 + i1 + i2 + i3 + i4
|
||||
}
|
||||
}
|
||||
}
|
||||
XCTAssertEqual(sum, 4999990)
|
||||
}
|
||||
|
||||
func testRegularFetchPerformance() {
|
||||
let databasePath = NSBundle(forClass: self.dynamicType).pathForResource("FetchPerformanceTests", ofType: "sqlite")!
|
||||
let dbQueue = try! DatabaseQueue(path: databasePath)
|
||||
|
||||
+102
-5
@@ -92,10 +92,34 @@ public struct Row: CollectionType {
|
||||
return Value.fromDatabaseValue(impl.databaseValue(atIndex: index))
|
||||
}
|
||||
|
||||
public func value<Value: protocol<DatabaseValueConvertible, MetalType>>(atIndex index: Int) -> Value? {
|
||||
let sqliteStatement = impl.sqliteStatement
|
||||
guard sqliteStatement != nil else {
|
||||
return Value.fromDatabaseValue(impl.databaseValue(atIndex: index))
|
||||
}
|
||||
|
||||
// Metal
|
||||
if sqlite3_column_type(sqliteStatement, Int32(index)) == SQLITE_NULL {
|
||||
return nil
|
||||
} else {
|
||||
return Value(sqliteStatement: sqliteStatement, index: Int32(index))
|
||||
}
|
||||
}
|
||||
|
||||
public func value<Value: DatabaseValueConvertible>(atIndex index: Int) -> Value {
|
||||
return Value.fromDatabaseValue(impl.databaseValue(atIndex: index))!
|
||||
}
|
||||
|
||||
public func value<Value: protocol<DatabaseValueConvertible, MetalType>>(atIndex index: Int) -> Value {
|
||||
let sqliteStatement = impl.sqliteStatement
|
||||
guard sqliteStatement != nil else {
|
||||
return Value.fromDatabaseValue(impl.databaseValue(atIndex: index))!
|
||||
}
|
||||
|
||||
// Metal
|
||||
return Value(sqliteStatement: sqliteStatement, index: Int32(index))
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Returns the value for the given column.
|
||||
@@ -162,7 +186,12 @@ public struct Row: CollectionType {
|
||||
*/
|
||||
public func value<Value: DatabaseValueConvertible>(named columnName: String) -> Value? {
|
||||
let index = impl.indexForColumn(named: columnName)!
|
||||
return impl.databaseValue(atIndex: index).value()
|
||||
return Value.fromDatabaseValue(impl.databaseValue(atIndex: index))
|
||||
}
|
||||
|
||||
public func value<Value: DatabaseValueConvertible>(named columnName: String) -> Value {
|
||||
let index = impl.indexForColumn(named: columnName)!
|
||||
return Value.fromDatabaseValue(impl.databaseValue(atIndex: index))!
|
||||
}
|
||||
|
||||
|
||||
@@ -302,6 +331,38 @@ public struct Row: CollectionType {
|
||||
|
||||
// MARK: - Fetching From Database
|
||||
|
||||
/**
|
||||
Returns a row generator that provides fast but unsafe access to database
|
||||
values:
|
||||
|
||||
for row in MetalRow.fetch(db, "SELECT id, name FROM persons") {
|
||||
let id = row.int64(atIndex: 0)
|
||||
let name = row.string(atIndex: 1)
|
||||
}
|
||||
|
||||
The returned generator is *unsafe* because it must be used with extra care,
|
||||
and GRDB.swift will not prevent invalid usage.
|
||||
|
||||
- It MUST be generated and iterated in the database queue.
|
||||
|
||||
- It MUST be iterated right away. Do not reserve it, do not wrap it in an
|
||||
Array.
|
||||
|
||||
Granted with those constraints, the unsafe generator grants extra speed for
|
||||
the typed row accessors Row.int64(atIndex:), Row.string(atIndex:), etc.
|
||||
|
||||
- parameter db: A Database.
|
||||
- parameter sql: An SQL query.
|
||||
- parameter arguments: Optional statement arguments.
|
||||
- returns: A lazy sequence of rows.
|
||||
*/
|
||||
public static func metalFetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> AnyGenerator<Row> {
|
||||
let statement = db.selectStatement(sql)
|
||||
return statement.metalGenerate(arguments: arguments) {
|
||||
Row(metalStatement: statement)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Fetches a lazy sequence of rows.
|
||||
|
||||
@@ -377,6 +438,10 @@ public struct Row: CollectionType {
|
||||
self.impl = StatementRowImpl(statement: statement)
|
||||
}
|
||||
|
||||
init(metalStatement statement: SelectStatement) {
|
||||
self.impl = MetalRowImpl(sqliteStatement: statement.sqliteStatement)
|
||||
}
|
||||
|
||||
|
||||
// MARK: - DictionaryRowImpl
|
||||
|
||||
@@ -444,6 +509,38 @@ public struct Row: CollectionType {
|
||||
return columnNames.indexOf { $0.lowercaseString == lowercaseName }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - MetalRowImpl
|
||||
|
||||
/// See Row.init(metalStatement:)
|
||||
private struct MetalRowImpl : RowImpl {
|
||||
let sqliteStatement: SQLiteStatement
|
||||
|
||||
var count: Int {
|
||||
return Int(sqlite3_column_count(sqliteStatement))
|
||||
}
|
||||
|
||||
func databaseValue(atIndex index: Int) -> DatabaseValue {
|
||||
return DatabaseValue(sqliteStatement: sqliteStatement, index: index)
|
||||
}
|
||||
|
||||
func columnName(atIndex index: Int) -> String {
|
||||
return String.fromCString(sqlite3_column_name(sqliteStatement, Int32(index)))!
|
||||
}
|
||||
|
||||
// This method MUST be case-insensitive.
|
||||
func indexForColumn(named name: String) -> Int? {
|
||||
let lowercaseName = name.lowercaseString
|
||||
for i in 0..<count {
|
||||
if columnName(atIndex: i).lowercaseString == lowercaseName {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -508,8 +605,8 @@ public func ==(lhs: RowIndex, rhs: RowIndex) -> Bool {
|
||||
public struct MetalRow {
|
||||
let sqliteStatement: SQLiteStatement
|
||||
|
||||
init(statement: SelectStatement) {
|
||||
self.sqliteStatement = statement.sqliteStatement
|
||||
init(sqliteStatement: SQLiteStatement) {
|
||||
self.sqliteStatement = sqliteStatement
|
||||
}
|
||||
|
||||
|
||||
@@ -542,8 +639,8 @@ public struct MetalRow {
|
||||
*/
|
||||
public static func fetch(db: Database, _ sql: String, arguments: StatementArguments? = nil) -> AnyGenerator<MetalRow> {
|
||||
let statement = db.selectStatement(sql)
|
||||
return statement.unsafeGenerate(arguments: arguments) {
|
||||
MetalRow(statement: statement)
|
||||
return statement.metalGenerate(arguments: arguments) {
|
||||
MetalRow(sqliteStatement: statement.sqliteStatement)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@ public final class SelectStatement : Statement {
|
||||
*read* function is called, the statement has just read a row.
|
||||
- returns: A lazy sequence.
|
||||
*/
|
||||
func unsafeGenerate<T>(arguments arguments: StatementArguments?, read: () -> T) -> AnyGenerator<T> {
|
||||
func metalGenerate<T>(arguments arguments: StatementArguments?, read: () -> T) -> AnyGenerator<T> {
|
||||
if let arguments = arguments {
|
||||
self.arguments = arguments
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user