use crate::storage::QueryStorage;
use crate::storage::StorageOperationContext;
use linkrs_core::Value;
use parking_lot::RwLock;
use std::collections::HashMap;
use std::sync::Arc;
#[derive(Debug, Clone, Default)]
pub struct QueryRequestContext {
pub session_id: Option<i64>,
pub user_name: Option<String>,
pub space_name: Option<String>,
pub query: String,
pub parameters: HashMap<String, Value>,
pub session_variables: HashMap<String, Value>,
pub query_id: Option<u64>,
pub transaction_id: Option<linkrs_core::types::TransactionId>,
pub auto_commit: bool,
pub read_only: bool,
pub isolation_level: Option<linkrs_core::types::TransactionIsolationLevel>,
pub operation_context: Option<StorageOperationContext>,
pub operation_storage: Option<Arc<RwLock<dyn QueryStorage>>>,
pub parsed_statement: Option<Arc<crate::parser::ast::stmt::Ast>>,
pub ryw_config: Option<linkrs_core::types::ReadYourWritesConfig>,
}
impl QueryRequestContext {
pub fn new(query: String) -> Self {
Self {
session_id: None,
user_name: None,
space_name: None,
query,
parameters: HashMap::new(),
session_variables: HashMap::new(),
query_id: None,
transaction_id: None,
auto_commit: true,
read_only: false,
isolation_level: None,
operation_context: None,
operation_storage: None,
parsed_statement: None,
ryw_config: None,
}
}
pub fn with_parameters(mut self, parameters: HashMap<String, Value>) -> Self {
self.parameters = parameters;
self
}
pub fn with_session_variables(mut self, variables: HashMap<String, Value>) -> Self {
self.session_variables = variables;
self
}
pub fn with_session_id(mut self, session_id: i64) -> Self {
self.session_id = Some(session_id);
self
}
pub fn with_user_name(mut self, user_name: String) -> Self {
self.user_name = Some(user_name);
self
}
pub fn with_space_name(mut self, space_name: String) -> Self {
self.space_name = Some(space_name);
self
}
pub fn with_query_id(mut self, query_id: u64) -> Self {
self.query_id = Some(query_id);
self
}
pub fn get_parameter(&self, param: &str) -> Option<Value> {
self.parameters.get(param).cloned()
}
pub fn has_parameter(&self, param: &str) -> bool {
self.parameters.contains_key(param)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_query_request_context_new() {
let ctx = QueryRequestContext::new("MATCH (n) RETURN n".to_string());
assert_eq!(ctx.query, "MATCH (n) RETURN n");
assert!(ctx.session_id.is_none());
assert!(ctx.space_name.is_none());
}
#[test]
fn test_query_request_context_with_params() {
let mut params = HashMap::new();
params.insert("name".into(), Value::from("test"));
let ctx = QueryRequestContext::new("QUERY".to_string())
.with_parameters(params)
.with_session_id(123)
.with_space_name("test_space".to_string());
assert_eq!(ctx.session_id, Some(123));
assert_eq!(ctx.space_name, Some("test_space".to_string()));
assert!(ctx.has_parameter("name"));
}
}