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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
use std::fmt;
use std::str::FromStr;
use crate::ast::defs::AstInfo;
use crate::ast::display::{self, AstDisplay, AstFormatter};
#[derive(Debug)]
pub struct ValueError(pub(crate) String);
impl std::error::Error for ValueError {}
impl fmt::Display for ValueError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Value {
Number(String),
String(String),
HexString(String),
Boolean(bool),
Interval(IntervalValue),
Array(Vec<Value>),
Null,
}
impl AstDisplay for Value {
fn fmt<W: fmt::Write>(&self, f: &mut AstFormatter<W>) {
match self {
Value::Number(v) => f.write_str(v),
Value::String(v) => {
f.write_str("'");
f.write_node(&display::escape_single_quote_string(v));
f.write_str("'");
}
Value::HexString(v) => {
f.write_str("X'");
f.write_str(v);
f.write_str("'");
}
Value::Boolean(v) => f.write_str(v),
Value::Interval(IntervalValue {
value,
precision_high: _,
precision_low: _,
fsec_max_precision: Some(fsec_max_precision),
}) => {
f.write_str("INTERVAL '");
f.write_node(&display::escape_single_quote_string(value));
f.write_str("' SECOND (");
f.write_str(fsec_max_precision);
f.write_str(")");
}
Value::Interval(IntervalValue {
value,
precision_high,
precision_low,
fsec_max_precision,
}) => {
f.write_str("INTERVAL '");
f.write_node(&display::escape_single_quote_string(value));
f.write_str("'");
match (precision_high, precision_low, fsec_max_precision) {
(DateTimeField::Year, DateTimeField::Second, None) => {}
(DateTimeField::Year, DateTimeField::Second, Some(ns)) => {
f.write_str(" SECOND(");
f.write_str(ns);
f.write_str(")");
}
(DateTimeField::Year, low, None) => {
f.write_str(" ");
f.write_str(low);
}
(high, low, None) => {
f.write_str(" ");
f.write_str(high);
f.write_str(" TO ");
f.write_str(low);
}
(high, low, Some(ns)) => {
f.write_str(" ");
f.write_str(high);
f.write_str(" TO ");
f.write_str(low);
f.write_str("(");
f.write_str(ns);
f.write_str(")");
}
}
}
Value::Array(values) => {
f.write_str("[");
f.write_node(&display::comma_separated(values));
f.write_str("]");
}
Value::Null => f.write_str("NULL"),
}
}
}
impl_display!(Value);
#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub enum DateTimeField {
Millennium,
Century,
Decade,
Year,
Month,
Day,
Hour,
Minute,
Second,
Milliseconds,
Microseconds,
}
impl fmt::Display for DateTimeField {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(match self {
DateTimeField::Millennium => "MILLENNIUM",
DateTimeField::Century => "CENTURY",
DateTimeField::Decade => "DECADE",
DateTimeField::Year => "YEAR",
DateTimeField::Month => "MONTH",
DateTimeField::Day => "DAY",
DateTimeField::Hour => "HOUR",
DateTimeField::Minute => "MINUTE",
DateTimeField::Second => "SECOND",
DateTimeField::Milliseconds => "MILLISECONDS",
DateTimeField::Microseconds => "MICROSECONDS",
})
}
}
impl FromStr for DateTimeField {
type Err = String;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s.to_uppercase().as_ref() {
"MILLENNIUM" | "MILLENNIA" | "MIL" | "MILS" => Ok(Self::Millennium),
"CENTURY" | "CENTURIES" | "CENT" | "C" => Ok(Self::Century),
"DECADE" | "DECADES" | "DEC" | "DECS" => Ok(Self::Decade),
"YEAR" | "YEARS" | "Y" => Ok(Self::Year),
"MONTH" | "MONTHS" | "MON" | "MONS" => Ok(Self::Month),
"DAY" | "DAYS" | "D" => Ok(Self::Day),
"HOUR" | "HOURS" | "H" => Ok(Self::Hour),
"MINUTE" | "MINUTES" | "M" => Ok(Self::Minute),
"SECOND" | "SECONDS" | "S" => Ok(Self::Second),
"MILLISECOND" | "MILLISECONDS" | "MILLISECON" | "MILLISECONS" | "MSECOND"
| "MSECONDS" | "MSEC" | "MSECS" | "MS" => Ok(Self::Milliseconds),
"MICROSECOND" | "MICROSECONDS" | "MICROSECON" | "MICROSECONS" | "USECOND"
| "USECONDS" | "USEC" | "USECS" | "US" => Ok(Self::Microseconds),
_ => Err(format!("invalid DateTimeField: {}", s)),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct IntervalValue {
pub value: String,
pub precision_high: DateTimeField,
pub precision_low: DateTimeField,
pub fsec_max_precision: Option<u64>,
}
impl Default for IntervalValue {
fn default() -> Self {
Self {
value: String::default(),
precision_high: DateTimeField::Year,
precision_low: DateTimeField::Second,
fsec_max_precision: None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum DataType<T: AstInfo> {
Array(Box<DataType<T>>),
List(Box<DataType<T>>),
Map {
key_type: Box<DataType<T>>,
value_type: Box<DataType<T>>,
},
Other {
name: T::ObjectName,
typ_mod: Vec<u64>,
},
}
impl<T: AstInfo> AstDisplay for DataType<T> {
fn fmt<W: fmt::Write>(&self, f: &mut AstFormatter<W>) {
match self {
DataType::Array(ty) => {
f.write_node(&ty);
f.write_str("[]");
}
DataType::List(ty) => {
f.write_node(&ty);
f.write_str(" list");
}
DataType::Map {
key_type,
value_type,
} => {
f.write_str("map[");
f.write_node(&key_type);
f.write_str("=>");
f.write_node(&value_type);
f.write_str("]");
}
DataType::Other { name, typ_mod } => {
f.write_node(name);
if typ_mod.len() > 0 {
f.write_str("(");
f.write_node(&display::comma_separated(typ_mod));
f.write_str(")");
}
}
}
}
}
impl_display_t!(DataType);