Skip to content

Commit 8dc9edf

Browse files
Kijin-SeijaHaydenOrz
authored andcommitted
feat: add alter table stmt (#312)
* fix: add alter table stmt * fix: delete personal unused scripts
1 parent 4a2c463 commit 8dc9edf

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

src/parser/common/entityCollector.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export enum StmtContextType {
1717
SELECT_STMT = 'selectStmt',
1818
INSERT_STMT = 'insertStmt',
1919
CREATE_FUNCTION_STMT = 'createFunctionStmt',
20+
ALTER_TABLE_STMT = 'alterTableStmt',
2021
}
2122

2223
export interface StmtContext {

src/parser/postgresql/postgreEntityCollector.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type {
2+
AltertablestmtContext,
23
ColumnCreateTableContext,
34
ColumnNameCreateContext,
45
CreateDatabaseContext,
@@ -145,4 +146,10 @@ export class PostgreSqlEntityCollector extends EntityCollector implements Postgr
145146
exitCreatefunctionstmt(ctx: CreatefunctionstmtContext) {
146147
this.popStmt();
147148
}
149+
enterAltertablestmt(ctx: AltertablestmtContext) {
150+
this.pushStmt(ctx, StmtContextType.ALTER_TABLE_STMT);
151+
}
152+
exitAltertablestmt(ctx: AltertablestmtContext) {
153+
this.popStmt();
154+
}
148155
}

test/parser/postgresql/suggestion/fixtures/suggestionWithEntity.sql

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ INSERT INTO insert_tb SELECT id, age, FROM from_tb;
88

99
CREATE TABLE sorted_census_data AS SELECT FROM unsorted_census_data;
1010

11-
CREATE TABLE sorted_census_data AS SELECT id, age, FROM unsorted_census_data;
11+
CREATE TABLE sorted_census_data AS SELECT id, age, FROM unsorted_census_data;
12+
13+
ALTER TABLE my_table DROP a_column;

test/parser/postgresql/suggestion/suggestionWithEntity.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,25 @@ describe('PostgreSql Syntax Suggestion with collect entity', () => {
153153
expect(entities[1].entityContextType).toBe(EntityContextType.TABLE);
154154
expect(entities[1].belongStmt.isContainCaret).toBeTruthy();
155155
});
156+
157+
test('alter table drop column', () => {
158+
const pos: CaretPosition = {
159+
lineNumber: 13,
160+
column: 35,
161+
};
162+
const sql = commentOtherLine(syntaxSql, pos.lineNumber);
163+
164+
const syntaxes = postgre.getSuggestionAtCaretPosition(sql, pos)?.syntax;
165+
const suggestion = syntaxes?.find(
166+
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
167+
);
168+
expect(suggestion).not.toBeUndefined();
169+
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['a_column']);
170+
171+
const entities = postgre.getAllEntities(sql, pos);
172+
expect(entities.length).toBe(1);
173+
expect(entities[0].text).toBe('my_table');
174+
expect(entities[0].entityContextType).toBe(EntityContextType.TABLE);
175+
expect(entities[0].belongStmt?.isContainCaret).toBeTruthy();
176+
});
156177
});

0 commit comments

Comments
 (0)