Skip to content

Commit 7664973

Browse files
committed
chore: improve create definition types
1 parent 14baeb6 commit 7664973

1 file changed

Lines changed: 221 additions & 37 deletions

File tree

types.d.ts

Lines changed: 221 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ export interface BaseFrom {
4242
loc?: LocationRange;
4343
}
4444
export interface Join extends BaseFrom {
45-
join: "INNER JOIN" | "LEFT JOIN" | "RIGHT JOIN";
46-
using?: string[];
47-
on?: Expr;
45+
join: "INNER JOIN" | "LEFT JOIN" | "RIGHT JOIN";
46+
using?: string[];
47+
on?: Expr;
4848
}
4949
export interface TableExpr {
50-
expr: {
51-
ast: Select;
52-
},
53-
as?: string | null;
50+
expr: {
51+
ast: Select;
52+
};
53+
as?: string | null;
5454
}
5555
export interface Dual {
5656
type: "dual";
@@ -74,7 +74,27 @@ export interface OrderBy {
7474
}
7575

7676
export interface ValueExpr<T = string | number | boolean> {
77-
type: "backticks_quote_string" | "string" | "regex_string" | "hex_string" | "full_hex_string" | "natural_string" | "bit_string" | "double_quote_string" | "single_quote_string" | "boolean" | "bool" | "null" | "star" | "param" | "origin" | "date" | "datetime" | "time" | "timestamp" | "var_string";
77+
type:
78+
| "backticks_quote_string"
79+
| "string"
80+
| "regex_string"
81+
| "hex_string"
82+
| "full_hex_string"
83+
| "natural_string"
84+
| "bit_string"
85+
| "double_quote_string"
86+
| "single_quote_string"
87+
| "boolean"
88+
| "bool"
89+
| "null"
90+
| "star"
91+
| "param"
92+
| "origin"
93+
| "date"
94+
| "datetime"
95+
| "time"
96+
| "timestamp"
97+
| "var_string";
7898
value: T;
7999
}
80100

@@ -104,14 +124,17 @@ export interface Star {
104124
export interface Case {
105125
type: "case";
106126
expr: null;
107-
args: Array<{
108-
cond: Expr;
109-
result: ExpressionValue;
110-
type: "when";
111-
} | {
112-
result: ExpressionValue;
113-
type: "else";
114-
}>;
127+
args: Array<
128+
| {
129+
cond: Expr;
130+
result: ExpressionValue;
131+
type: "when";
132+
}
133+
| {
134+
result: ExpressionValue;
135+
type: "else";
136+
}
137+
>;
115138
}
116139
export interface Cast {
117140
type: "cast";
@@ -121,7 +144,7 @@ export interface Cast {
121144
target: {
122145
dataType: string;
123146
suffix: unknown[];
124-
}
147+
};
125148
}
126149
export interface AggrFunc {
127150
type: "aggr_func";
@@ -135,7 +158,10 @@ export interface AggrFunc {
135158
loc?: LocationRange;
136159
}
137160

138-
export type FunctionName = { schema?: { value: string, type: string } , name: ValueExpr<string>[] }
161+
export type FunctionName = {
162+
schema?: { value: string; type: string };
163+
name: ValueExpr<string>[];
164+
};
139165
export interface Function {
140166
type: "function";
141167
name: FunctionName;
@@ -151,31 +177,39 @@ export interface Column {
151177
}
152178

153179
export interface Interval {
154-
type: "interval";
155-
unit: string;
156-
expr: ValueExpr & { loc?: LocationRange; }
180+
type: "interval";
181+
unit: string;
182+
expr: ValueExpr & { loc?: LocationRange };
157183
}
158184

159-
export type Param = { type: "param"; value: string, loc?: LocationRange; };
185+
export type Param = { type: "param"; value: string; loc?: LocationRange };
160186

161-
export type Value = { type: string; value: any, loc?: LocationRange; };
187+
export type Value = { type: string; value: any; loc?: LocationRange };
162188

163-
export type ExpressionValue = ColumnRef | Param | Function | Case | AggrFunc | Value | Cast | Interval;
189+
export type ExpressionValue =
190+
| ColumnRef
191+
| Param
192+
| Function
193+
| Case
194+
| AggrFunc
195+
| Value
196+
| Cast
197+
| Interval;
164198
export type Expr =
165199
| {
166-
type: "binary_expr";
167-
operator: "AND" | "OR";
168-
left: Expr;
169-
right: Expr;
170-
loc?: LocationRange;
171-
}
200+
type: "binary_expr";
201+
operator: "AND" | "OR";
202+
left: Expr;
203+
right: Expr;
204+
loc?: LocationRange;
205+
}
172206
| {
173-
type: "binary_expr";
174-
operator: string;
175-
left: ExpressionValue;
176-
right: ExpressionValue | ExprList;
177-
loc?: LocationRange;
178-
};
207+
type: "binary_expr";
208+
operator: string;
209+
left: ExpressionValue;
210+
right: ExpressionValue | ExprList;
211+
loc?: LocationRange;
212+
};
179213

180214
export type ExprList = {
181215
type: "expr_list";
@@ -237,6 +271,156 @@ export interface Use {
237271
loc?: LocationRange;
238272
}
239273

274+
type KW_UNSIGNED = "UNSIGNED";
275+
type KW_ZEROFILL = "ZEROFILL";
276+
277+
type Timezone = ["WITHOUT" | "WITH", "TIME", "ZONE"];
278+
279+
type KeywordComment = {
280+
type: "comment";
281+
keyword: "comment";
282+
symbol?: "=";
283+
value: string;
284+
};
285+
286+
type CollateExpr = {
287+
type: "collate";
288+
symbol?: "=";
289+
value: string;
290+
};
291+
292+
type DataType = {
293+
dataType: string;
294+
length?: number;
295+
parentheses?: true;
296+
suffix?: Timezone | (KW_UNSIGNED | KW_ZEROFILL)[];
297+
array?: "one" | "two";
298+
};
299+
300+
type LiteralNotNull = {
301+
type: "not null";
302+
value: "not null";
303+
};
304+
305+
type LiteralNull = { type: "null"; value: null };
306+
307+
type LiteralNumeric = number | { type: "bigint"; value: string };
308+
309+
type ColumnConstraint = {
310+
default_val: {
311+
type: "default";
312+
value: any;
313+
};
314+
nullable: LiteralNotNull | LiteralNull;
315+
};
316+
317+
type ColumnDefinitionOptList = {
318+
nullable?: ColumnConstraint["nullable"];
319+
default_val?: ColumnConstraint["default_val"];
320+
auto_increment?: "auto_increment";
321+
unique?: "unique" | "unique key";
322+
primary?: "key" | "primary key";
323+
comment?: KeywordComment;
324+
collate?: { collate: CollateExpr };
325+
column_format?: { column_format: any };
326+
storage?: { storage: any };
327+
reference_definition?: { reference_definition: any };
328+
character_set?: { type: "CHARACTER SET"; value: string; symbol?: "=" };
329+
};
330+
331+
type CreateColumnDefinition = {
332+
column: ColumnRef;
333+
definition: DataType;
334+
resource: "column";
335+
} & ColumnDefinitionOptList;
336+
337+
type IndexType = {
338+
keyword: "using";
339+
type: "btree" | "hash" | "gist" | "gin";
340+
};
341+
342+
type IndexOption = {
343+
type: "key_block_size";
344+
symbol?: "=";
345+
expr: LiteralNumeric;
346+
};
347+
348+
type CreateIndexDefinition = {
349+
index?: string;
350+
definition: ColumnRef[];
351+
keyword: "index" | "key";
352+
index_type?: IndexType;
353+
resource: "index";
354+
index_options?: IndexOption[];
355+
};
356+
357+
type CreateFulltextSpatialIndexDefinition = {
358+
index?: string;
359+
definition: ColumnRef[];
360+
keyword?:
361+
| "fulltext"
362+
| "spatial"
363+
| "fulltext key"
364+
| "spatial key"
365+
| "fulltext index"
366+
| "spatial index";
367+
index_options?: IndexOption[];
368+
resource: "index";
369+
};
370+
371+
type ConstraintName = { keyword: "constraint"; constraint: string };
372+
373+
type CreateConstraintPrimary = {
374+
constraint?: ConstraintName["constraint"];
375+
definition: ColumnRef[];
376+
constraint_type: "primary key";
377+
keyword?: ConstraintName["keyword"];
378+
index_type?: IndexType;
379+
resource: "constraint";
380+
index_options?: IndexOption[];
381+
};
382+
383+
type CreateConstraintUnique = {
384+
constraint?: ConstraintName["constraint"];
385+
definition: ColumnRef[];
386+
constraint_type: "unique key" | "unique" | "unique index";
387+
keyword?: ConstraintName["keyword"];
388+
index_type?: IndexType;
389+
index?: string;
390+
resource: "constraint";
391+
index_options?: IndexOption[];
392+
};
393+
394+
type CreateConstraintForeign = {
395+
constraint?: ConstraintName["constraint"];
396+
definition: ColumnRef[];
397+
constraint_type: "FOREIGN KEY";
398+
keyword?: ConstraintName["keyword"];
399+
index?: string;
400+
resource: "constraint";
401+
reference_definition?: any;
402+
};
403+
404+
type CreateConstraintCheck = {
405+
constraint?: ConstraintName["constraint"];
406+
definition: any[];
407+
constraint_type: "check";
408+
keyword?: ConstraintName["keyword"];
409+
resource: "constraint";
410+
};
411+
412+
type CreateConstraintDefinition =
413+
| CreateConstraintPrimary
414+
| CreateConstraintUnique
415+
| CreateConstraintForeign
416+
| CreateConstraintCheck;
417+
418+
type CreateDefinition =
419+
| CreateColumnDefinition
420+
| CreateIndexDefinition
421+
| CreateFulltextSpatialIndexDefinition
422+
| CreateConstraintDefinition;
423+
240424
export interface Create {
241425
type: "create";
242426
keyword: "table" | "index" | "database";
@@ -251,7 +435,7 @@ export interface Create {
251435
ignore_replace?: "ignore" | "replace" | null;
252436
as?: string | null;
253437
query_expr?: any | null;
254-
create_definitions?: any[] | null;
438+
create_definitions?: CreateDefinition[] | null;
255439
table_options?: any[] | null;
256440
index_using?: {
257441
keyword: "using";

0 commit comments

Comments
 (0)