Skip to content

Commit 0fca57d

Browse files
ravilockravime
andauthored
feat: support drop table statement (joe-re#218)
* 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 <ravime@g.globo>
1 parent 7c474f7 commit 0fca57d

File tree

8 files changed

+1379
-878
lines changed

8 files changed

+1379
-878
lines changed

packages/server/src/complete/complete.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
ExpectedLiteralNode,
1111
AST,
1212
AlterTableStatement,
13+
DropTableStatement,
1314
} from '@joe-re/sql-parser'
1415
import log4js from 'log4js'
1516
import { CompletionItem } from 'vscode-languageserver-types'
@@ -282,6 +283,12 @@ class Completer {
282283
}
283284
}
284285

286+
addCandidatesForParsedDropStatement(ast: DropTableStatement) {
287+
if (isPosInLocation(ast.table.location, this.pos)) {
288+
this.addCandidatesForTables(this.schema.tables, false)
289+
}
290+
}
291+
285292
addCandidatesForParsedAlterTableStatement(ast: AlterTableStatement) {
286293
if (ast.command.type === 'alter_table_drop_column') {
287294
if (isPosInLocation(ast.command.column.location, this.pos)) {
@@ -339,6 +346,8 @@ class Completer {
339346
this.addCandidatesForParsedSelectQuery(ast)
340347
} else if (ast.type === 'alter_table') {
341348
this.addCandidatesForParsedAlterTableStatement(ast)
349+
} else if (ast.type === 'drop_table') {
350+
this.addCandidatesForParsedDropStatement(ast)
342351
} else {
343352
console.log(`AST type not supported yet: ${ast.type}`)
344353
}

packages/server/test/complete.test.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,16 @@ describe('keyword completion', () => {
126126
test("complete 'DELETE' keyword", () => {
127127
const sql = 'D'
128128
const result = complete(sql, { line: 0, column: sql.length })
129-
expect(result.candidates.length).toEqual(1)
129+
expect(result.candidates.length).toEqual(2) // includes drop table
130130
expect(result.candidates[0].label).toEqual('DELETE')
131131
})
132+
133+
test("complete 'DROP TABLE' keyword", () => {
134+
const sql = 'D'
135+
const result = complete(sql, { line: 0, column: sql.length })
136+
expect(result.candidates.length).toEqual(2) // includes delete
137+
expect(result.candidates[1].label).toEqual('DROP TABLE')
138+
})
132139
})
133140

134141
const SIMPLE_SCHEMA = {
@@ -983,3 +990,12 @@ describe('DELETE statement', () => {
983990
expect(result.candidates[1].label).toEqual('COLUMN2')
984991
})
985992
})
993+
994+
describe('DROP statement', () => {
995+
test('complete table name', () => {
996+
const sql = 'DROP TABLE T'
997+
const result = complete(sql, { line: 0, column: sql.length }, SIMPLE_SCHEMA)
998+
expect(result.candidates.length).toEqual(1)
999+
expect(result.candidates[0].label).toEqual('TABLE1')
1000+
})
1001+
})

0 commit comments

Comments
 (0)