rust_store_core/dialect/
ir.rs1use serde_json::{json, Value};
4
5#[derive(Debug, Clone, PartialEq)]
7pub struct SqlStmt {
8 pub text: String,
10 pub params: Vec<Value>,
12 pub is_write: bool,
14 pub row_shape: Option<RowShape>,
16 pub returning: Vec<String>,
18}
19
20impl SqlStmt {
21 pub fn select(text: String, params: Vec<Value>, row_shape: RowShape) -> Self {
22 SqlStmt {
23 text,
24 params,
25 is_write: false,
26 row_shape: Some(row_shape),
27 returning: Vec::new(),
28 }
29 }
30
31 pub fn write(text: String, params: Vec<Value>) -> Self {
32 SqlStmt {
33 text,
34 params,
35 is_write: true,
36 row_shape: None,
37 returning: Vec::new(),
38 }
39 }
40
41 pub fn to_value(&self) -> Value {
42 json!({
43 "text": self.text,
44 "params": self.params,
45 "isWrite": self.is_write,
46 "rowShape": self.row_shape.as_ref().map(|r| r.to_value()).unwrap_or(Value::Null),
47 "returning": self.returning,
48 })
49 }
50}
51
52#[derive(Debug, Clone, PartialEq)]
54pub struct RowShape {
55 pub columns: Vec<RowCol>,
58 pub present_alias: Option<String>,
63}
64
65#[derive(Debug, Clone, PartialEq)]
66pub struct RowCol {
67 pub alias: String,
68 pub json_path: Vec<String>,
69 pub is_array: bool,
71 pub one: bool,
73 pub ones: Vec<bool>,
76 pub sub_shape: Option<RowShape>,
78 pub always: bool,
82 pub is_bool: bool,
86}
87
88impl RowShape {
89 pub fn empty() -> Self {
90 RowShape {
91 columns: Vec::new(),
92 present_alias: None,
93 }
94 }
95
96 pub fn to_value(&self) -> Value {
97 let cols: Vec<Value> = self
98 .columns
99 .iter()
100 .map(|c| {
101 json!({
102 "alias": c.alias,
103 "path": c.json_path,
104 "isArray": c.is_array,
105 "one": c.one,
106 "ones": c.ones,
107 "subShape": c.sub_shape.as_ref().map(|s| s.to_value()).unwrap_or(Value::Null),
108 "always": c.always,
109 "bool": c.is_bool,
110 })
111 })
112 .collect();
113 json!({
114 "columns": cols,
115 "present": self.present_alias.clone().unwrap_or_default(),
116 })
117 }
118}
119
120impl RowCol {
121 pub fn scalar(alias: &str, path: &[&str]) -> Self {
122 RowCol {
123 alias: alias.to_string(),
124 json_path: path.iter().map(|s| s.to_string()).collect(),
125 is_array: false,
126 one: false,
127 ones: Vec::new(),
128 sub_shape: None,
129 always: false,
130 is_bool: false,
131 }
132 }
133
134 pub fn scalar_bool(alias: &str, path: &[&str], is_bool: bool) -> Self {
136 let mut c = RowCol::scalar(alias, path);
137 c.is_bool = is_bool;
138 c
139 }
140
141 pub fn computed(alias: &str, path: &[&str]) -> Self {
143 RowCol {
144 alias: alias.to_string(),
145 json_path: path.iter().map(|s| s.to_string()).collect(),
146 is_array: false,
147 one: false,
148 ones: Vec::new(),
149 sub_shape: None,
150 always: true,
151 is_bool: false,
152 }
153 }
154
155 pub fn array(alias: &str, sub_shape: RowShape) -> Self {
156 RowCol {
157 alias: alias.to_string(),
158 json_path: vec![alias.to_string()],
159 is_array: true,
160 one: false,
161 ones: vec![false],
162 sub_shape: Some(sub_shape),
163 always: false,
164 is_bool: false,
165 }
166 }
167}