From bec03b6ead03356c902dc0fb21c7e1972c0e172f Mon Sep 17 00:00:00 2001 From: Arun Sharma Date: Sun, 10 May 2026 11:06:07 -0700 Subject: [PATCH] feat: expose prepared statement read-only status --- include/lbug_rs.h | 25 +++++++++++-------------- src/connection.rs | 23 ++++++++++++++++++++--- src/ffi.rs | 32 +------------------------------- src/lib.rs | 1 - 4 files changed, 32 insertions(+), 49 deletions(-) diff --git a/include/lbug_rs.h b/include/lbug_rs.h index 3b028a3..b979184 100644 --- a/include/lbug_rs.h +++ b/include/lbug_rs.h @@ -1,24 +1,20 @@ #pragma once #include +#include #include +#include +#include +#include +#include #include "rust/cxx.h" #ifdef LBUG_BUNDLED -#include "common/enums/statement_type.h" -#include "common/type_utils.h" -#include "common/types/int128_t.h" -#include "common/types/types.h" -#include "common/types/value/nested.h" -#include "common/types/value/node.h" -#include "common/types/value/recursive_rel.h" -#include "common/types/value/rel.h" -#include "common/types/value/value.h" +#include "c_api/lbug.h" #include "main/lbug.h" -#include "storage/storage_version_info.h" #else #include -#include "common/enums/statement_type.h" +#include #endif namespace lbug_rs { @@ -141,9 +137,10 @@ inline void connection_set_query_timeout(lbug::main::Connection& connection, uin /* PreparedStatement */ rust::String prepared_statement_error_message(const lbug::main::PreparedStatement& statement); -inline lbug::common::StatementType prepared_statement_get_statement_type( - const lbug::main::PreparedStatement& statement) { - return statement.getStatementType(); +inline bool prepared_statement_is_read_only(const lbug::main::PreparedStatement& statement) { + lbug_prepared_statement c_statement{ + const_cast(&statement), nullptr}; + return lbug_prepared_statement_is_read_only(&c_statement); } inline bool prepared_statement_is_success(const lbug::main::PreparedStatement& statement) { return statement.isSuccess(); diff --git a/src/connection.rs b/src/connection.rs index c3a2395..4034d31 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -14,9 +14,9 @@ pub struct PreparedStatement { } impl PreparedStatement { - /// Returns the type of the prepared statement (QUERY, CREATE_TABLE, etc.). - pub fn get_statement_type(&self) -> ffi::StatementType { - ffi::prepared_statement_get_statement_type(&self.statement) + /// Returns true if the prepared statement only performs read operations. + pub fn is_read_only(&self) -> bool { + ffi::prepared_statement_is_read_only(&self.statement) } } @@ -302,6 +302,23 @@ Invalid input : expected rule oC_SingleQuery (line: 1, o Ok(()) } + #[test] + fn test_prepared_statement_is_read_only() -> Result<()> { + let temp_dir = tempfile::tempdir()?; + let db = Database::new(temp_dir.path().join("test"), SYSTEM_CONFIG_FOR_TESTS)?; + let conn = Connection::new(&db)?; + + let read_statement = conn.prepare("RETURN 1")?; + assert!(read_statement.is_read_only()); + + let write_statement = + conn.prepare("CREATE NODE TABLE Person(name STRING, PRIMARY KEY(name));")?; + assert!(!write_statement.is_read_only()); + + temp_dir.close()?; + Ok(()) + } + #[test] fn test_multithreaded_single_conn() -> Result<()> { let temp_dir = tempfile::tempdir()?; diff --git a/src/ffi.rs b/src/ffi.rs index 376c4f3..e3fe36f 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -119,40 +119,10 @@ pub(crate) mod ffi { POINTER = 25, } - // Mirrors lbug::common::StatementType from common/enums/statement_type.h - #[namespace = "lbug::common"] - #[repr(u8)] - #[derive(Debug, Clone, Copy, PartialEq, Eq)] - enum StatementType { - QUERY = 0, - CREATE_TABLE = 1, - DROP = 2, - ALTER = 3, - COPY_TO = 19, - COPY_FROM = 20, - STANDALONE_CALL = 21, - STANDALONE_CALL_FUNCTION = 22, - EXPLAIN = 23, - CREATE_MACRO = 24, - TRANSACTION = 30, - EXTENSION = 31, - EXPORT_DATABASE = 32, - IMPORT_DATABASE = 33, - ATTACH_DATABASE = 34, - DETACH_DATABASE = 35, - USE_DATABASE = 36, - CREATE_SEQUENCE = 37, - CREATE_TYPE = 39, - EXTENSION_CLAUSE = 40, - CREATE_GRAPH = 41, - USE_GRAPH = 42, - } - #[namespace = "lbug::common"] unsafe extern "C++" { type LogicalTypeID; type PhysicalTypeID; - type StatementType; } #[namespace = "lbug::main"] @@ -165,7 +135,7 @@ pub(crate) mod ffi { fn prepared_statement_error_message(statement: &PreparedStatement) -> String; #[namespace = "lbug_rs"] - fn prepared_statement_get_statement_type(statement: &PreparedStatement) -> StatementType; + fn prepared_statement_is_read_only(statement: &PreparedStatement) -> bool; } #[namespace = "lbug_rs"] diff --git a/src/lib.rs b/src/lib.rs index 91addce..77eb2d5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,7 +71,6 @@ pub use connection::{Connection, PreparedStatement}; pub use database::{Database, SystemConfig}; pub use error::Error; -pub use ffi::ffi::StatementType; pub use logical_type::LogicalType; #[cfg(feature = "arrow")] pub use query_result::ArrowIterator;