Skip to main content

nounsql_core/
dialect.rs

1/// 出力ターゲット。型名・予約語・DDLの書き方を持つ。
2#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3pub struct Dialect {
4    pub name: &'static str,
5    pub types: &'static [&'static str],
6    pub reserved: &'static [&'static str],
7}
8
9pub const POSTGRES: Dialect = Dialect {
10    name: "postgres",
11    types: POSTGRES_TYPES,
12    reserved: POSTGRES_RESERVED,
13};
14
15pub const ALL: &[Dialect] = &[POSTGRES];
16
17pub fn default() -> Dialect {
18    POSTGRES
19}
20
21pub fn by_name(name: &str) -> Option<Dialect> {
22    ALL.iter().copied().find(|d| d.name == name)
23}
24
25pub fn names() -> Vec<&'static str> {
26    ALL.iter().map(|d| d.name).collect()
27}
28
29impl Dialect {
30    pub fn has_type(&self, name: &str) -> bool {
31        self.types.contains(&name)
32    }
33
34    pub fn is_reserved(&self, name: &str) -> bool {
35        self.reserved.contains(&name.to_ascii_lowercase().as_str())
36    }
37
38    /// serial 系のPKを参照するFKは対応する整数型になる。
39    pub fn fk_type(&self, pk_type: &str) -> String {
40        match pk_type {
41            "smallserial" | "serial2" => "smallint",
42            "serial" | "serial4" => "integer",
43            "bigserial" | "serial8" => "bigint",
44            other => other,
45        }
46        .to_string()
47    }
48}
49
50const POSTGRES_TYPES: &[&str] = &[
51    "smallint",
52    "integer",
53    "bigint",
54    "int2",
55    "int4",
56    "int8",
57    "decimal",
58    "numeric",
59    "real",
60    "float4",
61    "float8",
62    "smallserial",
63    "serial",
64    "bigserial",
65    "serial2",
66    "serial4",
67    "serial8",
68    "money",
69    "varchar",
70    "char",
71    "text",
72    "bytea",
73    "timestamp",
74    "timestamptz",
75    "date",
76    "time",
77    "timetz",
78    "interval",
79    "boolean",
80    "bool",
81    "point",
82    "line",
83    "lseg",
84    "box",
85    "path",
86    "polygon",
87    "circle",
88    "cidr",
89    "inet",
90    "macaddr",
91    "macaddr8",
92    "bit",
93    "varbit",
94    "tsvector",
95    "tsquery",
96    "uuid",
97    "xml",
98    "json",
99    "jsonb",
100    "jsonpath",
101    "int4range",
102    "int8range",
103    "numrange",
104    "tsrange",
105    "tstzrange",
106    "daterange",
107    "int4multirange",
108    "int8multirange",
109    "nummultirange",
110    "tsmultirange",
111    "tstzmultirange",
112    "datemultirange",
113    "pg_lsn",
114    "pg_snapshot",
115    "oid",
116    "regclass",
117    "regproc",
118    "regprocedure",
119    "regoper",
120    "regoperator",
121    "regtype",
122    "regrole",
123    "regnamespace",
124    "regconfig",
125    "regdictionary",
126];
127
128const POSTGRES_RESERVED: &[&str] = &[
129    "all",
130    "analyse",
131    "analyze",
132    "and",
133    "any",
134    "array",
135    "as",
136    "asc",
137    "asymmetric",
138    "authorization",
139    "binary",
140    "both",
141    "case",
142    "cast",
143    "check",
144    "collate",
145    "collation",
146    "column",
147    "concurrently",
148    "constraint",
149    "create",
150    "cross",
151    "current_catalog",
152    "current_date",
153    "current_role",
154    "current_schema",
155    "current_time",
156    "current_timestamp",
157    "current_user",
158    "default",
159    "deferrable",
160    "desc",
161    "distinct",
162    "do",
163    "else",
164    "end",
165    "except",
166    "false",
167    "fetch",
168    "for",
169    "foreign",
170    "freeze",
171    "from",
172    "full",
173    "grant",
174    "group",
175    "having",
176    "ilike",
177    "in",
178    "initially",
179    "inner",
180    "intersect",
181    "into",
182    "is",
183    "isnull",
184    "join",
185    "lateral",
186    "leading",
187    "left",
188    "like",
189    "limit",
190    "localtime",
191    "localtimestamp",
192    "natural",
193    "not",
194    "notnull",
195    "null",
196    "offset",
197    "on",
198    "only",
199    "or",
200    "order",
201    "outer",
202    "overlaps",
203    "placing",
204    "primary",
205    "references",
206    "returning",
207    "right",
208    "select",
209    "session_user",
210    "similar",
211    "some",
212    "symmetric",
213    "table",
214    "tablesample",
215    "then",
216    "to",
217    "trailing",
218    "true",
219    "union",
220    "unique",
221    "user",
222    "using",
223    "variadic",
224    "verbose",
225    "when",
226    "where",
227    "window",
228    "with",
229];