Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use associated types for SelectableExpression #709

Merged
merged 3 commits into from
Feb 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/

[transaction-0.11.0]: http://docs.diesel.rs/diesel/connection/trait.Connection.html#method.transaction

* The way tuples of columns from the right side of left outer joins interact
with `.select` has changed. If you are deserializing into an option of a tuple
(instead of a tuple of options), you will need to explicitly call
`.nullable()`. (e.g. `.select(users::name, (posts::title,
posts::body).nullable())`)

### Removed

* `result::TransactionError`
Expand Down
5 changes: 2 additions & 3 deletions diesel/src/expression/aliased.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ impl<'a, Expr> Aliased<'a, Expr> {
}
}

#[derive(Debug, Copy, Clone)]
pub struct FromEverywhere;

impl<'a, T> Expression for Aliased<'a, T> where
T: Expression,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, was this ever used for anything?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It used to be, yeah. I don't know when it stopped being used.

{
Expand Down Expand Up @@ -57,7 +54,9 @@ impl<'a, T> QueryId for Aliased<'a, T> {
// FIXME This is incorrect, should only be selectable from WithQuerySource
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still true?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still true.

impl<'a, T, QS> SelectableExpression<QS> for Aliased<'a, T> where
Aliased<'a, T>: Expression,
T: SelectableExpression<QS>,
{
type SqlTypeForSelect = T::SqlTypeForSelect;
}

impl<'a, T: Expression + Copy> QuerySource for Aliased<'a, T> {
Expand Down
11 changes: 8 additions & 3 deletions diesel/src/expression/array_comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ impl<T, U, QS> SelectableExpression<QS> for In<T, U> where
T: SelectableExpression<QS>,
U: SelectableExpression<QS>,
{
type SqlTypeForSelect = Self::SqlType;
}

impl<T, U, QS> SelectableExpression<QS> for NotIn<T, U> where
NotIn<T, U>: Expression,
T: SelectableExpression<QS>,
U: SelectableExpression<QS>,
{
type SqlTypeForSelect = Self::SqlType;
}

impl<T, U> NonAggregate for In<T, U> where
Expand Down Expand Up @@ -163,9 +165,10 @@ pub trait MaybeEmpty {
fn is_empty(&self) -> bool;
}

impl<ST, S, F, W, O, L, Of> AsInExpression<ST>
for SelectStatement<ST, S, F, W, O, L, Of> where
Subselect<SelectStatement<ST, S, F, W, O, L, Of>, ST>: Expression<SqlType=ST>,
impl<ST, S, F, W, O, L, Of, G> AsInExpression<ST>
for SelectStatement<S, F, W, O, L, Of, G> where
SelectStatement<S, F, W, O, L, Of, G>: Query<SqlType=ST>,
Subselect<SelectStatement<S, F, W, O, L, Of>, ST>: Expression,
{
type InExpression = Subselect<Self, ST>;

Expand Down Expand Up @@ -202,6 +205,7 @@ impl<T, QS> SelectableExpression<QS> for Many<T> where
Many<T>: Expression,
T: SelectableExpression<QS>,
{
type SqlTypeForSelect = T::SqlTypeForSelect;
}

impl<T, DB> QueryFragment<DB> for Many<T> where
Expand Down Expand Up @@ -251,6 +255,7 @@ impl<T, ST, QS> SelectableExpression<QS> for Subselect<T, ST> where
Subselect<T, ST>: Expression,
T: Query,
{
type SqlTypeForSelect = ST;
}

impl<T, ST, DB> QueryFragment<DB> for Subselect<T, ST> where
Expand Down
1 change: 1 addition & 0 deletions diesel/src/expression/bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ impl<T: QueryId, U> QueryId for Bound<T, U> {
impl<T, U, QS> SelectableExpression<QS> for Bound<T, U> where
Bound<T, U>: Expression,
{
type SqlTypeForSelect = T;
}

impl<T, U> NonAggregate for Bound<T, U> where
Expand Down
1 change: 1 addition & 0 deletions diesel/src/expression/coerce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl<T, ST> Expression for Coerce<T, ST> where
impl<T, ST, QS> SelectableExpression<QS> for Coerce<T, ST> where
T: SelectableExpression<QS>,
{
type SqlTypeForSelect = Self::SqlType;
}

impl<T, ST, DB> QueryFragment<DB> for Coerce<T, ST> where
Expand Down
2 changes: 2 additions & 0 deletions diesel/src/expression/count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl<T: QueryFragment<DB>, DB: Backend> QueryFragment<DB> for Count<T> {
impl_query_id!(Count<T>);

impl<T: Expression, QS> SelectableExpression<QS> for Count<T> {
type SqlTypeForSelect = BigInt;
}

#[derive(Debug, Clone, Copy)]
Expand All @@ -106,6 +107,7 @@ impl<DB: Backend> QueryFragment<DB> for CountStar {
}

impl<QS> SelectableExpression<QS> for CountStar {
type SqlTypeForSelect = BigInt;
}

impl_query_id!(CountStar);
1 change: 1 addition & 0 deletions diesel/src/expression/exists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ impl<T> Expression for Exists<T> where
impl<T, QS> SelectableExpression<QS> for Exists<T> where
Exists<T>: Expression,
{
type SqlTypeForSelect = Bool;
}

impl<T> NonAggregate for Exists<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ pub trait ExpressionMethods: Expression + Sized {
/// # fn main() {
/// # use self::users::dsl::*;
/// # let order = "name";
/// let ordering: Box<BoxableExpression<users, (), DB, SqlType=()>> =
/// let ordering: Box<BoxableExpression<users, DB, SqlType=(), SqlTypeForSelect=()>> =
/// if order == "name" {
/// Box::new(name.desc())
/// } else {
Expand Down
7 changes: 4 additions & 3 deletions diesel/src/expression/functions/aggregate_folding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ macro_rules! fold_function {

impl_query_id!($type_name<T>);

impl<ST, T, QS> SelectableExpression<QS> for $type_name<T> where
ST: Foldable,
T: Expression<SqlType=ST>,
impl<T, QS> SelectableExpression<QS> for $type_name<T> where
$type_name<T>: Expression,
T: SelectableExpression<QS>,
{
type SqlTypeForSelect = Self::SqlType;
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions diesel/src/expression/functions/aggregate_ordering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ macro_rules! ord_function {

impl<T, QS> SelectableExpression<QS> for $type_name<T> where
$type_name<T>: Expression,
T: SelectableExpression<QS>,
{
type SqlTypeForSelect = Self::SqlType;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions diesel/src/expression/functions/date_and_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ impl Expression for now {
}

impl<QS> SelectableExpression<QS> for now {
type SqlTypeForSelect = Timestamp;
}

impl NonAggregate for now {
Expand Down
2 changes: 2 additions & 0 deletions diesel/src/expression/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ macro_rules! sql_function_body {
$($arg_name: $crate::expression::SelectableExpression<QS>,)*
$struct_name<$($arg_name),*>: $crate::expression::Expression,
{
type SqlTypeForSelect = Self::SqlType;
}

#[allow(non_camel_case_types)]
Expand Down Expand Up @@ -132,6 +133,7 @@ macro_rules! no_arg_sql_function_body_except_to_sql {
}

impl<QS> $crate::expression::SelectableExpression<QS> for $type_name {
type SqlTypeForSelect = $return_type;
}

impl $crate::expression::NonAggregate for $type_name {
Expand Down
1 change: 1 addition & 0 deletions diesel/src/expression/grouped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ impl<T, QS> SelectableExpression<QS> for Grouped<T> where
T: SelectableExpression<QS>,
Grouped<T>: Expression,
{
type SqlTypeForSelect = T::SqlTypeForSelect;
}

impl<T: NonAggregate> NonAggregate for Grouped<T> where
Expand Down
33 changes: 17 additions & 16 deletions diesel/src/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,29 +104,30 @@ impl<T: Expression> AsExpression<T::SqlType> for T {
}
}

/// Indicates that an expression can be selected from a source. The second type
/// argument is optional, but is used to indicate that the right side of a left
/// outer join is nullable, even if it wasn't before.
/// Indicates that an expression can be selected from a source. The associated
/// type is usually the same as `Expression::SqlType`, but is used to indicate
/// that a column is always nullable when it appears on the right side of a left
/// outer join, even if it wasn't nullable to begin with.
///
/// Columns will implement this for their table. Certain special types, like
/// `CountStar` and `Bound` will implement this for all sources. All other
/// expressions will inherit this from their children.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inherit this from their children

Only noticed this now. Nicely put, will make people used to classical OOP with inheritance nervous, though 😄

pub trait SelectableExpression<
QS,
Type = <Self as Expression>::SqlType,
>: Expression {
pub trait SelectableExpression<QS>: Expression {
type SqlTypeForSelect;
}

impl<T: ?Sized, ST, QS> SelectableExpression<QS, ST> for Box<T> where
T: SelectableExpression<QS, ST>,
impl<T: ?Sized, QS> SelectableExpression<QS> for Box<T> where
T: SelectableExpression<QS>,
Box<T>: Expression,
{
type SqlTypeForSelect = T::SqlTypeForSelect;
}

impl<'a, T: ?Sized, ST, QS> SelectableExpression<QS, ST> for &'a T where
T: SelectableExpression<QS, ST>,
impl<'a, T: ?Sized, QS> SelectableExpression<QS> for &'a T where
T: SelectableExpression<QS>,
&'a T: Expression,
{
type SqlTypeForSelect = T::SqlTypeForSelect;
}

/// Marker trait to indicate that an expression does not include any aggregate
Expand All @@ -147,24 +148,24 @@ use query_builder::{QueryFragment, QueryId};
/// Helper trait used when boxing expressions. This exists to work around the
/// fact that Rust will not let us use non-core types as bounds on a trait
/// object (you could not return `Box<Expression+NonAggregate>`)
pub trait BoxableExpression<QS, ST, DB> where
pub trait BoxableExpression<QS, DB> where
DB: Backend,
Self: Expression,
Self: SelectableExpression<QS, ST>,
Self: SelectableExpression<QS>,
Self: NonAggregate,
Self: QueryFragment<DB>,
{}

impl<QS, T, ST, DB> BoxableExpression<QS, ST, DB> for T where
impl<QS, T, DB> BoxableExpression<QS, DB> for T where
DB: Backend,
T: Expression,
T: SelectableExpression<QS, ST>,
T: SelectableExpression<QS>,
T: NonAggregate,
T: QueryFragment<DB>,
{
}

impl<QS, ST, DB> QueryId for BoxableExpression<QS, ST, DB, SqlType=ST> {
impl<QS, ST, STS, DB> QueryId for BoxableExpression<QS, DB, SqlType=ST, SqlTypeForSelect=STS> {
type QueryId = ();

fn has_static_query_id() -> bool {
Expand Down
4 changes: 4 additions & 0 deletions diesel/src/expression/nullable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,14 @@ impl<T, DB> QueryFragment<DB> for Nullable<T> where
}
}

/// This impl relies on the fact that the only time `T::SqlType` will differ
/// from `T::SqlTypeForSelect` is to make the right side of a left join become
/// nullable.
impl<T, QS> SelectableExpression<QS> for Nullable<T> where
T: SelectableExpression<QS>,
Nullable<T>: Expression,
{
type SqlTypeForSelect = Self::SqlType;
}

impl<T: QueryId> QueryId for Nullable<T> {
Expand Down
1 change: 1 addition & 0 deletions diesel/src/expression/ops/numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ macro_rules! numeric_operation {
Rhs: SelectableExpression<QS>,
$name<Lhs, Rhs>: Expression,
{
type SqlTypeForSelect = Self::SqlType;
}

impl<Lhs, Rhs> NonAggregate for $name<Lhs, Rhs> where
Expand Down
2 changes: 2 additions & 0 deletions diesel/src/expression/predicates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ macro_rules! infix_predicate_body {
T: $crate::expression::SelectableExpression<QS>,
U: $crate::expression::SelectableExpression<QS>,
{
type SqlTypeForSelect = Self::SqlType;
}

impl<T, U> $crate::expression::NonAggregate for $name<T, U> where
Expand Down Expand Up @@ -219,6 +220,7 @@ macro_rules! postfix_predicate_body {
impl<T, QS> $crate::expression::SelectableExpression<QS> for $name<T> where
T: $crate::expression::SelectableExpression<QS>,
{
type SqlTypeForSelect = Self::SqlType;
}

impl<T> $crate::expression::NonAggregate for $name<T> where
Expand Down
1 change: 1 addition & 0 deletions diesel/src/expression/sql_literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impl<ST> Query for SqlLiteral<ST> {
}

impl<QS, ST> SelectableExpression<QS> for SqlLiteral<ST> {
type SqlTypeForSelect = ST;
}

impl<ST> NonAggregate for SqlLiteral<ST> {
Expand Down
34 changes: 22 additions & 12 deletions diesel/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,24 @@ macro_rules! __diesel_column {

impl_query_id!($column_name);

impl $crate::expression::SelectableExpression<$($table)::*> for $column_name {}
impl $crate::expression::SelectableExpression<$($table)::*> for $column_name {
type SqlTypeForSelect = $Type;
}

impl<'a, ST, Left, Right> SelectableExpression<
$crate::WithQuerySource<'a, Left, Right>, ST> for $column_name where
$column_name: SelectableExpression<Left, ST>
impl<'a, Left, Right> SelectableExpression<
$crate::WithQuerySource<'a, Left, Right>,
> for $column_name where
$column_name: SelectableExpression<Left>,
{
type SqlTypeForSelect = $Type;
}

impl<ST, Source, Predicate> SelectableExpression<
$crate::query_source::filter::FilteredQuerySource<Source, Predicate>, ST>
for $column_name where
$column_name: SelectableExpression<Source, ST>
impl<Source, Predicate> SelectableExpression<
$crate::query_source::filter::FilteredQuerySource<Source, Predicate>,
> for $column_name where
$column_name: SelectableExpression<Source>,
{
type SqlTypeForSelect = $Type;
}

impl $crate::expression::NonAggregate for $column_name {}
Expand Down Expand Up @@ -301,7 +306,7 @@ macro_rules! table_body {

impl AsQuery for table {
type SqlType = SqlType;
type Query = SelectStatement<SqlType, ($($column_name,)+), Self>;
type Query = SelectStatement<($($column_name,)+), Self>;

fn as_query(self) -> Self::Query {
SelectStatement::simple(all_columns, self)
Expand Down Expand Up @@ -376,7 +381,9 @@ macro_rules! table_body {
}
}

impl SelectableExpression<table> for star {}
impl SelectableExpression<table> for star {
type SqlTypeForSelect = Self::SqlType;
}

$(__diesel_column!(table, $column_name -> $column_ty);)+
}
Expand Down Expand Up @@ -485,26 +492,29 @@ macro_rules! select_column_inner {
$crate::query_source::InnerJoinSource<$child, $parent>,
> for $column
{
type SqlTypeForSelect = <Self as $crate::Expression>::SqlType;
}

impl $crate::expression::SelectableExpression<
$crate::query_source::InnerJoinSource<$parent, $child>,
> for $column
{
type SqlTypeForSelect = <Self as $crate::Expression>::SqlType;
}

impl $crate::expression::SelectableExpression<
$crate::query_source::LeftOuterJoinSource<$child, $parent>,
<<$column as $crate::Expression>::SqlType
as $crate::types::IntoNullable>::Nullable,
> for $column
{
type SqlTypeForSelect = <<Self as $crate::Expression>::SqlType
as $crate::types::IntoNullable>::Nullable;
}

impl $crate::expression::SelectableExpression<
$crate::query_source::LeftOuterJoinSource<$parent, $child>,
> for $column
{
type SqlTypeForSelect = <Self as $crate::Expression>::SqlType;
}
}
}
Expand Down
Loading