Skip to content
Merged
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
25 changes: 11 additions & 14 deletions include/lbug_rs.h
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
#pragma once

#include <cstdint>
#include <array>
#include <memory>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>

#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 <lbug.hpp>
#include "common/enums/statement_type.h"
#include <lbug.h>
#endif

namespace lbug_rs {
Expand Down Expand Up @@ -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<lbug::main::PreparedStatement*>(&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();
Expand Down
23 changes: 20 additions & 3 deletions src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -302,6 +302,23 @@ Invalid input <MATCH (a:Person RETURN>: 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()?;
Expand Down
32 changes: 1 addition & 31 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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"]
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading