StatementColumnConvertible
A type that can decode itself from the low-level C interface to SQLite results.
protocol StatementColumnConvertible
Browse conforming typesStatementColumnConvertible
is adopted by Bool
, Int
, String
, Date
, and most common values.
When a type conforms to both DatabaseValueConvertible
and StatementColumnConvertible
, GRDB can apply some optimization whenever direct access to SQLite is possible. For example:
// Optimized
let scores = Int.fetchAll(db, sql: "SELECT score FROM player")
let rows = try Row.fetchCursor(db, sql: "SELECT * FROM player")
while let row = try rows.next() {
// Optimized
let int: Int = row[0]
let name: String = row[1]
}
struct Player: FetchableRecord {
var name: String
var score: Int
init(row: Row) {
// Optimized
name = row["name"]
score = row["score"]
}
}
To conform to StatementColumnConvertible
, provide a custom implementation of init(sqliteStatement:index:)
. This implementation is ready-made for RawRepresentable
types whose RawValue
is StatementColumnConvertible
.
Related SQLite documentation: https://www.sqlite.org/c3ref/column_blob.html
Creating a Value
init?(sqliteStatement: SQLiteStatement, index: CInt
) Creates an instance from a raw SQLite statement pointer, if possible.
static func fromStatement(SQLiteStatement, atUncheckedIndex: CInt
) -> Self? Creates an instance from a raw SQLite statement pointer, if possible.
Fetching Values from Raw SQL
static func fetchCursor(Database, sql: String, arguments: StatementArguments, adapter: (any RowAdapter)?
) throws -> FastDatabaseValueCursor<Self> Returns a cursor over values fetched from an SQL query.
static func fetchAll(Database, sql: String, arguments: StatementArguments, adapter: (any RowAdapter)?
) throws -> [Self] Returns an array of values fetched from an SQL query.
static func fetchSet(Database, sql: String, arguments: StatementArguments, adapter: (any RowAdapter)?
) throws -> Set<Self> Returns a set of values fetched from an SQL query.
static func fetchOne(Database, sql: String, arguments: StatementArguments, adapter: (any RowAdapter)?
) throws -> Self? Returns a single value fetched from an SQL query.
Fetching Values from a Prepared Statement
static func fetchCursor(Statement, arguments: StatementArguments?, adapter: (any RowAdapter)?
) throws -> FastDatabaseValueCursor<Self> Returns a cursor over values fetched from a prepared statement.
static func fetchAll(Statement, arguments: StatementArguments?, adapter: (any RowAdapter)?
) throws -> [Self] Returns an array of values fetched from a prepared statement.
static func fetchSet(Statement, arguments: StatementArguments?, adapter: (any RowAdapter)?
) throws -> Set<Self> Returns a set of values fetched from a prepared statement.
static func fetchOne(Statement, arguments: StatementArguments?, adapter: (any RowAdapter)?
) throws -> Self? Returns a single value fetched from a prepared statement.
Fetching Values from a Request
static func fetchCursor(Database, some FetchRequest
) throws -> FastDatabaseValueCursor<Self> Returns a cursor over values fetched from a fetch request.
static func fetchAll(Database, some FetchRequest
) throws -> [Self] Returns an array of values fetched from a fetch request.
static func fetchSet(Database, some FetchRequest
) throws -> Set<Self> Returns a set of values fetched from a fetch request.
static func fetchOne(Database, some FetchRequest
) throws -> Self? Returns a single value fetched from a fetch request.
Supporting Types
class FastDatabaseValueCursor<Value>
A cursor of database values.