Make postgres and mariadb optional features of SQLx

This commit is contained in:
Ryan Leckey 2019-08-06 23:20:50 -07:00
parent bb17e3b284
commit 7a067387e8
6 changed files with 90 additions and 76 deletions

View File

@ -12,6 +12,11 @@ license = "MIT OR Apache-2.0"
description = "Asynchronous and expressive database client in pure Rust." description = "Asynchronous and expressive database client in pure Rust."
edition = "2018" edition = "2018"
[features]
default = []
postgres = []
mariadb = []
[dependencies] [dependencies]
bitflags = "1.1.0" bitflags = "1.1.0"
byteorder = "1.3.2" byteorder = "1.3.2"

View File

@ -4,7 +4,7 @@ version = "0.1.0"
edition = "2018" edition = "2018"
[dependencies] [dependencies]
sqlx = { path = "../.." } sqlx = { path = "../..", features = [ "postgres" ] }
failure = "0.1.5" failure = "0.1.5"
env_logger = "0.6.2" env_logger = "0.6.2"
runtime = { version = "=0.3.0-alpha.6", default-features = false } runtime = { version = "=0.3.0-alpha.6", default-features = false }

View File

@ -15,15 +15,21 @@ extern crate enum_tryfrom_derive;
mod options; mod options;
pub use self::options::ConnectOptions; pub use self::options::ConnectOptions;
pub mod mariadb;
pub mod postgres;
// Helper macro for writing long complex tests // Helper macro for writing long complex tests
#[macro_use] #[macro_use]
pub mod macros; pub mod macros;
pub mod backend; pub mod backend;
pub mod deserialize; pub mod deserialize;
#[macro_use]
pub mod row; pub mod row;
pub mod serialize; pub mod serialize;
pub mod types; pub mod types;
#[cfg(feature = "mariadb")]
pub mod mariadb;
#[cfg(feature = "postgres")]
pub mod postgres;

View File

@ -14,3 +14,6 @@ impl Backend for Postgres {
type RawRow = protocol::DataRow; type RawRow = protocol::DataRow;
type TypeMetadata = types::TypeMetadata; type TypeMetadata = types::TypeMetadata;
} }
// Generates tuple FromRow impls for this backend
impl_from_row_tuples_for_backend!(Postgres);

View File

@ -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 { pub trait RawRow {
fn is_empty(&self) -> bool; 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 { macro_rules! impl_from_row_tuple {
($B:ident: $( ($idx:tt) -> $T:ident, $ST:ident );+;) => { ($B:ident: $( ($idx:tt) -> $T:ident, $ST:ident );+;) => {
impl<$($ST,)+ $($T,)+> FromRow<Postgres, ($($ST,)+)> for ($($T,)+) impl<$($ST,)+ $($T,)+> crate::row::FromRow<$B, ($($ST,)+)> for ($($T,)+)
where where
$($ST: SqlType<Postgres>,)+ $($ST: crate::types::SqlType<$B>,)+
$($T: FromSql<Postgres, $ST>,)+ $($T: crate::deserialize::FromSql<$B, $ST>,)+
{ {
#[inline] #[inline]
fn from_row(row: Row<$B>) -> Self { fn from_row(row: crate::row::Row<$B>) -> Self {
($(row.get::<$ST, $T>($idx),)+) ($(row.get::<$ST, $T>($idx),)+)
} }
} }
}; };
} }
impl_from_row_tuple!(Postgres: macro_rules! impl_from_row_tuples_for_backend {
(0) -> ST1, T1; ($B:ident) => {
); impl_from_row_tuple!($B:
(0) -> ST1, T1;
);
impl_from_row_tuple!(Postgres: impl_from_row_tuple!($B:
(0) -> ST1, T1; (0) -> ST1, T1;
(1) -> ST2, T2; (1) -> ST2, T2;
); );
impl_from_row_tuple!(Postgres: impl_from_row_tuple!($B:
(0) -> ST1, T1; (0) -> ST1, T1;
(1) -> ST2, T2; (1) -> ST2, T2;
(2) -> ST3, T3; (2) -> ST3, T3;
); );
impl_from_row_tuple!(Postgres: impl_from_row_tuple!($B:
(0) -> ST1, T1; (0) -> ST1, T1;
(1) -> ST2, T2; (1) -> ST2, T2;
(2) -> ST3, T3; (2) -> ST3, T3;
(3) -> ST4, T4; (3) -> ST4, T4;
); );
impl_from_row_tuple!(Postgres: impl_from_row_tuple!($B:
(0) -> ST1, T1; (0) -> ST1, T1;
(1) -> ST2, T2; (1) -> ST2, T2;
(2) -> ST3, T3; (2) -> ST3, T3;
(3) -> ST4, T4; (3) -> ST4, T4;
(4) -> ST5, T5; (4) -> ST5, T5;
); );
impl_from_row_tuple!(Postgres: impl_from_row_tuple!($B:
(0) -> ST1, T1; (0) -> ST1, T1;
(1) -> ST2, T2; (1) -> ST2, T2;
(2) -> ST3, T3; (2) -> ST3, T3;
(3) -> ST4, T4; (3) -> ST4, T4;
(4) -> ST5, T5; (4) -> ST5, T5;
(5) -> ST6, T6; (5) -> ST6, T6;
); );
impl_from_row_tuple!(Postgres: impl_from_row_tuple!($B:
(0) -> ST1, T1; (0) -> ST1, T1;
(1) -> ST2, T2; (1) -> ST2, T2;
(2) -> ST3, T3; (2) -> ST3, T3;
(3) -> ST4, T4; (3) -> ST4, T4;
(4) -> ST5, T5; (4) -> ST5, T5;
(5) -> ST6, T6; (5) -> ST6, T6;
(6) -> ST7, T7; (6) -> ST7, T7;
); );
impl_from_row_tuple!(Postgres: impl_from_row_tuple!($B:
(0) -> ST1, T1; (0) -> ST1, T1;
(1) -> ST2, T2; (1) -> ST2, T2;
(2) -> ST3, T3; (2) -> ST3, T3;
(3) -> ST4, T4; (3) -> ST4, T4;
(4) -> ST5, T5; (4) -> ST5, T5;
(5) -> ST6, T6; (5) -> ST6, T6;
(6) -> ST7, T7; (6) -> ST7, T7;
(7) -> ST8, T8; (7) -> ST8, T8;
); );
impl_from_row_tuple!(Postgres: impl_from_row_tuple!($B:
(0) -> ST1, T1; (0) -> ST1, T1;
(1) -> ST2, T2; (1) -> ST2, T2;
(2) -> ST3, T3; (2) -> ST3, T3;
(3) -> ST4, T4; (3) -> ST4, T4;
(4) -> ST5, T5; (4) -> ST5, T5;
(5) -> ST6, T6; (5) -> ST6, T6;
(6) -> ST7, T7; (6) -> ST7, T7;
(7) -> ST8, T8; (7) -> ST8, T8;
(8) -> ST9, T9; (8) -> ST9, T9;
); );
}
}

View File

@ -1,7 +1,5 @@
use crate::backend::Backend; use crate::backend::Backend;
pub use crate::postgres::types::*;
// TODO: Does [AsSql] need to be generic over back-end ? // TODO: Does [AsSql] need to be generic over back-end ?
pub trait SqlType<B> pub trait SqlType<B>