1use 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 pub is_json: bool,
89}
90
91impl RowShape {
92 pub fn empty() -> Self {
93 RowShape {
94 columns: Vec::new(),
95 present_alias: None,
96 }
97 }
98
99 pub fn to_value(&self) -> Value {
100 let cols: Vec<Value> = self
101 .columns
102 .iter()
103 .map(|c| {
104 json!({
105 "alias": c.alias,
106 "path": c.json_path,
107 "isArray": c.is_array,
108 "one": c.one,
109 "ones": c.ones,
110 "subShape": c.sub_shape.as_ref().map(|s| s.to_value()).unwrap_or(Value::Null),
111 "always": c.always,
112 "bool": c.is_bool,
113 "json": c.is_json,
114 })
115 })
116 .collect();
117 json!({
118 "columns": cols,
119 "present": self.present_alias.clone().unwrap_or_default(),
120 })
121 }
122}
123
124impl RowCol {
125 pub fn scalar(alias: &str, path: &[&str]) -> Self {
126 RowCol {
127 alias: alias.to_string(),
128 json_path: path.iter().map(|s| s.to_string()).collect(),
129 is_array: false,
130 one: false,
131 ones: Vec::new(),
132 sub_shape: None,
133 always: false,
134 is_bool: false,
135 is_json: false,
136 }
137 }
138
139 pub fn scalar_bool(alias: &str, path: &[&str], is_bool: bool) -> Self {
141 let mut c = RowCol::scalar(alias, path);
142 c.is_bool = is_bool;
143 c
144 }
145
146 pub fn json(alias: &str, path: &[&str]) -> Self {
148 let mut c = RowCol::scalar(alias, path);
149 c.is_json = true;
150 c
151 }
152
153 pub fn computed(alias: &str, path: &[&str]) -> Self {
155 RowCol {
156 alias: alias.to_string(),
157 json_path: path.iter().map(|s| s.to_string()).collect(),
158 is_array: false,
159 one: false,
160 ones: Vec::new(),
161 sub_shape: None,
162 always: true,
163 is_bool: false,
164 is_json: false,
165 }
166 }
167
168 pub fn array(alias: &str, sub_shape: RowShape) -> Self {
169 RowCol {
170 alias: alias.to_string(),
171 json_path: vec![alias.to_string()],
172 is_array: true,
173 one: false,
174 ones: vec![false],
175 sub_shape: Some(sub_shape),
176 always: false,
177 is_bool: false,
178 is_json: false,
179 }
180 }
181}