Expand description
§parsql-postgres
Synchronous PostgreSQL integration for parsql. This crate provides synchronous APIs for working with PostgreSQL databases.
§Features
- Synchronous PostgreSQL operations
- Automatic SQL query generation
- Secure parameter management
- Generic CRUD operations
- Transaction support
- Extension methods for the Client object
§Usage
use postgres::{Client, NoTls, Error};
use parsql::postgres::{get, insert};
#[derive(Insertable, SqlParams)]
#[table("users")]
pub struct InsertUser {
pub name: String,
pub email: String,
}
#[derive(Queryable, SqlParams, FromRow)]
#[table("users")]
#[where_clause("id = $")]
pub struct GetUser {
pub id: i32,
pub name: String,
pub email: String,
}
fn main() -> Result<(), Error> {
let mut client = Client::connect(
"host=localhost user=postgres dbname=test",
NoTls,
)?;
// Insert a new user
let insert_user = InsertUser {
name: "John".to_string(),
email: "john@example.com".to_string(),
};
let id = insert(&mut client, insert_user)?;
// Get the user back
let get_user = GetUser::new(id as i32);
let user = get(&mut client, &get_user)?;
println!("User: {:?}", user);
Ok(())
}
§Using Extension Methods
You can also use the extension methods directly on the Client object:
use postgres::{Client, NoTls, Error};
use parsql::postgres::CrudOps; // Import the trait
use parsql::macros::{Insertable, SqlParams, Queryable, FromRow};
#[derive(Insertable, SqlParams)]
#[table("users")]
pub struct InsertUser {
pub name: String,
pub email: String,
}
#[derive(Queryable, FromRow, SqlParams)]
#[table("users")]
#[where_clause("id = $1")]
pub struct GetUser {
pub id: i32,
pub name: String,
pub email: String,
}
fn main() -> Result<(), Error> {
let mut client = Client::connect("host=localhost user=postgres", NoTls)?;
// Insert a new user using extension method
let insert_user = InsertUser {
name: "John".to_string(),
email: "john@example.com".to_string(),
};
let rows_affected = client.insert(insert_user)?;
// Get the user back using extension method
let get_user = GetUser {
id: 1,
name: String::new(),
email: String::new(),
};
let user = client.get(&get_user)?;
println!("User: {:?}", user);
Ok(())
}
§Using Transactions
This crate supports transaction operations in two ways: through the CrudOps
trait
methods directly on a Transaction
object, or through the helper functions provided
in the transactional
module.
§Using CrudOps with Transaction
use postgres::{Client, NoTls, Error};
use parsql::postgres::CrudOps;
use parsql::macros::{Insertable, SqlParams, Updateable, UpdateParams};
#[derive(Insertable, SqlParams)]
#[table("users")]
struct InsertUser {
name: String,
email: String,
}
#[derive(Updateable, UpdateParams)]
#[table("users")]
#[update("email")]
#[where_clause("id = $")]
struct UpdateUser {
id: i32,
email: String,
}
fn main() -> Result<(), Error> {
let mut client = Client::connect("host=localhost user=postgres", NoTls)?;
// Start a transaction
let mut tx = client.transaction()?;
// Use CrudOps methods directly on the transaction
let insert_user = InsertUser {
name: "John".to_string(),
email: "john@example.com".to_string(),
};
let rows_affected = tx.insert(insert_user)?;
let update_user = UpdateUser {
id: 1,
email: "john.updated@example.com".to_string(),
};
let rows_updated = tx.update(update_user)?;
// Commit the transaction
tx.commit()?;
Ok(())
}
§Using Transaction Helper Functions
use postgres::{Client, NoTls, Error};
use parsql::postgres::transactional::{begin, tx_insert, tx_update};
use parsql::macros::{Insertable, SqlParams, Updateable, UpdateParams};
#[derive(Insertable, SqlParams)]
#[table("users")]
struct InsertUser {
name: String,
email: String,
}
#[derive(Updateable, UpdateParams)]
#[table("users")]
#[update("email")]
#[where_clause("id = $")]
struct UpdateUser {
id: i32,
email: String,
}
fn main() -> Result<(), Error> {
let mut client = Client::connect("host=localhost user=postgres", NoTls)?;
// Begin a transaction
let tx = begin(&mut client)?;
// Chain transaction operations
let insert_user = InsertUser {
name: "John".to_string(),
email: "john@example.com".to_string(),
};
let (tx, _) = tx_insert(tx, insert_user)?;
let update_user = UpdateUser {
id: 1,
email: "john.updated@example.com".to_string(),
};
let (tx, _) = tx_update(tx, update_user)?;
// Commit the transaction
tx.commit()?;
Ok(())
}
Re-exports§
pub use crud_ops::delete;
pub use crud_ops::get;
pub use crud_ops::get_all;
pub use crud_ops::get_by_query;
pub use crud_ops::insert;
pub use crud_ops::select;
pub use crud_ops::select_all;
pub use crud_ops::update;
pub use crud_ops::CrudOps;
pub use parsql_macros as macros;
Modules§
Structs§
- Client
- A synchronous PostgreSQL client.
- Error
- An error communicating with the Postgres server.
- Row
- A row of data returned from the database by a query.
- Transaction
- A representation of a PostgreSQL database transaction.
Traits§
- FromRow
- Trait for converting database rows to Rust structs.
This trait is implemented by the derive macro
FromRow
. - SqlParams
- Trait for providing SQL parameters.
This trait is implemented by the derive macro
SqlParams
. - SqlQuery
- Trait for generating SQL queries.
This trait is implemented by the derive macro
Queryable
,Insertable
,Updateable
, andDeletable
. - ToSql
- A trait for types that can be converted into Postgres values.
- Update
Params - Trait for providing UPDATE parameters.
This trait is implemented by the derive macro
UpdateParams
.