Struct teil::Transaction
source · pub struct Transaction<'a> { /* private fields */ }Expand description
Wrapper structure around a postgres transaction
Implementations§
source§impl<'a> Transaction<'a>
impl<'a> Transaction<'a>
sourcepub fn new(transaction: Transaction<'a>) -> Transaction<'a>
pub fn new(transaction: Transaction<'a>) -> Transaction<'a>
Creates an instance of the (Transaction)Transaction structure
sourcepub async fn commit(self) -> Result<(), Error>
pub async fn commit(self) -> Result<(), Error>
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();sourcepub async fn rollback(self) -> Result<(), Error>
pub async fn rollback(self) -> Result<(), Error>
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();sourcepub async fn retrieve<T: Teil, A: Into<<T as Teil>::Id> + Send + Sync>(
&self,
id: A
) -> Result<Option<T>, Error>
pub async fn retrieve<T: Teil, A: Into<<T as Teil>::Id> + Send + Sync>(
&self,
id: A
) -> Result<Option<T>, Error>
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;
}
};sourcepub async fn save<T: Teil>(&self, teil: &mut T) -> Result<<T as Teil>::Id, Error>
pub async fn save<T: Teil>(&self, teil: &mut T) -> Result<<T as Teil>::Id, Error>
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);
}
};sourcepub async fn delete<T: Teil, A: Into<<T as Teil>::Id> + Send + Sync>(
&self,
id: A
) -> Result<Option<T>, Error>
pub async fn delete<T: Teil, A: Into<<T as Teil>::Id> + Send + Sync>(
&self,
id: A
) -> Result<Option<T>, Error>
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);
}
};sourcepub async fn update_all<T: Teil>(&self, teil: &T) -> Result<bool, Error>
pub async fn update_all<T: Teil>(&self, teil: &T) -> Result<bool, Error>
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);
}
};sourcepub async fn execute_update<U: Update>(&self, update: U) -> Result<bool, Error>
pub async fn execute_update<U: Update>(&self, update: U) -> Result<bool, Error>
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);
}
};sourcepub async fn query_vec<T: Teil>(
&self,
filters: Option<Vec<<T as Teil>::Filter>>,
sort: Option<Vec<<T as Teil>::Sort>>,
offset: Option<usize>,
limit: Option<usize>
) -> Result<Vec<T>, Error>
pub async fn query_vec<T: Teil>(
&self,
filters: Option<Vec<<T as Teil>::Filter>>,
sort: Option<Vec<<T as Teil>::Sort>>,
offset: Option<usize>,
limit: Option<usize>
) -> Result<Vec<T>, Error>
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);
}
};sourcepub async fn execute_query<T: Teil>(
&self,
teil_query: TeilQuery<T>
) -> Result<Vec<T>, Error>
pub async fn execute_query<T: Teil>(
&self,
teil_query: TeilQuery<T>
) -> Result<Vec<T>, Error>
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);
}
};sourcepub async fn count<T: Teil>(
&self,
filters: Vec<<T as Teil>::Filter>
) -> Result<i64, Error>
pub async fn count<T: Teil>(
&self,
filters: Vec<<T as Teil>::Filter>
) -> Result<i64, Error>
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);
}
};sourcepub async fn save_iter<'b, T: Teil, A: IntoIterator<Item = &'b mut T> + Send>(
&self,
elements: A
) -> Result<(), Error>
pub async fn save_iter<'b, T: Teil, A: IntoIterator<Item = &'b mut T> + Send>(
&self,
elements: A
) -> Result<(), Error>
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);
}
};sourcepub fn into_inner(self) -> Transaction<'a>
pub fn into_inner(self) -> Transaction<'a>
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>>§
sourcepub async fn prepare_cached(
&self,
query: &str
) -> impl Future<Output = Result<Statement, Error>>
pub async fn prepare_cached(
&self,
query: &str
) -> impl Future<Output = Result<Statement, Error>>
Like tokio_postgres::Transaction::prepare(), but uses an existing
Statement from the StatementCache if possible.
sourcepub async fn prepare_typed_cached(
&self,
query: &str,
types: &[Type]
) -> impl Future<Output = Result<Statement, Error>>
pub async fn prepare_typed_cached(
&self,
query: &str,
types: &[Type]
) -> impl Future<Output = Result<Statement, Error>>
Like tokio_postgres::Transaction::prepare_typed(), but uses an
existing Statement from the StatementCache if possible.
Methods from Deref<Target = Transaction<'a>>§
sourcepub async fn prepare(
&self,
query: &str
) -> impl Future<Output = Result<Statement, Error>>
pub async fn prepare(
&self,
query: &str
) -> impl Future<Output = Result<Statement, Error>>
Like Client::prepare.
sourcepub async fn prepare_typed(
&self,
query: &str,
parameter_types: &[Type]
) -> impl Future<Output = Result<Statement, Error>>
pub async fn prepare_typed(
&self,
query: &str,
parameter_types: &[Type]
) -> impl Future<Output = Result<Statement, Error>>
Like Client::prepare_typed.
sourcepub async fn query<T>(
&self,
statement: &T,
params: &[&(dyn ToSql + Sync)]
) -> impl Future<Output = Result<Vec<Row, Global>, Error>>where
T: ToStatement + ?Sized,
pub async fn query<T>(
&self,
statement: &T,
params: &[&(dyn ToSql + Sync)]
) -> impl Future<Output = Result<Vec<Row, Global>, Error>>where
T: ToStatement + ?Sized,
Like Client::query.
sourcepub async fn query_one<T>(
&self,
statement: &T,
params: &[&(dyn ToSql + Sync)]
) -> impl Future<Output = Result<Row, Error>>where
T: ToStatement + ?Sized,
pub async fn query_one<T>(
&self,
statement: &T,
params: &[&(dyn ToSql + Sync)]
) -> impl Future<Output = Result<Row, Error>>where
T: ToStatement + ?Sized,
Like Client::query_one.
sourcepub async fn query_opt<T>(
&self,
statement: &T,
params: &[&(dyn ToSql + Sync)]
) -> impl Future<Output = Result<Option<Row>, Error>>where
T: ToStatement + ?Sized,
pub async fn query_opt<T>(
&self,
statement: &T,
params: &[&(dyn ToSql + Sync)]
) -> impl Future<Output = Result<Option<Row>, Error>>where
T: ToStatement + ?Sized,
Like Client::query_opt.
sourcepub async fn query_raw<T, P, I>(
&self,
statement: &T,
params: I
) -> impl Future<Output = Result<RowStream, Error>>where
T: ToStatement + ?Sized,
P: BorrowToSql,
I: IntoIterator<Item = P>,
<I as IntoIterator>::IntoIter: ExactSizeIterator,
pub async fn query_raw<T, P, I>(
&self,
statement: &T,
params: I
) -> impl Future<Output = Result<RowStream, Error>>where
T: ToStatement + ?Sized,
P: BorrowToSql,
I: IntoIterator<Item = P>,
<I as IntoIterator>::IntoIter: ExactSizeIterator,
Like Client::query_raw.
sourcepub async fn execute<T>(
&self,
statement: &T,
params: &[&(dyn ToSql + Sync)]
) -> impl Future<Output = Result<u64, Error>>where
T: ToStatement + ?Sized,
pub async fn execute<T>(
&self,
statement: &T,
params: &[&(dyn ToSql + Sync)]
) -> impl Future<Output = Result<u64, Error>>where
T: ToStatement + ?Sized,
Like Client::execute.
sourcepub async fn execute_raw<P, I, T>(
&self,
statement: &T,
params: I
) -> impl Future<Output = Result<u64, Error>>where
T: ToStatement + ?Sized,
P: BorrowToSql,
I: IntoIterator<Item = P>,
<I as IntoIterator>::IntoIter: ExactSizeIterator,
pub async fn execute_raw<P, I, T>(
&self,
statement: &T,
params: I
) -> impl Future<Output = Result<u64, Error>>where
T: ToStatement + ?Sized,
P: BorrowToSql,
I: IntoIterator<Item = P>,
<I as IntoIterator>::IntoIter: ExactSizeIterator,
Like Client::execute_iter.
sourcepub async fn bind<T>(
&self,
statement: &T,
params: &[&(dyn ToSql + Sync)]
) -> impl Future<Output = Result<Portal, Error>>where
T: ToStatement + ?Sized,
pub async fn bind<T>(
&self,
statement: &T,
params: &[&(dyn ToSql + Sync)]
) -> impl Future<Output = Result<Portal, Error>>where
T: ToStatement + ?Sized,
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.
sourcepub async fn bind_raw<P, T, I>(
&self,
statement: &T,
params: I
) -> impl Future<Output = Result<Portal, Error>>where
T: ToStatement + ?Sized,
P: BorrowToSql,
I: IntoIterator<Item = P>,
<I as IntoIterator>::IntoIter: ExactSizeIterator,
pub async fn bind_raw<P, T, I>(
&self,
statement: &T,
params: I
) -> impl Future<Output = Result<Portal, Error>>where
T: ToStatement + ?Sized,
P: BorrowToSql,
I: IntoIterator<Item = P>,
<I as IntoIterator>::IntoIter: ExactSizeIterator,
A maximally flexible version of bind.
sourcepub async fn query_portal(
&self,
portal: &Portal,
max_rows: i32
) -> impl Future<Output = Result<Vec<Row, Global>, Error>>
pub async fn query_portal(
&self,
portal: &Portal,
max_rows: i32
) -> impl Future<Output = Result<Vec<Row, Global>, Error>>
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.
sourcepub async fn query_portal_raw(
&self,
portal: &Portal,
max_rows: i32
) -> impl Future<Output = Result<RowStream, Error>>
pub async fn query_portal_raw(
&self,
portal: &Portal,
max_rows: i32
) -> impl Future<Output = Result<RowStream, Error>>
The maximally flexible version of query_portal.
sourcepub async fn copy_in<T, U>(
&self,
statement: &T
) -> impl Future<Output = Result<CopyInSink<U>, Error>>where
T: ToStatement + ?Sized,
U: Buf + 'static + Send,
pub async fn copy_in<T, U>(
&self,
statement: &T
) -> impl Future<Output = Result<CopyInSink<U>, Error>>where
T: ToStatement + ?Sized,
U: Buf + 'static + Send,
Like Client::copy_in.
sourcepub async fn copy_out<T>(
&self,
statement: &T
) -> impl Future<Output = Result<CopyOutStream, Error>>where
T: ToStatement + ?Sized,
pub async fn copy_out<T>(
&self,
statement: &T
) -> impl Future<Output = Result<CopyOutStream, Error>>where
T: ToStatement + ?Sized,
Like Client::copy_out.
sourcepub async fn simple_query(
&self,
query: &str
) -> impl Future<Output = Result<Vec<SimpleQueryMessage, Global>, Error>>
pub async fn simple_query(
&self,
query: &str
) -> impl Future<Output = Result<Vec<SimpleQueryMessage, Global>, Error>>
Like Client::simple_query.
sourcepub async fn batch_execute(
&self,
query: &str
) -> impl Future<Output = Result<(), Error>>
pub async fn batch_execute(
&self,
query: &str
) -> impl Future<Output = Result<(), Error>>
Like Client::batch_execute.
sourcepub fn cancel_token(&self) -> CancelToken
pub fn cancel_token(&self) -> CancelToken
Like Client::cancel_token.
sourcepub async fn cancel_query<T>(
&self,
tls: T
) -> impl Future<Output = Result<(), Error>>where
T: MakeTlsConnect<Socket>,
👎Deprecated since 0.6.0: use Transaction::cancel_token() instead
pub async fn cancel_query<T>(
&self,
tls: T
) -> impl Future<Output = Result<(), Error>>where
T: MakeTlsConnect<Socket>,
Like Client::cancel_query.
sourcepub async fn cancel_query_raw<S, T>(
&self,
stream: S,
tls: T
) -> impl Future<Output = Result<(), Error>>where
S: AsyncRead + AsyncWrite + Unpin,
T: TlsConnect<S>,
👎Deprecated since 0.6.0: use Transaction::cancel_token() instead
pub async fn cancel_query_raw<S, T>(
&self,
stream: S,
tls: T
) -> impl Future<Output = Result<(), Error>>where
S: AsyncRead + AsyncWrite + Unpin,
T: TlsConnect<S>,
Like Client::cancel_query_raw.
sourcepub async fn transaction(
&mut self
) -> impl Future<Output = Result<Transaction<'_>, Error>>
pub async fn transaction(
&mut self
) -> impl Future<Output = Result<Transaction<'_>, Error>>
Like Client::transaction, but creates a nested transaction via a savepoint.