Db.Decode

Decode SQL results into Gren values.

These are typically used in the Db.get* functions via the decoder field, where the string value matches the name of the field in the query. For example:

-- Assuming a table with id INTEGER, name TEXT
Db.getOne connection
    { query = "select * from users where id = :id"
    , parameters = [ Db.Encode.int 1 ]
    , decoder = 
        Db.Decode.get2
            (Db.Decode.int "id")
            (Db.Decode.string "name")
            (\id name -> { id = id, name = name })
    }
type Decoder a

A decoder for a database row.

Decoding Individual Fields

Use these functions to decode an individual field in a row. You'll usually be using these to build up larger decoders via one of the getN functions.

string : String -> Decoder String

Decode a string field.

int : String -> Decoder Int

Decode an integer field.

float : String -> Decoder Float

Decode a float field.

bool : String -> Decoder Bool

Decode a boolean field.

Booleans in sqlite can be stored as integers with 1 and 0 as True and False, or as a string with value "TRUE" or "FALSE". All other values will fail decoding. See https://www.sqlite.org/datatype3.html#boolean_datatype

posix : String -> Decoder Posix

Decode an integer field into a Time.Posix value.

The integer in the database should represent unix time in milliseconds (the number of milliseconds since 1970 January 1 at 00:00:00 UTC). This is how a Time.Posix value will be saved when using Db.Encode.posix.

maybe : (String -> Decoder a) -> String -> Decoder (Maybe a)

Decode a nullable field in the database.

The first parameter is the decoder function for the field type if the value is not null. For example, to decode a nullable INTEGER field:

Db.Decode.maybe Db.Decode.int "myField"

Decoding Multiple Fields

Use these get functions to build a decoder that gets multiple fields from a row.

get2 : Decoder a -> Decoder b -> (a -> b -> c) -> Decoder c

Create a decoder that gets 2 fields from a row.

get3 :
Decoder a
-> Decoder b
-> Decoder c
-> (a -> b -> c -> d)
-> Decoder d

Create a decoder that gets 3 fields from a row.

get4 :
Decoder a
-> Decoder b
-> Decoder c
-> Decoder d
-> (a -> b -> c -> d -> e)
-> Decoder e

Create a decoder that gets 4 fields from a row.

get5 :
Decoder a
-> Decoder b
-> Decoder c
-> Decoder d
-> Decoder e
-> (a -> b -> c -> d -> e -> f)
-> Decoder f

Create a decoder that gets 5 fields from a row.

get6 :
Decoder a
-> Decoder b
-> Decoder c
-> Decoder d
-> Decoder e
-> Decoder f
-> (a -> b -> c -> d -> e -> f -> g)
-> Decoder g

Create a decoder that gets 6 fields from a row.

get7 :
Decoder a
-> Decoder b
-> Decoder c
-> Decoder d
-> Decoder e
-> Decoder f
-> Decoder g
-> (a -> b -> c -> d -> e -> f -> g -> h)
-> Decoder h

Create a decoder that gets 7 fields from a row.

get8 :
Decoder a
-> Decoder b
-> Decoder c
-> Decoder d
-> Decoder e
-> Decoder f
-> Decoder g
-> Decoder h
-> (a -> b -> c -> d -> e -> f -> g -> h -> i)
-> Decoder i

Create a decoder that gets 8 fields from a row.

If you need more than 8, you can tack on more with andThen.

Fancy Decoding

Build more complex decoders with these functions.

map : (a -> b) -> Decoder a -> Decoder b

Update a decoder with a function that will modify the decoded value.

For example:

Decode.string "email"
    |> Decode.map Email.fromString
succeed : a -> Decoder a

Create a decoder that's hard-coded to a specific value.

For example:

Decode.succeed "abc"

will always decode to "abc" regardless of input.

You will most likely use this in conjunction with andThen.

fail : String -> Decoder a

Force a decoder to fail.

You will most likely use this in conjunction with andThen.

andThen : (a -> Decoder b) -> Decoder a -> Decoder b

Chain decoders together.

You will most likely use this conjunction with succeed and fail. For example:

Decode.string "email"
    |> Decode.andThen
        (\email ->
            when Email.fromString email is
                Just email -> Decode.succeed email
                Nothing -> Decode.fail "invalid email"
        )

Util

You shouldn't need these unless you are bypassing this package's API to interact with the ws4sql server directly.

ws4sqlQueryResponse : Decoder a -> Decoder (Array a)

Get a json decoder for a raw ws4sql query response.

ws4sqlStatementResponse : Decoder (Array Int)

Get a json decoder for a raw ws4sql statement response.