Db.Encode
Encode SQL parameters.
These are used for encoding values to be replaced in sql parameters.
They will usually be passed in an array via the parameters
field,
where the string matches the colon-prefixed named of the parameter in the sql:
Db.execute connection
{ statement = "insert into users (id, name) values (:id, :name)"
, parameters =
[ Db.Encode.int "id" 1
, Db.Encode.string "name" "Justin"
]
}
type Value
An encoded parameter value.
string :
String -> String -> Value
Encode a String value.
Db.Encode.string "myStringField" "Hey there"
int :
String -> Int -> Value
Encode an integer.
Db.Encode.int "myIntField" 1
float :
String -> Float -> Value
Encode a float.
Db.Encode.float "myFloatField" 1.2
bool :
String -> Bool -> Value
Encode a boolean value.
Db.Encode.bool "myBoolField" True
null :
String -> Value
Encode null.
Db.Encode.null "myNullField"
posix :
String -> Posix -> Value
Encode a Time.Posix value.
This is what you should use for date fields. The database field should be an INTEGER type. It will be stored as the number of milliseconds since 1970 January 1 at 00:00:00 UTC (it was a Thursday).
Db.Encode.posix "myDateField" (Time.millisToPosix 0)
See Db.Decode.posix
toJson :
Array Value -> Value
Convert an array of Db.Encode.Value fields to a Json object.
This is useful if you want to bypass this package's API and interact with ws4sql directly.
[ Db.Encode.int "id" 123 ]
|> Db.Encode.toJson
|> Json.Encode.encode 0
-- results in: {"id": 123}