pub mod ast;
pub mod error;
pub mod executor;
pub mod lexer;
pub mod parser;
#[cfg(test)]
mod tests;
pub use ast::*;
pub use error::{GqlError, GqlResult};
pub use executor::GqlExecutor;
pub use lexer::Lexer;
pub use parser::Parser;
use crate::Graph;
use std::cell::RefCell;
pub struct Gql<'a> {
#[allow(dead_code)]
graph: &'a RefCell<Graph>,
executor: GqlExecutor<'a>,
}
impl<'a> Gql<'a> {
pub fn new(graph: &'a RefCell<Graph>) -> Self {
Self {
graph,
executor: GqlExecutor::new(graph),
}
}
pub fn execute(&self, query: &str) -> GqlResult<QueryResult> {
let mut lexer = Lexer::new(query);
let tokens = lexer.tokenize()?;
let mut parser = Parser::new(tokens);
let ast = parser.parse()?;
self.executor.execute(ast)
}
pub fn parse(&self, query: &str) -> GqlResult<GqlStatement> {
let mut lexer = Lexer::new(query);
let tokens = lexer.tokenize()?;
let mut parser = Parser::new(tokens);
parser.parse()
}
}
#[derive(Debug, Clone)]
pub struct QueryResult {
pub columns: Vec<String>,
pub rows: Vec<Vec<QueryValue>>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum QueryValue {
Null,
Boolean(bool),
Integer(i64),
Float(f64),
String(String),
Node {
id: u64,
labels: Vec<String>,
properties: std::collections::HashMap<String, serde_json::Value>,
},
Relationship {
id: u64,
from_id: u64,
to_id: u64,
rel_type: String,
properties: std::collections::HashMap<String, serde_json::Value>,
},
Path(Vec<QueryValue>),
List(Vec<QueryValue>),
}
impl QueryResult {
pub fn new() -> Self {
Self {
columns: Vec::new(),
rows: Vec::new(),
}
}
pub fn add_column(&mut self, name: String) {
self.columns.push(name);
}
pub fn add_row(&mut self, row: Vec<QueryValue>) {
self.rows.push(row);
}
pub fn row_count(&self) -> usize {
self.rows.len()
}
pub fn is_empty(&self) -> bool {
self.rows.is_empty()
}
}
impl Default for QueryResult {
fn default() -> Self {
Self::new()
}
}