Crate parsql_postgres

Source
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

§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(())
}

Re-exports§

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::delete;
pub use crud_ops::get;
pub use crud_ops::get_all;
pub use crud_ops::get_by_query;
pub use parsql_macros as macros;

Modules§

crud_ops

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.

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, and Deletable.
ToSql
A trait for types that can be converted into Postgres values.
UpdateParams
Trait for providing UPDATE parameters. This trait is implemented by the derive macro UpdateParams.