RowAdapter
A type that helps two incompatible row interfaces working together.
protocol RowAdapter : Sendable
Browse conforming typesRow adapters present database rows in the way expected by the row consumers.
For example, when a row consumer expects a column named “consumed”, but the raw row has a column named “produced”, the ColumnMapping
row adapter comes in handy:
// Feeds the "consumed" column from "produced":
let adapter = ColumnMapping(["consumed": "produced"])
let sql = "SELECT 'Hello' AS produced"
let row = try Row.fetchOne(db, sql: sql, adapter: adapter)!
// [consumed:"Hello"]
print(row)
// "Hello"
print(row["consumed"])
The raw fetched columns are not lost (see unadapted
):
// ▿ [consumed:"Hello"]
// unadapted: [produced:"Hello"]
print(row.debugDescription)
// [produced:"Hello"]
print(row.unadapted)
There are several situations where row adapters are useful. Among them:
Adapters help disambiguate columns with identical names, which may happen when you select columns from several tables. See
splittingRowAdapters(columnCounts:)
for some sample code.Adapters help when SQLite outputs unexpected column names, which may happen with some subqueries. See
RenameColumnAdapter
for an example.
Splitting a Row into Chunks
func splittingRowAdapters(columnCounts: [Int]
) -> [any RowAdapter] Returns an array of row adapters that split a row according to the provided numbers of columns.
Adding Scopes to an Adapter
func addingScopes([String : any RowAdapter]
) -> any RowAdapter Returns an adapter based on self, with added scopes.
Built-in Adapters
struct ColumnMapping
ColumnMapping
is a row adapter that maps column names.struct EmptyRowAdapter
EmptyRowAdapter
is a row adapter that hides all columns.struct RangeRowAdapter
RangeRowAdapter
is a row adapter that only exposes a range of columns.struct RenameColumnAdapter
RenameColumnAdapter
is a row adapter that renames columns.struct ScopeAdapter
ScopeAdapter
is a row adapter that defines row scopes.struct SuffixRowAdapter
SuffixRowAdapter
hides the leftmost columns in a row.