Row
A database row.
final class Row
To get Row
instances, you will generally fetch them from a Database
instance. For example:
try dbQueue.read { db in
let rows = try Row.fetchCursor(db, sql: """
SELECT * FROM player
""")
while let row = try rows.next() {
let id: Int64 = row["id"]
let name: String = row["name"]
}
}
Creating Rows
init(
) Creates an empty row.
init([String : (any DatabaseValueConvertible)?]
) Creates a row from a dictionary of database values.
init?([AnyHashable : Any]
) Creates a row from a dictionary.
Copying a Row
func copy(
) -> Row Returns an immutable copy of the row.
Row Informations
var columnNames: LazyMapCollection<Row, String>
The names of columns in the row, from left to right.
var containsNonNullValue: Bool
Returns a boolean value indicating if the row contains one value this is not
NULL
.let count: Int
The number of columns in the row.
var databaseValues: LazyMapCollection<Row, DatabaseValue>
The database values in the row, from left to right.
func hasColumn(String
) -> Bool Returns whether the row has one column with the given name (case-insensitive).
func hasNull(atIndex: Int
) -> Bool Returns whether the row has a
NULL
value at given index.
Accessing Row Values by Int Index
subscript(Int
) -> (any DatabaseValueConvertible)? Returns
Int64
,Double
,String
,Data
or nil, depending on the value stored at the given index.subscript<Value>(Int
) -> Value Returns the value at given index, converted to the requested type.
subscript<Value>(Int
) -> Value Returns the value at given index, converted to the requested type.
func withUnsafeData<T>(atIndex: Int, (Data?) throws -> T
) throws -> T Calls the given closure with the
Data
at given index.func dataNoCopy(atIndex: Int
) -> Data? Returns the optional
Data
at given index.
Accessing Row Values by Column Name
subscript(String
) -> (any DatabaseValueConvertible)? Returns
Int64
,Double
,String
,Data
or nil, depending on the value stored at the given column.subscript<Value>(String
) -> Value Returns the value at given column, converted to the requested type.
subscript<Value>(String
) -> Value Returns the value at given column, converted to the requested type.
func coalesce<T>(some Collection<String>
) -> T? Returns the first non-null value, if any. Identical to SQL
COALESCE
function.func coalesce<T>(some Collection<any ColumnExpression>
) -> T? Returns the first non-null value, if any. Identical to SQL
COALESCE
function.func withUnsafeData<T>(named: String, (Data?) throws -> T
) throws -> T Calls the given closure with the
Data
at the given column.func dataNoCopy(named: String
) -> Data? Returns the optional
Data
at given column.
Accessing Row Values by Column
subscript(some ColumnExpression
) -> (any DatabaseValueConvertible)? Returns
Int64
,Double
,String
,Data
or nil, depending on the value stored at the given column.subscript<Value>(some ColumnExpression
) -> Value Returns the value at given column, converted to the requested type.
subscript<Value>(some ColumnExpression
) -> Value Returns the value at given column, converted to the requested type.
func withUnsafeData<T>(at: some ColumnExpression, (Data?) throws -> T
) throws -> T Calls the given closure with the
Data
at the given column.func dataNoCopy(some ColumnExpression
) -> Data? Returns the optional
Data
at given column.
Row Scopes & Associated Rows
var prefetchedRows: Row.PrefetchedRowsView
A view on the prefetched associated rows.
var scopes: ScopesView
A view on the scopes defined by row adapters.
var scopesTree: ScopesTreeView
A view on the scopes tree defined by row adapters.
var unadapted: Row
The raw row fetched from the database.
var unscoped: Row
The row, without any scope of prefetched rows.
subscript<Record>(String
) -> Record? The eventual record associated to the given scope.
subscript<Record>(String
) -> Record The record associated to the given scope.
subscript<Records>(String
) -> Records A collection of prefetched records associated to the given association key.
subscript<Record>(String
) -> Set<Record> A set prefetched records associated to the given association key.
struct PrefetchedRowsView
A view on the prefetched associated rows.
struct ScopesTreeView
A view on the scopes tree defined by row adapters.
struct ScopesView
A view on the scopes defined by row adapters.
Fetching Rows from Raw SQL
static func fetchCursor(Database, sql: String, arguments: StatementArguments, adapter: (any RowAdapter)?
) throws -> RowCursor Returns a cursor over rows fetched from an SQL query.
static func fetchAll(Database, sql: String, arguments: StatementArguments, adapter: (any RowAdapter)?
) throws -> [Row] Returns an array of rows fetched from an SQL query.
static func fetchSet(Database, sql: String, arguments: StatementArguments, adapter: (any RowAdapter)?
) throws -> Set<Row> Returns a set of rows fetched from an SQL query.
static func fetchOne(Database, sql: String, arguments: StatementArguments, adapter: (any RowAdapter)?
) throws -> Row? Returns a single row fetched from an SQL query.
Fetching Rows from a Prepared Statement
static func fetchCursor(Statement, arguments: StatementArguments?, adapter: (any RowAdapter)?
) throws -> RowCursor Returns a cursor over rows fetched from a prepared statement.
static func fetchAll(Statement, arguments: StatementArguments?, adapter: (any RowAdapter)?
) throws -> [Row] Returns an array of rows fetched from a prepared statement.
static func fetchSet(Statement, arguments: StatementArguments?, adapter: (any RowAdapter)?
) throws -> Set<Row> Returns a set of rows fetched from a prepared statement.
static func fetchOne(Statement, arguments: StatementArguments?, adapter: (any RowAdapter)?
) throws -> Row? Returns a single row fetched from a prepared statement.
Fetching Rows from a Request
static func fetchCursor(Database, some FetchRequest
) throws -> RowCursor Returns a cursor over rows fetched from a fetch request.
static func fetchAll(Database, some FetchRequest
) throws -> [Row] Returns an array of rows fetched from a fetch request.
static func fetchSet(Database, some FetchRequest
) throws -> Set<Row> Returns a set of rows fetched from a fetch request.
static func fetchOne(Database, some FetchRequest
) throws -> Row? Returns a single row fetched from a fetch request.
Row as RandomAccessCollection
let count: Int
The number of columns in the row.
subscript(Index
) -> (String, DatabaseValue) Returns the (column, value) pair at given index.
struct Index
An index to a (column, value) pair in a
Row
.
Adapting Rows
protocol RowAdapter
A type that helps two incompatible row interfaces working together.
Supporting Types
class RowCursor
A cursor of raw database rows.