mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 17:05:18 +00:00
Improve accuracy of query platform compatibility check when WITH
expressions used (#3731)
This commit is contained in:
parent
371c533bfc
commit
dea23356de
1
changes/issue-3590-query-compatibilty
Normal file
1
changes/issue-3590-query-compatibilty
Normal file
@ -0,0 +1 @@
|
|||||||
|
* Improve accuracy of query platform compatibility check when `WITH` expressions used
|
@ -44,17 +44,22 @@ export const listCompatiblePlatforms = (tablesList) => {
|
|||||||
return compatiblePlatforms.length ? compatiblePlatforms : ["None"];
|
return compatiblePlatforms.length ? compatiblePlatforms : ["None"];
|
||||||
};
|
};
|
||||||
|
|
||||||
export const parseSqlTables = (sqlString) => {
|
export const parseSqlTables = (sqlString, inludeCteTables = false) => {
|
||||||
const tablesList = [];
|
let results = [];
|
||||||
|
|
||||||
|
// Tables defined via common table expression will be excluded from results by default
|
||||||
|
const cteTables = [];
|
||||||
|
|
||||||
const _callback = (node) => {
|
const _callback = (node) => {
|
||||||
if (node) {
|
if (node) {
|
||||||
if (node.variant === "recursive") {
|
if (
|
||||||
throw new Error(
|
(node.variant === "common" || node.variant === "recursive") &&
|
||||||
"Invalid usage: `recursive` is not supported by `parseSqlTables`"
|
node.format === "table" &&
|
||||||
);
|
node.type === "expression"
|
||||||
|
) {
|
||||||
|
cteTables.push(node.target?.name);
|
||||||
} else if (node.variant === "table") {
|
} else if (node.variant === "table") {
|
||||||
tablesList.push(node.name);
|
results.push(node.name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -63,7 +68,11 @@ export const parseSqlTables = (sqlString) => {
|
|||||||
const sqlTree = sqliteParser(sqlString);
|
const sqlTree = sqliteParser(sqlString);
|
||||||
_visit(sqlTree, _callback);
|
_visit(sqlTree, _callback);
|
||||||
|
|
||||||
return tablesList.length ? tablesList : ["No tables in query AST"];
|
if (cteTables.length) {
|
||||||
|
results = results.filter((t) => !cteTables.includes(t));
|
||||||
|
}
|
||||||
|
|
||||||
|
return results.length ? results : ["No tables in query AST"];
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// console.log(`Invalid query syntax: ${err.message}\n\n${sqlString}`);
|
// console.log(`Invalid query syntax: ${err.message}\n\n${sqlString}`);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user