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
type Decoder a

A decoder for a SQLite value.

string : Decoder String

Decode a string field.

int : Decoder Int

Decode an integer field.

float : Decoder Float

Decode a float field.

bool : Decoder Bool

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.

json : Decoder a -> Decoder a

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.

time : Decoder Posix

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.

maybe : Decoder a -> Decoder (Maybe a)

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
unwrap : Decoder a -> Decoder a

Transform a Decoder into a Json.Decode.Decoder. This is used by internal modules and is not needed for normal use.