serde_rusqlite
Support the project | Documentation
Usage
Run:
cargo add serde_rusqlite
Or add to your Cargo.toml:
[]
= "0.41.1"
Serde Rusqlite
This crate provides convenience functions to bridge serde and rusqlite. With their help
you can "deserialize" [rusqlite::Row]'s into [serde::Deserialize] types and "serialize" types
implementing [serde::Serialize] into bound query arguments (positional or named) that rusqlite
expects.
Serialization of named bound arguments is only supported from structs and maps because other
serde types lack column name information. Likewise, serialization of positional bound arguments
is only supported from tuples, sequences and primitive non-iterable types. In the latter case
the result will be a single-element vector. Each serialized field or element must implement
[rusqlite::types::ToSql].
For deserialization, you can use two families of functions: from_*() and from_*_with_columns().
The most used one is the former. The latter allows you to specify column names for types that need
them but don't supply them. This includes different Map types like [std::collections::HashMap].
Specifying columns for deserialization into e.g. struct doesn't have any effect as the field list
of the struct itself will be used in any case.
SQLite only supports 5 types: NULL ([None]), INTEGER ([i64]), REAL ([f64]), TEXT ([String])
and BLOB ([Vec]). Corresponding rust types are inside brackets.
Some types employ non-trivial handling, these are described below:
-
Serialization of
u64will fail if it can't be represented byi64due to the SQLite limitations. -
Simple
enums will be serialized as strings so:enum Gender { M, F, }will have two possible
TEXToptions in the database "M" and "F". Deserialization intoenumfromTEXTis also supported. -
[bool]s are serialized as
INTEGERs 0 or 1, can be deserialized fromINTEGERandREALwhere 0 and 0.0 arefalse, anything else istrue. -
[f64] and [f32] values of
NaNare serialized asNULLs. When deserializing such a value, [Option] will have the value of [None] and [f64] will have the value ofNaN. The same applies to [f32]. -
[serde_bytes::Bytes], [serde_bytes::ByteBuf] are supported as optimized way of handling
BLOBs. -
unitserializes toNULL. -
Only
sequences of [u8] are serialized and deserialized,BLOBdatabase type is used. It's more optimal, though, to use [serde_bytes::Bytes] and [serde_bytes::ByteBuf] for such fields. -
unit_structserializes tostructname asTEXT. When deserializing, the check is made to ensure that thestructname coincides with the string in the database.
Examples
use ;
use *;
let connection = open_in_memory.unwrap;
connection.execute.unwrap;
// using structure to generate named bound query arguments
let row1 = Example ;
connection.execute.unwrap;
// and limiting the set of fields that are to be serialized
let row2 = Example ;
connection.execute.unwrap;
// using tuple to generate positional bound query arguments
let row2 = ;
connection.execute.unwrap;
// deserializing using query() and from_rows(), the most efficient way
let mut statement = connection.prepare.unwrap;
let mut res = ;
assert_eq!;
assert_eq!;
// deserializing using query_and_then() and from_row(), incurs extra overhead in from_row() call
let mut statement = connection.prepare.unwrap;
let mut rows = statement.query_and_then.unwrap;
assert_eq!;
assert_eq!;
// deserializing using query_and_then() and from_row_with_columns(), better performance than from_row()
let mut statement = connection.prepare.unwrap;
let columns = columns_from_statement;
let mut rows = statement.query_and_then.unwrap;
assert_eq!;
assert_eq!;
// deserializing using query() and from_rows_ref()
let mut statement = connection.prepare.unwrap;
let mut rows = statement.query.unwrap;
// the second record is deserialized using the original Rows iterator
assert_eq!;
License
MIT OR Apache-2.0