#[cfg(feature = "libsql")]
mod libsql;
#[cfg(any(feature = "tokio-postgres", feature = "postgres-sync"))]
mod postgres;
#[cfg(feature = "rusqlite")]
mod rusqlite;
#[cfg(any(feature = "rusqlite", feature = "libsql", feature = "turso"))]
pub(crate) mod sqlite_value;
#[cfg(feature = "turso")]
mod turso;
use core::marker::PhantomData;
use crate::error::DrizzleError;
use crate::{Cons, Nil};
#[derive(Debug, Clone, Copy, Default)]
pub struct SelectStar;
#[derive(Debug, Clone, Copy, Default)]
pub struct SelectCols<Cols>(PhantomData<Cols>);
#[derive(Debug, Clone, Copy, Default)]
pub struct SelectExpr;
#[derive(Debug, Clone, Copy, Default)]
pub struct SelectAs<R>(PhantomData<R>);
#[derive(Debug, Clone, Copy, Default)]
pub struct Scoped<Marker, Scope>(PhantomData<(Marker, Scope)>);
pub trait SelectRequiredTables {
type RequiredTables;
}
#[derive(Debug, Clone, Copy, Default)]
pub struct ScopeHere;
#[derive(Debug, Clone, Copy, Default)]
pub struct ScopeThere<Prev>(PhantomData<Prev>);
pub trait ScopeContains<Table, Witness> {}
impl<Head, Tail> ScopeContains<Head, ScopeHere> for Cons<Head, Tail> {}
impl<Head, Tail, Table, Witness> ScopeContains<Table, ScopeThere<Witness>> for Cons<Head, Tail> where
Tail: ScopeContains<Table, Witness>
{
}
pub trait ScopeSatisfies<Required, Proof> {}
impl<Scope> ScopeSatisfies<Nil, ()> for Scope {}
impl<Scope, Head, Tail, HeadProof, TailProof>
ScopeSatisfies<Cons<Head, Tail>, (HeadProof, TailProof)> for Scope
where
Scope: ScopeContains<Head, HeadProof> + ScopeSatisfies<Tail, TailProof>,
{
}
pub trait MarkerRequiredTables {
type RequiredTables;
}
impl MarkerRequiredTables for SelectStar {
type RequiredTables = Nil;
}
impl<Cols> MarkerRequiredTables for SelectCols<Cols> {
type RequiredTables = Nil;
}
impl MarkerRequiredTables for SelectExpr {
type RequiredTables = Nil;
}
impl<R> MarkerRequiredTables for SelectAs<R>
where
R: SelectRequiredTables,
{
type RequiredTables = R::RequiredTables;
}
#[diagnostic::on_unimplemented(
message = "selected row requires tables not present in the current query scope",
label = "add .join(...) entries for every table referenced by this selector",
note = "for aliased selectors, use the same alias type in #[from(...)] and .from(...)"
)]
pub trait MarkerScopeValidFor<Proof> {}
impl<M, Scope, Proof> MarkerScopeValidFor<Proof> for Scoped<M, Scope>
where
M: MarkerRequiredTables,
Scope: ScopeSatisfies<<M as MarkerRequiredTables>::RequiredTables, Proof>,
{
}
pub trait AggStatus {
type Status;
}
impl<E: crate::expr::HasAggStatus> AggStatus for (E,) {
type Status = E::Status;
}
macro_rules! impl_tuple_agg_status {
($E0:ident; $i0:tt) => {};
($E0:ident, $E1:ident; $i0:tt, $i1:tt) => {
impl<$E0, $E1> AggStatus for ($E0, $E1)
where
$E0: crate::expr::HasAggStatus,
$E1: crate::expr::HasAggStatus,
<$E0 as crate::expr::HasAggStatus>::Status:
crate::expr::CombineAggStatus<<$E1 as crate::expr::HasAggStatus>::Status>,
{
type Status = <<$E0 as crate::expr::HasAggStatus>::Status as
crate::expr::CombineAggStatus<<$E1 as crate::expr::HasAggStatus>::Status>>::Output;
}
};
($E0:ident, $E1:ident, $($rest:ident),+; $i0:tt, $i1:tt, $($ri:tt),+) => {
impl<$E0, $E1, $($rest),+> AggStatus for ($E0, $E1, $($rest),+)
where
$E0: crate::expr::HasAggStatus,
($E1, $($rest),+): AggStatus,
<$E0 as crate::expr::HasAggStatus>::Status:
crate::expr::CombineAggStatus<<($E1, $($rest),+) as AggStatus>::Status>,
{
type Status = <<$E0 as crate::expr::HasAggStatus>::Status as
crate::expr::CombineAggStatus<<($E1, $($rest),+) as AggStatus>::Status>>::Output;
}
};
}
with_col_sizes_8!(impl_tuple_agg_status);
#[cfg(any(
feature = "col16",
feature = "col32",
feature = "col64",
feature = "col128",
feature = "col200"
))]
with_col_sizes_16!(impl_tuple_agg_status);
#[cfg(any(
feature = "col32",
feature = "col64",
feature = "col128",
feature = "col200"
))]
with_col_sizes_32!(impl_tuple_agg_status);
#[cfg(any(feature = "col64", feature = "col128", feature = "col200"))]
with_col_sizes_64!(impl_tuple_agg_status);
#[cfg(any(feature = "col128", feature = "col200"))]
with_col_sizes_128!(impl_tuple_agg_status);
#[cfg(feature = "col200")]
with_col_sizes_200!(impl_tuple_agg_status);
pub trait IntoGroupBy<'a, V: crate::SQLParam + 'a>: crate::ToSQL<'a, V> {
type Columns;
}
macro_rules! impl_into_group_by_tuple {
($T0:ident; $i0:tt) => {};
($T0:ident, $T1:ident; $i0:tt, $i1:tt) => {
impl<'a, V: crate::SQLParam + 'a, $T0, $T1> IntoGroupBy<'a, V> for ($T0, $T1)
where
$T0: crate::ToSQL<'a, V>,
$T1: crate::ToSQL<'a, V>,
{
type Columns = Cons<$T0, Cons<$T1, Nil>>;
}
};
($T0:ident, $T1:ident, $($rest:ident),+; $i0:tt, $i1:tt, $($ri:tt),+) => {
impl<'a, V: crate::SQLParam + 'a, $T0, $T1, $($rest),+> IntoGroupBy<'a, V> for ($T0, $T1, $($rest),+)
where
$T0: crate::ToSQL<'a, V>,
$T1: crate::ToSQL<'a, V>,
$($rest: crate::ToSQL<'a, V>,)+
{
type Columns = impl_into_group_by_tuple!(@cons $T0, $T1, $($rest),+);
}
};
(@cons $T:ident) => { Cons<$T, Nil> };
(@cons $T:ident, $($rest:ident),+) => { Cons<$T, impl_into_group_by_tuple!(@cons $($rest),+)> };
}
with_col_sizes_8!(impl_into_group_by_tuple);
#[cfg(any(
feature = "col16",
feature = "col32",
feature = "col64",
feature = "col128",
feature = "col200"
))]
with_col_sizes_16!(impl_into_group_by_tuple);
#[diagnostic::on_unimplemented(
message = "non-aggregate column in SELECT is not in GROUP BY",
label = "this column must appear in .group_by(...) or be wrapped in an aggregate function",
note = "when using GROUP BY, every non-aggregate column in SELECT must be listed in GROUP BY"
)]
pub trait ScalarColumnsIn<Grouped, Proof> {}
pub struct AggSkip;
pub struct ScalarCheck<W>(core::marker::PhantomData<W>);
impl<E, Grouped, Proof> ScalarColumnsIn<Grouped, (Proof,)> for (E,) where
E: SingleColGroupCheck<Grouped, Proof>
{
}
pub trait SingleColGroupCheck<Grouped, Proof> {}
pub trait GroupByIdentity {
type Identity;
}
impl<E: GroupByIdentity> GroupByIdentity for crate::expr::AliasedExpr<E> {
type Identity = E::Identity;
}
impl<V: crate::SQLParam, T, N, A> GroupByIdentity for crate::expr::SQLExpr<'_, V, T, N, A>
where
T: crate::types::DataType,
N: crate::expr::Nullability,
A: crate::expr::AggregateKind,
{
type Identity = Self;
}
impl<Lhs, Rhs, Op> GroupByIdentity for crate::expr::ColumnBinOp<Lhs, Rhs, Op> {
type Identity = Self;
}
impl<T> GroupByIdentity for crate::expr::ColumnNeg<T> {
type Identity = Self;
}
impl<E, Grouped> SingleColGroupCheck<Grouped, AggSkip> for E where
E: crate::expr::HasAggStatus<Status = crate::expr::AllAgg>
{
}
impl<E, Grouped, W> SingleColGroupCheck<Grouped, ScalarCheck<W>> for E
where
E: crate::expr::HasAggStatus<Status = crate::expr::AllScalar> + GroupByIdentity,
Grouped: ScopeContains<E::Identity, W>,
{
}
impl<T0, T1, Grouped, P0, P1> ScalarColumnsIn<Grouped, (P0, P1)> for (T0, T1)
where
T0: SingleColGroupCheck<Grouped, P0>,
T1: SingleColGroupCheck<Grouped, P1>,
{
}
macro_rules! impl_scalar_columns_in {
($T0:ident; $i0:tt) => {};
($T0:ident, $T1:ident; $i0:tt, $i1:tt) => {};
($T0:ident, $($rest:ident),+; $i0:tt, $($ri:tt),+) => {
impl<$T0, $($rest),+, Grouped, HeadProof, TailProof>
ScalarColumnsIn<Grouped, (HeadProof, TailProof)>
for ($T0, $($rest),+)
where
$T0: SingleColGroupCheck<Grouped, HeadProof>,
($($rest,)+): ScalarColumnsIn<Grouped, TailProof>,
{}
};
}
with_col_sizes_8!(impl_scalar_columns_in);
#[cfg(any(
feature = "col16",
feature = "col32",
feature = "col64",
feature = "col128",
feature = "col200"
))]
with_col_sizes_16!(impl_scalar_columns_in);
#[diagnostic::on_unimplemented(
message = "non-aggregate column in SELECT is not in GROUP BY",
label = "add this column to .group_by(...) or wrap it in an aggregate function"
)]
pub trait MarkerAggValidFor<Grouped, Proof = ()> {}
impl<Mk> MarkerAggValidFor<()> for Mk {}
impl<Scope, Head, Tail> MarkerAggValidFor<Cons<Head, Tail>> for Scoped<SelectStar, Scope> {}
impl<Scope, Head, Tail> MarkerAggValidFor<Cons<Head, Tail>> for Scoped<SelectExpr, Scope> {}
impl<Scope, R, Head, Tail> MarkerAggValidFor<Cons<Head, Tail>> for Scoped<SelectAs<R>, Scope> {}
impl<Scope, Cols, Head, Tail, Proof> MarkerAggValidFor<Cons<Head, Tail>, Proof>
for Scoped<SelectCols<Cols>, Scope>
where
Cols: ScalarColumnsIn<Cons<Head, Tail>, Proof>,
{
}
pub trait RowColumnList<Row: ?Sized> {
type Columns: crate::TypeSet;
}
pub trait SelectedColumnList {
type Columns: crate::TypeSet;
}
trait SameType<T> {}
impl<T> SameType<T> for T {}
trait ColumnTypeCompatible<Row: ?Sized, Expected, Actual> {}
impl<Row: ?Sized, T> ColumnTypeCompatible<Row, T, T> for () {}
trait TypeListCompatible<Row: ?Sized, ActualList> {}
impl<Row: ?Sized> TypeListCompatible<Row, Self> for crate::Nil {}
impl<Row: ?Sized, EH, ET, AH, AT> TypeListCompatible<Row, crate::Cons<AH, AT>>
for crate::Cons<EH, ET>
where
(): ColumnTypeCompatible<Row, EH, AH>,
ET: TypeListCompatible<Row, AT>,
{
}
trait SqliteDecodeRow {}
#[cfg(feature = "rusqlite")]
impl SqliteDecodeRow for ::rusqlite::Row<'_> {}
#[cfg(feature = "libsql")]
impl SqliteDecodeRow for ::libsql::Row {}
#[cfg(feature = "turso")]
impl SqliteDecodeRow for ::turso::Row {}
macro_rules! impl_sqlite_integer_decode_compat {
($expected:ty => $($actual:ty),+ $(,)?) => {
$(
impl<Row> ColumnTypeCompatible<Row, $expected, $actual> for ()
where
Row: SqliteDecodeRow,
{
}
)+
};
}
impl_sqlite_integer_decode_compat!(
i64 => i8, i16, i32, isize, u8, u16, u32, u64, usize, bool
);
impl_sqlite_integer_decode_compat!(
Option<i64> =>
Option<i8>,
Option<i16>,
Option<i32>,
Option<isize>,
Option<u8>,
Option<u16>,
Option<u32>,
Option<u64>,
Option<usize>,
Option<bool>
);
macro_rules! impl_row_column_list_one {
($($ty:ty),+ $(,)?) => {
$(
impl<Row: ?Sized> RowColumnList<Row> for $ty {
type Columns = crate::Cons<$ty, crate::Nil>;
}
)+
};
}
impl_row_column_list_one!(
i8,
i16,
i32,
i64,
isize,
u8,
u16,
u32,
u64,
usize,
f32,
f64,
bool,
crate::prelude::String,
crate::prelude::Vec<u8>
);
impl<Row: ?Sized> RowColumnList<Row> for () {
type Columns = crate::Cons<(), crate::Nil>;
}
#[cfg(feature = "uuid")]
impl<Row: ?Sized> RowColumnList<Row> for uuid::Uuid {
type Columns = crate::Cons<Self, crate::Nil>;
}
#[cfg(feature = "chrono")]
impl<Row: ?Sized> RowColumnList<Row> for chrono::NaiveDate {
type Columns = crate::Cons<Self, crate::Nil>;
}
#[cfg(feature = "chrono")]
impl<Row: ?Sized> RowColumnList<Row> for chrono::NaiveTime {
type Columns = crate::Cons<Self, crate::Nil>;
}
#[cfg(feature = "chrono")]
impl<Row: ?Sized> RowColumnList<Row> for chrono::NaiveDateTime {
type Columns = crate::Cons<Self, crate::Nil>;
}
#[cfg(feature = "chrono")]
impl<Row: ?Sized> RowColumnList<Row> for chrono::DateTime<chrono::Utc> {
type Columns = crate::Cons<Self, crate::Nil>;
}
#[cfg(feature = "serde")]
impl<Row: ?Sized> RowColumnList<Row> for serde_json::Value {
type Columns = crate::Cons<Self, crate::Nil>;
}
#[cfg(feature = "rust-decimal")]
impl<Row: ?Sized> RowColumnList<Row> for rust_decimal::Decimal {
type Columns = crate::Cons<Self, crate::Nil>;
}
#[cfg(feature = "chrono")]
impl<Row: ?Sized> RowColumnList<Row> for chrono::Duration {
type Columns = crate::Cons<Self, crate::Nil>;
}
#[cfg(feature = "cidr")]
impl<Row: ?Sized> RowColumnList<Row> for cidr::IpInet {
type Columns = crate::Cons<Self, crate::Nil>;
}
#[cfg(feature = "cidr")]
impl<Row: ?Sized> RowColumnList<Row> for cidr::IpCidr {
type Columns = crate::Cons<Self, crate::Nil>;
}
#[cfg(feature = "geo-types")]
impl<Row: ?Sized> RowColumnList<Row> for geo_types::Point<f64> {
type Columns = crate::Cons<Self, crate::Nil>;
}
#[cfg(feature = "geo-types")]
impl<Row: ?Sized> RowColumnList<Row> for geo_types::LineString<f64> {
type Columns = crate::Cons<Self, crate::Nil>;
}
#[cfg(feature = "geo-types")]
impl<Row: ?Sized> RowColumnList<Row> for geo_types::Rect<f64> {
type Columns = crate::Cons<Self, crate::Nil>;
}
#[cfg(feature = "bit-vec")]
impl<Row: ?Sized> RowColumnList<Row> for bit_vec::BitVec {
type Columns = crate::Cons<Self, crate::Nil>;
}
#[cfg(feature = "arrayvec")]
impl<Row: ?Sized, const N: usize> RowColumnList<Row> for arrayvec::ArrayString<N> {
type Columns = crate::Cons<Self, crate::Nil>;
}
#[cfg(feature = "arrayvec")]
impl<Row: ?Sized, T, const N: usize> RowColumnList<Row> for arrayvec::ArrayVec<T, N> {
type Columns = crate::Cons<Self, crate::Nil>;
}
impl<Row: ?Sized> RowColumnList<Row> for compact_str::CompactString {
type Columns = crate::Cons<Self, crate::Nil>;
}
#[cfg(feature = "bytes")]
impl<Row: ?Sized> RowColumnList<Row> for bytes::Bytes {
type Columns = crate::Cons<Self, crate::Nil>;
}
#[cfg(feature = "bytes")]
impl<Row: ?Sized> RowColumnList<Row> for bytes::BytesMut {
type Columns = crate::Cons<Self, crate::Nil>;
}
impl<Row: ?Sized, A: smallvec::Array> RowColumnList<Row> for smallvec::SmallVec<A> {
type Columns = crate::Cons<Self, crate::Nil>;
}
impl<Row: ?Sized, T> RowColumnList<Row> for Option<T> {
type Columns = crate::Cons<Self, crate::Nil>;
}
impl<Row: ?Sized, A> RowColumnList<Row> for (A,)
where
A: RowColumnList<Row>,
{
type Columns = <A as RowColumnList<Row>>::Columns;
}
impl<Row: ?Sized, A, B> RowColumnList<Row> for (A, B)
where
A: RowColumnList<Row>,
B: RowColumnList<Row>,
<A as RowColumnList<Row>>::Columns: crate::Concat<<B as RowColumnList<Row>>::Columns>,
{
type Columns = <<A as RowColumnList<Row>>::Columns as crate::Concat<
<B as RowColumnList<Row>>::Columns,
>>::Output;
}
impl<Row: ?Sized, A, B, C> RowColumnList<Row> for (A, B, C)
where
A: RowColumnList<Row>,
B: RowColumnList<Row>,
C: RowColumnList<Row>,
<A as RowColumnList<Row>>::Columns: crate::Concat<<B as RowColumnList<Row>>::Columns>,
<<A as RowColumnList<Row>>::Columns as crate::Concat<<B as RowColumnList<Row>>::Columns>>::Output:
crate::Concat<<C as RowColumnList<Row>>::Columns>,
{
type Columns = <<<A as RowColumnList<Row>>::Columns as crate::Concat<
<B as RowColumnList<Row>>::Columns,
>>::Output as crate::Concat<<C as RowColumnList<Row>>::Columns>>::Output;
}
impl<Row: ?Sized, A, B, C, D> RowColumnList<Row> for (A, B, C, D)
where
A: RowColumnList<Row>,
B: RowColumnList<Row>,
C: RowColumnList<Row>,
D: RowColumnList<Row>,
(A, B, C): RowColumnList<Row>,
<(A, B, C) as RowColumnList<Row>>::Columns: crate::Concat<<D as RowColumnList<Row>>::Columns>,
{
type Columns = <<(A, B, C) as RowColumnList<Row>>::Columns as crate::Concat<
<D as RowColumnList<Row>>::Columns,
>>::Output;
}
impl<Row: ?Sized, A, B, C, D, E> RowColumnList<Row> for (A, B, C, D, E)
where
E: RowColumnList<Row>,
(A, B, C, D): RowColumnList<Row>,
<(A, B, C, D) as RowColumnList<Row>>::Columns:
crate::Concat<<E as RowColumnList<Row>>::Columns>,
{
type Columns = <<(A, B, C, D) as RowColumnList<Row>>::Columns as crate::Concat<
<E as RowColumnList<Row>>::Columns,
>>::Output;
}
impl<Row: ?Sized, A, B, C, D, E, F> RowColumnList<Row> for (A, B, C, D, E, F)
where
F: RowColumnList<Row>,
(A, B, C, D, E): RowColumnList<Row>,
<(A, B, C, D, E) as RowColumnList<Row>>::Columns:
crate::Concat<<F as RowColumnList<Row>>::Columns>,
{
type Columns = <<(A, B, C, D, E) as RowColumnList<Row>>::Columns as crate::Concat<
<F as RowColumnList<Row>>::Columns,
>>::Output;
}
impl<Row: ?Sized, A, B, C, D, E, F, G> RowColumnList<Row> for (A, B, C, D, E, F, G)
where
G: RowColumnList<Row>,
(A, B, C, D, E, F): RowColumnList<Row>,
<(A, B, C, D, E, F) as RowColumnList<Row>>::Columns:
crate::Concat<<G as RowColumnList<Row>>::Columns>,
{
type Columns = <<(A, B, C, D, E, F) as RowColumnList<Row>>::Columns as crate::Concat<
<G as RowColumnList<Row>>::Columns,
>>::Output;
}
impl<Row: ?Sized, A, B, C, D, E, F, G, H> RowColumnList<Row> for (A, B, C, D, E, F, G, H)
where
H: RowColumnList<Row>,
(A, B, C, D, E, F, G): RowColumnList<Row>,
<(A, B, C, D, E, F, G) as RowColumnList<Row>>::Columns:
crate::Concat<<H as RowColumnList<Row>>::Columns>,
{
type Columns = <<(A, B, C, D, E, F, G) as RowColumnList<Row>>::Columns as crate::Concat<
<H as RowColumnList<Row>>::Columns,
>>::Output;
}
#[diagnostic::on_unimplemented(
message = "selected shape does not match decode target `{Actual}`",
label = "this decode target is not type-compatible with .select(...) output",
note = "use typed expressions or derive FromRow for explicit remapping when selecting custom expressions"
)]
pub trait MarkerColumnCountValid<Row: ?Sized, Inferred, Actual> {}
#[diagnostic::on_unimplemented(
message = "raw select expressions require explicit typing in strict decode",
label = "`select(sql!(...)).all()/get()` is not allowed in strict mode",
note = "use typed wrappers like `raw_non_null`/`raw_nullable` or derive FromRow"
)]
pub trait StrictDecodeMarker {}
impl StrictDecodeMarker for SelectStar {}
impl<Cols> StrictDecodeMarker for SelectCols<Cols> {}
impl<R> StrictDecodeMarker for SelectAs<R> {}
impl<M, Scope> StrictDecodeMarker for Scoped<M, Scope> where M: StrictDecodeMarker {}
impl<Row: ?Sized, Inferred, Actual> MarkerColumnCountValid<Row, Inferred, Actual> for SelectStar {}
impl<Row: ?Sized, Cols, Inferred, Actual> MarkerColumnCountValid<Row, Inferred, Actual>
for SelectCols<Cols>
where
Cols: SelectedColumnList,
Actual: RowColumnList<Row>,
<Cols as SelectedColumnList>::Columns:
TypeListCompatible<Row, <Actual as RowColumnList<Row>>::Columns>,
{
}
impl<Row: ?Sized, Inferred, Actual> MarkerColumnCountValid<Row, Inferred, Actual> for SelectExpr where
Inferred: SameType<Actual>
{
}
impl<Row: ?Sized, R, Inferred, Actual> MarkerColumnCountValid<Row, Inferred, Actual>
for SelectAs<R>
{
}
impl<M, Scope, Row: ?Sized, Inferred, Actual> MarkerColumnCountValid<Row, Inferred, Actual>
for Scoped<M, Scope>
where
M: MarkerColumnCountValid<Row, Inferred, Actual>,
{
}
pub trait ScopePush<Joined> {
type Out;
}
impl<M, Scope, Joined> ScopePush<Joined> for Scoped<M, Scope> {
type Out = Scoped<M, Cons<Joined, Scope>>;
}
pub trait DecodeSelectedRef<RowRef, R> {
fn decode(row: RowRef) -> Result<R, DrizzleError>;
}
impl<RowRef, R> DecodeSelectedRef<RowRef, R> for SelectAs<R>
where
R: TryFrom<RowRef>,
<R as TryFrom<RowRef>>::Error: Into<DrizzleError>,
{
fn decode(row: RowRef) -> Result<R, DrizzleError> {
R::try_from(row).map_err(Into::into)
}
}
impl<RowRef, R, M, Scope> DecodeSelectedRef<RowRef, R> for Scoped<M, Scope>
where
M: DecodeSelectedRef<RowRef, R>,
{
fn decode(row: RowRef) -> Result<R, DrizzleError> {
M::decode(row)
}
}
impl<RowRef, Row: ?Sized, R> DecodeSelectedRef<RowRef, R> for SelectStar
where
RowRef: core::ops::Deref<Target = Row>,
R: FromDrizzleRow<Row>,
{
fn decode(row: RowRef) -> Result<R, DrizzleError> {
R::from_row(&*row)
}
}
impl<RowRef, Row: ?Sized, Cols, R> DecodeSelectedRef<RowRef, R> for SelectCols<Cols>
where
RowRef: core::ops::Deref<Target = Row>,
R: FromDrizzleRow<Row>,
{
fn decode(row: RowRef) -> Result<R, DrizzleError> {
R::from_row(&*row)
}
}
impl<RowRef, Row: ?Sized, R> DecodeSelectedRef<RowRef, R> for SelectExpr
where
RowRef: core::ops::Deref<Target = Row>,
R: FromDrizzleRow<Row>,
{
fn decode(row: RowRef) -> Result<R, DrizzleError> {
R::from_row(&*row)
}
}
#[diagnostic::on_unimplemented(
message = "cannot deserialize `{Self}` from a database row",
label = "this type does not implement FromDrizzleRow",
note = "derive #[SQLiteFromRow] or #[PostgresFromRow]"
)]
pub trait FromDrizzleRow<Row: ?Sized>: Sized {
const COLUMN_COUNT: usize;
fn from_row_at(row: &Row, offset: usize) -> Result<Self, DrizzleError>;
fn from_row(row: &Row) -> Result<Self, DrizzleError> {
Self::from_row_at(row, 0)
}
}
pub trait NullProbeRow<Row: ?Sized>: FromDrizzleRow<Row> {
fn is_null_at(row: &Row, offset: usize) -> Result<bool, DrizzleError>;
}
macro_rules! impl_from_drizzle_row_tuple {
($($T:ident),+; $($idx:tt),+) => {
impl<__Row: ?Sized, $($T: FromDrizzleRow<__Row>),+> FromDrizzleRow<__Row> for ($($T,)+) {
const COLUMN_COUNT: usize = 0 $(+ <$T as FromDrizzleRow<__Row>>::COLUMN_COUNT)+;
#[allow(non_snake_case)]
fn from_row_at(
row: &__Row,
offset: usize,
) -> Result<Self, DrizzleError> {
let mut __off = offset;
$(
let $T = <$T as FromDrizzleRow<__Row>>::from_row_at(row, __off)?;
__off += <$T as FromDrizzleRow<__Row>>::COLUMN_COUNT;
)+
Ok(($($T,)+))
}
}
};
}
with_col_sizes_8!(impl_from_drizzle_row_tuple);
#[cfg(any(
feature = "col16",
feature = "col32",
feature = "col64",
feature = "col128",
feature = "col200"
))]
with_col_sizes_16!(impl_from_drizzle_row_tuple);
#[cfg(any(
feature = "col32",
feature = "col64",
feature = "col128",
feature = "col200"
))]
with_col_sizes_32!(impl_from_drizzle_row_tuple);
#[cfg(any(feature = "col64", feature = "col128", feature = "col200"))]
with_col_sizes_64!(impl_from_drizzle_row_tuple);
#[cfg(any(feature = "col128", feature = "col200"))]
with_col_sizes_128!(impl_from_drizzle_row_tuple);
#[cfg(feature = "col200")]
with_col_sizes_200!(impl_from_drizzle_row_tuple);
#[diagnostic::on_unimplemented(
message = "SQL type `{Self}` has no default Rust mapping for dialect `{D}`",
label = "this SQL type has no default Rust mapping for this dialect",
note = "enable `chrono` for Date/Time/Timestamp/TimestampTz, `uuid` for Uuid, or `serde` for Json/Jsonb"
)]
pub trait SQLTypeToRust<D> {
type RustType;
}
use crate::dialect::{PostgresDialect, SQLiteDialect};
impl<D, T> SQLTypeToRust<D> for crate::types::Array<T>
where
T: crate::types::DataType + SQLTypeToRust<D>,
{
type RustType = crate::prelude::Vec<<T as SQLTypeToRust<D>>::RustType>;
}
impl SQLTypeToRust<SQLiteDialect> for drizzle_types::sqlite::types::Integer {
type RustType = i64;
}
impl SQLTypeToRust<SQLiteDialect> for drizzle_types::sqlite::types::Text {
type RustType = crate::prelude::String;
}
impl SQLTypeToRust<SQLiteDialect> for drizzle_types::sqlite::types::Real {
type RustType = f64;
}
impl SQLTypeToRust<SQLiteDialect> for drizzle_types::sqlite::types::Blob {
type RustType = crate::prelude::Vec<u8>;
}
impl SQLTypeToRust<SQLiteDialect> for drizzle_types::sqlite::types::Numeric {
type RustType = f64;
}
impl SQLTypeToRust<SQLiteDialect> for drizzle_types::sqlite::types::Any {
type RustType = crate::prelude::String;
}
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Int2 {
type RustType = i16;
}
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Int4 {
type RustType = i32;
}
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Int8 {
type RustType = i64;
}
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Float4 {
type RustType = f32;
}
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Float8 {
type RustType = f64;
}
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Varchar {
type RustType = crate::prelude::String;
}
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Text {
type RustType = crate::prelude::String;
}
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Char {
type RustType = crate::prelude::String;
}
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Bytea {
type RustType = crate::prelude::Vec<u8>;
}
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Boolean {
type RustType = bool;
}
#[cfg(feature = "rust-decimal")]
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Numeric {
type RustType = rust_decimal::Decimal;
}
#[cfg(not(feature = "rust-decimal"))]
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Numeric {
type RustType = crate::prelude::String;
}
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Any {
type RustType = crate::prelude::String;
}
#[cfg(feature = "chrono")]
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Timestamptz {
type RustType = chrono::DateTime<chrono::Utc>;
}
#[cfg(feature = "chrono")]
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Timestamp {
type RustType = chrono::NaiveDateTime;
}
#[cfg(feature = "chrono")]
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Date {
type RustType = chrono::NaiveDate;
}
#[cfg(feature = "chrono")]
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Time {
type RustType = chrono::NaiveTime;
}
#[cfg(feature = "chrono")]
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Timetz {
type RustType = chrono::NaiveTime;
}
#[cfg(feature = "uuid")]
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Uuid {
type RustType = uuid::Uuid;
}
#[cfg(feature = "serde")]
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Json {
type RustType = serde_json::Value;
}
#[cfg(feature = "serde")]
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Jsonb {
type RustType = serde_json::Value;
}
#[cfg(feature = "chrono")]
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Interval {
type RustType = chrono::Duration;
}
#[cfg(not(feature = "chrono"))]
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Interval {
type RustType = crate::prelude::String;
}
#[cfg(feature = "cidr")]
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Inet {
type RustType = cidr::IpInet;
}
#[cfg(not(feature = "cidr"))]
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Inet {
type RustType = crate::prelude::String;
}
#[cfg(feature = "cidr")]
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Cidr {
type RustType = cidr::IpCidr;
}
#[cfg(not(feature = "cidr"))]
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Cidr {
type RustType = crate::prelude::String;
}
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::MacAddr {
type RustType = crate::prelude::String;
}
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::MacAddr8 {
type RustType = crate::prelude::String;
}
#[cfg(feature = "geo-types")]
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Point {
type RustType = geo_types::Point<f64>;
}
#[cfg(not(feature = "geo-types"))]
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Point {
type RustType = crate::prelude::String;
}
#[cfg(feature = "geo-types")]
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::LineString {
type RustType = geo_types::LineString<f64>;
}
#[cfg(not(feature = "geo-types"))]
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::LineString {
type RustType = crate::prelude::String;
}
#[cfg(feature = "geo-types")]
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Rect {
type RustType = geo_types::Rect<f64>;
}
#[cfg(not(feature = "geo-types"))]
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Rect {
type RustType = crate::prelude::String;
}
#[cfg(feature = "bit-vec")]
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::BitString {
type RustType = bit_vec::BitVec;
}
#[cfg(not(feature = "bit-vec"))]
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::BitString {
type RustType = crate::prelude::String;
}
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Line {
type RustType = crate::prelude::String;
}
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::LineSegment {
type RustType = crate::prelude::String;
}
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Polygon {
type RustType = crate::prelude::String;
}
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Circle {
type RustType = crate::prelude::String;
}
impl SQLTypeToRust<PostgresDialect> for drizzle_types::postgres::types::Enum {
type RustType = crate::prelude::String;
}
pub trait WrapNullable<T> {
type Output;
}
impl<T> WrapNullable<T> for crate::expr::NonNull {
type Output = T;
}
impl<T> WrapNullable<T> for crate::expr::Null {
type Output = Option<T>;
}
#[diagnostic::on_unimplemented(
message = "cannot infer Rust type for expression `{Self}`",
label = "use typed expressions or derive FromRow to specify the Rust type",
note = "raw SQL and JSON expressions require explicit type annotation"
)]
pub trait ExprValueType {
type ValueType;
}
impl<V: crate::SQLParam, T, N, A> ExprValueType for crate::expr::SQLExpr<'_, V, T, N, A>
where
T: crate::types::DataType + SQLTypeToRust<V::DialectMarker>,
N: crate::expr::Nullability + WrapNullable<<T as SQLTypeToRust<V::DialectMarker>>::RustType>,
A: crate::expr::AggregateKind,
{
type ValueType = <N as WrapNullable<<T as SQLTypeToRust<V::DialectMarker>>::RustType>>::Output;
}
impl<V: crate::SQLParam> ExprValueType for crate::sql::SQL<'_, V> {
type ValueType = ();
}
#[diagnostic::on_unimplemented(
message = "`{Self}` is not a drizzle table",
label = "ensure this type was derived with #[SQLiteTable] or #[PostgresTable]"
)]
pub trait HasSelectModel {
type SelectModel;
const COLUMN_COUNT: usize;
}
#[diagnostic::on_unimplemented(
message = "cannot resolve return type for this query",
label = "the selected columns and table do not produce a known row type"
)]
pub trait ResolveRow<Table> {
type Row;
}
impl<T: HasSelectModel> ResolveRow<T> for SelectStar {
type Row = T::SelectModel;
}
impl<T> ResolveRow<T> for SelectExpr {
type Row = ();
}
impl<R, T> ResolveRow<T> for SelectAs<R>
where
R: SelectAsFrom<T>,
{
type Row = R;
}
impl<M, Scope, T> ResolveRow<T> for Scoped<M, Scope>
where
M: ResolveRow<T>,
{
type Row = M::Row;
}
#[diagnostic::on_unimplemented(
message = "row selector `{Self}` cannot be used with table `{Table}`",
label = "the #[from(...)] table does not match .from(...)",
note = "set #[from(TheTable)] to the same table passed to .from(...)"
)]
pub trait SelectAsFrom<Table> {}
macro_rules! impl_resolve_row_cols {
($($T:ident),+; $($idx:tt),+) => {
impl<__Table, $($T: ExprValueType),+> ResolveRow<__Table> for SelectCols<($($T,)+)> {
type Row = ($(<$T as ExprValueType>::ValueType,)+);
}
};
}
with_col_sizes_8!(impl_resolve_row_cols);
#[cfg(any(
feature = "col16",
feature = "col32",
feature = "col64",
feature = "col128",
feature = "col200"
))]
with_col_sizes_16!(impl_resolve_row_cols);
#[cfg(any(
feature = "col32",
feature = "col64",
feature = "col128",
feature = "col200"
))]
with_col_sizes_32!(impl_resolve_row_cols);
#[cfg(any(feature = "col64", feature = "col128", feature = "col200"))]
with_col_sizes_64!(impl_resolve_row_cols);
#[cfg(any(feature = "col128", feature = "col200"))]
with_col_sizes_128!(impl_resolve_row_cols);
#[cfg(feature = "col200")]
with_col_sizes_200!(impl_resolve_row_cols);
macro_rules! selected_columns_cons {
() => {
crate::Nil
};
($head:ident $(, $tail:ident)*) => {
crate::Cons<<$head as ExprValueType>::ValueType, selected_columns_cons!($($tail),*)>
};
}
macro_rules! impl_selected_column_list_tuple {
($($T:ident),+; $($idx:tt),+) => {
impl<$($T: ExprValueType),+> SelectedColumnList for ($($T,)+) {
type Columns = selected_columns_cons!($($T),+);
}
};
}
with_col_sizes_8!(impl_selected_column_list_tuple);
#[cfg(any(
feature = "col16",
feature = "col32",
feature = "col64",
feature = "col128",
feature = "col200"
))]
with_col_sizes_16!(impl_selected_column_list_tuple);
#[cfg(any(
feature = "col32",
feature = "col64",
feature = "col128",
feature = "col200"
))]
with_col_sizes_32!(impl_selected_column_list_tuple);
#[cfg(any(feature = "col64", feature = "col128", feature = "col200"))]
with_col_sizes_64!(impl_selected_column_list_tuple);
#[cfg(any(feature = "col128", feature = "col200"))]
with_col_sizes_128!(impl_selected_column_list_tuple);
#[cfg(feature = "col200")]
with_col_sizes_200!(impl_selected_column_list_tuple);
pub trait AfterJoin<CurrentRow, JoinedTable> {
type NewRow;
}
pub trait AfterLeftJoin<CurrentRow, JoinedTable> {
type NewRow;
}
pub trait AfterRightJoin<CurrentRow, JoinedTable> {
type NewRow;
}
pub trait AfterFullJoin<CurrentRow, JoinedTable> {
type NewRow;
}
impl<R, T: HasSelectModel> AfterJoin<R, T> for SelectStar {
type NewRow = (R, T::SelectModel);
}
impl<R, T: HasSelectModel> AfterLeftJoin<R, T> for SelectStar {
type NewRow = (R, Option<T::SelectModel>);
}
impl<R, T: HasSelectModel> AfterRightJoin<R, T> for SelectStar {
type NewRow = (Option<R>, T::SelectModel);
}
impl<R, T: HasSelectModel> AfterFullJoin<R, T> for SelectStar {
type NewRow = (Option<R>, Option<T::SelectModel>);
}
impl<Cols, R, T> AfterJoin<R, T> for SelectCols<Cols> {
type NewRow = R;
}
impl<Cols, R, T> AfterLeftJoin<R, T> for SelectCols<Cols> {
type NewRow = R;
}
impl<Cols, R, T> AfterRightJoin<R, T> for SelectCols<Cols> {
type NewRow = R;
}
impl<Cols, R, T> AfterFullJoin<R, T> for SelectCols<Cols> {
type NewRow = R;
}
impl<R, T> AfterJoin<R, T> for SelectExpr {
type NewRow = R;
}
impl<R, T> AfterLeftJoin<R, T> for SelectExpr {
type NewRow = R;
}
impl<R, T> AfterRightJoin<R, T> for SelectExpr {
type NewRow = R;
}
impl<R, T> AfterFullJoin<R, T> for SelectExpr {
type NewRow = R;
}
impl<Row, R, T> AfterJoin<R, T> for SelectAs<Row> {
type NewRow = R;
}
impl<Row, R, T> AfterLeftJoin<R, T> for SelectAs<Row> {
type NewRow = R;
}
impl<Row, R, T> AfterRightJoin<R, T> for SelectAs<Row> {
type NewRow = R;
}
impl<Row, R, T> AfterFullJoin<R, T> for SelectAs<Row> {
type NewRow = R;
}
impl<M, Scope, R, T> AfterJoin<R, T> for Scoped<M, Scope>
where
M: AfterJoin<R, T>,
{
type NewRow = M::NewRow;
}
impl<M, Scope, R, T> AfterLeftJoin<R, T> for Scoped<M, Scope>
where
M: AfterLeftJoin<R, T>,
{
type NewRow = M::NewRow;
}
impl<M, Scope, R, T> AfterRightJoin<R, T> for Scoped<M, Scope>
where
M: AfterRightJoin<R, T>,
{
type NewRow = M::NewRow;
}
impl<M, Scope, R, T> AfterFullJoin<R, T> for Scoped<M, Scope>
where
M: AfterFullJoin<R, T>,
{
type NewRow = M::NewRow;
}
#[diagnostic::on_unimplemented(
message = "`{Self}` cannot be used as a select target",
label = "this type does not implement IntoSelectTarget",
note = "implement IntoSelectTarget or use a column, table, or typed expression"
)]
pub trait IntoSelectTarget {
type Marker;
}
impl IntoSelectTarget for () {
type Marker = SelectStar;
}
impl<V: crate::SQLParam> IntoSelectTarget for crate::sql::SQL<'_, V> {
type Marker = SelectExpr;
}
impl<V: crate::SQLParam, T, N, A> IntoSelectTarget for crate::expr::SQLExpr<'_, V, T, N, A>
where
T: crate::types::DataType,
N: crate::expr::Nullability,
A: crate::expr::AggregateKind,
{
type Marker = SelectCols<(Self,)>;
}
macro_rules! impl_into_select_target_tuple {
($($T:ident),+; $($idx:tt),+) => {
impl<$($T),+> IntoSelectTarget for ($($T,)+) {
type Marker = SelectCols<($($T,)+)>;
}
};
}
with_col_sizes_8!(impl_into_select_target_tuple);
#[cfg(any(
feature = "col16",
feature = "col32",
feature = "col64",
feature = "col128",
feature = "col200"
))]
with_col_sizes_16!(impl_into_select_target_tuple);
#[cfg(any(
feature = "col32",
feature = "col64",
feature = "col128",
feature = "col200"
))]
with_col_sizes_32!(impl_into_select_target_tuple);
#[cfg(any(feature = "col64", feature = "col128", feature = "col200"))]
with_col_sizes_64!(impl_into_select_target_tuple);
#[cfg(any(feature = "col128", feature = "col200"))]
with_col_sizes_128!(impl_into_select_target_tuple);
#[cfg(feature = "col200")]
with_col_sizes_200!(impl_into_select_target_tuple);