From 7a067387e8b82bef127c412905cac26c2009cb8d Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Tue, 6 Aug 2019 23:20:50 -0700 Subject: [PATCH] Make postgres and mariadb optional features of SQLx --- Cargo.toml | 5 ++ examples/todo/Cargo.toml | 2 +- src/lib.rs | 12 +++- src/postgres/mod.rs | 3 + src/row.rs | 142 ++++++++++++++++++++------------------- src/types.rs | 2 - 6 files changed, 90 insertions(+), 76 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 18db9ddd..ad5dbee3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,6 +12,11 @@ license = "MIT OR Apache-2.0" description = "Asynchronous and expressive database client in pure Rust." edition = "2018" +[features] +default = [] +postgres = [] +mariadb = [] + [dependencies] bitflags = "1.1.0" byteorder = "1.3.2" diff --git a/examples/todo/Cargo.toml b/examples/todo/Cargo.toml index 601398cb..422cbde6 100644 --- a/examples/todo/Cargo.toml +++ b/examples/todo/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2018" [dependencies] -sqlx = { path = "../.." } +sqlx = { path = "../..", features = [ "postgres" ] } failure = "0.1.5" env_logger = "0.6.2" runtime = { version = "=0.3.0-alpha.6", default-features = false } diff --git a/src/lib.rs b/src/lib.rs index 0c754f68..a541aea9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,15 +15,21 @@ extern crate enum_tryfrom_derive; mod options; pub use self::options::ConnectOptions; -pub mod mariadb; -pub mod postgres; - // Helper macro for writing long complex tests #[macro_use] pub mod macros; pub mod backend; pub mod deserialize; + +#[macro_use] pub mod row; + pub mod serialize; pub mod types; + +#[cfg(feature = "mariadb")] +pub mod mariadb; + +#[cfg(feature = "postgres")] +pub mod postgres; diff --git a/src/postgres/mod.rs b/src/postgres/mod.rs index 92110494..06fa9a98 100644 --- a/src/postgres/mod.rs +++ b/src/postgres/mod.rs @@ -14,3 +14,6 @@ impl Backend for Postgres { type RawRow = protocol::DataRow; type TypeMetadata = types::TypeMetadata; } + +// Generates tuple FromRow impls for this backend +impl_from_row_tuples_for_backend!(Postgres); diff --git a/src/row.rs b/src/row.rs index 1c81549c..a6920d1a 100644 --- a/src/row.rs +++ b/src/row.rs @@ -1,4 +1,4 @@ -use crate::{backend::Backend, deserialize::FromSql, postgres::Postgres, types::SqlType}; +use crate::{backend::Backend, deserialize::FromSql, types::SqlType}; pub trait RawRow { fn is_empty(&self) -> bool; @@ -45,91 +45,93 @@ where } } -// TODO: Think of a better way to generate these tuple implementations - macro_rules! impl_from_row_tuple { ($B:ident: $( ($idx:tt) -> $T:ident, $ST:ident );+;) => { - impl<$($ST,)+ $($T,)+> FromRow for ($($T,)+) + impl<$($ST,)+ $($T,)+> crate::row::FromRow<$B, ($($ST,)+)> for ($($T,)+) where - $($ST: SqlType,)+ - $($T: FromSql,)+ + $($ST: crate::types::SqlType<$B>,)+ + $($T: crate::deserialize::FromSql<$B, $ST>,)+ { #[inline] - fn from_row(row: Row<$B>) -> Self { + fn from_row(row: crate::row::Row<$B>) -> Self { ($(row.get::<$ST, $T>($idx),)+) } } }; } -impl_from_row_tuple!(Postgres: - (0) -> ST1, T1; -); +macro_rules! impl_from_row_tuples_for_backend { + ($B:ident) => { + impl_from_row_tuple!($B: + (0) -> ST1, T1; + ); -impl_from_row_tuple!(Postgres: - (0) -> ST1, T1; - (1) -> ST2, T2; -); + impl_from_row_tuple!($B: + (0) -> ST1, T1; + (1) -> ST2, T2; + ); -impl_from_row_tuple!(Postgres: - (0) -> ST1, T1; - (1) -> ST2, T2; - (2) -> ST3, T3; -); + impl_from_row_tuple!($B: + (0) -> ST1, T1; + (1) -> ST2, T2; + (2) -> ST3, T3; + ); -impl_from_row_tuple!(Postgres: - (0) -> ST1, T1; - (1) -> ST2, T2; - (2) -> ST3, T3; - (3) -> ST4, T4; -); + impl_from_row_tuple!($B: + (0) -> ST1, T1; + (1) -> ST2, T2; + (2) -> ST3, T3; + (3) -> ST4, T4; + ); -impl_from_row_tuple!(Postgres: - (0) -> ST1, T1; - (1) -> ST2, T2; - (2) -> ST3, T3; - (3) -> ST4, T4; - (4) -> ST5, T5; -); + impl_from_row_tuple!($B: + (0) -> ST1, T1; + (1) -> ST2, T2; + (2) -> ST3, T3; + (3) -> ST4, T4; + (4) -> ST5, T5; + ); -impl_from_row_tuple!(Postgres: - (0) -> ST1, T1; - (1) -> ST2, T2; - (2) -> ST3, T3; - (3) -> ST4, T4; - (4) -> ST5, T5; - (5) -> ST6, T6; -); + impl_from_row_tuple!($B: + (0) -> ST1, T1; + (1) -> ST2, T2; + (2) -> ST3, T3; + (3) -> ST4, T4; + (4) -> ST5, T5; + (5) -> ST6, T6; + ); -impl_from_row_tuple!(Postgres: - (0) -> ST1, T1; - (1) -> ST2, T2; - (2) -> ST3, T3; - (3) -> ST4, T4; - (4) -> ST5, T5; - (5) -> ST6, T6; - (6) -> ST7, T7; -); + impl_from_row_tuple!($B: + (0) -> ST1, T1; + (1) -> ST2, T2; + (2) -> ST3, T3; + (3) -> ST4, T4; + (4) -> ST5, T5; + (5) -> ST6, T6; + (6) -> ST7, T7; + ); -impl_from_row_tuple!(Postgres: - (0) -> ST1, T1; - (1) -> ST2, T2; - (2) -> ST3, T3; - (3) -> ST4, T4; - (4) -> ST5, T5; - (5) -> ST6, T6; - (6) -> ST7, T7; - (7) -> ST8, T8; -); + impl_from_row_tuple!($B: + (0) -> ST1, T1; + (1) -> ST2, T2; + (2) -> ST3, T3; + (3) -> ST4, T4; + (4) -> ST5, T5; + (5) -> ST6, T6; + (6) -> ST7, T7; + (7) -> ST8, T8; + ); -impl_from_row_tuple!(Postgres: - (0) -> ST1, T1; - (1) -> ST2, T2; - (2) -> ST3, T3; - (3) -> ST4, T4; - (4) -> ST5, T5; - (5) -> ST6, T6; - (6) -> ST7, T7; - (7) -> ST8, T8; - (8) -> ST9, T9; -); + impl_from_row_tuple!($B: + (0) -> ST1, T1; + (1) -> ST2, T2; + (2) -> ST3, T3; + (3) -> ST4, T4; + (4) -> ST5, T5; + (5) -> ST6, T6; + (6) -> ST7, T7; + (7) -> ST8, T8; + (8) -> ST9, T9; + ); + } +} diff --git a/src/types.rs b/src/types.rs index 7b002cbe..6c7b39de 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,7 +1,5 @@ use crate::backend::Backend; -pub use crate::postgres::types::*; - // TODO: Does [AsSql] need to be generic over back-end ? pub trait SqlType