Skip to main content

teaql_runtime/data_service/
types.rs

1use teaql_core::{EntityDescriptor, SelectQuery};
2
3use crate::{MetadataStore, UserContext};
4
5pub(crate) struct RuntimeDataService<'a, M, E> {
6    pub(super) metadata: &'a M,
7    pub(super) executor: &'a E,
8}
9
10pub(crate) struct ContextDataService<'a, E> {
11    pub(super) metadata: UserContextMetadata<'a>,
12    pub(crate) executor: &'a E,
13}
14
15pub struct EntityDataService<'a, E> {
16    pub(super) entity: String,
17    pub(super) data_service: ContextDataService<'a, E>,
18    pub(super) trace_context: Vec<teaql_core::TraceNode>,
19}
20
21impl<'a, E> EntityDataService<'a, E> {
22    pub(crate) fn for_executor(
23        context: &'a UserContext,
24        entity: impl Into<String>,
25        executor: &'a E,
26    ) -> Self {
27        Self {
28            entity: entity.into(),
29            data_service: ContextDataService {
30                metadata: UserContextMetadata { context },
31                executor,
32            },
33            trace_context: Vec::new(),
34        }
35    }
36
37    pub fn with_trace_context(mut self, trace_context: Vec<teaql_core::TraceNode>) -> Self {
38        self.trace_context = trace_context;
39        self
40    }
41}
42
43#[derive(Debug, Clone, PartialEq)]
44pub struct RelationLoadPlan {
45    pub parent_entity: String,
46    pub relation_name: String,
47    pub path: String,
48    pub target_entity: String,
49    pub local_key: String,
50    pub foreign_key: String,
51    pub many: bool,
52    pub query: Option<SelectQuery>,
53    pub children: Vec<RelationLoadPlan>,
54}
55
56pub(crate) struct UserContextMetadata<'a> {
57    pub(crate) context: &'a UserContext,
58}
59
60impl MetadataStore for UserContextMetadata<'_> {
61    fn entity(&self, name: &str) -> Option<&EntityDescriptor> {
62        self.context.entity(name)
63    }
64
65    fn all_entities(&self) -> Vec<&EntityDescriptor> {
66        self.context
67            .metadata
68            .as_ref()
69            .map(|metadata| metadata.all_entities())
70            .unwrap_or_default()
71    }
72
73    fn record_metadata_log(&self, metadata: &teaql_data_service::ExecutionMetadata) {
74        self.context.record_metadata_log(metadata);
75    }
76
77    fn capture_query_debug(&self) -> bool {
78        self.context.sql_log_options().select
79    }
80
81    fn capture_execution_metadata(&self) -> bool {
82        self.context.sql_log_options().select
83    }
84}