Trait CrudOps

Source
pub trait CrudOps {
    // Required methods
    fn insert<T: SqlQuery + SqlParams>(&self, entity: T) -> Result<usize, Error>;
    fn update<T: SqlQuery + UpdateParams>(
        &self,
        entity: T,
    ) -> Result<usize, Error>;
    fn delete<T: SqlQuery + SqlParams>(&self, entity: T) -> Result<usize, Error>;
    fn get<T: SqlQuery + FromRow + SqlParams>(
        &self,
        entity: &T,
    ) -> Result<T, Error>;
    fn get_all<T: SqlQuery + FromRow + SqlParams>(
        &self,
        entity: &T,
    ) -> Result<Vec<T>, Error>;
    fn select<T: SqlQuery + SqlParams, F, R>(
        &self,
        entity: &T,
        to_model: F,
    ) -> Result<R, Error>
       where F: Fn(&Row<'_>) -> Result<R, Error>;
    fn select_all<T: SqlQuery + SqlParams, F, R>(
        &self,
        entity: &T,
        to_model: F,
    ) -> Result<Vec<R>, Error>
       where F: Fn(&Row<'_>) -> Result<R, Error>;
}
Expand description

CrudOps trait defines the CRUD (Create, Read, Update, Delete) operations that can be performed on a SQLite database.

This trait is implemented for the rusqlite::Connection struct, allowing CRUD operations to be called as extension methods on a connection.

§Example

use rusqlite::{Connection, Result};
use parsql::sqlite::CrudOps;
use parsql::sqlite::macros::{Insertable, SqlParams, Queryable, FromRow};

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

#[derive(Queryable, FromRow, SqlParams)]
#[table("users")]
#[where_clause("id = ?")]
struct GetUser {
    id: i64,
    name: String,
    email: String,
}

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

Required Methods§

Source

fn insert<T: SqlQuery + SqlParams>(&self, entity: T) -> Result<usize, Error>

Inserts a new record into the SQLite database.

§Arguments
  • entity - Data object to be inserted (must implement SqlQuery and SqlParams traits)
§Returns
  • Result<usize, Error> - On success, returns the number of inserted records; on failure, returns Error
Source

fn update<T: SqlQuery + UpdateParams>(&self, entity: T) -> Result<usize, Error>

Updates records in the SQLite database.

§Arguments
  • entity - Data object containing the update information (must implement SqlQuery and UpdateParams traits)
§Returns
  • Result<usize, Error> - On success, returns the number of updated records; on failure, returns Error
Source

fn delete<T: SqlQuery + SqlParams>(&self, entity: T) -> Result<usize, Error>

Deletes records from the SQLite database.

§Arguments
  • entity - Data object containing delete conditions (must implement SqlQuery and SqlParams traits)
§Returns
  • Result<usize, Error> - On success, returns the number of deleted records; on failure, returns Error
Source

fn get<T: SqlQuery + FromRow + SqlParams>(&self, entity: &T) -> Result<T, Error>

Retrieves a single record from the SQLite database.

§Arguments
  • entity - Data object containing query parameters (must implement SqlQuery, FromRow, and SqlParams traits)
§Returns
  • Result<T, Error> - On success, returns the retrieved record; on failure, returns Error
Source

fn get_all<T: SqlQuery + FromRow + SqlParams>( &self, entity: &T, ) -> Result<Vec<T>, Error>

Retrieves multiple records from the SQLite database.

§Arguments
  • entity - Data object containing query parameters (must implement SqlQuery, FromRow, and SqlParams traits)
§Returns
  • Result<Vec<T>, Error> - On success, returns a vector of records; on failure, returns Error
Source

fn select<T: SqlQuery + SqlParams, F, R>( &self, entity: &T, to_model: F, ) -> Result<R, Error>
where F: Fn(&Row<'_>) -> Result<R, Error>,

Executes a custom query and transforms the result using the provided function.

§Arguments
  • entity - Data object containing query parameters (must implement SqlQuery and SqlParams traits)
  • to_model - Function to transform the database row into the desired type
§Returns
  • Result<R, Error> - On success, returns the transformed result; on failure, returns Error
Source

fn select_all<T: SqlQuery + SqlParams, F, R>( &self, entity: &T, to_model: F, ) -> Result<Vec<R>, Error>
where F: Fn(&Row<'_>) -> Result<R, Error>,

Executes a custom query and transforms all results using the provided function.

§Arguments
  • entity - Data object containing query parameters (must implement SqlQuery and SqlParams traits)
  • to_model - Function to transform database rows into the desired type
§Returns
  • Result<Vec<R>, Error> - On success, returns a vector of transformed results; on failure, returns Error

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl<'conn> CrudOps for Transaction<'conn>

Implementation of CrudOps for Transaction

Source§

fn insert<T: SqlQuery + SqlParams>(&self, entity: T) -> Result<usize, Error>

Inserts a record into the database and returns the number of rows affected. This function is an extension to the Transaction struct and is available when the CrudOps trait is in scope.

§Arguments
  • entity - A struct that implements Insertable and SqlParams traits
§Returns
  • Result<usize, Error> - Number of affected rows or an error
§Example
use rusqlite::{Connection, Result};
use parsql::sqlite::CrudOps;
use parsql::sqlite::transactional;
use parsql::macros::{Insertable, SqlParams};

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

fn main() -> Result<()> {
    let conn = Connection::open("test.db")?;
    let tx = transactional::begin(&conn)?;
     
    let user = InsertUser {
        name: "John".to_string(),
        email: "john@example.com".to_string(),
    };
     
    let rows_affected = tx.insert(user)?;
     
    tx.commit()?;
    Ok(())
}
Source§

fn update<T: SqlQuery + UpdateParams>(&self, entity: T) -> Result<usize, Error>

Updates a record in the database and returns the number of rows affected. This function is an extension to the Transaction struct and is available when the CrudOps trait is in scope.

§Arguments
  • entity - A struct that implements Updateable and UpdateParams traits
§Returns
  • Result<usize, Error> - Number of affected rows or an error
§Example
use rusqlite::{Connection, Result};
use parsql::sqlite::CrudOps;
use parsql::sqlite::transactional;
use parsql::macros::{Updateable, UpdateParams};

#[derive(Updateable, UpdateParams)]
#[table("users")]
#[update("name, email")]
#[where_clause("id = ?")]
struct UpdateUser {
    id: i64,
    name: String,
    email: String,
}

fn main() -> Result<()> {
    let conn = Connection::open("test.db")?;
    let tx = transactional::begin(&conn)?;
     
    let user = UpdateUser {
        id: 1,
        name: "John Doe".to_string(),
        email: "john.doe@example.com".to_string(),
    };
     
    let rows_affected = tx.update(user)?;
     
    tx.commit()?;
    Ok(())
}
Source§

fn delete<T: SqlQuery + SqlParams>(&self, entity: T) -> Result<usize, Error>

Deletes a record from the database and returns the number of rows affected. This function is an extension to the Transaction struct and is available when the CrudOps trait is in scope.

§Arguments
  • entity - A struct that implements Deletable and SqlParams traits
§Returns
  • Result<usize, Error> - Number of affected rows or an error
§Example
use rusqlite::{Connection, Result};
use parsql::sqlite::CrudOps;
use parsql::sqlite::transactional;
use parsql::macros::{Deletable, SqlParams};

#[derive(Deletable, SqlParams)]
#[table("users")]
#[where_clause("id = ?")]
struct DeleteUser {
    id: i64,
}

fn main() -> Result<()> {
    let conn = Connection::open("test.db")?;
    let tx = transactional::begin(&conn)?;
     
    let user = DeleteUser { id: 1 };
     
    let rows_affected = tx.delete(user)?;
     
    tx.commit()?;
    Ok(())
}
Source§

fn get<T: SqlQuery + FromRow + SqlParams>(&self, entity: &T) -> Result<T, Error>

Gets a single record from the database and converts it to a struct. This function is an extension to the Transaction struct and is available when the CrudOps trait is in scope.

§Arguments
  • entity - A struct that implements Queryable, SqlParams, and FromRow traits
§Returns
  • Result<T, Error> - The retrieved record as a struct or an error
§Example
use rusqlite::{Connection, Result};
use parsql::sqlite::CrudOps;
use parsql::sqlite::transactional;
use parsql::macros::{Queryable, SqlParams, FromRow};

#[derive(Queryable, SqlParams, FromRow)]
#[table("users")]
#[where_clause("id = ?")]
struct GetUser {
    id: i64,
    name: String,
    email: String,
}

fn main() -> Result<()> {
    let conn = Connection::open("test.db")?;
    let tx = transactional::begin(&conn)?;
     
    let param = GetUser {
        id: 1,
        name: String::new(),
        email: String::new(),
    };
     
    let user = tx.get(&param)?;
     
    tx.commit()?;
    println!("Found user: {} - {}", user.name, user.email);
    Ok(())
}
Source§

fn get_all<T: SqlQuery + FromRow + SqlParams>( &self, entity: &T, ) -> Result<Vec<T>, Error>

Gets multiple records from the database and converts them to a vector of structs. This function is an extension to the Transaction struct and is available when the CrudOps trait is in scope.

§Arguments
  • entity - A struct that implements Queryable, SqlParams, and FromRow traits
§Returns
  • Result<Vec<T>, Error> - A vector of retrieved records as structs or an error
§Example
use rusqlite::{Connection, Result};
use parsql::sqlite::CrudOps;
use parsql::sqlite::transactional;
use parsql::macros::{Queryable, SqlParams, FromRow};

#[derive(Queryable, SqlParams, FromRow)]
#[table("users")]
#[where_clause("email LIKE ?")]
struct GetUsers {
    id: i64,
    name: String,
    email: String,
}

fn main() -> Result<()> {
    let conn = Connection::open("test.db")?;
    let tx = transactional::begin(&conn)?;
     
    let param = GetUsers {
        id: 0,
        name: String::new(),
        email: "%example.com".to_string(),
    };
     
    let users = tx.get_all(&param)?;
     
    tx.commit()?;
    for user in users {
        println!("Found user: {} - {}", user.name, user.email);
    }
    Ok(())
}
Source§

fn select<T: SqlQuery + SqlParams, F, R>( &self, entity: &T, to_model: F, ) -> Result<R, Error>
where F: Fn(&Row<'_>) -> Result<R, Error>,

Executes a custom SELECT query and transforms the result using a provided function. This function is an extension to the Transaction struct and is available when the CrudOps trait is in scope.

§Arguments
  • entity - Data object containing query parameters
  • to_model - Function to transform a row into a value
§Returns
  • Result<R, Error> - The transformed value or an error
§Example
use rusqlite::{Connection, Result};
use parsql::sqlite::CrudOps;
use parsql::sqlite::transactional;
use parsql::macros::{Queryable, SqlParams};

#[derive(Queryable, SqlParams)]
#[table("users")]
#[where_clause("email LIKE ?")]
struct CountUsers {
    email: String,
}

fn main() -> Result<()> {
    let conn = Connection::open("test.db")?;
    let tx = transactional::begin(&conn)?;
     
    let param = CountUsers {
        email: "%example.com".to_string(),
    };
     
    let count: i64 = tx.select(&param, |row| row.get(0))?;
     
    tx.commit()?;
    println!("Number of users: {}", count);
    Ok(())
}
Source§

fn select_all<T: SqlQuery + SqlParams, F, R>( &self, entity: &T, to_model: F, ) -> Result<Vec<R>, Error>
where F: Fn(&Row<'_>) -> Result<R, Error>,

Executes a custom SELECT query and transforms all results using a provided function. This function is an extension to the Transaction struct and is available when the CrudOps trait is in scope.

§Arguments
  • entity - Data object containing query parameters
  • to_model - Function to transform rows into values
§Returns
  • Result<Vec<R>, Error> - A vector of transformed values or an error
§Example
use rusqlite::{Connection, Result};
use parsql::sqlite::CrudOps;
use parsql::sqlite::transactional;
use parsql::macros::{Queryable, SqlParams};

#[derive(Queryable, SqlParams)]
#[table("users")]
#[where_clause("email LIKE ?")]
struct GetUserNames {
    email: String,
}

fn main() -> Result<()> {
    let conn = Connection::open("test.db")?;
    let tx = transactional::begin(&conn)?;
     
    let param = GetUserNames {
        email: "%example.com".to_string(),
    };
     
    let names: Vec<String> = tx.select_all(&param, |row| row.get(0))?;
     
    tx.commit()?;
    for name in names {
        println!("User name: {}", name);
    }
    Ok(())
}

Implementors§