This repository was archived by the owner on Jul 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.rs
More file actions
112 lines (94 loc) · 3.23 KB
/
Copy pathmain.rs
File metadata and controls
112 lines (94 loc) · 3.23 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
//! Very basic demo of using the ORM for optd-persistent.
//!
//! You may run into errors when you first clone this repository.
//! See the `README.md` for setup instructions.
#![allow(dead_code, unused_imports)]
use sea_orm::*;
use sea_orm_migration::prelude::*;
use serde_json::json;
mod entities;
mod migrator;
use entities::{prelude::*, *};
use optd_persistent::DATABASE_URL;
#[tokio::main]
async fn main() {
basic_demo().await;
memo_demo().await;
}
async fn memo_demo() {
let _db = Database::connect(DATABASE_URL).await.unwrap();
todo!()
}
async fn basic_demo() {
let db = Database::connect(DATABASE_URL).await.unwrap();
// Create a new `CascadesGroup`.
let group = cascades_group::ActiveModel {
latest_winner: ActiveValue::Set(None),
in_progress: ActiveValue::Set(false),
is_optimized: ActiveValue::Set(false),
..Default::default()
}
.save(&db)
.await
.unwrap();
// Create a new logical expression.
let l_expr = logical_expression::ActiveModel {
group_id: group.id.clone(),
fingerprint: ActiveValue::Set(42), // Example fingerprint
variant_tag: ActiveValue::Set(1), // Example variant tag
data: ActiveValue::Set(json!({ // Example operator
"type": "Scan",
"table": "lineitem",
"predicate": "l_quantity < 10",
})),
..Default::default()
}
.save(&db)
.await
.unwrap();
// Create a link between the group and the logical expression in the junction table.
let _link = logical_children::ActiveModel {
group_id: group.id.clone(),
logical_expression_id: l_expr.id.clone(),
}
.insert(&db)
.await
.unwrap();
// Basic lookup test on each table.
{
let groups: Vec<cascades_group::Model> = CascadesGroup::find().all(&db).await.unwrap();
assert_eq!(groups.len(), 1);
let l_expressions: Vec<logical_expression::Model> =
LogicalExpression::find().all(&db).await.unwrap();
assert_eq!(l_expressions.len(), 1);
}
// Retrieve all logical expressions that belong to this group with lazy loading.
{
let group = CascadesGroup::find_by_id(*group.id.try_as_ref().unwrap())
.one(&db)
.await
.unwrap()
.unwrap();
let group_expressions: Vec<logical_expression::Model> = group
.find_related(LogicalExpression)
.all(&db)
.await
.unwrap();
assert_eq!(group_expressions.len(), 1);
}
// Retrieve all logical expressions that belong to this group with eager loading.
{
let group_with_expressions: Vec<(cascades_group::Model, Vec<logical_expression::Model>)> =
CascadesGroup::find()
.find_with_related(LogicalExpression)
.all(&db)
.await
.unwrap();
assert_eq!(group_with_expressions.len(), 1);
assert_eq!(group_with_expressions[0].1.len(), 1);
}
// Clean up everything. Since everything is cascading, we only need to manually delete the group
// and then SeaORM will take care of the expression and the junction.
group.delete(&db).await.unwrap();
println!("Demo Finished!");
}