diff --git a/src/data/ast.test.ts b/src/data/ast.test.ts index 01a7f07d..58f7a5f2 100644 --- a/src/data/ast.test.ts +++ b/src/data/ast.test.ts @@ -1,4 +1,5 @@ -import { getFields } from './ast'; +import { getFields, sqlToStatement } from './ast'; +import { toSql } from 'pgsql-ast-parser' describe('ast', () => { describe('getFields', () => { @@ -7,4 +8,12 @@ describe('ast', () => { expect(stm.length).toBe(1); }); }); + describe('sqlToStatement', () => { + it('settings parse correctly', () => { + const sql = 'SELECT count(*) FROM foo SETTINGS setting1=stuff setting2=stuff'; + const stm = sqlToStatement(sql); + // this is formatted like this to match how pgsql generates its sql + expect(toSql.statement(stm)).toEqual('SELECT (count (*) ) FROM foo'); + }); + }); }); diff --git a/src/data/ast.ts b/src/data/ast.ts index 122b3c7d..a4249dae 100644 --- a/src/data/ast.ts +++ b/src/data/ast.ts @@ -7,7 +7,7 @@ export function sqlToStatement(sql: string): Statement { replacementName: string; }> = []; //default is a key word in this grammar, but it can be used in CH - const re = /(\$__|\$|default)/gi; + const re = /(\$__|\$|default|settings)/gi; let regExpArray: RegExpExecArray | null; while ((regExpArray = re.exec(sql)) !== null) { replaceFuncs.push({ startIndex: regExpArray.index, name: regExpArray[0], replacementName: '' }); @@ -18,6 +18,11 @@ export function sqlToStatement(sql: string): Statement { const si = replaceFuncs[i].startIndex; const replacementName = 'f' + (Math.random() + 1).toString(36).substring(7); replaceFuncs[i].replacementName = replacementName; + // settings do not parse and we do not need information from them so we will remove them + if (replaceFuncs[i].name.toLowerCase() === "settings") { + sql = sql.substring(0, si) + continue; + } sql = sql.substring(0, si) + replacementName + sql.substring(si + replaceFuncs[i].name.length); }