-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathtransaction.rs
More file actions
94 lines (79 loc) · 2.84 KB
/
transaction.rs
File metadata and controls
94 lines (79 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2024 KipData/KiteSQL
//
// 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.
#[cfg(not(target_arch = "wasm32"))]
mod app {
use kite_sql::db::DataBaseBuilder;
use kite_sql::errors::DatabaseError;
use kite_sql::types::tuple::Tuple;
use kite_sql::types::value::DataValue;
use std::fs;
use std::io::ErrorKind;
use std::path::Path;
const EXAMPLE_DB_PATH: &str = "./example_data/transaction";
fn reset_example_dir() -> Result<(), DatabaseError> {
if let Err(err) = fs::remove_dir_all(EXAMPLE_DB_PATH) {
if err.kind() != ErrorKind::NotFound {
return Err(err.into());
}
}
if let Some(parent) = Path::new(EXAMPLE_DB_PATH).parent() {
fs::create_dir_all(parent)?;
}
Ok(())
}
pub fn run() -> Result<(), DatabaseError> {
reset_example_dir()?;
// Optimistic transactions are currently backed by RocksDB.
let database = DataBaseBuilder::path(EXAMPLE_DB_PATH).build_optimistic()?;
database
.run("create table if not exists t1 (c1 int primary key, c2 int)")?
.done()?;
let mut transaction = database.new_transaction()?;
transaction
.run("insert into t1 values(0, 0), (1, 1)")?
.done()?;
assert!(database.run("select * from t1")?.next().is_none());
transaction.commit()?;
let mut iter = database.run("select * from t1")?;
assert_eq!(
iter.next().unwrap()?,
Tuple::new(None, vec![DataValue::Int32(0), DataValue::Int32(0)])
);
assert_eq!(
iter.next().unwrap()?,
Tuple::new(None, vec![DataValue::Int32(1), DataValue::Int32(1)])
);
assert!(iter.next().is_none());
let mut tx2 = database.new_transaction()?;
tx2.run("update t1 set c2 = 99 where c1 = 0")?.done()?;
assert_eq!(
database
.run("select c2 from t1 where c1 = 0")?
.next()
.unwrap()?
.values[0]
.i32(),
Some(0)
);
drop(tx2);
database.run("drop table t1")?.done()?;
Ok(())
}
}
#[cfg(target_arch = "wasm32")]
fn main() {}
#[cfg(not(target_arch = "wasm32"))]
fn main() -> Result<(), kite_sql::errors::DatabaseError> {
app::run()
}