liveBook · Manning

ToRow

As explained in Learn Haskell by Example

ToRow is a sqlite-simple type class for converting Haskell values into parameters that can be bound to SQL statements.

ToRow converts a Haskell value into the list of SQLData values that sqlite-simple binds to ? placeholders in query and execute calls. Tuple and list instances cover common parameter groups when their elements support ToField. section 16.3.2 section 16.3.1

Common values already fit ToRow:

  • Only wraps one value when a query needs a single parameter; a one-element tuple is unavailable. section 16.3.2

Mapping a record to a database row

A TodoEntry instance can call toField for each record field: entryId, text, checked, and rank. Nothing becomes SQLNull, while Bool becomes an integer because SQLite has no dedicated Boolean type. The resulting row can be passed directly to execute.

For application records, define ToRow explicitly with toField calls or use the generic ToRow instance. Reading the row back requires the separate FromRow class. section 16.3.2

How Learn Haskell by Example covers it

How deep it goes
Defines toRow as SQLData serialization, demonstrates built-in instances, and implements custom and generic record instances.
Examples
Uses tuples, lists, Only values, and TodoEntry records containing optional fields and a Boolean.
What it assumes
Assumes basic Haskell values and SQLite statements, while introducing SQLData, ToField, ToRow, and FromRow from their roles.
What you can do after
Apply ToRow through built-in parameters or implement serialization for application records inserted with sqlite-simple.
How it connects
Connects ToRow with ToField, FromRow, SQLData, query parameters, and Generic-derived database instances.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.