Skip to content

Commit

Permalink
Support basic CREATE SEQUENCE
Browse files Browse the repository at this point in the history
  • Loading branch information
nene committed Feb 1, 2024
1 parent 57c096d commit 00adcae
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/cst/Node.ts
Expand Up @@ -28,6 +28,7 @@ export * from "./Program";
export * from "./RenameTable";
export * from "./Schema";
export * from "./Select";
export * from "./Sequence";
export * from "./dialects/Sqlite";
export * from "./Statement";
export * from "./Transaction";
Expand Down Expand Up @@ -61,6 +62,7 @@ import { AllProceduralNodes } from "./ProceduralLanguage";
import { AllProcedureNodes } from "./Procedure";
import { AllRenameTableNodes } from "./RenameTable";
import { AllSelectNodes } from "./Select";
import { AllSequenceNodes } from "./Sequence";
import { AllSqliteNodes } from "./dialects/Sqlite";
import { AllTransactionNodes } from "./Transaction";
import { AllTriggerNodes } from "./Trigger";
Expand Down Expand Up @@ -94,6 +96,7 @@ export type Node =
| AllProcedureNodes
| AllRenameTableNodes
| AllSelectNodes
| AllSequenceNodes
| AllSqliteNodes
| AllTransactionNodes
| AllTriggerNodes
Expand Down
15 changes: 15 additions & 0 deletions src/cst/Sequence.ts
@@ -0,0 +1,15 @@
import { BaseNode, Keyword } from "./Base";
import { EntityName } from "./Expr";

export type AllSequenceNodes = AllSequenceStatements;

export type AllSequenceStatements = CreateSequenceStmt;

// CREATE VIEW
export interface CreateSequenceStmt extends BaseNode {
type: "create_sequence_stmt";
createKw: Keyword<"CREATE">;
sequenceKw: Keyword<"SEQUENCE">;
ifNotExistsKw?: [Keyword<"IF">, Keyword<"NOT">, Keyword<"EXISTS">];
name: EntityName;
}
2 changes: 2 additions & 0 deletions src/cst/Statement.ts
Expand Up @@ -6,6 +6,7 @@ import { AllIndexStatements } from "./Index";
import { AllProceduralStatements } from "./ProceduralLanguage";
import { AllProcedureStatements } from "./Procedure";
import { AllSchemaStatements } from "./Schema";
import { AllSequenceStatements } from "./Sequence";
import { AllSqliteStatements } from "./dialects/Sqlite";
import { AllTransactionStatements } from "./Transaction";
import { AllTriggerStatements } from "./Trigger";
Expand Down Expand Up @@ -33,6 +34,7 @@ export type Statement =
| AllProceduralStatements
| AllProcedureStatements
| AllSchemaStatements
| AllSequenceStatements
| AllSqliteStatements
| AllTransactionStatements
| AllTriggerStatements
Expand Down
23 changes: 23 additions & 0 deletions src/parser.pegjs
Expand Up @@ -139,6 +139,7 @@ ddl_statement_postgres
/ create_schema_stmt
/ drop_schema_stmt
/ alter_schema_stmt
/ create_sequence_stmt

dml_statement
= compound_select_stmt
Expand Down Expand Up @@ -3573,6 +3574,27 @@ drop_procedure_stmt
});
}

/**
* ------------------------------------------------------------------------------------ *
* *
* CREATE SEQUENCE / DROP SEQUENCE *
* *
* ------------------------------------------------------------------------------------ *
*/
create_sequence_stmt
= kw:(CREATE __)
sequenceKw:(SEQUENCE __)
ifNotExistsKw:(if_not_exists __)?
name:entity_name {
return loc({
type: "create_sequence_stmt",
createKw: read(kw),
sequenceKw: read(sequenceKw),
ifNotExistsKw: read(ifNotExistsKw),
name,
});
}

/**
* ------------------------------------------------------------------------------------ *
* *
Expand Down Expand Up @@ -7646,6 +7668,7 @@ SECOND_MICROSECOND = kw:"SECOND_MICROSECOND" !ident_part { return loc(createK
SECONDARY_ENGINE_ATTRIBUTE = kw:"SECONDARY_ENGINE_ATTRIBUTE"i !ident_part { return loc(createKeyword(kw)); }
SECURITY = kw:"SECURITY"i !ident_part { return loc(createKeyword(kw)); }
SELECT = kw:"SELECT"i !ident_part { return loc(createKeyword(kw)); }
SEQUENCE = kw:"SEQUENCE"i !ident_part { return loc(createKeyword(kw)); }
SERVER = kw:"SERVER"i !ident_part { return loc(createKeyword(kw)); }
SESSION = kw:"SESSION"i !ident_part { return loc(createKeyword(kw)); }
SESSION_USER = kw:"SESSION_USER"i !ident_part { return loc(createKeyword(kw)); }
Expand Down
8 changes: 8 additions & 0 deletions src/showNode/sequence.ts
@@ -0,0 +1,8 @@
import { show } from "../show";
import { FullTransformMap } from "../cstTransformer";
import { AllSequenceNodes } from "../cst/Node";

export const sequenceMap: FullTransformMap<string, AllSequenceNodes> = {
create_sequence_stmt: (node) =>
show([node.createKw, node.sequenceKw, node.ifNotExistsKw, node.name]),
};
4 changes: 3 additions & 1 deletion src/showNode/transformMap.ts
Expand Up @@ -20,6 +20,7 @@ import { procedureMap } from "./procedure";
import { procClauseMap } from "./proc_clause";
import { schemaMap } from "./schema";
import { selectMap } from "./select";
import { sequenceMap } from "./sequence";
import { transactionMap } from "./transaction";
import { triggerMap } from "./trigger";
import { truncateMap } from "./truncate";
Expand Down Expand Up @@ -55,11 +56,12 @@ export const transformMap: FullTransformMap<string> = {
...dropTableMap,
...renameTableMap,

// CREATE/DROP/ALTER SCHEMA/VIEW/INDEX/TRIGGER
// CREATE/DROP/ALTER SCHEMA/VIEW/INDEX/TRIGGER/SEQUENCE
...schemaMap,
...viewMap,
...indexMap,
...triggerMap,
...sequenceMap,

// CREATE/DROP FUNCTION/PROCEDURE
...functionMap,
Expand Down
23 changes: 23 additions & 0 deletions test/ddl/sequence.test.ts
@@ -0,0 +1,23 @@
import { dialect, notDialect, test, testWc } from "../test_utils";

describe("sequence", () => {
dialect("postgresql", () => {
describe("CREATE SEQUENCE", () => {
it("supports basic CREATE SEQUENCE statement", () => {
testWc("CREATE SEQUENCE my_seq");
testWc("CREATE SEQUENCE my_schema.seq");
});

it("supports IF NOT EXISTS", () => {
testWc("CREATE SEQUENCE IF NOT EXISTS my_seq");
});
});
});

notDialect("postgresql", () => {
it("does not support CREATE/DROP SEQUENCE", () => {
expect(() => test("CREATE SEQUENCE my_schema")).toThrowError();
expect(() => test("DROP SEQUENCE my_schema")).toThrowError();
});
});
});

0 comments on commit 00adcae

Please sign in to comment.