Skip to content

Commit

Permalink
feat: support drop table statement (#218)
Browse files Browse the repository at this point in the history
* feat: support drop table statement

* test: server should add drop as a candidate

* fix: create drop statement type and use it on server

* ref: renmae statement interface and remove db from it

---------

Co-authored-by: ravime <[email protected]>
  • Loading branch information
ravilock and ravime committed Aug 13, 2023
1 parent 7c474f7 commit 0fca57d
Show file tree
Hide file tree
Showing 8 changed files with 1,379 additions and 878 deletions.
9 changes: 9 additions & 0 deletions packages/server/src/complete/complete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ExpectedLiteralNode,
AST,
AlterTableStatement,
DropTableStatement,
} from '@joe-re/sql-parser'
import log4js from 'log4js'
import { CompletionItem } from 'vscode-languageserver-types'
Expand Down Expand Up @@ -282,6 +283,12 @@ class Completer {
}
}

addCandidatesForParsedDropStatement(ast: DropTableStatement) {
if (isPosInLocation(ast.table.location, this.pos)) {
this.addCandidatesForTables(this.schema.tables, false)
}
}

addCandidatesForParsedAlterTableStatement(ast: AlterTableStatement) {
if (ast.command.type === 'alter_table_drop_column') {
if (isPosInLocation(ast.command.column.location, this.pos)) {
Expand Down Expand Up @@ -339,6 +346,8 @@ class Completer {
this.addCandidatesForParsedSelectQuery(ast)
} else if (ast.type === 'alter_table') {
this.addCandidatesForParsedAlterTableStatement(ast)
} else if (ast.type === 'drop_table') {
this.addCandidatesForParsedDropStatement(ast)
} else {
console.log(`AST type not supported yet: ${ast.type}`)
}
Expand Down
18 changes: 17 additions & 1 deletion packages/server/test/complete.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,16 @@ describe('keyword completion', () => {
test("complete 'DELETE' keyword", () => {
const sql = 'D'
const result = complete(sql, { line: 0, column: sql.length })
expect(result.candidates.length).toEqual(1)
expect(result.candidates.length).toEqual(2) // includes drop table
expect(result.candidates[0].label).toEqual('DELETE')
})

test("complete 'DROP TABLE' keyword", () => {
const sql = 'D'
const result = complete(sql, { line: 0, column: sql.length })
expect(result.candidates.length).toEqual(2) // includes delete
expect(result.candidates[1].label).toEqual('DROP TABLE')
})
})

const SIMPLE_SCHEMA = {
Expand Down Expand Up @@ -983,3 +990,12 @@ describe('DELETE statement', () => {
expect(result.candidates[1].label).toEqual('COLUMN2')
})
})

describe('DROP statement', () => {
test('complete table name', () => {
const sql = 'DROP TABLE T'
const result = complete(sql, { line: 0, column: sql.length }, SIMPLE_SCHEMA)
expect(result.candidates.length).toEqual(1)
expect(result.candidates[0].label).toEqual('TABLE1')
})
})
Loading

0 comments on commit 0fca57d

Please sign in to comment.