Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add transaction related method #360

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
15 changes: 15 additions & 0 deletions driver/src/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,21 @@ pub trait Connection: DynClone + Send + Sync {
))
}

async fn begin(&self) -> Result<()> {
self.exec("BEGIN").await?;
Ok(())
}

async fn commit(&self) -> Result<()> {
self.exec("COMMIT").await?;
Ok(())
}

async fn rollback(&self) -> Result<()> {
self.exec("ROLLBACK").await?;
Ok(())
}

async fn get_files(&self, stage: &str, local_file: &str) -> Result<RowStatsIterator> {
let mut total_count: usize = 0;
let mut total_size: usize = 0;
Expand Down
2 changes: 1 addition & 1 deletion driver/src/flight_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ impl Args {
return Err(Error::BadArgument(format!(
"Invalid value for sslmode: {}",
v.as_ref()
)))
)));
}
},
"tls_ca_file" => args.tls_ca_file = Some(v.to_string()),
Expand Down
3 changes: 2 additions & 1 deletion driver/tests/driver/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
// See the License for the specific language governing permissions and
// limitations under the License.

pub static DEFAULT_DSN: &str = "databend://root:@localhost:8000/default?sslmode=disable";
pub static DEFAULT_DSN: &str =
"databend://databend:databend@localhost:8000/default?sslmode=disable";
1 change: 1 addition & 0 deletions driver/tests/driver/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ mod load;
mod select_iter;
mod select_simple;
mod session;
mod transaction;
63 changes: 63 additions & 0 deletions driver/tests/driver/transaction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2021 Datafuse Labs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use databend_driver::Client;

use crate::common::DEFAULT_DSN;

#[tokio::test]
async fn test_commit() {
let dsn = option_env!("TEST_DATABEND_DSN").unwrap_or(DEFAULT_DSN);
let client = Client::new(dsn.to_string());
let conn = client.get_conn().await.unwrap();

conn.exec("CREATE OR REPLACE TABLE t(c int);")
.await
.unwrap();
conn.begin().await.unwrap();
conn.exec("INSERT INTO t VALUES(1);").await.unwrap();
let row = conn.query_row("SELECT * FROM t").await.unwrap();
let row = row.unwrap();
let (val,): (i32,) = row.try_into().unwrap();
assert_eq!(val, 1);
conn.commit().await.unwrap();
let row = conn.query_row("SELECT * FROM t").await.unwrap();
let row = row.unwrap();
let (val,): (i32,) = row.try_into().unwrap();
assert_eq!(val, 1);
}

#[tokio::test]
async fn test_rollback() {
let dsn = option_env!("TEST_DATABEND_DSN").unwrap_or(DEFAULT_DSN);
let client = Client::new(dsn.to_string());
let conn = client.get_conn().await.unwrap();

conn.exec("CREATE OR REPLACE TABLE t(c int);")
.await
.unwrap();
conn.begin().await.unwrap();
conn.exec("INSERT INTO t VALUES(1);").await.unwrap();
let row = conn.query_row("SELECT * FROM t").await.unwrap();
let row = row.unwrap();
let (val,): (i32,) = row.try_into().unwrap();
assert_eq!(val, 1);

conn.rollback().await.unwrap();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should recheck after rollback?


let client = Client::new(dsn.to_string());
let conn = client.get_conn().await.unwrap();
let row = conn.query_row("SELECT * FROM t").await.unwrap();
assert!(row.is_none());
}
Loading