Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ zip = { version = "2", features = [ "deflate" ] }
# TOML parsing (drivers.toml)
toml = "0.8"
sha2 = "0.10"
sqlparser = "0.55"
sqlparser = "0.62"
russh = "0.60"
axum = "0.8"
portpicker = "0.1.1"
Expand Down
48 changes: 39 additions & 9 deletions src-tauri/src/capabilities/sql_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ fn combine_kind(a: SqlKind, b: SqlKind) -> SqlKind {

/// Classify a Query, walking into its body and every CTE.
///
/// sqlparser represents `WITH … INSERT/UPDATE` (PostgreSQL data-modifying
/// sqlparser represents `WITH … INSERT/UPDATE/DELETE/MERGE` (data-modifying
/// CTEs) as a `Query` whose body carries the DML statement, so checking only
/// the top-level variant would let write statements through as Read.
fn classify_query(query: &Query) -> SqlKind {
Expand All @@ -92,7 +92,10 @@ fn classify_set_expr(body: &SetExpr) -> SqlKind {
SetExpr::SetOperation { left, right, .. } => {
combine_kind(classify_set_expr(left), classify_set_expr(right))
}
SetExpr::Insert(stmt) | SetExpr::Update(stmt) => classify_statement(stmt),
SetExpr::Insert(stmt)
| SetExpr::Update(stmt)
| SetExpr::Delete(stmt)
| SetExpr::Merge(stmt) => classify_statement(stmt),
}
}

Expand Down Expand Up @@ -166,13 +169,7 @@ fn classify_statement(stmt: &Statement) -> SqlKind {
| Statement::Comment { .. }
| Statement::LockTables { .. }
| Statement::UnlockTables { .. }
| Statement::SetVariable { .. }
| Statement::SetNames { .. }
| Statement::SetNamesDefault { .. }
| Statement::SetRole { .. }
| Statement::SetSessionParam(_)
| Statement::SetTimeZone { .. }
| Statement::SetTransaction { .. }
| Statement::Set(_)
| Statement::Commit { .. }
| Statement::Rollback { .. }
| Statement::Savepoint { .. }
Expand Down Expand Up @@ -495,6 +492,39 @@ mod tests {
);
}

#[test]
fn classifies_with_delete_as_delete() {
assert_eq!(
classify_sql(
"postgres",
"WITH x AS (SELECT 1 AS id) DELETE FROM t WHERE id IN (SELECT id FROM x)"
)
.unwrap(),
SqlKind::Delete
);
assert_eq!(
classify_sql(
"postgres",
"WITH d AS (DELETE FROM t RETURNING id) SELECT * FROM d"
)
.unwrap(),
SqlKind::Delete
);
}

#[test]
fn classifies_with_merge_as_write() {
assert_eq!(
classify_sql(
"postgres",
"WITH x AS (SELECT 1 AS id) MERGE INTO t USING x ON t.id = x.id \
WHEN MATCHED THEN UPDATE SET c = 1"
)
.unwrap(),
SqlKind::Write
);
}

#[test]
fn read_guard_rejects_with_insert() {
let err = ensure_read_only(
Expand Down
4 changes: 4 additions & 0 deletions src-tauri/src/database/sql_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,10 @@ fn extract_select_items(items: &[SelectItem]) -> Vec<(String, usize)> {
let name = match item {
SelectItem::UnnamedExpr(expr) => expr_to_name(expr),
SelectItem::ExprWithAlias { alias, .. } => alias.value.clone(),
SelectItem::ExprWithAliases { expr, aliases } => aliases
.first()
.map(|a| a.value.clone())
.unwrap_or_else(|| expr_to_name(expr)),
SelectItem::QualifiedWildcard(kind, _) => match kind {
SelectItemQualifiedWildcardKind::ObjectName(obj) => obj.to_string() + ".*",
SelectItemQualifiedWildcardKind::Expr(e) => format!("({}).*", expr_to_name(e)),
Expand Down