serde_rusqlite

Documentation

See full documentation

Usage

Add this to your Cargo.toml: [dependencies] serde_rusqlite = "0.18"

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 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 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 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<u8>). Corresponding rust types are inside brackets.

Some types employ non-trivial handling, these are described below:

Examples

```rust extern crate rusqlite;

[macro_use]

extern crate serdederive; extern crate serderusqlite;

use serderusqlite::*; use rusqlite::NOPARAMS;

[derive(Serialize, Deserialize, Debug, PartialEq)]

struct Example { id: i64, name: String, }

fn main() { let connection = rusqlite::Connection::openinmemory().unwrap(); connection.execute("CREATE TABLE example (id INT, name TEXT)", NO_PARAMS).unwrap();

// using structure to generate named bound query arguments let row1 = Example{ id: 1, name: "first name".into() }; connection.executenamed("INSERT INTO example (id, name) VALUES (:id, :name)", &toparamsnamed(&row1).unwrap().toslice()).unwrap();

// using tuple to generate positional bound query arguments let row2 = (2, "second name"); connection.execute("INSERT INTO example (id, name) VALUES (?, ?)", &toparams(&row2).unwrap().toslice()).unwrap();

// deserializing using query() and fromrows() let mut statement = connection.prepare("SELECT * FROM example").unwrap(); let mut res = fromrows::(statement.query(NOPARAMS).unwrap()); asserteq!(res.next().unwrap(), row1); assert_eq!(res.next().unwrap(), Example{ id: 2, name: "second name".into() });

// deserializing using queryandthen() and fromrow() let mut statement = connection.prepare("SELECT * FROM example").unwrap(); let mut rows = statement.queryandthen(NOPARAMS, fromrow::).unwrap(); asserteq!(rows.next().unwrap().unwrap(), row1); assert_eq!(rows.next().unwrap().unwrap(), Example{ id: 2, name: "second name".into() });

// deserializing using query() and fromrowsref() let mut statement = connection.prepare("SELECT * FROM example").unwrap(); let mut rows = statement.query(NOPARAMS).unwrap(); { // only first record is deserialized here let mut res = fromrowsref::(&mut rows); asserteq!(res.next().unwrap(), row1); } // the second record is deserialized using the original Rows iterator asserteq!(fromrow::(&rows.next().unwrap().unwrap()).unwrap(), Example{ id: 2, name: "second name".into() });

// deserializing using query() and fromrowswithcolumns() let mut statement = connection.prepare("SELECT * FROM example").unwrap(); let columns = columnsfromstatement(&statement); let mut res = fromrowswithcolumns::(statement.query(NOPARAMS).unwrap(), &columns); asserteq!(res.next().unwrap(), row1); assert_eq!(res.next().unwrap(), Example{ id: 2, name: "second name".into() });

// deserializing using queryandthen() and fromrowwithcolumns() let mut statement = connection.prepare("SELECT * FROM example").unwrap(); let columns = columnsfromstatement(&statement); let mut rows = statement.queryandthen(NOPARAMS, |row| fromrowwithcolumns::(row, &columns)).unwrap(); asserteq!(rows.next().unwrap().unwrap(), row1); assert_eq!(rows.next().unwrap().unwrap(), Example{ id: 2, name: "second name".into() });

// deserializing using query() and fromrowsrefwithcolumns() let mut statement = connection.prepare("SELECT * FROM example").unwrap(); let columns = columnsfromstatement(&statement); let mut rows = statement.query(NOPARAMS).unwrap(); { // only first record is deserialized here let mut res = fromrowsrefwithcolumns::(&mut rows, &columns); asserteq!(res.next().unwrap(), row1); } // the second record is deserialized using the original Rows iterator asserteq!(fromrowwithcolumns::(&rows.next().unwrap().unwrap(), &columns).unwrap(), Example{ id: 2, name: "second name".into() });

} ```

License: LGPL-3.0