5个版本
0.2.0 | 2020年8月8日 |
---|---|
0.1.3 | 2019年3月18日 |
0.1.2 | 2018年12月3日 |
0.1.1 | 2018年12月3日 |
0.1.0 | 2018年11月21日 |
#927 在 解析器实现
1,632 每月下载量
25KB
502 代码行
Serde Postgres
轻松将来自tokio-postgres
或postgres
的行反序列化为任意结构体。(仅支持反序列化)。
示例
tokio-postgres
(异步)
use std::error::Error;
use serde::Deserialize;
use tokio_postgres::{connect, NoTls};
#[derive(Clone, Debug, Deserialize)]
struct Person {
name: String,
age: i32,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let (client, connection) = connect("postgres://postgres@localhost:5432", NoTls).await?;
tokio::spawn(connection);
client.execute("CREATE TABLE IF NOT EXISTS Person (
name VARCHAR NOT NULL,
age INT NOT NULL
)", &[]).await?;
client.execute("INSERT INTO Person (name, age) VALUES ($1, $2)",
&[&"Jane", &23i32]).await?;
client.execute("INSERT INTO Person (name, age) VALUES ($1, $2)",
&[&"Alice", &32i32]).await?;
let rows = client.query("SELECT name, age FROM Person", &[]).await?;
let people: Vec<Person> = serde_postgres::from_rows(&rows)?;
for person in people {
println!("{:?}", person);
}
Ok(())
}
postgres
(同步)
use postgres::{Client, NoTls};
use serde::Deserialize;
use std::error::Error;
#[derive(Clone, Debug, Deserialize)]
struct Person {
name: String,
age: i32,
}
fn main() -> Result<(), Box<dyn Error>> {
let mut client = Client::connect("postgres://postgres@localhost:5432", NoTls)?;
client.execute("CREATE TABLE IF NOT EXISTS Person (
name VARCHAR NOT NULL,
age INT NOT NULL
)", &[])?;
client.execute("INSERT INTO Person (name, age) VALUES ($1, $2)",
&[&"Jane", &23i32])?;
client.execute("INSERT INTO Person (name, age) VALUES ($1, $2)",
&[&"Alice", &32i32])?;
let rows = client.query("SELECT name, age FROM Person", &[])?;
let people: Vec<Person> = serde_postgres::from_rows(&rows)?;
for person in people {
println!("{:?}", person);
}
Ok(())
}
依赖关系
~10MB
~202K SLoC