Skip to content

Commit 4ecc9bd

Browse files
committed
Add the Create Index statement
1 parent d379242 commit 4ecc9bd

1 file changed

Lines changed: 318 additions & 6 deletions

File tree

lib/statement.ts

Lines changed: 318 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { ColumnDef, CommentDef, DataType, NullsDistinctOption, SqlOption } from "./data-type";
1+
import { ColumnDef, ColumnOption, CommentDef, DataType, GeneratedAs, NullsDistinctOption, SqlOption } from "./data-type";
22
import type { Expr, ExprWithAlias, OneOrManyWithParens } from "./expr";
33
import { FunctionArg, SQLFunction } from "./function";
44
import type { Ident, ObjectName } from "./ident";
5-
import type { AttachedToken, Value } from "./token";
5+
import type { AttachedToken, Value, ValueWithSpan } from "./token";
66

77
/**
88
* The list of possible SQL statements.
@@ -44,7 +44,7 @@ export type Statement = {
4444
CreateView?: unknown;
4545
CreateTable?: CreateTable;
4646
CreateVirtualTable?: unknown;
47-
CreateIndex?: unknown;
47+
CreateIndex?: CreateIndex;
4848
CreateRole?: unknown;
4949
CreateSecret?: unknown;
5050
CreateServer?: unknown;
@@ -58,7 +58,7 @@ export type Statement = {
5858
AlterRole?: unknown;
5959
AlterPolicy?: unknown;
6060
AlterConnector?: unknown;
61-
AlterSession ?: unknown;
61+
AlterSession?: unknown;
6262
AttachDatabase?: unknown;
6363
AttachDuckDBDatabase?: unknown;
6464
DetachDuckDBDatabase?: unknown;
@@ -81,8 +81,8 @@ export type Statement = {
8181
ShowVariables?: unknown;
8282
ShowCreate?: unknown;
8383
ShowColumns?: unknown;
84-
ShowDatabases ?: unknown;
85-
ShowSchemas ?: unknown;
84+
ShowDatabases?: unknown;
85+
ShowSchemas?: unknown;
8686
ShowCharset?: unknown;
8787
ShowObjects?: unknown;
8888
ShowTables?: unknown;
@@ -139,6 +139,318 @@ export type Statement = {
139139
Vacuum?: unknown;
140140
}
141141

142+
/**
143+
* A SQL `CREATE INDEX` statement.
144+
*
145+
* @see https://docs.rs/sqlparser/latest/sqlparser/ast/struct.CreateIndex.html
146+
*/
147+
export interface CreateIndex {
148+
name?: ObjectName,
149+
table_name: ObjectName,
150+
using?: IndexType,
151+
columns: IndexColumn[],
152+
unique: boolean,
153+
concurrently: boolean,
154+
if_not_exists: boolean,
155+
include: Ident[],
156+
nulls_distinct?: boolean,
157+
with: Expr[],
158+
predicate?: Expr,
159+
index_options: IndexOption[],
160+
alter_options: AlterTableOperation[],
161+
}
162+
163+
/**
164+
* An `ALTER TABLE` operation
165+
*
166+
* @see https://docs.rs/sqlparser/latest/sqlparser/ast/enum.AlterTableOperation.html
167+
*/
168+
export type AlterTableOperation =
169+
| 'DisableRowLevelSecurity'
170+
| 'EnableRowLevelSecurity'
171+
| 'DropClusteringKey'
172+
| 'SuspendRecluster'
173+
| 'ResumeRecluster' | {
174+
AddConstraint?: {
175+
constraint: TableConstraint,
176+
not_valid: boolean,
177+
},
178+
AddColumn?: {
179+
column_keyword: boolean,
180+
if_not_exists: boolean,
181+
column_def: ColumnDef,
182+
column_position?: MySQLColumnPosition,
183+
},
184+
AddProjection?: {
185+
if_not_exists: boolean,
186+
name: Ident,
187+
select: unknown, // TODO: Define proper type
188+
},
189+
DropProjection?: {
190+
if_exists: boolean,
191+
name: Ident,
192+
},
193+
MaterializeProjection?: {
194+
if_exists: boolean,
195+
name: Ident,
196+
partition?: Ident,
197+
},
198+
ClearProjection?: {
199+
if_exists: boolean,
200+
name: Ident,
201+
partition?: Ident,
202+
},
203+
DisableRule?: {
204+
name: Ident,
205+
},
206+
DisableTrigger?: {
207+
name: Ident,
208+
},
209+
DropConstraint?: {
210+
if_exists: boolean,
211+
name: Ident,
212+
drop_behavior?: DropBehavior,
213+
},
214+
DropColumn?: {
215+
has_column_keyword: boolean,
216+
column_names: Ident[],
217+
if_exists: boolean,
218+
drop_behavior?: DropBehavior,
219+
},
220+
AttachPartition?: {
221+
partition: unknown,
222+
},
223+
DetachPartition?: {
224+
partition: unknown,
225+
},
226+
FreezePartition?: {
227+
partition: unknown,
228+
with_name?: Ident,
229+
},
230+
UnfreezePartition?: {
231+
partition: unknown,
232+
with_name?: Ident,
233+
},
234+
DropPrimaryKey?: {
235+
drop_behavior?: DropBehavior,
236+
},
237+
DropForeignKey?: {
238+
name: Ident,
239+
drop_behavior?: DropBehavior,
240+
},
241+
DropIndex?: {
242+
name: Ident,
243+
},
244+
EnableAlwaysRule?: {
245+
name: Ident,
246+
},
247+
EnableAlwaysTrigger?: {
248+
name: Ident,
249+
},
250+
EnableReplicaRule?: {
251+
name: Ident,
252+
},
253+
EnableReplicaTrigger?: {
254+
name: Ident,
255+
},
256+
EnableRule?: {
257+
name: Ident,
258+
},
259+
EnableTrigger?: {
260+
name: Ident,
261+
},
262+
RenamePartitions?: {
263+
old_partitions: Expr[],
264+
new_partitions: Expr[],
265+
},
266+
ReplicaIdentity?: {
267+
identity: ReplicaIdentity,
268+
},
269+
AddPartitions?: {
270+
if_not_exists: boolean,
271+
new_partitions: unknown[],
272+
},
273+
DropPartitions?: {
274+
partitions: Expr[],
275+
if_exists: boolean,
276+
},
277+
RenameColumn?: {
278+
old_column_name: Ident,
279+
new_column_name: Ident,
280+
},
281+
RenameTable?: {
282+
table_name: RenameTableNameKind,
283+
},
284+
ChangeColumn?: {
285+
old_name: Ident,
286+
new_name: Ident,
287+
data_type: DataType,
288+
options: ColumnOption[],
289+
column_position?: MySQLColumnPosition,
290+
},
291+
ModifyColumn?: {
292+
col_name: Ident,
293+
data_type: DataType,
294+
options: ColumnOption[],
295+
column_position?: MySQLColumnPosition,
296+
},
297+
RenameConstraint?: {
298+
old_name: Ident,
299+
new_name: Ident,
300+
},
301+
AlterColumn?: {
302+
column_name: Ident,
303+
op: AlterColumnOperation,
304+
},
305+
SwapWith?: {
306+
table_name: ObjectName,
307+
},
308+
SetTblProperties?: {
309+
table_properties: SqlOption[],
310+
},
311+
OwnerTo?: {
312+
new_owner: Owner,
313+
},
314+
ClusterBy?: {
315+
exprs: Expr[],
316+
},
317+
Algorithm?: {
318+
equals: boolean,
319+
algorithm: AlterTableAlgorithm,
320+
},
321+
Lock?: {
322+
equals: boolean,
323+
lock: AlterTableLock,
324+
},
325+
AutoIncrement?: {
326+
equals: boolean,
327+
value: ValueWithSpan,
328+
},
329+
ValidateConstraint?: {
330+
name: Ident,
331+
},
332+
SetOptionsParens?: {
333+
options: SqlOption[],
334+
},
335+
}
336+
337+
/**
338+
* [MySQL](https://dev.mysql.com/doc/refman/8.4/en/alter-table.html) ALTER TABLE lock.
339+
*
340+
* @see https://docs.rs/sqlparser/latest/sqlparser/ast/enum.AlterTableLock.html
341+
*/
342+
export type AlterTableLock =
343+
| 'Default'
344+
| 'None'
345+
| 'Shared'
346+
| 'Exclusive';
347+
348+
/**
349+
* [MySQL](https://dev.mysql.com/doc/refman/8.4/en/alter-table.html) ALTER TABLE algorithm.
350+
*
351+
* @see https://docs.rs/sqlparser/latest/sqlparser/ast/enum.AlterTableAlgorithm.html
352+
*/
353+
export type AlterTableAlgorithm =
354+
| 'Default'
355+
| 'Instant'
356+
| 'Inplace'
357+
| 'Copy';
358+
359+
/**
360+
* @see https://docs.rs/sqlparser/latest/sqlparser/ast/enum.Owner.html
361+
*/
362+
export type Owner =
363+
| 'CurrentRole'
364+
| 'CurrentUser'
365+
| 'SessionUser'
366+
| {
367+
Ident: Ident
368+
};
369+
370+
/**
371+
* MySQL `ALTER TABLE` only `[FIRST | AFTER column_name]`
372+
*
373+
* @see https://docs.rs/sqlparser/latest/sqlparser/ast/enum.MySQLColumnPosition.html
374+
*/
375+
export type MySQLColumnPosition =
376+
| 'First'
377+
| { After: Ident }
378+
379+
/**
380+
* `<drop behavior> ::= CASCADE | RESTRICT`
381+
* Used in DROP statements.
382+
*
383+
* @see https://docs.rs/sqlparser/latest/sqlparser/ast/enum.DropBehavior.html
384+
*/
385+
export type DropBehavior =
386+
| 'Restrict'
387+
| 'Cascade';
388+
389+
/**
390+
* ALTER TABLE operation `REPLICA IDENTITY` values See [Postgres ALTER TABLE docs](https://www.postgresql.org/docs/current/sql-altertable.html).
391+
*
392+
* @see https://docs.rs/sqlparser/latest/sqlparser/ast/enum.ReplicaIdentity.html
393+
*/
394+
export type ReplicaIdentity =
395+
| 'None'
396+
| 'Full'
397+
| 'Default'
398+
| { Index: Ident }
399+
400+
/**
401+
* RenameTableNameKind is the kind used in an `ALTER TABLE _ RENAME` statement.
402+
*
403+
* Note: [MySQL](https://dev.mysql.com/doc/refman/8.4/en/alter-table.html) is the only database that supports the AS keyword for this operation.
404+
*
405+
* @see https://docs.rs/sqlparser/latest/sqlparser/ast/enum.RenameTableNameKind.html
406+
*/
407+
export type RenameTableNameKind = {
408+
As?: ObjectName;
409+
To?: ObjectName;
410+
}
411+
412+
/**
413+
* An `ALTER COLUMN` operation.
414+
*
415+
* @see https://docs.rs/sqlparser/latest/sqlparser/ast/enum.AlterColumnOperation.html
416+
*/
417+
export type AlterColumnOperation =
418+
| 'SetNotNull'
419+
| 'DropNotNull'
420+
| 'DropDefault'
421+
| {
422+
SetDefault?: {
423+
value: Expr,
424+
},
425+
SetDataType?: {
426+
data_type: DataType,
427+
using?: Expr,
428+
had_set: boolean,
429+
},
430+
AddGenerated?: {
431+
generated_as?: GeneratedAs,
432+
sequence_options?: SequenceOptions[],
433+
},
434+
}
435+
436+
/**
437+
* ```sql
438+
* [ INCREMENT [ BY ] increment ]
439+
[ MINVALUE minvalue | NO MINVALUE ] [ MAXVALUE maxvalue | NO MAXVALUE ]
440+
[ START [ WITH ] start ] [ CACHE cache ] [ [ NO ] CYCLE ]
441+
```
442+
*
443+
* @see https://docs.rs/sqlparser/latest/sqlparser/ast/enum.SequenceOptions.html
444+
*/
445+
export type SequenceOptions = {
446+
IncrementBy?: [Expr, boolean],
447+
MinValue?: Expr,
448+
MaxValue?: Expr,
449+
StartWith?: [Expr, boolean],
450+
Cache?: Expr,
451+
Cycle?: boolean,
452+
}
453+
142454
/**
143455
* SQL `CREATE TABLE` statement.
144456
*

0 commit comments

Comments
 (0)