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