mirror of
https://github.com/fleetdm/fleet
synced 2026-05-23 17:08:53 +00:00
Improve accuracy of query platform compatibility check when WITH expressions used (#3731)
This commit is contained in:
parent
371c533bfc
commit
dea23356de
2 changed files with 18 additions and 8 deletions
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"];
|
||||
};
|
||||
|
||||
export const parseSqlTables = (sqlString) => {
|
||||
const tablesList = [];
|
||||
export const parseSqlTables = (sqlString, inludeCteTables = false) => {
|
||||
let results = [];
|
||||
|
||||
// Tables defined via common table expression will be excluded from results by default
|
||||
const cteTables = [];
|
||||
|
||||
const _callback = (node) => {
|
||||
if (node) {
|
||||
if (node.variant === "recursive") {
|
||||
throw new Error(
|
||||
"Invalid usage: `recursive` is not supported by `parseSqlTables`"
|
||||
);
|
||||
if (
|
||||
(node.variant === "common" || node.variant === "recursive") &&
|
||||
node.format === "table" &&
|
||||
node.type === "expression"
|
||||
) {
|
||||
cteTables.push(node.target?.name);
|
||||
} 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);
|
||||
_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) {
|
||||
// console.log(`Invalid query syntax: ${err.message}\n\n${sqlString}`);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue