-
-
Notifications
You must be signed in to change notification settings - Fork 184
/
types.d.ts
546 lines (490 loc) · 11.4 KB
/
types.d.ts
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
// Type definitions for node-sql-parser 1.0
// Project: https://github.com/taozhi8833998/node-sql-parser#readme
// Definitions by: taozhi8833998 <https://github.com/taozhi8833998>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.4
export interface With {
name: { value: string };
stmt: {
_parentheses?: boolean;
tableList: string[];
columnList: string[];
ast: Select;
};
columns?: any[];
}
import { LocationRange } from "pegjs";
export { LocationRange, Location } from "pegjs";
export type WhilteListCheckMode = "table" | "column";
export interface ParseOptions {
includeLocations?: boolean;
}
export interface Option {
database?: string;
type?: string;
trimQuery?: boolean;
parseOptions?: ParseOptions;
}
export interface TableColumnAst {
tableList: string[];
columnList: string[];
ast: AST[] | AST;
loc?: LocationRange;
}
export interface BaseFrom {
db: string | null;
table: string;
as: string | null;
schema?: string;
loc?: LocationRange;
}
export interface Join extends BaseFrom {
join: "INNER JOIN" | "LEFT JOIN" | "RIGHT JOIN";
using?: string[];
on?: Binary;
}
export interface TableExpr {
expr: {
ast: Select;
};
as?: string | null;
parentheses: boolean | { length: number }
}
export interface Dual {
type: "dual";
loc?: LocationRange;
}
export type From = BaseFrom | Join | TableExpr | Dual;
export interface LimitValue {
type: string;
value: number;
loc?: LocationRange;
}
export interface Limit {
seperator: string;
value: LimitValue[];
loc?: LocationRange;
}
export interface OrderBy {
type: "ASC" | "DESC";
expr: any;
loc?: LocationRange;
}
export interface ValueExpr<T = string | number | boolean> {
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"
| "default"
| "time"
| "timestamp"
| "var_string";
value: T;
}
export interface ColumnRefItem {
type: "column_ref";
table: string | null;
column: string | { expr: ValueExpr };
options?: ExprList;
loc?: LocationRange;
}
export interface ColumnRefExpr {
type: "expr";
expr: ColumnRefItem;
as: string | null;
}
export type ColumnRef = ColumnRefItem | ColumnRefExpr;
export interface SetList {
column: string;
value: any;
table: string | null;
loc?: LocationRange;
}
export interface InsertReplaceValue {
type: "expr_list";
value: any[];
loc?: LocationRange;
}
export interface Star {
type: "star";
value: "*" | "";
loc?: LocationRange;
}
export interface Case {
type: "case";
expr: null;
args: Array<
| {
cond: Binary;
result: ExpressionValue;
type: "when";
}
| {
result: ExpressionValue;
type: "else";
}
>;
}
export interface Cast {
type: "cast";
keyword: "cast";
expr: ExpressionValue;
symbol: "as";
target: {
dataType: string;
suffix: unknown[];
};
}
export interface AggrFunc {
type: "aggr_func";
name: string;
args: {
expr: ExpressionValue;
distinct: "DISTINCT" | null;
orderby: OrderBy[] | null;
parentheses?: boolean;
};
loc?: LocationRange;
}
export type FunctionName = {
schema?: { value: string; type: string };
name: ValueExpr<string>[];
};
export interface Function {
type: "function";
name: FunctionName;
args?: ExprList;
suffix?: any;
loc?: LocationRange;
}
export interface Column {
expr: ExpressionValue;
as: ValueExpr<string> | string | null;
type?: string;
loc?: LocationRange;
}
export interface Interval {
type: "interval";
unit: string;
expr: ValueExpr & { loc?: LocationRange };
}
export type Param = { type: "param"; value: string; loc?: LocationRange };
export type Value = { type: string; value: any; loc?: LocationRange };
export type Binary = {
type: "binary_expr";
operator: string;
left: ExpressionValue | ExprList;
right: ExpressionValue | ExprList;
loc?: LocationRange;
parentheses?: boolean;
};
export type Expr = Binary;
export type ExpressionValue =
| ColumnRef
| Param
| Function
| Case
| AggrFunc
| Value
| Binary
| Cast
| Interval;
export type ExprList = {
type: "expr_list";
value: ExpressionValue[];
loc?: LocationRange;
parentheses?: boolean;
separator?: string;
};
export type PartitionBy = {
type: 'expr';
expr: ColumnRef[];
}[];
export type WindowSpec = {
name: null;
partitionby: PartitionBy;
orderby: OrderBy[] | null;
window_frame_clause: string | null; };
export type AsWindowSpec = string | { window_specification: WindowSpec; parentheses: boolean };
export type NamedWindowExpr = {
name: string;
as_window_specification: AsWindowSpec;
};
export type WindowExpr = {
keyword: 'window';
type: 'window',
expr: NamedWindowExpr[];
};
export interface Select {
with: With[] | null;
type: "select";
options: any[] | null;
distinct: "DISTINCT" | null;
columns: any[] | Column[];
from: From[] | TableExpr | null ;
where: Binary | Function | null;
groupby: { columns: ColumnRef[] | null, modifiers: ValueExpr<string>[] };
having: any[] | null;
orderby: OrderBy[] | null;
limit: Limit | null;
window?: WindowExpr;
qualify?: any[] | null;
_orderby?: OrderBy[] | null;
_limit?: Limit | null;
parentheses_symbol?: boolean;
_parentheses?: boolean;
loc?: LocationRange;
_next?: Select;
set_op?: string;
}
export interface Insert_Replace {
type: "replace" | "insert";
table: any;
columns: string[] | null;
values: InsertReplaceValue[] | Select;
partition: any[];
prefix: string;
on_duplicate_update: {
keyword: "on duplicate key update",
set: SetList[];
};
loc?: LocationRange;
}
export interface Update {
type: "update";
db: string | null;
table: Array<From | Dual> | null;
set: SetList[];
where: Binary | Function | null;
loc?: LocationRange;
}
export interface Delete {
type: "delete";
table: any;
from: Array<From | Dual>;
where: Binary | Function | null;
loc?: LocationRange;
}
export interface Alter {
type: "alter";
table: From[];
expr: any;
loc?: LocationRange;
}
export interface Use {
type: "use";
db: string;
loc?: LocationRange;
}
type KW_UNSIGNED = "UNSIGNED";
type KW_ZEROFILL = "ZEROFILL";
type Timezone = ["WITHOUT" | "WITH", "TIME", "ZONE"];
type KeywordComment = {
type: "comment";
keyword: "comment";
symbol?: "=";
value: string;
};
type CollateExpr = {
type: "collate";
symbol?: "=";
value: string;
};
type DataType = {
dataType: string;
length?: number;
parentheses?: true;
suffix?: Timezone | (KW_UNSIGNED | KW_ZEROFILL)[];
array?: "one" | "two";
};
type LiteralNotNull = {
type: "not null";
value: "not null";
};
type LiteralNull = { type: "null"; value: null };
type LiteralNumeric = number | { type: "bigint"; value: string };
type ColumnConstraint = {
default_val: {
type: "default";
value: any;
};
nullable: LiteralNotNull | LiteralNull;
};
type ColumnDefinitionOptList = {
nullable?: ColumnConstraint["nullable"];
default_val?: ColumnConstraint["default_val"];
auto_increment?: "auto_increment";
unique?: "unique" | "unique key";
primary?: "key" | "primary key";
comment?: KeywordComment;
collate?: { collate: CollateExpr };
column_format?: { column_format: any };
storage?: { storage: any };
reference_definition?: { reference_definition: any };
character_set?: { type: "CHARACTER SET"; value: string; symbol?: "=" };
};
type CreateColumnDefinition = {
column: ColumnRef;
definition: DataType;
resource: "column";
} & ColumnDefinitionOptList;
type IndexType = {
keyword: "using";
type: "btree" | "hash" | "gist" | "gin";
};
type IndexOption = {
type: "key_block_size";
symbol?: "=";
expr: LiteralNumeric;
};
type CreateIndexDefinition = {
index?: string;
definition: ColumnRef[];
keyword: "index" | "key";
index_type?: IndexType;
resource: "index";
index_options?: IndexOption[];
};
type CreateFulltextSpatialIndexDefinition = {
index?: string;
definition: ColumnRef[];
keyword?:
| "fulltext"
| "spatial"
| "fulltext key"
| "spatial key"
| "fulltext index"
| "spatial index";
index_options?: IndexOption[];
resource: "index";
};
type ConstraintName = { keyword: "constraint"; constraint: string };
type CreateConstraintPrimary = {
constraint?: ConstraintName["constraint"];
definition: ColumnRef[];
constraint_type: "primary key";
keyword?: ConstraintName["keyword"];
index_type?: IndexType;
resource: "constraint";
index_options?: IndexOption[];
};
type CreateConstraintUnique = {
constraint?: ConstraintName["constraint"];
definition: ColumnRef[];
constraint_type: "unique key" | "unique" | "unique index";
keyword?: ConstraintName["keyword"];
index_type?: IndexType;
index?: string;
resource: "constraint";
index_options?: IndexOption[];
};
type CreateConstraintForeign = {
constraint?: ConstraintName["constraint"];
definition: ColumnRef[];
constraint_type: "FOREIGN KEY";
keyword?: ConstraintName["keyword"];
index?: string;
resource: "constraint";
reference_definition?: any;
};
type CreateConstraintCheck = {
constraint?: ConstraintName["constraint"];
definition: any[];
constraint_type: "check";
keyword?: ConstraintName["keyword"];
resource: "constraint";
};
type CreateConstraintDefinition =
| CreateConstraintPrimary
| CreateConstraintUnique
| CreateConstraintForeign
| CreateConstraintCheck;
type CreateDefinition =
| CreateColumnDefinition
| CreateIndexDefinition
| CreateFulltextSpatialIndexDefinition
| CreateConstraintDefinition;
export interface Create {
type: "create";
keyword: "table" | "index" | "database";
temporary?: "temporary" | null;
table?: { db: string; table: string }[];
if_not_exists?: "if not exists" | null;
like?: {
type: "like";
table: string;
parentheses?: boolean;
} | null;
ignore_replace?: "ignore" | "replace" | null;
as?: string | null;
query_expr?: any | null;
create_definitions?: CreateDefinition[] | null;
table_options?: any[] | null;
index_using?: {
keyword: "using";
type: "btree" | "hash";
} | null;
index?: string | null;
on_kw?: "on" | null;
index_columns?: any[] | null;
index_type?: "unique" | "fulltext" | "spatial" | null;
index_options?: any[] | null;
algorithm_option?: {
type: "alter";
keyword: "algorithm";
resource: "algorithm";
symbol: "=" | null;
algorithm: "default" | "instant" | "inplace" | "copy";
} | null;
lock_option?: {
type: "alter";
keyword: "lock";
resource: "lock";
symbol: "=" | null;
lock: "default" | "none" | "shared" | "exclusive";
} | null;
database?: string;
loc?: LocationRange;
}
export interface Drop {
type: "drop";
keyword: string;
name: any[];
}
export type AST =
| Use
| Select
| Insert_Replace
| Update
| Delete
| Alter
| Create
| Drop;
export class Parser {
constructor();
parse(sql: string, opt?: Option): TableColumnAst;
astify(sql: string, opt?: Option): AST[] | AST;
sqlify(ast: AST[] | AST, opt?: Option): string;
exprToSQL(ast: any, opt?: Option): string;
whiteListCheck(
sql: string,
whiteList: string[],
opt?: Option
): Error | undefined;
tableList(sql: string, opt?: Option): string[];
columnList(sql: string, opt?: Option): string[];
}