|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | | -#![cfg(not(target_arch = "wasm32"))] |
| 15 | +#[cfg(not(target_arch = "wasm32"))] |
| 16 | +mod app { |
| 17 | + use kite_sql::db::{DataBaseBuilder, ResultIter}; |
| 18 | + use kite_sql::errors::DatabaseError; |
| 19 | + use kite_sql::implement_from_tuple; |
| 20 | + use kite_sql::types::value::DataValue; |
16 | 21 |
|
17 | | -use kite_sql::db::{DataBaseBuilder, ResultIter}; |
18 | | -use kite_sql::errors::DatabaseError; |
19 | | -use kite_sql::implement_from_tuple; |
20 | | -use kite_sql::types::value::DataValue; |
21 | | - |
22 | | -#[derive(Default, Debug, PartialEq)] |
23 | | -struct MyStruct { |
24 | | - pub c1: i32, |
25 | | - pub c2: String, |
26 | | -} |
| 22 | + #[derive(Default, Debug, PartialEq)] |
| 23 | + pub struct MyStruct { |
| 24 | + pub c1: i32, |
| 25 | + pub c2: String, |
| 26 | + } |
27 | 27 |
|
28 | | -implement_from_tuple!( |
29 | | - MyStruct, ( |
30 | | - c1: i32 => |inner: &mut MyStruct, value| { |
31 | | - if let DataValue::Int32(val) = value { |
32 | | - inner.c1 = val; |
| 28 | + implement_from_tuple!( |
| 29 | + MyStruct, ( |
| 30 | + c1: i32 => |inner: &mut MyStruct, value| { |
| 31 | + if let DataValue::Int32(val) = value { |
| 32 | + inner.c1 = val; |
| 33 | + } |
| 34 | + }, |
| 35 | + c2: String => |inner: &mut MyStruct, value| { |
| 36 | + if let DataValue::Utf8 { value, .. } = value { |
| 37 | + inner.c2 = value; |
| 38 | + } |
33 | 39 | } |
34 | | - }, |
35 | | - c2: String => |inner: &mut MyStruct, value| { |
36 | | - if let DataValue::Utf8 { value, .. } = value { |
37 | | - inner.c2 = value; |
38 | | - } |
39 | | - } |
40 | | - ) |
41 | | -); |
| 40 | + ) |
| 41 | + ); |
42 | 42 |
|
43 | | -#[cfg(feature = "macros")] |
44 | | -fn main() -> Result<(), DatabaseError> { |
45 | | - let database = DataBaseBuilder::path("./example_data/hello_world").build()?; |
| 43 | + pub fn run() -> Result<(), DatabaseError> { |
| 44 | + let database = DataBaseBuilder::path("./example_data/hello_world").build()?; |
46 | 45 |
|
47 | | - // 1) Create table and insert multiple rows with mixed types. |
48 | | - database |
49 | | - .run( |
50 | | - "create table if not exists my_struct ( |
51 | | - c1 int primary key, |
52 | | - c2 varchar, |
53 | | - c3 int |
54 | | - )", |
55 | | - )? |
56 | | - .done()?; |
57 | | - database |
58 | | - .run( |
59 | | - r#" |
60 | | - insert into my_struct values |
61 | | - (0, 'zero', 0), |
62 | | - (1, 'one', 1), |
63 | | - (2, 'two', 2) |
64 | | - "#, |
65 | | - )? |
66 | | - .done()?; |
| 46 | + database |
| 47 | + .run( |
| 48 | + "create table if not exists my_struct ( |
| 49 | + c1 int primary key, |
| 50 | + c2 varchar, |
| 51 | + c3 int |
| 52 | + )", |
| 53 | + )? |
| 54 | + .done()?; |
| 55 | + database |
| 56 | + .run( |
| 57 | + r#" |
| 58 | + insert into my_struct values |
| 59 | + (0, 'zero', 0), |
| 60 | + (1, 'one', 1), |
| 61 | + (2, 'two', 2) |
| 62 | + "#, |
| 63 | + )? |
| 64 | + .done()?; |
67 | 65 |
|
68 | | - // 2) Update and delete demo. |
69 | | - database |
70 | | - .run("update my_struct set c3 = c3 + 10 where c1 = 1")? |
71 | | - .done()?; |
72 | | - database.run("delete from my_struct where c1 = 2")?.done()?; |
| 66 | + database |
| 67 | + .run("update my_struct set c3 = c3 + 10 where c1 = 1")? |
| 68 | + .done()?; |
| 69 | + database.run("delete from my_struct where c1 = 2")?.done()?; |
73 | 70 |
|
74 | | - // 3) Query and deserialize into Rust struct. |
75 | | - let iter = database.run("select * from my_struct")?; |
76 | | - let schema = iter.schema().clone(); |
| 71 | + let iter = database.run("select * from my_struct")?; |
| 72 | + let schema = iter.schema().clone(); |
77 | 73 |
|
78 | | - for tuple in iter { |
79 | | - println!("{:?}", MyStruct::from((&schema, tuple?))); |
80 | | - } |
| 74 | + for tuple in iter { |
| 75 | + println!("{:?}", MyStruct::from((&schema, tuple?))); |
| 76 | + } |
| 77 | + |
| 78 | + let mut agg = database.run("select count(*) from my_struct")?; |
| 79 | + if let Some(count_row) = agg.next() { |
| 80 | + println!("row count = {:?}", count_row?); |
| 81 | + } |
| 82 | + agg.done()?; |
81 | 83 |
|
82 | | - // 4) Aggregate example. |
83 | | - let mut agg = database.run("select count(*) from my_struct")?; |
84 | | - if let Some(count_row) = agg.next() { |
85 | | - println!("row count = {:?}", count_row?); |
| 84 | + database.run("drop table my_struct")?.done()?; |
| 85 | + |
| 86 | + Ok(()) |
86 | 87 | } |
87 | | - agg.done()?; |
| 88 | +} |
88 | 89 |
|
89 | | - database.run("drop table my_struct")?.done()?; |
| 90 | +#[cfg(target_arch = "wasm32")] |
| 91 | +fn main() {} |
90 | 92 |
|
91 | | - Ok(()) |
| 93 | +#[cfg(all(not(target_arch = "wasm32"), feature = "macros"))] |
| 94 | +fn main() -> Result<(), kite_sql::errors::DatabaseError> { |
| 95 | + app::run() |
92 | 96 | } |
0 commit comments