2024-03-27 10:33:25 +08:00
|
|
|
import { PostgreSQL, PostgreSqlSplitListener } from 'src/parser/postgresql';
|
|
|
|
import { PostgreSqlParserListener } from 'src/lib/postgresql/PostgreSqlParserListener';
|
2023-12-11 17:34:49 +08:00
|
|
|
|
|
|
|
const validSQL1 = `INSERT INTO country_page_view
|
|
|
|
VALUES ('Chinese', 'mumiao', 18),
|
|
|
|
('Amercian', 'georage', 22);`;
|
|
|
|
const validSQL2 = 'SELECT * FROM tb;';
|
|
|
|
const inValidSQL = 'CREATE TABLE';
|
|
|
|
|
|
|
|
describe('PgSQL ErrorStrategy test', () => {
|
2024-03-27 10:33:25 +08:00
|
|
|
const pgSQL = new PostgreSQL();
|
2023-12-11 17:34:49 +08:00
|
|
|
|
|
|
|
// TODO: handle unexpected case
|
|
|
|
// test('begin inValid', () => {
|
|
|
|
// const sql = [inValidSQL, validSQL1, validSQL2].join('\n');
|
|
|
|
// // parse with empty errorListener
|
|
|
|
// const parseTree = pgSQL.parse(sql, () => {});
|
2024-03-27 10:33:25 +08:00
|
|
|
// const splitListener = new PostgreSQLSplitListener();
|
|
|
|
// pgSQL.listen(splitListener as PostgreSqlParserListener, parseTree);
|
2023-12-11 17:34:49 +08:00
|
|
|
|
|
|
|
// const statementCount = splitListener.statementsContext.length;
|
|
|
|
// splitListener.statementsContext.map((item, index) => {
|
|
|
|
// if(index !== statementCount-1 && index !== statementCount - 2) {
|
2024-02-26 20:25:09 +08:00
|
|
|
// expect(item.exception).not.toBe(null);
|
2023-12-11 17:34:49 +08:00
|
|
|
// } else {
|
2024-02-26 20:25:09 +08:00
|
|
|
// expect(item.exception).toBe(null);
|
2023-12-11 17:34:49 +08:00
|
|
|
// }
|
|
|
|
// })
|
|
|
|
// });
|
|
|
|
|
|
|
|
test('middle inValid', () => {
|
|
|
|
const sql = [validSQL1, inValidSQL, validSQL2].join('\n');
|
|
|
|
// parse with empty errorListener
|
|
|
|
const parseTree = pgSQL.parse(sql, () => {});
|
2024-03-26 14:28:27 +08:00
|
|
|
const splitListener = new PostgreSqlSplitListener();
|
2024-03-27 10:33:25 +08:00
|
|
|
pgSQL.listen(splitListener as PostgreSqlParserListener, parseTree);
|
2023-12-11 17:34:49 +08:00
|
|
|
|
|
|
|
const statementCount = splitListener.statementsContext.length;
|
|
|
|
splitListener.statementsContext.map((item, index) => {
|
|
|
|
if (index !== statementCount - 1 && index !== 0) {
|
2024-02-26 20:25:09 +08:00
|
|
|
expect(item.exception).not.toBe(null);
|
2023-12-11 17:34:49 +08:00
|
|
|
} else {
|
2024-02-26 20:25:09 +08:00
|
|
|
expect(item.exception).toBe(null);
|
2023-12-11 17:34:49 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('end inValid', () => {
|
|
|
|
const sql = [validSQL1, validSQL2, inValidSQL].join('\n');
|
|
|
|
// parse with empty errorListener
|
|
|
|
const parseTree = pgSQL.parse(sql, () => {});
|
2024-03-26 14:28:27 +08:00
|
|
|
const splitListener = new PostgreSqlSplitListener();
|
2024-03-27 10:33:25 +08:00
|
|
|
pgSQL.listen(splitListener as PostgreSqlParserListener, parseTree);
|
2023-12-11 17:34:49 +08:00
|
|
|
|
|
|
|
splitListener.statementsContext.map((item, index) => {
|
|
|
|
if (index !== 0 && index !== 1) {
|
2024-02-26 20:25:09 +08:00
|
|
|
expect(item.exception).not.toBe(null);
|
2023-12-11 17:34:49 +08:00
|
|
|
} else {
|
2024-02-26 20:25:09 +08:00
|
|
|
expect(item.exception).toBe(null);
|
2023-12-11 17:34:49 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|