Sqlite.Encode
Encode SQL parameters.
You will typically use these in the parameters field. For example:
Sqlite.execute
{ statement = "INSERT INTO hero (name, level) VALUES (:name, :level)"
, parameters =
[ Sqlite.Row.Encode "name" <| Encode.string "name"
, Sqlite.Row.Encode "level" <| Encode.int 99
]
}
db
They are also used to encode values in custom SQLite functions and aggregates.
An encoded SQLite value.
Encode a String value.
Encode an integer.
Encode a float.
Encode a boolean value.
This will be stored in the db as 1 for true and 0 for false. See https://www.sqlite.org/datatype3.html#boolean_datatype for more information.
Encode a JSON value.
Sqlite.execute
{ statement = "INSERT INTO players (name, tags) VALUES (:name, :tags)"
, parameters =
[ Sqlite.Encode.string "name" "Peach"
, Sqlite.Encode.json "tags"
(Json.Encode.array Json.Encode.string [ "hero", "princess" ])
]
}
db
JSON can be stored as human-readable text or as a more compact binary format
using SQLite's jsonb() function:
-- Store as text (TEXT column)
"INSERT INTO items (data) VALUES (:data)"
-- Store as binary (BLOB column)
"INSERT INTO items (data) VALUES (jsonb(:data))"
When reading back, use json() in your SELECT to ensure you get text
regardless of how it was stored:
"SELECT json(data) as data FROM items"
See https://www.sqlite.org/json1.html for more information.
Encode a Time.Posix value.
The value will be encoded as an int representing the number of seconds since
unix epoch, which aligns with SQLite's unixepoch() function.
See https://sqlite.org/lang_datefunc.html for more information.
If you need subsecond precision, use timeWithMillis.
Encode a Time.Posix value with subsecond precision.
The value will be encoded as a float representing the number of seconds since
unix epoch, with subsecond precision to the millisecond, which aligns with
SQLite's unixepoch function using the "subsec" modifier.
See https://sqlite.org/lang_datefunc.html for more information.
If you don't need subsecond precision, use time, which will save storage for large data sets.
Encode null.
Encode a nullable value.
The first parameter is the encoder for the type if the value is not null. For example, to encode a nullable TEXT field:
Sqlite.Encode.Row "nickname" (Sqlite.Encode.maybe Sqlite.Encode.string maybeName)
Transform a Decoder into a Json.Decode.Decoder. This is used by internal modules
and is not needed for normal use.