1mod query;
7mod sql;
8#[cfg(test)]
12mod tests;
13mod write;
14
15use crate::{
16 db::{
17 Db, EntitySchemaDescription, FluentDeleteQuery, FluentLoadQuery, IntegrityReport,
18 MigrationPlan, MigrationRunOutcome, MissingRowPolicy, PlanError, Query, QueryError,
19 StorageReport, StoreRegistry, WriteBatchResponse,
20 commit::EntityRuntimeHooks,
21 cursor::decode_optional_cursor_token,
22 executor::{DeleteExecutor, ExecutorPlanError, LoadExecutor, SaveExecutor},
23 schema::{describe_entity_model, show_indexes_for_model},
24 },
25 error::InternalError,
26 metrics::sink::{MetricsSink, with_metrics_sink},
27 traits::{CanisterKind, EntityKind, EntityValue},
28 value::Value,
29};
30use std::thread::LocalKey;
31
32pub use sql::SqlStatementRoute;
33
34fn map_executor_plan_error(err: ExecutorPlanError) -> QueryError {
36 match err {
37 ExecutorPlanError::Cursor(err) => QueryError::from(PlanError::from(*err)),
38 }
39}
40
41fn decode_optional_cursor_bytes(cursor_token: Option<&str>) -> Result<Option<Vec<u8>>, QueryError> {
44 decode_optional_cursor_token(cursor_token).map_err(|err| QueryError::from(PlanError::from(err)))
45}
46
47pub struct DbSession<C: CanisterKind> {
54 db: Db<C>,
55 debug: bool,
56 metrics: Option<&'static dyn MetricsSink>,
57}
58
59impl<C: CanisterKind> DbSession<C> {
60 #[must_use]
62 pub(crate) const fn new(db: Db<C>) -> Self {
63 Self {
64 db,
65 debug: false,
66 metrics: None,
67 }
68 }
69
70 #[must_use]
72 pub const fn new_with_hooks(
73 store: &'static LocalKey<StoreRegistry>,
74 entity_runtime_hooks: &'static [EntityRuntimeHooks<C>],
75 ) -> Self {
76 Self::new(Db::new_with_hooks(store, entity_runtime_hooks))
77 }
78
79 #[must_use]
81 pub const fn debug(mut self) -> Self {
82 self.debug = true;
83 self
84 }
85
86 #[must_use]
88 pub const fn metrics_sink(mut self, sink: &'static dyn MetricsSink) -> Self {
89 self.metrics = Some(sink);
90 self
91 }
92
93 fn with_metrics<T>(&self, f: impl FnOnce() -> T) -> T {
94 if let Some(sink) = self.metrics {
95 with_metrics_sink(sink, f)
96 } else {
97 f()
98 }
99 }
100
101 fn execute_save_with<E, T, R>(
103 &self,
104 op: impl FnOnce(SaveExecutor<E>) -> Result<T, InternalError>,
105 map: impl FnOnce(T) -> R,
106 ) -> Result<R, InternalError>
107 where
108 E: EntityKind<Canister = C> + EntityValue,
109 {
110 let value = self.with_metrics(|| op(self.save_executor::<E>()))?;
111
112 Ok(map(value))
113 }
114
115 fn execute_save_entity<E>(
117 &self,
118 op: impl FnOnce(SaveExecutor<E>) -> Result<E, InternalError>,
119 ) -> Result<E, InternalError>
120 where
121 E: EntityKind<Canister = C> + EntityValue,
122 {
123 self.execute_save_with(op, std::convert::identity)
124 }
125
126 fn execute_save_batch<E>(
127 &self,
128 op: impl FnOnce(SaveExecutor<E>) -> Result<Vec<E>, InternalError>,
129 ) -> Result<WriteBatchResponse<E>, InternalError>
130 where
131 E: EntityKind<Canister = C> + EntityValue,
132 {
133 self.execute_save_with(op, WriteBatchResponse::new)
134 }
135
136 fn execute_save_view<E>(
137 &self,
138 op: impl FnOnce(SaveExecutor<E>) -> Result<E::ViewType, InternalError>,
139 ) -> Result<E::ViewType, InternalError>
140 where
141 E: EntityKind<Canister = C> + EntityValue,
142 {
143 self.execute_save_with(op, std::convert::identity)
144 }
145
146 #[must_use]
152 pub const fn load<E>(&self) -> FluentLoadQuery<'_, E>
153 where
154 E: EntityKind<Canister = C>,
155 {
156 FluentLoadQuery::new(self, Query::new(MissingRowPolicy::Ignore))
157 }
158
159 #[must_use]
161 pub const fn load_with_consistency<E>(
162 &self,
163 consistency: MissingRowPolicy,
164 ) -> FluentLoadQuery<'_, E>
165 where
166 E: EntityKind<Canister = C>,
167 {
168 FluentLoadQuery::new(self, Query::new(consistency))
169 }
170
171 #[must_use]
173 pub fn delete<E>(&self) -> FluentDeleteQuery<'_, E>
174 where
175 E: EntityKind<Canister = C>,
176 {
177 FluentDeleteQuery::new(self, Query::new(MissingRowPolicy::Ignore).delete())
178 }
179
180 #[must_use]
182 pub fn delete_with_consistency<E>(
183 &self,
184 consistency: MissingRowPolicy,
185 ) -> FluentDeleteQuery<'_, E>
186 where
187 E: EntityKind<Canister = C>,
188 {
189 FluentDeleteQuery::new(self, Query::new(consistency).delete())
190 }
191
192 #[must_use]
196 pub const fn select_one(&self) -> Value {
197 Value::Int(1)
198 }
199
200 #[must_use]
207 pub fn show_indexes<E>(&self) -> Vec<String>
208 where
209 E: EntityKind<Canister = C>,
210 {
211 show_indexes_for_model(E::MODEL)
212 }
213
214 #[must_use]
219 pub fn describe_entity<E>(&self) -> EntitySchemaDescription
220 where
221 E: EntityKind<Canister = C>,
222 {
223 describe_entity_model(E::MODEL)
224 }
225
226 pub fn storage_report(
228 &self,
229 name_to_path: &[(&'static str, &'static str)],
230 ) -> Result<StorageReport, InternalError> {
231 self.db.storage_report(name_to_path)
232 }
233
234 pub fn integrity_report(&self) -> Result<IntegrityReport, InternalError> {
236 self.db.integrity_report()
237 }
238
239 pub fn execute_migration_plan(
244 &self,
245 plan: &MigrationPlan,
246 max_steps: usize,
247 ) -> Result<MigrationRunOutcome, InternalError> {
248 self.with_metrics(|| self.db.execute_migration_plan(plan, max_steps))
249 }
250
251 #[must_use]
256 pub(in crate::db) const fn load_executor<E>(&self) -> LoadExecutor<E>
257 where
258 E: EntityKind<Canister = C> + EntityValue,
259 {
260 LoadExecutor::new(self.db, self.debug)
261 }
262
263 #[must_use]
264 pub(in crate::db) const fn delete_executor<E>(&self) -> DeleteExecutor<E>
265 where
266 E: EntityKind<Canister = C> + EntityValue,
267 {
268 DeleteExecutor::new(self.db, self.debug)
269 }
270
271 #[must_use]
272 pub(in crate::db) const fn save_executor<E>(&self) -> SaveExecutor<E>
273 where
274 E: EntityKind<Canister = C> + EntityValue,
275 {
276 SaveExecutor::new(self.db, self.debug)
277 }
278}