Skip to content

Commit

Permalink
feat: add function to determine if a statement is a query operation
Browse files Browse the repository at this point in the history
  • Loading branch information
saibatizoku committed May 16, 2024
1 parent 1696f88 commit 9e0e2c9
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions catalyst-gateway/bin/src/event_db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{str::FromStr, sync::Arc};
use bb8::Pool;
use bb8_postgres::PostgresConnectionManager;
use dotenvy::dotenv;
use stringzilla::StringZilla;
use tokio::sync::RwLock;
use tokio_postgres::{types::ToSql, NoTls, Row};
use tracing::{debug, debug_span, Instrument};
Expand Down Expand Up @@ -211,3 +212,36 @@ pub(crate) async fn establish_connection(url: Option<String>) -> anyhow::Result<
inspection_settings: Arc::new(RwLock::new(DatabaseInspectionSettings::default())),
})
}

/// Determine if the statement is a query statement.
///
/// If the query statement starts with `SELECT` or contains `RETURNING`, then it is a
/// query.
#[allow(dead_code)]
fn is_query_stmt(stmt: &str) -> bool {
matches!(
(stmt.sz_find("SELECT"), stmt.sz_find("RETURNING"),),
(Some(0), _) | (_, Some(_)),
)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_is_query_statement() {
let stmt = "SELECT * FROM dummy";
assert!(is_query_stmt(stmt));
let stmt = "UPDATE dummy SET foo = $1 WHERE bar = $2 RETURNING *";
assert!(is_query_stmt(stmt));
}

#[test]
fn test_is_not_query_statement() {
let stmt = "UPDATE dummy SET foo_count = foo_count + 1 WHERE bar = (SELECT bar_id FROM foos WHERE name = 'FooBar')";
assert!(!is_query_stmt(stmt));
let stmt = "UPDATE dummy SET foo = $1 WHERE bar = $2";
assert!(!is_query_stmt(stmt));
}
}

0 comments on commit 9e0e2c9

Please sign in to comment.