hamelin_legacy 0.4.3

Legacy AST translation code for Hamelin (to be deprecated)
Documentation
use std::{cell::RefCell, rc::Rc, sync::Arc};

use hamelin_lib::completion::Completion;
use hamelin_lib::func::def::FunctionTranslationContext;
use hamelin_lib::func::registry::FunctionRegistry;
use hamelin_lib::provider::EnvironmentProvider;
use hamelin_lib::sql::expression::SQLExpression;
use hamelin_sql::TranslationRegistry;

use crate::env::Environment;

pub mod apply;
pub mod assignment_clause;
pub mod cast;
pub mod column_ref;
pub mod command;
pub mod deref;
pub mod expression;
pub mod from_clause;
pub mod index;
pub mod pipeline;
pub mod query;
pub mod sort_expression;
pub mod string;

pub struct QueryTranslationContext {
    pub within: Option<SQLExpression>,
    pub provider: Arc<dyn EnvironmentProvider>,
    pub registry: Arc<FunctionRegistry>,
    pub translation_registry: Arc<TranslationRegistry>,
    pub at: Option<usize>,
    pub completions: Rc<RefCell<Option<Completion>>>,
}

impl QueryTranslationContext {
    pub fn new(
        within: Option<SQLExpression>,
        provider: Arc<dyn EnvironmentProvider>,
        registry: Arc<FunctionRegistry>,
        translation_registry: Arc<TranslationRegistry>,
        at: Option<usize>,
    ) -> Rc<Self> {
        Rc::new(Self {
            within,
            provider,
            registry,
            translation_registry,
            at,
            completions: Rc::new(RefCell::new(None)),
        })
    }

    pub fn default_expression_translation_context(
        &self,
        env: &Environment,
    ) -> Rc<ExpressionTranslationContext> {
        Rc::new(ExpressionTranslationContext {
            fctx: FunctionTranslationContext::default(),
            bindings: Arc::new(env.clone()),
            registry: self.registry.clone(),
            translation_registry: self.translation_registry.clone(),
            at: self.at.clone(),
            completions: self.completions.clone(),
        })
    }

    pub fn expression_translation_context(
        &self,
        env: &Environment,
        fctx: FunctionTranslationContext,
    ) -> Rc<ExpressionTranslationContext> {
        Rc::new(ExpressionTranslationContext {
            fctx,
            bindings: Arc::new(env.clone()),
            registry: self.registry.clone(),
            translation_registry: self.translation_registry.clone(),
            at: self.at.clone(),
            completions: self.completions.clone(),
        })
    }

    pub fn without_completion(&self) -> Rc<Self> {
        Self::new(
            self.within.clone(),
            self.provider.clone(),
            self.registry.clone(),
            self.translation_registry.clone(),
            None,
        )
    }
}

#[derive(Clone, Debug)]
pub struct ExpressionTranslationContext {
    pub fctx: FunctionTranslationContext,
    pub bindings: Arc<Environment>, // TODO: Rc
    pub registry: Arc<FunctionRegistry>,
    pub translation_registry: Arc<TranslationRegistry>,
    pub at: Option<usize>,
    pub completions: Rc<RefCell<Option<Completion>>>,
}

impl ExpressionTranslationContext {
    pub fn new(
        bindings: Arc<Environment>,
        registry: Arc<FunctionRegistry>,
        translation_registry: Arc<TranslationRegistry>,
        fctx: FunctionTranslationContext,
        at: Option<usize>,
        completions: Rc<RefCell<Option<Completion>>>,
    ) -> Rc<Self> {
        Rc::new(Self {
            fctx,
            bindings,
            registry,
            translation_registry,
            at,
            completions,
        })
    }

    pub fn without_completion(
        self: Rc<ExpressionTranslationContext>,
    ) -> Rc<ExpressionTranslationContext> {
        Rc::new(ExpressionTranslationContext {
            fctx: self.fctx.clone(),
            bindings: self.bindings.clone(),
            registry: self.registry.clone(),
            translation_registry: self.translation_registry.clone(),
            at: None,
            completions: self.completions.clone(),
        })
    }

    pub fn with_env(
        self: Rc<ExpressionTranslationContext>,
        env: Arc<Environment>,
    ) -> Rc<ExpressionTranslationContext> {
        Rc::new(ExpressionTranslationContext {
            fctx: self.fctx.clone(),
            bindings: env,
            registry: self.registry.clone(),
            translation_registry: self.translation_registry.clone(),
            at: self.at,
            completions: self.completions.clone(),
        })
    }
}