pub struct ReadWriteTransaction { /* private fields */ }Expand description
A read-write transaction.
Implementations§
Source§impl ReadWriteTransaction
impl ReadWriteTransaction
Sourcepub fn buffer<I>(&self, mutations: I) -> Result<()>where
I: IntoIterator<Item = Mutation>,
pub fn buffer<I>(&self, mutations: I) -> Result<()>where
I: IntoIterator<Item = Mutation>,
Buffers one or more mutations to be applied when the transaction commits.
§Example
let db_client = spanner.database_client("projects/p/instances/i/databases/d").build().await?;
let runner = db_client.read_write_transaction().build().await?;
runner.run(async |tx| {
let mutation = Mutation::new_insert_builder("users")
.set("id").to(&1)
.build();
tx.buffer([mutation])?;
Ok(())
}).await?;Sourcepub async fn execute_query<T: Into<Statement>>(
&self,
statement: T,
) -> Result<ResultSet>
pub async fn execute_query<T: Into<Statement>>( &self, statement: T, ) -> Result<ResultSet>
Executes a query using this transaction.
Sourcepub async fn execute_read<T: Into<ReadRequest>>(
&self,
read: T,
) -> Result<ResultSet>
pub async fn execute_read<T: Into<ReadRequest>>( &self, read: T, ) -> Result<ResultSet>
Reads rows from the database using key lookups and scans, as a simple key/value style alternative to execute_query.
Sourcepub async fn execute_update<T: Into<Statement>>(
&self,
statement: T,
) -> Result<i64>
pub async fn execute_update<T: Into<Statement>>( &self, statement: T, ) -> Result<i64>
Executes an update using this transaction.
Sourcepub async fn execute_batch_update<T: Into<BatchDml>>(
&self,
batch: T,
) -> Result<Vec<i64>>
pub async fn execute_batch_update<T: Into<BatchDml>>( &self, batch: T, ) -> Result<Vec<i64>>
Executes a batch of DML statements using this transaction.
§Example
let db_client = spanner.database_client("projects/p/instances/i/databases/d").build().await?;
let runner = db_client.read_write_transaction().build().await?;
let result = runner.run(async |transaction| {
let statement1 = Statement::builder("UPDATE users SET active = true WHERE id = @id")
.add_param("id", &1)
.build();
let statement2 = Statement::builder("UPDATE users SET active = true WHERE id = @id")
.add_param("id", &2)
.build();
let batch = BatchDml::builder()
.add_statement(statement1)
.add_statement(statement2)
.build();
let update_counts = transaction.execute_batch_update(batch).await?;
Ok(())
}).await?;If a BatchDml request fails halfway through execution, execute_batch_update will return a
BatchUpdateError indicating exactly which statements succeeded (and their respective update counts)
before the batch execution failed.
§Error Handling Example
let statement1 = Statement::builder("UPDATE users SET active = true WHERE id = 1").build();
let statement2 = Statement::builder("UPDATE non_existent_table SET active = true WHERE id = 2").build();
let batch = BatchDml::builder()
.add_statement(statement1)
.add_statement(statement2)
.build();
match transaction.execute_batch_update(batch).await {
Ok(update_counts) => {
println!("All statements succeeded. Update counts: {:?}", update_counts);
}
Err(e) => {
if let Some(batch_error) = BatchUpdateError::extract(&e) {
println!("Batch execution failed. Successful update counts: {:?}", batch_error.update_counts);
} else {
println!("RPC failed or internal error occurred: {}", e);
}
}
}Trait Implementations§
Source§impl Clone for ReadWriteTransaction
impl Clone for ReadWriteTransaction
Source§fn clone(&self) -> ReadWriteTransaction
fn clone(&self) -> ReadWriteTransaction
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl !Freeze for ReadWriteTransaction
impl !RefUnwindSafe for ReadWriteTransaction
impl !UnwindSafe for ReadWriteTransaction
impl Send for ReadWriteTransaction
impl Sync for ReadWriteTransaction
impl Unpin for ReadWriteTransaction
impl UnsafeUnpin for ReadWriteTransaction
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request