RowAdapter

A type that helps two incompatible row interfaces working together.

RowAdapter.swift:319
protocol RowAdapter : Sendable
Browse conforming types

Row 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

Adding Scopes to an Adapter

Built-in Adapters