Crate parsql_sqlite

Source
Expand description

§parsql-sqlite

SQLite integration for parsql. This crate provides synchronous APIs for working with SQLite databases.

§Features

  • Synchronous SQLite operations
  • Automatic SQL query generation
  • Secure parameter management
  • Generic CRUD operations
  • Transaction support
  • Extension methods for the Connection object

§Usage

use rusqlite::{Connection, Result};
use parsql::sqlite::{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<()> {
    let conn = Connection::open("test.db")?;
     
    // Insert a new user
    let insert_user = InsertUser {
        name: "John".to_string(),
        email: "john@example.com".to_string(),
    };
     
    let id = insert(&conn, insert_user)?;
     
    // Get the user back
    let get_user = GetUser::new(id as i32);
    let user = fetch(&conn, &get_user)?;
     
    println!("User: {:?}", user);
    Ok(())
}

§Using Extension Methods

You can also use the extension methods directly on the Connection object:

use rusqlite::{Connection, Result};
use parsql::sqlite::CrudOps;  // Import the trait
use parsql::sqlite::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 = ?")]
pub struct GetUser {
    pub id: i32,
    pub name: String,
    pub email: String,
}

fn main() -> Result<()> {
    let conn = Connection::open("test.db")?;
     
    // Insert a new user using extension method
    let insert_user = InsertUser {
        name: "John".to_string(),
        email: "john@example.com".to_string(),
    };
     
    let rows_affected = conn.insert(insert_user)?;
     
    // Get the user back using extension method
    let get_user = GetUser {
        id: 1,
        name: String::new(),
        email: String::new(),
    };
    let user = conn.fetch(&get_user)?;
     
    println!("User: {:?}", user);
    Ok(())
}

§Using Transactions

You can perform database operations within a transaction to ensure atomicity:

use rusqlite::{Connection, Result};
use parsql::sqlite::transactional;
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: i64,
    email: String,
}
 
fn main() -> Result<()> {
    let conn = Connection::open("test.db")?;
     
    // Begin a transaction
    let tx = transactional::begin(&conn)?;
     
    // Insert a user within the transaction
    let insert_user = InsertUser {
        name: "John".to_string(),
        email: "john@example.com".to_string(),
    };
    let (tx, _) = transactional::tx_insert(tx, insert_user)?;
     
    // Update the user within the same transaction
    let update_user = UpdateUser {
        id: 1,
        email: "john.updated@example.com".to_string(),
    };
    let (tx, _) = transactional::tx_update(tx, update_user)?;
     
    // Commit the transaction - both operations succeed or fail together
    tx.commit()?;
     
    Ok(())
}

§Installation

Add to your Cargo.toml file as follows:

[dependencies]
parsql = { version = "0.3.7", features = ["sqlite"] }

or if you want to use this package directly:

[dependencies]
parsql-sqlite = "0.3.7"
parsql-macros = "0.3.7"

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::fetch;
pub use crud_ops::fetch_all;
pub use transactional_ops as transactional;
pub use macros::*;

Modules§

crud_ops
macros
traits
transactional_ops
Transaction operations for SQLite

Structs§

Connection
A connection to a SQLite database.
Row
A single result row of a query.

Enums§

Error
Enum listing possible errors from rusqlite.

Traits§

ToSql
A trait for types that can be converted into SQLite values. Returns Error::ToSqlConversionFailure if the conversion fails.