feat: optimize suggestion (#231)

* feat: optimize the strategy of finding the right range

* test: apply commentOtherLine util to all suggestion tests

* test: decomment suggestion test cases

* test: add suggestion test cases in multiple statements

* chore: improve comments

* test: update log info in test
This commit is contained in:
Hayden
2023-12-13 11:33:47 +08:00
committed by GitHub
parent fd50c09a86
commit 3c7c59fb70
29 changed files with 930 additions and 155 deletions

View File

@ -0,0 +1,21 @@
SELECT * FROM -- unfinished
CREATE SCHEMA IF NOT EXISTS db_name DEFAULT ENCRYPTION 'Y';
CREATE TABLE test (a INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (a), KEY(b)) ENGINE=InnoDB SELECT b,c FROM test2;
ALTER LOGFILE GROUP lg_3 ADD UNDOFILE 'undo_10.dat' INITIAL_SIZE=32M ENGINE=NDBCLUSTER;
CREATE TABLE db. LIKE orig_tbl; -- unfinished
INSERT HIGH_PRIORITY IGNORE INTO tbl_temp2 (fld_id) VALUES ROW(1,-2,3), ROW(5,7,9), ROW(4,6,8);
CHANGE REPLICATION FILTER REPLICATE_DO_DB = (db3, db4);
INSERT INTO VALUES (1,2,3),(4,5,6) ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b); -- unfinished
ALTER FUNCTION function_name LANGUAGE SQL;
ALTER FUNCTION function_name NOT DETERMINISTIC;
INSERT LOW_PRIORITY IGNORE INTO tbl_temp2 (fld_id) SELECT tbl_temp1.fld_order_id FROM -- unfinished

View File

@ -0,0 +1,69 @@
import fs from 'fs';
import path from 'path';
import { CaretPosition, SyntaxContextType } from '../../../../src/parser/common/basic-parser-types';
import MySQL from '../../../../src/parser/mysql';
const syntaxSql = fs.readFileSync(
path.join(__dirname, 'fixtures', 'multipleStatement.sql'),
'utf-8'
);
describe('MySQL Multiple Statements Syntax Suggestion', () => {
const parser = new MySQL();
test('Select from table ', () => {
const pos: CaretPosition = {
lineNumber: 1,
column: 15,
};
const syntaxes = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax;
const suggestion = syntaxes?.find(
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE
);
expect(suggestion).not.toBeUndefined();
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
});
test('Create table ', () => {
const pos: CaretPosition = {
lineNumber: 9,
column: 17,
};
const syntaxes = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax;
const suggestion = syntaxes?.find(
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE_CREATE
);
expect(suggestion).not.toBeUndefined();
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.']);
});
test('Insert into table ', () => {
const pos: CaretPosition = {
lineNumber: 15,
column: 13,
};
const syntaxes = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax;
const suggestion = syntaxes?.find(
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE
);
expect(suggestion).not.toBeUndefined();
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
});
test('Insert into select from table ', () => {
const pos: CaretPosition = {
lineNumber: 21,
column: 87,
};
const syntaxes = parser.getSuggestionAtCaretPosition(syntaxSql, pos)?.syntax;
const suggestion = syntaxes?.find(
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE
);
expect(suggestion).not.toBeUndefined();
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual([]);
});
});

View File

@ -50,23 +50,22 @@ describe('MySQL Syntax Suggestion', () => {
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.']);
});
// TODO: fix bug of basic parser and decomment following test
// test('Create table ', () => {
// const pos: CaretPosition = {
// lineNumber: 5,
// column: 17,
// };
// const syntaxes = parser.getSuggestionAtCaretPosition(
// commentOtherLine(syntaxSql, pos.lineNumber),
// pos
// )?.syntax;
// const suggestion = syntaxes?.find(
// (syn) => syn.syntaxContextType === SyntaxContextType.TABLE_CREATE
// );
test('Create table ', () => {
const pos: CaretPosition = {
lineNumber: 5,
column: 17,
};
const syntaxes = parser.getSuggestionAtCaretPosition(
commentOtherLine(syntaxSql, pos.lineNumber),
pos
)?.syntax;
const suggestion = syntaxes?.find(
(syn) => syn.syntaxContextType === SyntaxContextType.TABLE_CREATE
);
// expect(suggestion).not.toBeUndefined();
// expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.']);
// });
expect(suggestion).not.toBeUndefined();
expect(suggestion?.wordRanges.map((token) => token.text)).toEqual(['db', '.']);
});
test('DROP table ', () => {
const pos: CaretPosition = {

View File

@ -42,7 +42,7 @@ describe('MySQL Database Administration Syntax Tests', () => {
it(sql, () => {
const result = parser.validate(sql);
if (result.length) {
console.log(result, `\n请检查 sql: ${sql}`);
console.log(result, `\nPlease check sql: ${sql}`);
}
expect(result.length).toBe(0);
});

View File

@ -49,7 +49,7 @@ describe('MySQL DDL Syntax Tests', () => {
it(sql, () => {
const result = parser.validate(sql);
if (result.length) {
console.log(result, `\n请检查 sql: ${sql}`);
console.log(result, `\nPlease check sql: ${sql}`);
}
expect(result.length).toBe(0);
});

View File

@ -33,7 +33,7 @@ describe('MySQL DML Syntax Tests', () => {
it(sql, () => {
const result = parser.validate(sql);
if (result.length) {
console.log(result, `\n请检查 sql: ${sql}`);
console.log(result, `\nPlease check sql: ${sql}`);
}
expect(result.length).toBe(0);
});

View File

@ -27,7 +27,7 @@ describe('MySQL Transactional and Locking, Replication, Prepared Compound and Ut
it(sql, () => {
const result = parser.validate(sql);
if (result.length) {
console.log(result, `\n请检查 sql: ${sql}`);
console.log(result, `\nPlease check sql: ${sql}`);
}
expect(result.length).toBe(0);
});