2023-05-04 10:13:05 +08:00
|
|
|
import { lexer, splitSql, cleanSql } from '../../src/utils';
|
2021-05-11 13:33:03 +08:00
|
|
|
import { TokenType } from '../../src/utils/token';
|
2021-01-05 18:02:01 +08:00
|
|
|
|
2019-09-25 15:43:36 +08:00
|
|
|
describe('utils', () => {
|
2020-12-09 17:59:24 +08:00
|
|
|
test('split single sql', () => {
|
|
|
|
const sql = 'select id,name from user';
|
2021-01-05 18:02:01 +08:00
|
|
|
const result = splitSql(sql);
|
2020-12-09 17:59:24 +08:00
|
|
|
expect(result.length).toEqual(1);
|
|
|
|
});
|
2021-09-08 20:33:30 +08:00
|
|
|
|
2020-12-09 17:59:24 +08:00
|
|
|
test('split multiple sql', () => {
|
|
|
|
const sql = `-- a ;
|
|
|
|
select * from a;
|
|
|
|
/*
|
|
|
|
xxx
|
|
|
|
xxx
|
|
|
|
*/
|
|
|
|
select user from b`;
|
2021-01-05 18:02:01 +08:00
|
|
|
const result = splitSql(sql);
|
2020-12-09 17:59:24 +08:00
|
|
|
expect(result.length).toEqual(2);
|
|
|
|
});
|
2021-09-08 20:33:30 +08:00
|
|
|
|
|
|
|
test('split special quotation sql', () => {
|
|
|
|
const sql = `select regexp_replace('a', 'bc', 'xfe'feefe', '233');
|
|
|
|
select regexp_replace('abc', "fe", '233');`;
|
|
|
|
const result = splitSql(sql);
|
|
|
|
expect(result.length).toEqual(2);
|
|
|
|
});
|
|
|
|
|
2022-12-24 14:55:27 +08:00
|
|
|
// test nested bracket
|
|
|
|
test('split nested bracket sql', () => {
|
|
|
|
const sql = `WITH cte_sales_amounts (staff, sales, year)
|
|
|
|
AS
|
|
|
|
(
|
|
|
|
SELECT
|
|
|
|
first_name + ' ' + last_name,
|
|
|
|
SUM(quantity * list_price * (1 - discount)),
|
|
|
|
YEAR(order_date)
|
|
|
|
FROM
|
|
|
|
sales.orders o
|
|
|
|
INNER JOIN sales.order_items i ON i.order_id = o.order_id
|
|
|
|
INNER JOIN sales.staffs s ON s.staff_id = o.staff_id
|
|
|
|
)
|
|
|
|
SELECT staff, sales
|
|
|
|
FROM cte_sales_amounts
|
|
|
|
WHERE year = 2018;
|
|
|
|
SELECT * FROM table;`;
|
|
|
|
const result = splitSql(sql);
|
|
|
|
expect(result.length).toEqual(2);
|
|
|
|
});
|
|
|
|
|
2020-12-09 17:59:24 +08:00
|
|
|
test('lexer', () => {
|
|
|
|
const sql = `-- a ;
|
|
|
|
select * from a;
|
|
|
|
/*
|
|
|
|
xxx
|
|
|
|
xxx
|
|
|
|
*/
|
|
|
|
select user from b;`;
|
2021-01-05 18:02:01 +08:00
|
|
|
const result = lexer(sql);
|
2020-12-09 17:59:24 +08:00
|
|
|
expect(result.length).toEqual(4);
|
|
|
|
});
|
2021-05-11 13:33:03 +08:00
|
|
|
test('lexer for comments', () => {
|
|
|
|
const sql = `select * from a;--comments`;
|
|
|
|
const expected = `--comments`;
|
|
|
|
const result = lexer(sql);
|
|
|
|
const comments = result.find((token) =>
|
|
|
|
token.type === TokenType.Comment,
|
|
|
|
);
|
2023-05-04 10:13:05 +08:00
|
|
|
expect(comments?.value).toEqual(expected);
|
2021-05-11 13:33:03 +08:00
|
|
|
});
|
2020-12-09 17:59:24 +08:00
|
|
|
test('cleanSql', () => {
|
|
|
|
const sql = `-- a ;
|
|
|
|
select * from a;
|
|
|
|
/*
|
|
|
|
xxx
|
|
|
|
xxx
|
|
|
|
*/
|
|
|
|
select user from b`;
|
2021-01-05 18:02:01 +08:00
|
|
|
const result = cleanSql(sql);
|
2020-12-09 17:59:24 +08:00
|
|
|
expect(result.indexOf('xxx')).toEqual(-1);
|
2020-08-28 13:29:47 +08:00
|
|
|
});
|
2021-05-11 13:39:42 +08:00
|
|
|
|
|
|
|
test('clean SQL white spaces', () => {
|
|
|
|
const sql = `
|
|
|
|
select * from a; `;
|
|
|
|
const expected = 'select * from a;';
|
|
|
|
const result = cleanSql(sql);
|
|
|
|
expect(result).toEqual(expected);
|
|
|
|
});
|
2020-08-28 13:29:47 +08:00
|
|
|
});
|