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    /// Bind one entity data service to an explicit executor.
23    ///
24    /// Transaction scopes use this constructor to ensure generated repositories
25    /// execute against the transaction-owned connection instead of the ambient
26    /// context executor.
27    pub fn for_executor(
28        context: &'a UserContext,
29        entity: impl Into<String>,
30        executor: &'a E,
31    ) -> Self {
32        Self {
33            entity: entity.into(),
34            data_service: ContextDataService {
35                metadata: UserContextMetadata { context },
36                executor,
37            },
38            trace_context: Vec::new(),
39        }
40    }
41
42    pub fn with_trace_context(mut self, trace_context: Vec<teaql_core::TraceNode>) -> Self {
43        self.trace_context = trace_context;
44        self
45    }
46}
47
48#[derive(Debug, Clone, PartialEq)]
49pub struct RelationLoadPlan {
50    pub parent_entity: String,
51    pub relation_name: String,
52    pub path: String,
53    pub target_entity: String,
54    pub local_key: String,
55    pub foreign_key: String,
56    pub many: bool,
57    pub query: Option<SelectQuery>,
58    pub children: Vec<RelationLoadPlan>,
59}
60
61pub(crate) struct UserContextMetadata<'a> {
62    pub(crate) context: &'a UserContext,
63}
64
65impl MetadataStore for UserContextMetadata<'_> {
66    fn entity(&self, name: &str) -> Option<&EntityDescriptor> {
67        self.context.entity(name)
68    }
69
70    fn all_entities(&self) -> Vec<&EntityDescriptor> {
71        self.context
72            .metadata
73            .as_ref()
74            .map(|metadata| metadata.all_entities())
75            .unwrap_or_default()
76    }
77
78    fn record_metadata_log(&self, metadata: &teaql_data_service::ExecutionMetadata) {
79        self.context.record_metadata_log(metadata);
80    }
81
82    fn capture_query_debug(&self) -> bool {
83        self.context.sql_log_options().select
84    }
85
86    fn capture_execution_metadata(&self) -> bool {
87        self.context.sql_log_options().select
88    }
89}