real-rs 0.1.0

Universal query engine with relational algebra - compile the same query to PostgreSQL, SQLite, MongoDB, and YottaDB
Documentation
//! Backend trait and implementations
//!
//! The key abstraction: can we compile algebra to fundamentally different data models?

use crate::algebra::Expr;
use crate::schema::{ResultSet, Schema};
use crate::Result;

pub mod yottadb;

#[cfg(feature = "backend-postgres")]
pub mod postgres;

#[cfg(feature = "backend-sqlite")]
pub mod sqlite;

#[cfg(feature = "backend-mongodb")]
pub mod mongodb;

/// Universal backend trait
///
/// Implementations must translate relational algebra to their native query model.
/// This is the hard part that proves true universality.
pub trait Backend {
    /// Backend-specific connection/session type
    type Connection;

    /// Backend-specific compiled query representation
    type CompiledQuery;

    /// Compile a relational algebra expression to backend-native format
    ///
    /// This is where the magic happens: translating formal algebra to
    /// SQL, hierarchical paths, document queries, etc.
    fn compile(&self, expr: &Expr) -> Result<Self::CompiledQuery>;

    /// Execute a compiled query
    fn execute(&self, conn: &mut Self::Connection, query: &Self::CompiledQuery) -> Result<ResultSet>;

    /// One-shot: compile and execute
    fn query(&self, conn: &mut Self::Connection, expr: &Expr) -> Result<ResultSet> {
        let compiled = self.compile(expr)?;
        self.execute(conn, &compiled)
    }

    /// Get the schema of a relation in this backend
    fn get_schema(&self, conn: &mut Self::Connection, relation: &str) -> Result<Schema>;
}

/// Query plan representation (for debugging/introspection)
#[derive(Debug, Clone)]
pub struct QueryPlan {
    pub algebra: String,
    pub backend_native: String,
    pub estimated_cost: Option<f64>,
}