Skip to main content

omnia_wasi_sql/
host.rs

1//! # Host implementation for WASI SQL Service
2//!
3//! This module implements the host-side logic for the WASI SQL service.
4
5pub 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: "sql",
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/// Host-side service for `wasi:sql`.
48#[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
67/// A trait which provides internal WASI SQL state.
68///
69/// This is implemented by the `T` in `Linker<T>` — a single type shared across
70/// all WASI components for the runtime build.
71pub trait WasiSqlView: Send {
72    /// Return a [`WasiSqlCtxView`] from mutable reference to self.
73    fn sql(&mut self) -> WasiSqlCtxView<'_>;
74}
75
76/// View into [`WasiSqlCtx`] implementation and [`ResourceTable`].
77pub struct WasiSqlCtxView<'a> {
78    /// Mutable reference to the WASI SQL context.
79    pub ctx: &'a mut dyn WasiSqlCtx,
80
81    /// Mutable reference to table used to manage resources.
82    pub table: &'a mut ResourceTable,
83}
84
85/// A trait which provides internal WASI SQL context.
86///
87/// This is implemented by the resource-specific provider of SQL
88/// functionality. For example, `PostgreSQL`, `MySQL`, `SQLite`, etc.
89pub trait WasiSqlCtx: Debug + Send + Sync + 'static {
90    /// Open a connection to the database.
91    fn open(&self, name: String) -> FutureResult<Arc<dyn Connection>>;
92}
93
94/// Implementation of the `WasiSqlView` trait for the store context.
95#[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}