sqlx_macros_core/
lib.rs

1//! Support crate for SQLx's proc macros.
2//!
3//! ### Note: Semver Exempt API
4//! The API of this crate is not meant for general use and does *not* follow Semantic Versioning.
5//! The only crate that follows Semantic Versioning in the project is the `sqlx` crate itself.
6//! If you are building a custom SQLx driver, you should pin an exact version of this and
7//! `sqlx-core` to avoid breakages:
8//!
9//! ```toml
10//! sqlx-core = "=0.6.2"
11//! sqlx-macros-core = "=0.6.2"
12//! ```
13//!
14//! And then make releases in lockstep with `sqlx-core`. We recommend all driver crates, in-tree
15//! or otherwise, use the same version numbers as `sqlx-core` to avoid confusion.
16
17#![cfg_attr(
18    any(sqlx_macros_unstable, procmacro2_semver_exempt),
19    feature(track_path)
20)]
21
22#[cfg(feature = "macros")]
23use crate::query::QueryDriver;
24
25pub type Error = Box<dyn std::error::Error>;
26
27pub type Result<T> = std::result::Result<T, Error>;
28
29mod common;
30mod database;
31
32#[cfg(feature = "derive")]
33pub mod derives;
34#[cfg(feature = "macros")]
35pub mod query;
36
37#[cfg(feature = "macros")]
38// The compiler gives misleading help messages about `#[cfg(test)]` when this is just named `test`.
39pub mod test_attr;
40
41#[cfg(feature = "migrate")]
42pub mod migrate;
43
44#[cfg(feature = "macros")]
45pub const FOSS_DRIVERS: &[QueryDriver] = &[
46    #[cfg(feature = "mysql")]
47    QueryDriver::new::<sqlx_mysql::MySql>(),
48    #[cfg(feature = "postgres")]
49    QueryDriver::new::<sqlx_postgres::Postgres>(),
50    #[cfg(feature = "_sqlite")]
51    QueryDriver::new::<sqlx_sqlite::Sqlite>(),
52];
53
54pub fn block_on<F>(f: F) -> F::Output
55where
56    F: std::future::Future,
57{
58    #[cfg(feature = "_rt-tokio")]
59    {
60        use once_cell::sync::Lazy;
61        use tokio::runtime::{self, Runtime};
62
63        // We need a single, persistent Tokio runtime since we're caching connections,
64        // otherwise we'll get "IO driver has terminated" errors.
65        static TOKIO_RT: Lazy<Runtime> = Lazy::new(|| {
66            runtime::Builder::new_current_thread()
67                .enable_all()
68                .build()
69                .expect("failed to start Tokio runtime")
70        });
71
72        TOKIO_RT.block_on(f)
73    }
74
75    #[cfg(all(feature = "_rt-async-std", not(feature = "tokio")))]
76    {
77        async_std::task::block_on(f)
78    }
79
80    #[cfg(not(any(feature = "_rt-async-std", feature = "tokio")))]
81    sqlx_core::rt::missing_rt(f)
82}