Struct teil::SyncConnection

source ·
pub struct SyncConnection<'a> { /* private fields */ }
Expand description

Wrapper structure around a rusqlite connection (inside a Mutex) to allow for connection re-use

Implementations§

Performs a retrieve operation using the connection.

use teil::{SyncTeil, SyncConnection};

#[derive(SyncTeil)]
struct User {
    #[teil(primary_key)]
    name: String
}
 
let connection = SyncConnection::new().unwrap();
 
let user: User = match connection.retrieve("some_name") {
    Ok(Some(v)) => v,
    Ok(None) => {
        println!("user with name some_name not found");
        return;
    },
    Err(e) => {
        println!("could not retrieve object from db, {}", e);
        return;
    }
};

Performs a save operation using the connection.

use teil::{SyncTeil, SyncConnection};

#[derive(SyncTeil)]
struct User {
    #[teil(primary_key)]
    name: String
}
 
let connection = SyncConnection::new().unwrap();
 
let mut user = User{name: "some_name".to_string()};
match connection.save(&mut user) {
    Ok(_id) => {
        println!("saved user to the database!");
    },
    Err(e) => {
        println!("could not save object to db, {}", e);
    }
};

Performs a delete operation using the connection.

use teil::{SyncTeil, SyncConnection};

#[derive(SyncTeil)]
struct User {
    #[teil(primary_key)]
    name: String
}
 
let connection = SyncConnection::new().unwrap();
 
// Using the turbofish...
match connection.delete::<User, _>("some_name") {
    Ok(Some(_)) => (),
    Ok(None) => {
        println!("user with name some_name not found");
    },
    Err(e) => {
        println!("could not retrieve object from db, {}", e);
    }
};

Performs a full update to an object using the connection.

use teil::{SyncTeil, SyncConnection};

#[derive(SyncTeil)]
struct User {
    #[teil(primary_key)]
    name: String,
    amount: i32
}
 
let connection = SyncConnection::new().unwrap();
 
let mut user = User{name: "some_name".to_string(), amount: 10};
// Will update all non-primary keys (amount)
match connection.update_all(&user) {
    Ok(_) => {
        println!("saved user to the database!");
    },
    Err(e) => {
        println!("could not save object to db, {}", e);
    }
};

Performs a partial update to an object using the connection.

use teil::{SyncTeil, SyncConnection};

#[derive(SyncTeil)]
struct User {
    #[teil(primary_key)]
    name: String,
    amount: i32,
    base_amount: i32
}
 
let connection = SyncConnection::new().unwrap();
 
let user_update = User::update("some_name").set_base_amount(100);
// Will update only the base_amount
match connection.execute_update(user_update) {
    Ok(_) => {
        println!("updated user object in the database");
    },
    Err(e) => {
        println!("could not save object to db, {}", e);
    }
};

Performs a query with optional parameters using the connection.

This function is cumbersome to use, you should prefer execute_query instead.

use teil::{SyncTeil, SyncConnection};

#[derive(SyncTeil)]
struct User {
    #[teil(primary_key)]
    name: String
}
 
let connection = SyncConnection::new().unwrap();
 
// Gets all elements from the user table
match connection.query_vec::<User>(None, None, None, None) {
    Ok(_vals) => {
        println!("retrieved objects from the database");
        // Do something with the objects...
    },
    Err(e) => {
        println!("could not save object to db, {}", e);
    }
};

Executes a TeilQuery object using the connection

use teil::{SyncTeil, SyncConnection};

#[derive(SyncTeil)]
struct User {
    #[teil(primary_key)]
    name: String,
    amount: i32,
    base_amount: i32
}
 
let connection = SyncConnection::new().unwrap();
 
let user_query = User::query();
// Will update only the base_amount
match connection.execute_query(user_query) {
    Ok(_elems) => {
        println!("got elements from the database");
        // Do something with the elements
    },
    Err(e) => {
        println!("could not save object to db, {}", e);
    }
};

Counts the number of elements in the database with the given filters, using the connection

use teil::{SyncTeil, SyncConnection};

#[derive(SyncTeil)]
struct User {
    #[teil(primary_key)]
    name: String
}
 
let connection = SyncConnection::new().unwrap();
 
// Counts all the elements
match connection.count::<User>(vec![]) {
    Ok(num_elems) => {
        println!("there are {} elems!", num_elems);
    },
    Err(e) => {
        println!("could not get object count from the db, {}", e);
    }
};

Saves a group of elements to the database using the connection

use teil::{SyncTeil, SyncConnection};

#[derive(SyncTeil)]
struct User {
    #[teil(primary_key)]
    name: String
}
 
let mut connection = SyncConnection::new().unwrap();
 
let mut elems = vec![User{name: "Foo".to_string()}, User{name: "Bar".to_string()}];
// Counts all the elements
match connection.save_iter(&mut elems) {
    Ok(_) => {
        println!("saved all elements to the database");
    },
    Err(e) => {
        println!("could not save elements, {}", e);
    }
};

Returns the inner rusqlite connection

use teil::SyncConnection;
 
let connection = SyncConnection::new().unwrap().into_inner();

// Use the pool transaction, for example...
connection.execute("DELETE FROM some_table WHERE id = ?;", ["some_id"]).unwrap();

Trait Implementations§

The resulting type after dereferencing.
Dereferences the value.
Mutably dereferences the value.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more