Skip to content

Commit a466365

Browse files
committed
feat: expose prepared statement read-only status
1 parent a1ab53f commit a466365

4 files changed

Lines changed: 32 additions & 49 deletions

File tree

‎include/lbug_rs.h‎

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
#pragma once
22

33
#include <cstdint>
4+
#include <array>
45
#include <memory>
6+
#include <string>
7+
#include <string_view>
8+
#include <unordered_map>
9+
#include <vector>
510

611
#include "rust/cxx.h"
712
#ifdef LBUG_BUNDLED
8-
#include "common/enums/statement_type.h"
9-
#include "common/type_utils.h"
10-
#include "common/types/int128_t.h"
11-
#include "common/types/types.h"
12-
#include "common/types/value/nested.h"
13-
#include "common/types/value/node.h"
14-
#include "common/types/value/recursive_rel.h"
15-
#include "common/types/value/rel.h"
16-
#include "common/types/value/value.h"
13+
#include "c_api/lbug.h"
1714
#include "main/lbug.h"
18-
#include "storage/storage_version_info.h"
1915
#else
2016
#include <lbug.hpp>
21-
#include "common/enums/statement_type.h"
17+
#include <lbug.h>
2218
#endif
2319

2420
namespace lbug_rs {
@@ -141,9 +137,10 @@ inline void connection_set_query_timeout(lbug::main::Connection& connection, uin
141137

142138
/* PreparedStatement */
143139
rust::String prepared_statement_error_message(const lbug::main::PreparedStatement& statement);
144-
inline lbug::common::StatementType prepared_statement_get_statement_type(
145-
const lbug::main::PreparedStatement& statement) {
146-
return statement.getStatementType();
140+
inline bool prepared_statement_is_read_only(const lbug::main::PreparedStatement& statement) {
141+
lbug_prepared_statement c_statement{
142+
const_cast<lbug::main::PreparedStatement*>(&statement), nullptr};
143+
return lbug_prepared_statement_is_read_only(&c_statement);
147144
}
148145
inline bool prepared_statement_is_success(const lbug::main::PreparedStatement& statement) {
149146
return statement.isSuccess();

‎src/connection.rs‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ pub struct PreparedStatement {
1414
}
1515

1616
impl PreparedStatement {
17-
/// Returns the type of the prepared statement (QUERY, CREATE_TABLE, etc.).
18-
pub fn get_statement_type(&self) -> ffi::StatementType {
19-
ffi::prepared_statement_get_statement_type(&self.statement)
17+
/// Returns true if the prepared statement only performs read operations.
18+
pub fn is_read_only(&self) -> bool {
19+
ffi::prepared_statement_is_read_only(&self.statement)
2020
}
2121
}
2222

@@ -302,6 +302,23 @@ Invalid input <MATCH (a:Person RETURN>: expected rule oC_SingleQuery (line: 1, o
302302
Ok(())
303303
}
304304

305+
#[test]
306+
fn test_prepared_statement_is_read_only() -> Result<()> {
307+
let temp_dir = tempfile::tempdir()?;
308+
let db = Database::new(temp_dir.path().join("test"), SYSTEM_CONFIG_FOR_TESTS)?;
309+
let conn = Connection::new(&db)?;
310+
311+
let read_statement = conn.prepare("RETURN 1")?;
312+
assert!(read_statement.is_read_only());
313+
314+
let write_statement =
315+
conn.prepare("CREATE NODE TABLE Person(name STRING, PRIMARY KEY(name));")?;
316+
assert!(!write_statement.is_read_only());
317+
318+
temp_dir.close()?;
319+
Ok(())
320+
}
321+
305322
#[test]
306323
fn test_multithreaded_single_conn() -> Result<()> {
307324
let temp_dir = tempfile::tempdir()?;

‎src/ffi.rs‎

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -119,40 +119,10 @@ pub(crate) mod ffi {
119119
POINTER = 25,
120120
}
121121

122-
// Mirrors lbug::common::StatementType from common/enums/statement_type.h
123-
#[namespace = "lbug::common"]
124-
#[repr(u8)]
125-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
126-
enum StatementType {
127-
QUERY = 0,
128-
CREATE_TABLE = 1,
129-
DROP = 2,
130-
ALTER = 3,
131-
COPY_TO = 19,
132-
COPY_FROM = 20,
133-
STANDALONE_CALL = 21,
134-
STANDALONE_CALL_FUNCTION = 22,
135-
EXPLAIN = 23,
136-
CREATE_MACRO = 24,
137-
TRANSACTION = 30,
138-
EXTENSION = 31,
139-
EXPORT_DATABASE = 32,
140-
IMPORT_DATABASE = 33,
141-
ATTACH_DATABASE = 34,
142-
DETACH_DATABASE = 35,
143-
USE_DATABASE = 36,
144-
CREATE_SEQUENCE = 37,
145-
CREATE_TYPE = 39,
146-
EXTENSION_CLAUSE = 40,
147-
CREATE_GRAPH = 41,
148-
USE_GRAPH = 42,
149-
}
150-
151122
#[namespace = "lbug::common"]
152123
unsafe extern "C++" {
153124
type LogicalTypeID;
154125
type PhysicalTypeID;
155-
type StatementType;
156126
}
157127

158128
#[namespace = "lbug::main"]
@@ -165,7 +135,7 @@ pub(crate) mod ffi {
165135
fn prepared_statement_error_message(statement: &PreparedStatement) -> String;
166136

167137
#[namespace = "lbug_rs"]
168-
fn prepared_statement_get_statement_type(statement: &PreparedStatement) -> StatementType;
138+
fn prepared_statement_is_read_only(statement: &PreparedStatement) -> bool;
169139
}
170140

171141
#[namespace = "lbug_rs"]

‎src/lib.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
pub use connection::{Connection, PreparedStatement};
7272
pub use database::{Database, SystemConfig};
7373
pub use error::Error;
74-
pub use ffi::ffi::StatementType;
7574
pub use logical_type::LogicalType;
7675
#[cfg(feature = "arrow")]
7776
pub use query_result::ArrowIterator;

0 commit comments

Comments
 (0)