Struct teil::Transaction

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

Wrapper structure around a postgres transaction

Implementations§

Creates an instance of the (Transaction)Transaction structure

Commits all the changes and consumes the transaction

use teil::{Connection};
 
let mut connection = Connection::new().await.unwrap();
let transaction = connection.transaction().await.unwrap();
 
// We use the transaction somehow
 
transaction.commit().await.unwrap();

Performs a rollback to the changes and consumes the transaction

use teil::{Connection};
 
let mut connection = Connection::new().await.unwrap();
let transaction = connection.transaction().await.unwrap();
 
// We use the transaction somehow
 
transaction.rollback().await.unwrap();

Performs a retrieve operation using the transaction.

use teil::{Teil, Connection};

#[derive(Teil)]
struct User {
    #[teil(primary_key)]
    name: String
}
 
let mut connection = Connection::new().await.unwrap();
let transaction = connection.transaction().await.unwrap();
 
let user: User = match transaction.retrieve("some_name").await {
    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 transaction.

use teil::{Teil, Connection};

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

Performs a delete operation using the transaction.

use teil::{Teil, Connection};

#[derive(Teil)]
struct User {
    #[teil(primary_key)]
    name: String
}
 
let mut connection = Connection::new().await.unwrap();
let transaction = connection.transaction().await.unwrap();
 
// Using the turbofish...
match transaction.delete::<User, _>("some_name").await {
    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 transaction.

use teil::{Teil, Connection};

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

Performs a partial update to an object using the transaction.

use teil::{Teil, Connection};

#[derive(Teil)]
struct User {
    #[teil(primary_key)]
    name: String,
    amount: i32,
    base_amount: i32
}
 
let mut connection = Connection::new().await.unwrap();
let transaction = connection.transaction().await.unwrap();
 
let user_update = User::update("some_name").set_base_amount(100);
// Will update only the base_amount
match transaction.execute_update(user_update).await {
    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 transaction.

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

use teil::{Teil, Connection};

#[derive(Teil)]
struct User {
    #[teil(primary_key)]
    name: String
}
 
let mut connection = Connection::new().await.unwrap();
let transaction = connection.transaction().await.unwrap();
 
// Gets all elements from the user table
match transaction.query_vec::<User>(None, None, None, None).await {
    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 transaction

use teil::{Teil, Connection};

#[derive(Teil)]
struct User {
    #[teil(primary_key)]
    name: String,
    amount: i32,
    base_amount: i32
}
 
let mut connection = Connection::new().await.unwrap();
let transaction = connection.transaction().await.unwrap();
 
let user_query = User::query();
// We query all values from the database
match transaction.execute_query(user_query).await {
    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 transaction

use teil::{Teil, Connection};

#[derive(Teil)]
struct User {
    #[teil(primary_key)]
    name: String
}
 
let mut connection = Connection::new().await.unwrap();
let transaction = connection.transaction().await.unwrap();
 
// Counts all the elements
match transaction.count::<User>(vec![]).await {
    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 transaction

use teil::{Teil, Connection};

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

Returns the inner deadpool transaction

use teil::Connection;
 
let mut connection = Connection::new().await.unwrap();
let transaction = connection.transaction().await.unwrap().into_inner();

// Use the pool transaction, for example...
transaction.execute("DELETE FROM some_table;", &[]).await.unwrap();

Methods from Deref<Target = Transaction<'a>>§

Like tokio_postgres::Transaction::prepare(), but uses an existing Statement from the StatementCache if possible.

Like tokio_postgres::Transaction::prepare_typed(), but uses an existing Statement from the StatementCache if possible.

Methods from Deref<Target = Transaction<'a>>§

Like Client::prepare.

Like Client::prepare_typed.

Like Client::query.

Like Client::query_one.

Like Client::query_opt.

Like Client::query_raw.

Like Client::execute.

Like Client::execute_iter.

Binds a statement to a set of parameters, creating a Portal which can be incrementally queried.

Portals only last for the duration of the transaction in which they are created, and can only be used on the connection that created them.

Panics

Panics if the number of parameters provided does not match the number expected.

A maximally flexible version of bind.

Continues execution of a portal, returning a stream of the resulting rows.

Unlike query, portals can be incrementally evaluated by limiting the number of rows returned in each call to query_portal. If the requested number is negative or 0, all rows will be returned.

The maximally flexible version of query_portal.

Like Client::copy_in.

Like Client::copy_out.

Like Client::simple_query.

Like Client::batch_execute.

Like Client::cancel_token.

👎Deprecated since 0.6.0: use Transaction::cancel_token() instead

Like Client::cancel_query.

👎Deprecated since 0.6.0: use Transaction::cancel_token() instead

Like Client::cancel_query_raw.

Like Client::transaction, but creates a nested transaction via a savepoint.

Like Client::transaction, but creates a nested transaction via a savepoint with the specified name.

Returns a reference to the underlying Client.

Trait Implementations§

The resulting type after dereferencing.
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