move detect_sql_access_type to common mod

This commit is contained in:
Diego Imbert
2025-12-02 12:01:21 +01:00
parent f866f4819d
commit 20ffb3a2c9
2 changed files with 35 additions and 34 deletions
@@ -5,7 +5,8 @@ use swc_ecma_ast::{CallExpr, Expr, Lit, MemberExpr, MemberProp, Str};
use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax, TsSyntax};
use swc_ecma_visit::{Visit, VisitWith};
use windmill_parser::asset_parser::{
merge_assets, parse_asset_syntax, AssetKind, AssetUsageAccessType, ParseAssetsResult,
detect_sql_access_type, merge_assets, parse_asset_syntax, AssetKind, AssetUsageAccessType,
ParseAssetsResult,
};
use AssetUsageAccessType::*;
@@ -229,36 +230,3 @@ impl AssetsFinder {
Ok(())
}
}
fn detect_sql_access_type(sql: &str) -> Option<AssetUsageAccessType> {
let first_kw = sql
.trim()
.split_whitespace()
.next()
.unwrap_or("")
.to_lowercase();
// Check for write operations
let has_write = first_kw.starts_with("insert")
|| first_kw.starts_with("update")
|| first_kw.starts_with("delete")
|| first_kw.starts_with("drop")
|| first_kw.starts_with("create")
|| first_kw.starts_with("alter")
|| first_kw.starts_with("truncate")
|| first_kw.starts_with("merge");
// Check for read operations
let has_read = first_kw.starts_with("select")
|| first_kw.starts_with("with") // CTEs, usually for reads
|| first_kw.starts_with("show")
|| first_kw.starts_with("describe")
|| first_kw.starts_with("explain");
match (has_read, has_write) {
(true, true) => Some(RW),
(true, false) => Some(R),
(false, true) => Some(W),
(false, false) => None, // Unknown - couldn't determine
}
}
@@ -73,3 +73,36 @@ pub fn parse_asset_syntax(s: &str) -> Option<(AssetKind, &str)> {
None
}
}
pub fn detect_sql_access_type(sql: &str) -> Option<AssetUsageAccessType> {
let first_kw = sql
.trim()
.split_whitespace()
.next()
.unwrap_or("")
.to_lowercase();
// Check for write operations
let has_write = first_kw.starts_with("insert")
|| first_kw.starts_with("update")
|| first_kw.starts_with("delete")
|| first_kw.starts_with("drop")
|| first_kw.starts_with("create")
|| first_kw.starts_with("alter")
|| first_kw.starts_with("truncate")
|| first_kw.starts_with("merge");
// Check for read operations
let has_read = first_kw.starts_with("select")
|| first_kw.starts_with("with") // CTEs, usually for reads
|| first_kw.starts_with("show")
|| first_kw.starts_with("describe")
|| first_kw.starts_with("explain");
match (has_read, has_write) {
(true, true) => Some(RW),
(true, false) => Some(R),
(false, true) => Some(W),
(false, false) => None, // Unknown - couldn't determine
}
}