Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

readme update #1144

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions postgres/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,8 @@ rustls-pemfile = { version = "2", optional = true }
xitca-postgres-codegen = "0.1"
bb8 = "0.8.5"
futures = { version = "0.3", default-features = false }
postgres-derive = "0.4"
postgres-types = { version = "0.2", features = ["with-uuid-1"] }
rcgen = "0.13"
tokio = { version = "1.30", features = ["macros", "rt-multi-thread", "time"] }
uuid = "1"
42 changes: 41 additions & 1 deletion postgres/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,44 @@ fn parse(row: Row<'_>) {
// any type implement FromSqlExt trait will be parsable by get_zc API.
let foo: Baz = row.get_zc("baz");
}
```
```

## Interop with `rust-postgres` crates
`xitca-postgres` uses the same postgres protocol and types definition with them. For extended types and utilities related
crates can be imported for additional features
```rust
// add postgres-types = { .., features = ["with-uuid-1"] } to Cargo dependency to enable uuid type support.
use postgres_types::Type;
use uuid::Uuid;
use xitca_postgres::{Client, Error, Execute, Statement};

// an example of using extended type support rust-postgres offers through postgres-types crate
async fn bind_ext(cli: Client) -> Result<(), Error> {
// prepare statement and query with Uuid type
let stmt = Statement::named("SELECT * FROM users WHERE id = $1", &[Type::UUID]).execute(&cli).await?;
let stream = stmt.bind([Uuid::default()]).query(&cli).await?;
Ok(())
}

// add postgres-derive to Cargo dependency to enable derive macro support.
use postgres_derive::ToSql;

// implement derive macro to custom type
#[derive(ToSql, Debug)]
struct Worker {
id: i32,
wage: i32,
}

async fn derive_macro(cli: Client) -> Result<(), Error> {
// create custom type
"CREATE TYPE pg_temp.\"Worker\" AS (id INT, wage INT);".execute(&cli).await?;
// prepare statement with custom type
let stmt = Statement::named("SELECT $1::Worker", &[]).execute(&cli).await?;
// bind statement to custom type and execute. this works because the derive macro
// added necessary trait implement that make Worker type can be encoded and used
// as custom type we just created.
stmt.bind([Worker { id: 9527, wage: 0}]).execute(&cli).await?;
Ok(())
}
```
Loading