1pub mod default_impl;
6mod readwrite_impl;
7mod resource;
8mod types_impl;
9
10mod generated {
11 #![allow(missing_docs)]
12
13 pub use anyhow::Error;
14
15 pub use super::{ConnectionProxy, Statement};
16
17 wasmtime::component::bindgen!({
18 world: "imports",
19 path: "wit",
20 imports: {
21 default: store | tracing | trappable,
22 },
23 with: {
24 "wasi:sql/types.connection": ConnectionProxy,
25 "wasi:sql/types.statement": Statement,
26 "wasi:sql/types.error": Error,
27 },
28 trappable_error_type: {
29 "wasi:sql/types.error" => Error,
30 },
31 });
32}
33
34use std::fmt::Debug;
35use std::sync::Arc;
36
37pub use omnia::FutureResult;
38use omnia::{Host, Server, State};
39use wasmtime::component::{HasData, Linker};
40use wasmtime_wasi::ResourceTable;
41
42use self::generated::wasi::sql::{readwrite, types};
43pub use crate::host::default_impl::SqlDefault;
44pub use crate::host::generated::wasi::sql::types::{DataType, Field, Row};
45pub use crate::host::resource::*;
46
47#[derive(Debug)]
49pub struct WasiSql;
50
51impl HasData for WasiSql {
52 type Data<'a> = WasiSqlCtxView<'a>;
53}
54
55impl<T> Host<T> for WasiSql
56where
57 T: WasiSqlView + 'static,
58{
59 fn add_to_linker(linker: &mut Linker<T>) -> anyhow::Result<()> {
60 readwrite::add_to_linker::<_, Self>(linker, T::sql)?;
61 Ok(types::add_to_linker::<_, Self>(linker, T::sql)?)
62 }
63}
64
65impl<S> Server<S> for WasiSql where S: State {}
66
67pub trait WasiSqlView: Send {
72 fn sql(&mut self) -> WasiSqlCtxView<'_>;
74}
75
76pub struct WasiSqlCtxView<'a> {
78 pub ctx: &'a mut dyn WasiSqlCtx,
80
81 pub table: &'a mut ResourceTable,
83}
84
85pub trait WasiSqlCtx: Debug + Send + Sync + 'static {
90 fn open(&self, name: String) -> FutureResult<Arc<dyn Connection>>;
92}
93
94#[macro_export]
96macro_rules! omnia_wasi_view {
97 ($store_ctx:ty, $field_name:ident) => {
98 impl omnia_wasi_sql::WasiSqlView for $store_ctx {
99 fn sql(&mut self) -> omnia_wasi_sql::WasiSqlCtxView<'_> {
100 omnia_wasi_sql::WasiSqlCtxView {
101 ctx: &mut self.$field_name,
102 table: &mut self.table,
103 }
104 }
105 }
106 };
107}