Skip to content

fix: add alter table stmt #312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/parser/common/entityCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export enum StmtContextType {
SELECT_STMT = 'selectStmt',
INSERT_STMT = 'insertStmt',
CREATE_FUNCTION_STMT = 'createFunctionStmt',
ALTER_TABLE_STMT = 'alterTableStmt',
}

export interface StmtContext {
Expand Down
7 changes: 7 additions & 0 deletions src/parser/postgresql/postgreEntityCollector.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {
AltertablestmtContext,
ColumnCreateTableContext,
ColumnNameCreateContext,
CreateDatabaseContext,
Expand Down Expand Up @@ -145,4 +146,10 @@ export class PostgreSqlEntityCollector extends EntityCollector implements Postgr
exitCreatefunctionstmt(ctx: CreatefunctionstmtContext) {
this.popStmt();
}
enterAltertablestmt(ctx: AltertablestmtContext) {
this.pushStmt(ctx, StmtContextType.ALTER_TABLE_STMT);
}
exitAltertablestmt(ctx: AltertablestmtContext) {
this.popStmt();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ INSERT INTO insert_tb SELECT id, age, FROM from_tb;

CREATE TABLE sorted_census_data AS SELECT FROM unsorted_census_data;

CREATE TABLE sorted_census_data AS SELECT id, age, FROM unsorted_census_data;
CREATE TABLE sorted_census_data AS SELECT id, age, FROM unsorted_census_data;

ALTER TABLE my_table DROP a_column;
21 changes: 21 additions & 0 deletions test/parser/postgresql/suggestion/suggestionWithEntity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,25 @@ describe('PostgreSql Syntax Suggestion with collect entity', () => {
expect(entities[1].entityContextType).toBe(EntityContextType.TABLE);
expect(entities[1].belongStmt.isContainCaret).toBeTruthy();
});

test('alter table drop column', () => {
const pos: CaretPosition = {
lineNumber: 13,
column: 35,
};
const sql = commentOtherLine(syntaxSql, pos.lineNumber);

const syntaxes = postgre.getSuggestionAtCaretPosition(sql, pos)?.syntax;
const suggestion = syntaxes?.find(
(syn) => syn.syntaxContextType === EntityContextType.COLUMN
);
expect(suggestion).not.toBeUndefined();
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['a_column']);

const entities = postgre.getAllEntities(sql, pos);
expect(entities.length).toBe(1);
expect(entities[0].text).toBe('my_table');
expect(entities[0].entityContextType).toBe(EntityContextType.TABLE);
expect(entities[0].belongStmt?.isContainCaret).toBeTruthy();
});
});
Loading