Introduce DatabaseValueCursor, NullableDatabaseValueCursor

This commit is contained in:
Gwendal Roué committed 2017-08-25 05:54:37 +02:00
1 parent 1a7b0b0626
commit a4a05b1f0a
8 files changed
+112 -34

No files matched your search

+8 -6
View File
@@ -178,21 +178,23 @@ Release Notes
+}
-final class DatabaseCursor { }
+final class ColumnCursor<Value: StatementColumnConvertible> : Cursor { }
+final class NullableColumnCursor<Value: StatementColumnConvertible> : Cursor { }
+final class ColumnCursor<Value: DatabaseValueConvertible & StatementColumnConvertible> : Cursor { }
+final class DatabaseValueCursor<Value: DatabaseValueConvertible> : Cursor { }
+final class NullableColumnCursor<Value: DatabaseValueConvertible & StatementColumnConvertible> : Cursor { }
+final class NullableDatabaseValueCursor<Value: DatabaseValueConvertible> : Cursor { }
+final class RecordCursor<Record: RowConvertible> : Cursor { }
+final class RowCursor : Cursor { }
+final class StatementCursor: Cursor { }
extension DatabaseValueConvertible {
- static func fetchCursor(...) throws -> DatabaseCursor<Self>
+ static func fetchCursor(...) throws -> MapCursor<RowCursor, Self>
+ static func fetchCursor(...) throws -> DatabaseValueCursor<Self>
}
extension DatabaseValueConvertible where Self: StatementColumnConvertible {
+ static func fetchCursor(...) throws -> ColumnCursor<Self>
}
extension Optional where Wrapped: DatabaseValueConvertible {
- static func fetchCursor(...) throws -> DatabaseCursor<Wrapped?>
+ static func fetchCursor(...) throws -> MapCursor<RowCursor, Wrapped?>
+ static func fetchCursor(...) throws -> NullableDatabaseValueCursor<Wrapped>
}
extension Optional where Wrapped: DatabaseValueConvertible & StatementColumnConvertible {
+ static func fetchCursor(...) throws -> NullableColumnCursor<Wrapped>
@@ -214,14 +216,14 @@ Release Notes
}
extension TypedRequest where RowDecoder: DatabaseValueConvertible {
- func fetchCursor(_ db: Database) throws -> DatabaseCursor<RowDecoder>
+ func fetchCursor(_ db: Database) throws -> MapCursor<RowCursor, RowDecoder>
+ func fetchCursor(_ db: Database) throws -> DatabaseValueCursor<RowDecoder>
}
extension TypedRequest where RowDecoder: DatabaseValueConvertible & StatementColumnConvertible {
+ func fetchCursor(_ db: Database) throws -> ColumnCursor<RowDecoder>
}
extension TypedRequest where RowDecoder: _OptionalProtocol, RowDecoder._Wrapped: DatabaseValueConvertible {
- func fetchCursor(_ db: Database) throws -> DatabaseCursor<RowDecoder._Wrapped?>
+ func fetchCursor(_ db: Database) throws -> MapCursor<RowCursor, RowDecoder._Wrapped?>
+ func fetchCursor(_ db: Database) throws -> NullableDatabaseValueCursor<RowDecoder._Wrapped>
}
extension TypedRequest where RowDecoder: _OptionalProtocol, RowDecoder._Wrapped: DatabaseValueConvertible & StatementColumnConvertible {
+ func fetchCursor(_ db: Database) throws -> NullableColumnCursor<RowDecoder._Wrapped>
+93 -18
View File
@@ -1,3 +1,9 @@
#if SWIFT_PACKAGE
import CSQLite
#elseif !GRDBCUSTOMSQLITE && !GRDBCIPHER
import SQLite3
#endif
// MARK: - DatabaseValueConvertible
/// Types that adopt DatabaseValueConvertible can be initialized from
@@ -33,6 +39,83 @@ extension DatabaseValueConvertible {
}
}
/// A cursor of database values extracted from a single column.
/// For example:
///
/// try dbQueue.inDatabase { db in
/// let urls: DatabaseValueCursor<URL> = try URL.fetchCursor(db, "SELECT url FROM links")
/// while let url = urls.next() { // URL
/// print(url)
/// }
/// }
public final class DatabaseValueCursor<Value: DatabaseValueConvertible> : Cursor {
private let statement: SelectStatement
private let sqliteStatement: SQLiteStatement
private let columnIndex: Int32
private var done = false
init(statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws {
self.statement = statement
// We'll read from leftmost column at index 0, unless adapter mangles columns
self.columnIndex = try Int32(adapter?.baseColumnIndex(atIndex: 0, layout: statement) ?? 0)
self.sqliteStatement = statement.sqliteStatement
statement.cursorReset(arguments: arguments)
}
public func next() throws -> Value? {
if done { return nil }
switch sqlite3_step(sqliteStatement) {
case SQLITE_DONE:
done = true
return nil
case SQLITE_ROW:
let dbValue = DatabaseValue(sqliteStatement: sqliteStatement, index: columnIndex)
return dbValue.losslessConvert() as Value
case let code:
statement.database.selectStatementDidFail(statement)
throw DatabaseError(resultCode: code, message: statement.database.lastErrorMessage, sql: statement.sql, arguments: statement.arguments)
}
}
}
/// A cursor of optional database values extracted from a single column.
/// For example:
///
/// try dbQueue.inDatabase { db in
/// let urls: NullableDatabaseValueCursor<URL> = try Optional<URL>.fetchCursor(db, "SELECT url FROM links")
/// while let url = urls.next() { // URL?
/// print(url)
/// }
/// }
public final class NullableDatabaseValueCursor<Value: DatabaseValueConvertible> : Cursor {
private let statement: SelectStatement
private let sqliteStatement: SQLiteStatement
private let columnIndex: Int32
private var done = false
init(statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws {
self.statement = statement
// We'll read from leftmost column at index 0, unless adapter mangles columns
self.columnIndex = try Int32(adapter?.baseColumnIndex(atIndex: 0, layout: statement) ?? 0)
self.sqliteStatement = statement.sqliteStatement
statement.cursorReset(arguments: arguments)
}
public func next() throws -> Value?? {
if done { return nil }
switch sqlite3_step(sqliteStatement) {
case SQLITE_DONE:
done = true
return nil
case SQLITE_ROW:
let dbValue = DatabaseValue(sqliteStatement: sqliteStatement, index: columnIndex)
return dbValue.losslessConvert() as Value?
case let code:
statement.database.selectStatementDidFail(statement)
throw DatabaseError(resultCode: code, message: statement.database.lastErrorMessage, sql: statement.sql, arguments: statement.arguments)
}
}
}
/// DatabaseValueConvertible comes with built-in methods that allow to fetch
/// cursors, arrays, or single values:
@@ -71,11 +154,8 @@ extension DatabaseValueConvertible {
/// - adapter: Optional RowAdapter
/// - returns: A cursor over fetched values.
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
public static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> MapCursor<RowCursor, Self> {
return try RowCursor(statement: statement, arguments: arguments, adapter: adapter).map { row in
let dbValue: DatabaseValue = row[0]
return dbValue.losslessConvert(sql: statement.sql, arguments: arguments) as Self
}
public static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> DatabaseValueCursor<Self> {
return try DatabaseValueCursor(statement: statement, arguments: arguments, adapter: adapter)
}
/// Returns an array of values fetched from a prepared statement.
@@ -108,10 +188,8 @@ extension DatabaseValueConvertible {
/// - returns: An optional value.
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
public static func fetchOne(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> Self? {
let cursor = try RowCursor(statement: statement, arguments: arguments, adapter: adapter).map { row -> Self? in
let dbValue: DatabaseValue = row[0]
return dbValue.losslessConvert(sql: statement.sql, arguments: arguments) as Self?
}
// fetchOne returns nil if there is no row, or if there is a row with a null value
let cursor = try NullableDatabaseValueCursor<Self>(statement: statement, arguments: arguments, adapter: adapter)
return try cursor.next() ?? nil
}
}
@@ -139,7 +217,7 @@ extension DatabaseValueConvertible {
/// - request: A fetch request.
/// - returns: A cursor over fetched values.
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
public static func fetchCursor(_ db: Database, _ request: Request) throws -> MapCursor<RowCursor, Self> {
public static func fetchCursor(_ db: Database, _ request: Request) throws -> DatabaseValueCursor<Self> {
let (statement, adapter) = try request.prepare(db)
return try fetchCursor(statement, adapter: adapter)
}
@@ -197,7 +275,7 @@ extension DatabaseValueConvertible {
/// - adapter: Optional RowAdapter
/// - returns: A cursor over fetched values.
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
public static func fetchCursor(_ db: Database, _ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> MapCursor<RowCursor, Self> {
public static func fetchCursor(_ db: Database, _ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> DatabaseValueCursor<Self> {
return try fetchCursor(db, SQLRequest(sql, arguments: arguments, adapter: adapter))
}
@@ -270,11 +348,8 @@ extension Optional where Wrapped: DatabaseValueConvertible {
/// - adapter: Optional RowAdapter
/// - returns: A cursor over fetched optional values.
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
public static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> MapCursor<RowCursor, Wrapped?> {
return try RowCursor(statement: statement, arguments: arguments, adapter: adapter).map { row in
let dbValue: DatabaseValue = row[0]
return dbValue.losslessConvert(sql: statement.sql, arguments: arguments) as Wrapped?
}
public static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> NullableDatabaseValueCursor<Wrapped> {
return try NullableDatabaseValueCursor(statement: statement, arguments: arguments, adapter: adapter)
}
/// Returns an array of optional values fetched from a prepared statement.
@@ -316,7 +391,7 @@ extension Optional where Wrapped: DatabaseValueConvertible {
/// - requet: A fetch request.
/// - returns: A cursor over fetched optional values.
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
public static func fetchCursor(_ db: Database, _ request: Request) throws -> MapCursor<RowCursor, Wrapped?> {
public static func fetchCursor(_ db: Database, _ request: Request) throws -> NullableDatabaseValueCursor<Wrapped> {
let (statement, adapter) = try request.prepare(db)
return try fetchCursor(statement, adapter: adapter)
}
@@ -358,7 +433,7 @@ extension Optional where Wrapped: DatabaseValueConvertible {
/// - adapter: Optional RowAdapter
/// - returns: A cursor over fetched optional values.
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
public static func fetchCursor(_ db: Database, _ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> MapCursor<RowCursor, Wrapped?> {
public static func fetchCursor(_ db: Database, _ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> NullableDatabaseValueCursor<Wrapped> {
return try fetchCursor(db, SQLRequest(sql, arguments: arguments, adapter: adapter))
}
+2 -2
View File
@@ -339,7 +339,7 @@ extension TypedRequest where RowDecoder: DatabaseValueConvertible {
/// - parameter db: A database connection.
/// - returns: A cursor over fetched values.
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
public func fetchCursor(_ db: Database) throws -> MapCursor<RowCursor, RowDecoder> {
public func fetchCursor(_ db: Database) throws -> DatabaseValueCursor<RowDecoder> {
return try RowDecoder.fetchCursor(db, self)
}
@@ -443,7 +443,7 @@ extension TypedRequest where RowDecoder: _OptionalProtocol, RowDecoder._Wrapped:
/// - parameter db: A database connection.
/// - returns: A cursor over fetched values.
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
public func fetchCursor(_ db: Database) throws -> MapCursor<RowCursor, RowDecoder._Wrapped?> {
public func fetchCursor(_ db: Database) throws -> NullableDatabaseValueCursor<RowDecoder._Wrapped> {
return try Optional<RowDecoder._Wrapped>.fetchCursor(db, self)
}
+1 -1
View File
@@ -500,10 +500,10 @@ public final class RowCursor : Cursor {
private var done = false
init(statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws {
statement.cursorReset(arguments: arguments)
self.statement = statement
self.row = try Row(statement: statement).adapted(with: adapter, layout: statement)
self.sqliteStatement = statement.sqliteStatement
statement.cursorReset(arguments: arguments)
}
public func next() throws -> Row? {
+1 -1
View File
@@ -344,9 +344,9 @@ public final class StatementCursor: Cursor {
// Use SelectStatement.cursor() instead
init(statement: SelectStatement, arguments: StatementArguments? = nil) {
statement.cursorReset(arguments: arguments)
self.statement = statement
self.sqliteStatement = statement.sqliteStatement
statement.cursorReset(arguments: arguments)
}
public func next() throws -> Void? {
+5 -4
View File
@@ -59,18 +59,18 @@ public protocol StatementColumnConvertible {
/// print(name)
/// }
/// }
public final class ColumnCursor<Value: StatementColumnConvertible> : Cursor {
public final class ColumnCursor<Value: DatabaseValueConvertible & StatementColumnConvertible> : Cursor {
private let statement: SelectStatement
private let sqliteStatement: SQLiteStatement
private let columnIndex: Int32
private var done = false
init(statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws {
statement.cursorReset(arguments: arguments)
self.statement = statement
// We'll read from leftmost column at index 0, unless adapter mangles columns
self.columnIndex = try Int32(adapter?.baseColumnIndex(atIndex: 0, layout: statement) ?? 0)
self.sqliteStatement = statement.sqliteStatement
statement.cursorReset(arguments: arguments)
}
public func next() throws -> Value? {
@@ -104,18 +104,18 @@ public final class ColumnCursor<Value: StatementColumnConvertible> : Cursor {
/// print(email ?? "<NULL>")
/// }
/// }
public final class NullableColumnCursor<Value: StatementColumnConvertible> : Cursor {
public final class NullableColumnCursor<Value: DatabaseValueConvertible & StatementColumnConvertible> : Cursor {
private let statement: SelectStatement
private let sqliteStatement: SQLiteStatement
private let columnIndex: Int32
private var done = false
init(statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws {
statement.cursorReset(arguments: arguments)
self.statement = statement
// We'll read from leftmost column at index 0, unless adapter mangles columns
self.columnIndex = try Int32(adapter?.baseColumnIndex(atIndex: 0, layout: statement) ?? 0)
self.sqliteStatement = statement.sqliteStatement
statement.cursorReset(arguments: arguments)
}
public func next() throws -> Value?? {
@@ -196,6 +196,7 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible {
/// - returns: An optional value.
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
public static func fetchOne(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> Self? {
// fetchOne returns nil if there is no row, or if there is a row with a null value
let cursor = try NullableColumnCursor<Self>(statement: statement, arguments: arguments, adapter: adapter)
return try cursor.next() ?? nil
}
+1 -1
View File
@@ -45,10 +45,10 @@ public final class RecordCursor<Record: RowConvertible> : Cursor {
private var done = false
init(statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws {
statement.cursorReset(arguments: arguments)
self.statement = statement
self.row = try Row(statement: statement).adapted(with: adapter, layout: statement)
self.sqliteStatement = statement.sqliteStatement
statement.cursorReset(arguments: arguments)
}
public func next() throws -> Record? {
+1 -1
View File
@@ -648,7 +648,7 @@ If you don't see, or don't care about the difference, use arrays. If you care ab
```swift
Row.fetchCursor(...) // RowCursor
Int.fetchCursor(...) // ColumnCursor<Int>
Date.fetchCursor(...) // MapCursor<RowCursor, Date>
URL.fetchCursor(...) // DatabaseValueCursor<URL>
Player.fetchCursor(...) // RecordCursor<Player>
```