1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
mod connection;
#[cfg(feature = "driver-adapters")]
mod js;
mod transaction;

#[cfg(not(target_arch = "wasm32"))]
pub(crate) mod native {
    pub(crate) mod mssql;
    pub(crate) mod mysql;
    pub(crate) mod postgresql;
    pub(crate) mod sqlite;
}

pub(crate) mod operations;

use async_trait::async_trait;
use connector_interface::{error::ConnectorError, Connector};

#[cfg(feature = "driver-adapters")]
pub use js::*;

#[cfg(not(target_arch = "wasm32"))]
pub use native::{mssql::*, mysql::*, postgresql::*, sqlite::*};

#[async_trait]
pub trait FromSource {
    /// Instantiate a query connector from a Datasource.
    ///
    /// The resolved url is passed distinctly from the datasource for two
    /// reasons:
    ///
    /// 1. Extracting the final url from the datasource involves resolving env
    ///    vars and validating, which can fail with a schema parser error. We
    ///    want to handle this as early as possible and in a single place.
    ///
    /// 2. The url may be modified with the config dir, in the case of Node-API.
    async fn from_source(
        source: &psl::Datasource,
        url: &str,
        features: psl::PreviewFeatures,
    ) -> connector_interface::Result<Self>
    where
        Self: Connector + Sized;
}

#[inline]
async fn catch<O>(
    connection_info: &quaint::prelude::ConnectionInfo,
    fut: impl std::future::Future<Output = Result<O, crate::SqlError>>,
) -> Result<O, ConnectorError> {
    fut.await.map_err(|err| err.into_connector_error(connection_info))
}