-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathhello_world.rs
More file actions
121 lines (104 loc) · 3.75 KB
/
hello_world.rs
File metadata and controls
121 lines (104 loc) · 3.75 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// 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(all(not(target_arch = "wasm32"), feature = "orm"))]
mod app {
use kite_sql::db::{DataBaseBuilder, Database};
use kite_sql::errors::DatabaseError;
use kite_sql::storage::Storage;
use kite_sql::Model;
use std::env;
use std::fs;
use std::io::ErrorKind;
use std::path::Path;
const EXAMPLE_DB_PATH: &str = "./example_data/hello_world";
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(())
}
#[derive(Default, Debug, PartialEq, Model)]
#[model(table = "my_struct")]
pub struct MyStruct {
#[model(primary_key)]
pub c1: i32,
pub c2: String,
}
fn run_with_database<S: Storage>(database: Database<S>) -> Result<(), DatabaseError> {
database.create_table_if_not_exists::<MyStruct>()?;
database.insert(&MyStruct {
c1: 0,
c2: "zero".to_string(),
})?;
database.insert(&MyStruct {
c1: 1,
c2: "one".to_string(),
})?;
database.insert(&MyStruct {
c1: 2,
c2: "two".to_string(),
})?;
database
.from::<MyStruct>()
.eq(MyStruct::c1(), 1)
.update()
.set(MyStruct::c2(), "ONE")
.execute()?;
database.from::<MyStruct>().eq(MyStruct::c1(), 2).delete()?;
for row in database.fetch::<MyStruct>()? {
println!("{:?}", row?);
}
let mut agg = database.run("select count(*) from my_struct")?;
if let Some(count_row) = agg.next() {
println!("row count = {:?}", count_row?);
}
agg.done()?;
database.drop_table::<MyStruct>()?;
Ok(())
}
pub fn run() -> Result<(), DatabaseError> {
reset_example_dir()?;
let backend = env::var("KITESQL_BACKEND").unwrap_or_else(|_| "rocksdb".to_string());
match backend.to_ascii_lowercase().as_str() {
#[cfg(feature = "rocksdb")]
"rocksdb" => run_with_database(DataBaseBuilder::path(EXAMPLE_DB_PATH).build_rocksdb()?),
#[cfg(feature = "lmdb")]
"lmdb" => run_with_database(DataBaseBuilder::path(EXAMPLE_DB_PATH).build_lmdb()?),
other => Err(DatabaseError::InvalidValue(format!(
"unsupported example backend '{other}', expected {}",
{
let mut expected = Vec::new();
#[cfg(feature = "rocksdb")]
expected.push("rocksdb");
#[cfg(feature = "lmdb")]
expected.push("lmdb");
expected.join(" or ")
}
))),
}
}
}
#[cfg(target_arch = "wasm32")]
fn main() {}
#[cfg(all(not(target_arch = "wasm32"), feature = "orm"))]
fn main() -> Result<(), kite_sql::errors::DatabaseError> {
app::run()
}
#[cfg(all(not(target_arch = "wasm32"), not(feature = "orm")))]
fn main() {}