-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Support transaction running SQL * feat: add examples * code fmt
- Loading branch information
Showing
8 changed files
with
196 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,7 @@ Cargo.lock | |
/.vscode | ||
/.idea | ||
/.obsidian | ||
.DS_Store | ||
.DS_Store | ||
|
||
/hello_world | ||
/transaction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use itertools::Itertools; | ||
use kip_sql::db::{Database, DatabaseError}; | ||
use kip_sql::implement_from_tuple; | ||
use kip_sql::types::tuple::Tuple; | ||
use kip_sql::types::value::DataValue; | ||
use kip_sql::types::LogicalType; | ||
|
||
#[derive(Default, Debug, PartialEq)] | ||
struct MyStruct { | ||
pub c1: i32, | ||
pub c2: String, | ||
} | ||
|
||
implement_from_tuple!( | ||
MyStruct, ( | ||
c1: i32 => |inner: &mut MyStruct, value| { | ||
if let DataValue::Int32(Some(val)) = value { | ||
inner.c1 = val; | ||
} | ||
}, | ||
c2: String => |inner: &mut MyStruct, value| { | ||
if let DataValue::Utf8(Some(val)) = value { | ||
inner.c2 = val; | ||
} | ||
} | ||
) | ||
); | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), DatabaseError> { | ||
let database = Database::with_kipdb("./hello_world").await?; | ||
|
||
let _ = database | ||
.run("create table if not exists my_struct (c1 int primary key, c2 int)") | ||
.await?; | ||
let _ = database | ||
.run("insert into my_struct values(0, 0), (1, 1)") | ||
.await?; | ||
let tuples = database | ||
.run("select * from my_struct") | ||
.await? | ||
.into_iter() | ||
.map(MyStruct::from) | ||
.collect_vec(); | ||
|
||
println!("{:#?}", tuples); | ||
|
||
let _ = database.run("drop table my_struct").await?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use kip_sql::db::{Database, DatabaseError}; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), DatabaseError> { | ||
let database = Database::with_kipdb("./transaction").await?; | ||
let mut tx_1 = database.new_transaction().await?; | ||
|
||
let _ = tx_1 | ||
.run("create table if not exists t1 (c1 int primary key, c2 int)") | ||
.await?; | ||
let _ = tx_1.run("insert into t1 values(0, 0), (1, 1)").await?; | ||
|
||
assert!(database.run("select * from t1").await.is_err()); | ||
|
||
tx_1.commit().await?; | ||
|
||
assert!(database.run("select * from t1").await.is_ok()); | ||
|
||
let _ = database.run("drop table t1").await?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters