Sqlite.Decode
Decode SQL values into Gren values.
You will typically see these while decoding results from queries:
Sqlite.getAll
{ statement = "SELECT name, level FROM hero"
, parameters = []
, rowDecoder =
Sqlite.Decode.Row "name" Sqlite.Decode.string <| \name ->
Sqlite.Decode.Row "level" Sqlite.Decode.int <| \level ->
Sqlite.Decode.Row.suceed { name = name, level = level }
}
db
A decoder for a SQLite value.
Decode a string field.
Decode an integer field.
Decode a float field.
Decode a boolean field.
Booleans in SQLite are stored as integers with 1 and 0 as True and False. See https://www.sqlite.org/datatype3.html#boolean_datatype for more information.
Decode a JSON field.
The first parameter is a Json.Decode.Decoder for the value stored in the column.
Use json() in your SELECT to ensure you get text regardless of how the JSON was stored:
Sqlite.getOne
{ query = "SELECT json(tags) as tags FROM items WHERE id = 1"
, parameters = []
, rowDecoder =
Sqlite.Decode.Row.column "tags" Sqlite.Decode.json (Json.Decode.array Json.Decode.string) <| \tags ->
Sqlite.Decode.Row.succeed { tags = tags }
}
See https://www.sqlite.org/json1.html for more information.
Decode a Time.Posix value.
Expects the database field to hold the number of seconds since unix epoch, which is
how both Sqlite.Encode.time and
Sqlite.Encode.timeWithMillis store it, and
which aligns with SQLite's unixepoch function.
See https://sqlite.org/lang_datefunc.html for more information.
Decode a nullable field in the database.
The first parameter is the field decoder function for the type if the value is not null. For example, to decode a nullable TEXT field:
Sqlite.Decode.Row.column "nickname" (Decode.maybe Decode.string) <| \maybeNickname ->
Sqlite.Decode.Row.succeed maybeNickname
Transform a Decoder into a Json.Decode.Decoder. This is used by internal modules
and is not needed for normal use.