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
  • Extension methods for the Client object

§Usage

use postgres::{Client, NoTls, Error};
use parsql::postgres::{fetch, 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: "[email protected]".to_string(),
    };

    let id = insert(&mut client, insert_user)?;

    // Get the user back
    let get_user = GetUser::new(id as i32);
    let user = fetch(&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: "[email protected]".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.fetch(&get_user)?;

    println!("User: {:?}", user);
    Ok(())
}

§Using Transactions

You can also use transactions to ensure atomicity of operations:

use postgres::{Client, NoTls, Error};
use parsql::postgres::transactional::{begin, tx_insert, tx_update};

#[derive(Insertable, SqlParams)]
#[table("users")]
pub struct InsertUser {
    pub name: String,
    pub email: String,
}

#[derive(Updateable, UpdateParams)]
#[table("users")]
#[where_clause("id = $1")]
pub struct UpdateUser {
    pub id: i32,
    pub email: String,
}

fn main() -> Result<(), Error> {
    let mut client = Client::connect(
        "host=localhost user=postgres dbname=test",
        NoTls,
    )?;

    // Start a transaction
    let tx = begin(&mut client)?;

    // Insert a new user within the transaction
    let insert_user = InsertUser {
        name: "John".to_string(),
        email: "[email protected]".to_string(),
    };

    let (tx, _) = tx_insert(tx, insert_user)?;

    // Update the user within the same transaction
    let update_user = UpdateUser {
        id: 1,
        email: "[email protected]".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::fetch;
pub use crud_ops::fetch_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::get;Deprecated
pub use crud_ops::get_all;Deprecated
pub use macros::*;

Modules§

crud_ops
macros
traits
transaction_ops
transactional

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§

ToSql
A trait for types that can be converted into Postgres values.