25 releases
0.12.0 | Oct 13, 2024 |
---|---|
0.11.1 | Feb 14, 2023 |
0.11.0 | Jul 1, 2022 |
0.10.1 | Feb 22, 2022 |
0.3.1 | Nov 6, 2017 |
#253 in Encoding
267 downloads per month
Used in 3 crates
(via hdbconnect_impl)
87KB
2K
SLoC
serde_db
Support for flexibly deserializing database result sets and rows into rust types, and for serializing rust types into database parameter values.
Being based on serde, this crate can facilitate the data exchange between applications and a database driver. It is meant to be used by the implementors of database drivers, who then can expose a more comfortable driver API.
Usage
Add serde_db
to the dependencies section in your project's Cargo.toml
, with
[dependencies]
serde_db = "0.12"
Examples for Deserialization
The below examples assume the DB driver exposes on its result set type a function
fn try_into<'de, T: serde::Deserialize<'de>>(self) -> Result<T, E>
which is implemented using serde_db
.
The application then can directly assign the database results to appropriate rust variables!
Convert a n×m result set into a Vec of structs
#[macro_use]
extern crate serde_derive;
...
#[derive(Deserialize)]
struct MyStruct {...}
...
let result_set = ...;
let data: Vec<MyStruct> = result_set.try_into()?;
Note that MyStruct
has to implement serde::Deserialize
.
Assign a n×1 result set to a Vec of fields
let vec_s: Vec<String> = result_set.try_into()?;
Assign a 1×1 result set to a single field
let s: String = result_set.try_into()?;
Assign rows to tuples or structs
For better streaming of large result sets, you might want to iterate over the rows, like in
for row in result_set {
let t: (String, NaiveDateTime, i32, Option<i32>) = row.try_into()?;
}
or
for row in result_set {
let data: MyStruct = row.try_into()?;
}
Examples for Serialization
serde_db
also when a DB driver needs to translate rust values into DB types.
A Prepared Statement for example might have a generic function
fn add_batch<T>(&mut self, input: &T) -> DbResult<()>
where T: serde::Serialize
If it is implemented with serde_db
, then the application can hand over
a tuple of rust values that correspond to the parameters of the prepared statement,
or they can hand over an appropriate struct that implements serde::Serialize
.
In both cases they do not need to differentiate between nullable and non-nullable
database values (except that they cannot convert an Option::None
into a non-nullable database
value).
In its implementation of DbvFactory
,
the DB driver can decide to make their life even easier by converting flexibly between
different number types (an example can be found in the tests of this crate).
The implementation of add_batch()
converts input
into
a Vec of the driver's database values that can subsequently be sent to the DB server:
let db_values: Vec<DBValue> = serde_db::ser::to_params(&input, input_metadata)?;
It is required that the prepared statement has metadata about the needed input parameters,
which implement DbvFactory
.
Cargo Features
trace
(no default)
Adds trace output (using the log
macros).
Dependencies
~0.3–1MB
~22K SLoC