-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdatabase.rs
300 lines (267 loc) · 9.22 KB
/
database.rs
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//! Metadata information regarding the database and tracked information.
// This code was copied from a different place that predated the introduction of clippy to the
// project. Therefore we disregard certain clippy lints:
#![allow(
clippy::enum_variant_names,
clippy::upper_case_acronyms,
clippy::wrong_self_convention
)]
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, BTreeSet};
/// Map of all known composite types.
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct CompositeTypes(pub BTreeMap<String, CompositeType>);
/// A Scalar Type.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ScalarType(pub String);
/// The type of values that a column, field, or argument may take.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum Type {
ScalarType(ScalarType),
CompositeType(String),
ArrayType(Box<Type>),
}
/// Information about a composite type. These are very similar to tables, but with the crucial
/// difference that composite types do not support constraints (such as NOT NULL).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct CompositeType {
pub name: String,
pub fields: BTreeMap<String, FieldInfo>,
#[serde(default)]
pub description: Option<String>,
}
/// Information about a composite type field.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct FieldInfo {
pub name: String,
pub r#type: Type,
#[serde(default)]
pub description: Option<String>,
}
/// The complete list of supported binary operators for scalar types.
/// Not all of these are supported for every type.
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ComparisonOperators(pub BTreeMap<ScalarType, BTreeMap<String, ComparisonOperator>>);
/// Represents a postgres binary comparison operator
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ComparisonOperator {
pub operator_name: String,
pub operator_kind: OperatorKind,
pub argument_type: ScalarType,
#[serde(default = "default_true")]
pub is_infix: bool,
}
/// Is it a built-in operator, or a custom operator.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum OperatorKind {
Equal,
In,
Custom,
}
/// This is quite unfortunate: https://github.com/serde-rs/serde/issues/368
/// TL;DR: we can't set default literals for serde, so if we want 'is_infix' to
/// default to 'true', we have to set its default as a function that returns 'true'.
fn default_true() -> bool {
true
}
/// Mapping from a "table" name to its information.
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct TablesInfo(pub BTreeMap<String, TableInfo>);
/// Information about a database table (or any other kind of relation).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct TableInfo {
pub schema_name: String,
pub table_name: String,
pub columns: BTreeMap<String, ColumnInfo>,
#[serde(default)]
pub uniqueness_constraints: UniquenessConstraints,
#[serde(default)]
pub foreign_relations: ForeignRelations,
#[serde(default)]
pub description: Option<String>,
}
/// Can this column contain null values
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum Nullable {
#[default]
Nullable,
NonNullable,
}
/// Does this column have a default value.
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum HasDefault {
#[default]
NoDefault,
HasDefault,
}
/// Is this column an identity column.
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum IsIdentity {
#[default]
NotIdentity,
IdentityByDefault,
IdentityAlways,
}
/// Is this column a generated column.
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum IsGenerated {
#[default]
NotGenerated,
Stored,
}
/// Information about a database column.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ColumnInfo {
pub name: String,
pub r#type: Type,
#[serde(default)]
pub nullable: Nullable,
#[serde(skip_serializing_if = "does_not_have_default")]
#[serde(default)]
pub has_default: HasDefault,
#[serde(skip_serializing_if = "is_not_identity")]
#[serde(default)]
pub is_identity: IsIdentity,
#[serde(skip_serializing_if = "is_not_generated")]
#[serde(default)]
pub is_generated: IsGenerated,
#[serde(default)]
pub description: Option<String>,
}
fn does_not_have_default(has_default: &HasDefault) -> bool {
matches!(has_default, HasDefault::NoDefault)
}
fn is_not_identity(is_identity: &IsIdentity) -> bool {
matches!(is_identity, IsIdentity::NotIdentity)
}
fn is_not_generated(is_generated: &IsGenerated) -> bool {
matches!(is_generated, IsGenerated::NotGenerated)
}
/// A mapping from the name of a unique constraint to its value.
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct UniquenessConstraints(pub BTreeMap<String, UniquenessConstraint>);
/// The set of columns that make up a uniqueness constraint.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct UniquenessConstraint(pub BTreeSet<String>);
/// A mapping from the name of a foreign key constraint to its value.
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ForeignRelations(pub BTreeMap<String, ForeignRelation>);
/// A foreign key constraint.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ForeignRelation {
#[serde(skip_serializing_if = "Option::is_none")]
pub foreign_schema: Option<String>,
pub foreign_table: String,
pub column_mapping: BTreeMap<String, String>,
}
/// All supported aggregate functions, grouped by type.
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct AggregateFunctions(pub BTreeMap<ScalarType, BTreeMap<String, AggregateFunction>>);
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct AggregateFunction {
pub return_type: ScalarType,
}
/// Type representation of scalar types, grouped by type.
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct TypeRepresentations(pub BTreeMap<ScalarType, TypeRepresentation>);
/// Type representation of a scalar type.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum TypeRepresentation {
/// JSON booleans
Boolean,
/// Any JSON string
String,
/// float4
Float32,
/// float8
Float64,
/// int2
Int16,
/// int4
Int32,
/// int8 as integer
Int64,
/// int8 as string
Int64AsString,
/// numeric
BigDecimal,
/// numeric as string
BigDecimalAsString,
/// timestamp
Timestamp,
/// timestamp with timezone
Timestamptz,
/// time
Time,
/// time with timezone
Timetz,
/// date
Date,
/// uuid
UUID,
/// geography
Geography,
/// geometry
Geometry,
/// Any JSON number
Number,
/// Any JSON number, with no decimal part
Integer,
/// An arbitrary json.
Json,
/// One of the specified string values
Enum(Vec<String>),
}
// tests
#[cfg(test)]
mod tests {
use super::{ScalarType, TypeRepresentation, TypeRepresentations};
#[test]
fn parse_type_representations() {
assert_eq!(
serde_json::from_str::<TypeRepresentations>(
r#"{"int4": "integer", "card_suit": {"enum": ["hearts", "clubs", "diamonds", "spades"]}}"#
)
.unwrap(),
TypeRepresentations(
[(
ScalarType("int4".to_string()),
TypeRepresentation::Integer
), (
ScalarType("card_suit".to_string()),
TypeRepresentation::Enum(vec![
"hearts".into(),
"clubs".into(),
"diamonds".into(),
"spades".into()
])
)]
.into()
)
);
}
}