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."
edition = "2018"
[features]
default = []
postgres = []
mariadb = []
[dependencies]
bitflags = "1.1.0"
byteorder = "1.3.2"

View file

@ -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 }

View file

@ -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;

View file

@ -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);

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 {
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<Postgres, ($($ST,)+)> for ($($T,)+)
impl<$($ST,)+ $($T,)+> crate::row::FromRow<$B, ($($ST,)+)> for ($($T,)+)
where
$($ST: SqlType<Postgres>,)+
$($T: FromSql<Postgres, $ST>,)+
$($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;
);
}
}

View file

@ -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<B>