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 })
}
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.
Decode a string field.
Decode an integer field.
Decode a float field.
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
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.
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.
Create a decoder that gets 2 fields from a row.
Create a decoder that gets 3 fields from a row.
Create a decoder that gets 4 fields from a row.
Create a decoder that gets 5 fields from a row.
Create a decoder that gets 6 fields from a row.
Create a decoder that gets 7 fields from a row.
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.
Update a decoder with a function that will modify the decoded value.
For example:
Decode.string "email"
|> Decode.map Email.fromString
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
.
Force a decoder to fail.
You will most likely use this in conjunction with andThen
.
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.
Get a json decoder for a raw ws4sql query response.
Get a json decoder for a raw ws4sql statement response.