pub struct ReadWriteTransaction { /* private fields */ }Expand description
A read-write transaction.
Implementations§
Source§impl ReadWriteTransaction
impl ReadWriteTransaction
Sourcepub fn buffer(
&self,
mutations: impl IntoIterator<Item = Mutation>,
) -> Result<()>
pub fn buffer( &self, mutations: impl IntoIterator<Item = Mutation>, ) -> Result<()>
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();
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(
&self,
statement: impl Into<Statement>,
) -> Result<ResultSet>
pub async fn execute_query( &self, statement: impl Into<Statement>, ) -> Result<ResultSet>
Executes a query using this transaction.
Sourcepub async fn execute_read(
&self,
read: impl Into<ReadRequest>,
) -> Result<ResultSet>
pub async fn execute_read( &self, read: impl Into<ReadRequest>, ) -> 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(
&self,
statement: impl Into<Statement>,
) -> Result<i64>
pub async fn execute_update( &self, statement: impl Into<Statement>, ) -> Result<i64>
Executes an update using this transaction.
Sourcepub async fn execute_batch_update(
&self,
batch: impl Into<BatchDml>,
) -> Result<Vec<i64>>
pub async fn execute_batch_update( &self, batch: impl Into<BatchDml>, ) -> 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();
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
Auto Trait Implementations§
impl !Freeze for ReadWriteTransaction
impl !RefUnwindSafe for ReadWriteTransaction
impl !UnwindSafe for ReadWriteTransaction
impl Forget for ReadWriteTransaction
impl Move 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