Skip to content

Adds support for Views. #4077

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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
[refactor] changes Table and View trait bounds.
  • Loading branch information
coderPaddyS committed Jun 18, 2024
commit b818e0603b1705d14dc553ee9d8aac528962eb5f
6 changes: 4 additions & 2 deletions diesel/src/expression/operators.rs
Original file line number Diff line number Diff line change
@@ -557,12 +557,14 @@ use crate::insertable::{ColumnInsertValue, Insertable};
use crate::query_builder::{QueryFragment, QueryId, ValuesClause};
use crate::query_source::Column;
use crate::sql_types::{DieselNumericOps, SqlType};
use crate::Table;

impl<T, U> Insertable<T::Table> for Eq<T, U>
impl<T, U> Insertable<T::Source> for Eq<T, U>
where
T: Column,
T::Source: Table
{
type Values = ValuesClause<ColumnInsertValue<T, U>, T::Table>;
type Values = ValuesClause<ColumnInsertValue<T, U>, T::Source>;

fn values(self) -> Self::Values {
ValuesClause::new(ColumnInsertValue::new(self.right))
20 changes: 10 additions & 10 deletions diesel/src/macros/mod.rs
Original file line number Diff line number Diff line change
@@ -211,8 +211,8 @@ macro_rules! joinable_inner {
macro_rules! allow_tables_to_appear_in_same_query {
($left_mod:ident, $($right_mod:ident),+ $(,)*) => {
$(
impl $crate::query_source::TableNotEqual<$left_mod::table> for $right_mod::table {}
impl $crate::query_source::TableNotEqual<$right_mod::table> for $left_mod::table {}
impl $crate::query_source::ViewNotEqual<$left_mod::table> for $right_mod::table {}
Copy link
Member

Choose a reason for hiding this comment

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

Hmm in some other places we name this around Source as the common denominator of view and table, it looks like this may be something we want to be consistent with here.

impl $crate::query_source::ViewNotEqual<$right_mod::table> for $left_mod::table {}
$crate::__diesel_internal_backend_specific_allow_tables_to_appear_in_same_query!($left_mod, $right_mod);
)+
$crate::allow_tables_to_appear_in_same_query!($($right_mod,)+);
@@ -227,44 +227,44 @@ macro_rules! allow_tables_to_appear_in_same_query {
#[cfg(feature = "postgres_backend")]
macro_rules! __diesel_internal_backend_specific_allow_tables_to_appear_in_same_query {
($left:ident, $right:ident) => {
impl $crate::query_source::TableNotEqual<$left::table>
impl $crate::query_source::ViewNotEqual<$left::table>
for $crate::query_builder::Only<$right::table>
{
}
impl $crate::query_source::TableNotEqual<$right::table>
impl $crate::query_source::ViewNotEqual<$right::table>
for $crate::query_builder::Only<$left::table>
{
}
impl $crate::query_source::TableNotEqual<$crate::query_builder::Only<$left::table>>
impl $crate::query_source::ViewNotEqual<$crate::query_builder::Only<$left::table>>
for $right::table
{
}
impl $crate::query_source::TableNotEqual<$crate::query_builder::Only<$right::table>>
impl $crate::query_source::ViewNotEqual<$crate::query_builder::Only<$right::table>>
for $left::table
{
}
impl<TSM> $crate::query_source::TableNotEqual<$left::table>
impl<TSM> $crate::query_source::ViewNotEqual<$left::table>
for $crate::query_builder::Tablesample<$right::table, TSM>
where
TSM: $crate::internal::table_macro::TablesampleMethod,
{
}
impl<TSM> $crate::query_source::TableNotEqual<$right::table>
impl<TSM> $crate::query_source::ViewNotEqual<$right::table>
for $crate::query_builder::Tablesample<$left::table, TSM>
where
TSM: $crate::internal::table_macro::TablesampleMethod,
{
}
impl<TSM>
$crate::query_source::TableNotEqual<
$crate::query_source::ViewNotEqual<
$crate::query_builder::Tablesample<$left::table, TSM>,
> for $right::table
where
TSM: $crate::internal::table_macro::TablesampleMethod,
{
}
impl<TSM>
$crate::query_source::TableNotEqual<
$crate::query_source::ViewNotEqual<
$crate::query_builder::Tablesample<$right::table, TSM>,
> for $left::table
where
5 changes: 3 additions & 2 deletions diesel/src/pg/expression/operators.rs
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ use crate::query_builder::{AstPass, QueryFragment, QueryId};
use crate::sql_types::{
Array, Bigint, Bool, DieselNumericOps, Inet, Integer, Jsonb, SqlType, Text,
};
use crate::{Column, QueryResult};
use crate::{Column, QueryResult, Table};

__diesel_infix_operator!(IsDistinctFrom, " IS DISTINCT FROM ", ConstantNullability Bool, backend: Pg);
__diesel_infix_operator!(IsNotDistinctFrom, " IS NOT DISTINCT FROM ", ConstantNullability Bool, backend: Pg);
@@ -94,8 +94,9 @@ where
impl<L, R> AssignmentTarget for ArrayIndex<L, R>
where
L: Column,
L::Source: Table
{
type Table = <L as Column>::Table;
type Table = <L as Column>::Source;
type QueryAstNode = ArrayIndex<UncorrelatedColumn<L>, R>;

fn into_target(self) -> Self::QueryAstNode {
4 changes: 2 additions & 2 deletions diesel/src/pg/query_builder/copy/copy_from.rs
Original file line number Diff line number Diff line change
@@ -198,7 +198,7 @@ macro_rules! impl_copy_from_insertable_helper_for_values_clause {
T>
where
T: Table,
$($ST: Column<Table = T>,)*
$($ST: Column<Source = T>,)*
($($ST,)*): CopyTarget,
$($TT: ToSql<$T, Pg>,)*
{
@@ -229,7 +229,7 @@ macro_rules! impl_copy_from_insertable_helper_for_values_clause {
T>
where
T: Table,
$($ST: Column<Table = T>,)*
$($ST: Column<Source = T>,)*
($($ST,)*): CopyTarget,
$($TT: ToSql<$T, Pg>,)*
{
2 changes: 1 addition & 1 deletion diesel/src/pg/query_builder/copy/mod.rs
Original file line number Diff line number Diff line change
@@ -148,7 +148,7 @@ macro_rules! copy_target_for_columns {
$(
impl<T, $($ST,)*> CopyTarget for ($($ST,)*)
where
$($ST: Column<Table = T>,)*
$($ST: Column<Source = T>,)*
($(<$ST as Expression>::SqlType,)*): SqlType,
T: Table + StaticQueryFragment,
T::Component: QueryFragment<Pg>,
24 changes: 17 additions & 7 deletions diesel/src/pg/query_builder/only.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::expression::{Expression, ValidGrouping};
use crate::pg::Pg;
use crate::query_builder::{AsQuery, AstPass, FromClause, QueryFragment, QueryId, SelectStatement};
use crate::query_source::QuerySource;
use crate::query_source::{QuerySource, View};
use crate::result::QueryResult;
use crate::{JoinTo, SelectableExpression, Table};

@@ -74,22 +74,32 @@ where
<S as JoinTo<T>>::join_target(rhs)
}
}

impl<S> View for Only<S>
where
S: Table + Clone + AsQuery,

<S as Table>::PrimaryKey: SelectableExpression<Only<S>>,
<S as View>::AllColumns: SelectableExpression<Only<S>>,
<S as QuerySource>::DefaultSelection: ValidGrouping<()> + SelectableExpression<Only<S>>,
{
type AllColumns = <S as View>::AllColumns;
fn all_columns() -> Self::AllColumns {
S::all_columns()
}
}

impl<S> Table for Only<S>
where
S: Table + Clone + AsQuery,

<S as Table>::PrimaryKey: SelectableExpression<Only<S>>,
<S as Table>::AllColumns: SelectableExpression<Only<S>>,
<S as View>::AllColumns: SelectableExpression<Only<S>>,
<S as QuerySource>::DefaultSelection: ValidGrouping<()> + SelectableExpression<Only<S>>,
{
type PrimaryKey = <S as Table>::PrimaryKey;
type AllColumns = <S as Table>::AllColumns;

fn primary_key(&self) -> Self::PrimaryKey {
self.source.primary_key()
}

fn all_columns() -> Self::AllColumns {
S::all_columns()
}
}
25 changes: 19 additions & 6 deletions diesel/src/pg/query_builder/tablesample.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::expression::{Expression, ValidGrouping};
use crate::pg::Pg;
use crate::query_builder::{AsQuery, AstPass, FromClause, QueryFragment, QueryId, SelectStatement};
use crate::query_source::QuerySource;
use crate::query_source::{QuerySource, View};
use crate::result::QueryResult;
use crate::sql_types::{Double, SmallInt};
use crate::{JoinTo, SelectableExpression, Table};
@@ -147,26 +147,39 @@ where
}
}

impl<S, TSM> View for Tablesample<S, TSM>
where
S: Table + Clone + AsQuery,
TSM: TablesampleMethod,

<S as Table>::PrimaryKey: SelectableExpression<Tablesample<S, TSM>>,
<S as View>::AllColumns: SelectableExpression<Tablesample<S, TSM>>,
<S as QuerySource>::DefaultSelection:
ValidGrouping<()> + SelectableExpression<Tablesample<S, TSM>>,
{
type AllColumns = <S as View>::AllColumns;

fn all_columns() -> Self::AllColumns {
S::all_columns()
}
}

impl<S, TSM> Table for Tablesample<S, TSM>
where
S: Table + Clone + AsQuery,
TSM: TablesampleMethod,

<S as Table>::PrimaryKey: SelectableExpression<Tablesample<S, TSM>>,
<S as Table>::AllColumns: SelectableExpression<Tablesample<S, TSM>>,
<S as View>::AllColumns: SelectableExpression<Tablesample<S, TSM>>,
<S as QuerySource>::DefaultSelection:
ValidGrouping<()> + SelectableExpression<Tablesample<S, TSM>>,
{
type PrimaryKey = <S as Table>::PrimaryKey;
type AllColumns = <S as Table>::AllColumns;

fn primary_key(&self) -> Self::PrimaryKey {
self.source.primary_key()
}

fn all_columns() -> Self::AllColumns {
S::all_columns()
}
}

#[cfg(test)]
5 changes: 3 additions & 2 deletions diesel/src/query_builder/insert_statement/column_list.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::query_builder::*;
use crate::{query_builder::*, Table};
use crate::query_source::Column;

/// Represents the column list for use in an insert statement.
@@ -17,8 +17,9 @@ pub trait ColumnList {
impl<C> ColumnList for C
where
C: Column,
C::Source: Table
{
type Table = <C as Column>::Table;
type Table = <C as Column>::Source;

fn walk_ast<DB: Backend>(&self, mut out: AstPass<'_, '_, DB>) -> QueryResult<()> {
out.push_identifier(C::NAME)?;
9 changes: 5 additions & 4 deletions diesel/src/query_builder/insert_statement/mod.rs
Original file line number Diff line number Diff line change
@@ -322,12 +322,13 @@ impl<'a, T, Tab> UndecoratedInsertRecord<Tab> for &'a T where
{
}

impl<T, U> UndecoratedInsertRecord<T::Table> for ColumnInsertValue<T, U> where T: Column {}
impl<T, U> UndecoratedInsertRecord<T::Source> for ColumnInsertValue<T, U> where T: Column, T::Source: Table {}

impl<T, U> UndecoratedInsertRecord<T::Table>
impl<T, U> UndecoratedInsertRecord<T::Source>
for DefaultableColumnInsertValue<ColumnInsertValue<T, U>>
where
T: Column,
T::Source: Table
{
}

@@ -342,14 +343,14 @@ where

impl<T, Table> UndecoratedInsertRecord<Table> for Vec<T> where [T]: UndecoratedInsertRecord<Table> {}

impl<Lhs, Rhs> UndecoratedInsertRecord<Lhs::Table> for Eq<Lhs, Rhs> where Lhs: Column {}
impl<Lhs, Rhs> UndecoratedInsertRecord<Lhs::Source> for Eq<Lhs, Rhs> where Lhs: Column, Lhs::Source: Table {}

impl<Lhs, Rhs, Tab> UndecoratedInsertRecord<Tab> for Option<Eq<Lhs, Rhs>> where
Eq<Lhs, Rhs>: UndecoratedInsertRecord<Tab>
{
}

impl<Lhs, Rhs> UndecoratedInsertRecord<Lhs::Table> for Grouped<Eq<Lhs, Rhs>> where Lhs: Column {}
impl<Lhs, Rhs> UndecoratedInsertRecord<Lhs::Source> for Grouped<Eq<Lhs, Rhs>> where Lhs: Column, Lhs::Source: Table {}

impl<Lhs, Rhs, Tab> UndecoratedInsertRecord<Tab> for Option<Grouped<Eq<Lhs, Rhs>>> where
Eq<Lhs, Rhs>: UndecoratedInsertRecord<Tab>
3 changes: 2 additions & 1 deletion diesel/src/query_builder/update_statement/changeset.rs
Original file line number Diff line number Diff line change
@@ -120,8 +120,9 @@ where
impl<C> AssignmentTarget for C
where
C: Column,
C::Source: Table
{
type Table = C::Table;
type Table = C::Source;
type QueryAstNode = ColumnWrapperForUpdate<C>;

fn into_target(self) -> Self::QueryAstNode {
3 changes: 2 additions & 1 deletion diesel/src/query_builder/upsert/on_conflict_actions.rs
Original file line number Diff line number Diff line change
@@ -120,9 +120,10 @@ where
type SqlType = T::SqlType;
}

impl<T> AppearsOnTable<T::Table> for Excluded<T>
impl<T> AppearsOnTable<T::Source> for Excluded<T>
where
T: Column,
T::Source: Table,
Excluded<T>: Expression,
{
}
12 changes: 6 additions & 6 deletions diesel/src/query_builder/upsert/on_conflict_target.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::backend::sql_dialect;
use crate::expression::SqlLiteral;
use crate::query_builder::*;
use crate::{query_builder::*, Table};
use crate::query_source::Column;

#[doc(hidden)]
@@ -50,7 +50,7 @@ where
}
}

impl<T> OnConflictTarget<T::Table> for ConflictTarget<T> where T: Column {}
impl<T> OnConflictTarget<T::Source> for ConflictTarget<T> where T: Column, T::Source: Table {}

impl<DB, ST, SP> QueryFragment<DB, SP> for ConflictTarget<SqlLiteral<ST>>
where
@@ -81,7 +81,7 @@ where
}
}

impl<T> OnConflictTarget<T::Table> for ConflictTarget<(T,)> where T: Column {}
impl<T> OnConflictTarget<T::Source> for ConflictTarget<(T,)> where T: Column, T::Source: Table {}

macro_rules! on_conflict_tuples {
($(
@@ -94,7 +94,7 @@ macro_rules! on_conflict_tuples {
_DB: Backend<OnConflictClause = _SP>,
_SP: sql_dialect::on_conflict_clause::PgLikeOnConflictClause,
_T: Column,
$($T: Column<Table=_T::Table>,)*
$($T: Column<Source=_T::Source>,)*
{
fn walk_ast<'b>(&'b self, mut out: AstPass<'_, 'b, _DB>) -> QueryResult<()>
{
@@ -109,9 +109,9 @@ macro_rules! on_conflict_tuples {
}
}

impl<_T, $($T),*> OnConflictTarget<_T::Table> for ConflictTarget<(_T, $($T),*)> where
impl<_T, $($T),*> OnConflictTarget<_T::Source> for ConflictTarget<(_T, $($T),*)> where
_T: Column,
$($T: Column<Table=_T::Table>,)*
$($T: Column<Source=_T::Source>,)*
{
}
)*
14 changes: 7 additions & 7 deletions diesel/src/query_dsl/save_changes_dsl.rs
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ use crate::query_dsl::methods::{ExecuteDsl, FindDsl};
use crate::query_dsl::{LoadQuery, RunQueryDsl};
use crate::result::QueryResult;
#[cfg(any(feature = "sqlite", feature = "postgres", feature = "mysql"))]
use crate::Table;
use crate::query_source::View;

/// A trait defining how to update a record and fetch the updated entry
/// on a certain backend.
@@ -40,8 +40,8 @@ impl<'b, Changes, Output> UpdateAndFetchResults<Changes, Output> for PgConnectio
where
Changes: Copy + AsChangeset<Target = <Changes as HasTable>::Table> + IntoUpdateTarget,
Update<Changes, Changes>: LoadQuery<'b, PgConnection, Output>,
<Changes::Table as Table>::AllColumns: ValidGrouping<()>,
<<Changes::Table as Table>::AllColumns as ValidGrouping<()>>::IsAggregate:
<Changes::Table as View>::AllColumns: ValidGrouping<()>,
<<Changes::Table as View>::AllColumns as ValidGrouping<()>>::IsAggregate:
MixedAggregates<is_aggregate::No, Output = is_aggregate::No>,
{
fn update_and_fetch(&mut self, changeset: Changes) -> QueryResult<Output> {
@@ -60,8 +60,8 @@ where
Changes::Table: FindDsl<Changes::Id>,
Update<Changes, Changes>: ExecuteDsl<SqliteConnection>,
Find<Changes::Table, Changes::Id>: LoadQuery<'b, SqliteConnection, Output>,
<Changes::Table as Table>::AllColumns: ValidGrouping<()>,
<<Changes::Table as Table>::AllColumns as ValidGrouping<()>>::IsAggregate:
<Changes::Table as View>::AllColumns: ValidGrouping<()>,
<<Changes::Table as View>::AllColumns as ValidGrouping<()>>::IsAggregate:
MixedAggregates<is_aggregate::No, Output = is_aggregate::No>,
{
fn update_and_fetch(&mut self, changeset: Changes) -> QueryResult<Output> {
@@ -81,8 +81,8 @@ where
Changes::Table: FindDsl<Changes::Id>,
Update<Changes, Changes>: ExecuteDsl<MysqlConnection>,
Find<Changes::Table, Changes::Id>: LoadQuery<'b, MysqlConnection, Output>,
<Changes::Table as Table>::AllColumns: ValidGrouping<()>,
<<Changes::Table as Table>::AllColumns as ValidGrouping<()>>::IsAggregate:
<Changes::Table as View>::AllColumns: ValidGrouping<()>,
<<Changes::Table as View>::AllColumns as ValidGrouping<()>>::IsAggregate:
MixedAggregates<is_aggregate::No, Output = is_aggregate::No>,
{
fn update_and_fetch(&mut self, changeset: Changes) -> QueryResult<Output> {
18 changes: 9 additions & 9 deletions diesel/src/query_source/aliasing/aliased_field.rs
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ impl<S, C> QueryId for AliasedField<S, C>
where
S: AliasSource + 'static,
S::Target: 'static,
C: Column<Table = S::Target> + 'static + QueryId,
C: Column<Source = S::Target> + 'static + QueryId,
{
type QueryId = Self;
const HAS_STATIC_QUERY_ID: bool = <C as QueryId>::HAS_STATIC_QUERY_ID;
@@ -38,15 +38,15 @@ impl<QS, S, C> AppearsOnTable<QS> for AliasedField<S, C>
where
S: AliasSource,
QS: AppearsInFromClause<Alias<S>, Count = Once>,
C: Column<Table = S::Target>,
C: Column<Source = S::Target>,
{
}

impl<S, C, DB> QueryFragment<DB> for AliasedField<S, C>
where
S: AliasSource,
DB: Backend,
C: Column<Table = S::Target>,
C: Column<Source = S::Target>,
{
fn walk_ast<'b>(&'b self, mut pass: AstPass<'_, 'b, DB>) -> QueryResult<()> {
pass.push_identifier(S::NAME)?;
@@ -59,32 +59,32 @@ where
impl<S, C> Expression for AliasedField<S, C>
where
S: AliasSource,
C: Column<Table = S::Target> + Expression,
C: Column<Source = S::Target> + Expression,
{
type SqlType = C::SqlType;
}

impl<S, C> SelectableExpression<Alias<S>> for AliasedField<S, C>
where
S: AliasSource,
C: Column<Table = S::Target>,
C: Column<Source = S::Target>,
Self: AppearsOnTable<Alias<S>>,
{
}

impl<S, C> ValidGrouping<()> for AliasedField<S, C>
where
S: AliasSource,
C: Column<Table = S::Target>,
C: Column<Source = S::Target>,
{
type IsAggregate = is_aggregate::No;
}

impl<S, C1, C2> ValidGrouping<AliasedField<S, C1>> for AliasedField<S, C2>
where
S: AliasSource,
C1: Column<Table = S::Target>,
C2: Column<Table = S::Target>,
C1: Column<Source = S::Target>,
C2: Column<Source = S::Target>,
C2: ValidGrouping<C1, IsAggregate = is_aggregate::Yes>,
{
type IsAggregate = is_aggregate::Yes;
@@ -101,7 +101,7 @@ where
impl<S, C, T> EqAll<T> for AliasedField<S, C>
where
S: AliasSource,
C: Column<Table = S::Target>,
C: Column<Source = S::Target>,
Self: ExpressionMethods,
<Self as Expression>::SqlType: sql_types::SqlType,
T: AsExpression<<Self as Expression>::SqlType>,
4 changes: 4 additions & 0 deletions diesel/src/query_source/mod.rs
Original file line number Diff line number Diff line change
@@ -52,8 +52,12 @@ pub trait Column: Expression {
const NAME: &'static str;
}

/// A SQL database view. Types which implement this trait should have been
/// generated by the [`view!` macro](crate::view!).
pub trait View: QuerySource + AsQuery + Sized {
/// The type returned by `all_columns`
type AllColumns: SelectableExpression<Self> + ValidGrouping<()>;
/// Returns a tuple of all columns belonging to this table.
fn all_columns() -> Self::AllColumns;
}

7 changes: 4 additions & 3 deletions diesel_derives/src/multiconnection.rs
Original file line number Diff line number Diff line change
@@ -1195,7 +1195,7 @@ fn generate_querybuilder(connection_types: &[ConnectionVariant]) -> TokenStream
let ty = c.ty;
quote::quote! {
super::backend::MultiBackend::#ident(_) => {
<Self as diesel::insertable::InsertValues<<#ty as diesel::connection::Connection>::Backend, Col::Table>>::column_names(
<Self as diesel::insertable::InsertValues<<#ty as diesel::connection::Connection>::Backend, Col::Source>>::column_names(
&self,
out.cast_database(
super::bind_collector::MultiBindCollector::#lower_ident,
@@ -1214,7 +1214,7 @@ fn generate_querybuilder(connection_types: &[ConnectionVariant]) -> TokenStream
let insert_values_backend_bounds = connection_types.iter().map(|c| {
let ty = c.ty;
quote::quote! {
diesel::insertable::DefaultableColumnInsertValue<diesel::insertable::ColumnInsertValue<Col, Expr>>: diesel::insertable::InsertValues<<#ty as diesel::connection::Connection>::Backend, Col::Table>
diesel::insertable::DefaultableColumnInsertValue<diesel::insertable::ColumnInsertValue<Col, Expr>>: diesel::insertable::InsertValues<<#ty as diesel::connection::Connection>::Backend, Col::Source>
}
});

@@ -1405,10 +1405,11 @@ fn generate_querybuilder(connection_types: &[ConnectionVariant]) -> TokenStream
}
}

impl<Col, Expr> diesel::insertable::InsertValues<super::multi_connection_impl::backend::MultiBackend, Col::Table>
impl<Col, Expr> diesel::insertable::InsertValues<super::multi_connection_impl::backend::MultiBackend, Col::Source>
for diesel::insertable::DefaultableColumnInsertValue<diesel::insertable::ColumnInsertValue<Col, Expr>>
where
Col: diesel::prelude::Column,
Col::Source: diesel::Table,
Expr: diesel::prelude::Expression<SqlType = Col::SqlType>,
Expr: diesel::prelude::AppearsOnTable<diesel::internal::derives::multiconnection::NoFromClause>,
Self: diesel::query_builder::QueryFragment<super::multi_connection_impl::backend::MultiBackend>,
21 changes: 13 additions & 8 deletions diesel_derives/src/table.rs
Original file line number Diff line number Diff line change
@@ -253,9 +253,11 @@ pub(crate) fn expand(input: TableDecl) -> TokenStream {
/// Helper type for representing a boxed query from this table
pub type BoxedQuery<'a, DB, ST = SqlType> = diesel::internal::table_macro::BoxedSelectStatement<'a, ST, diesel::internal::table_macro::FromClause<table>, DB>;

use diesel::query_source::View;

impl diesel::QuerySource for table {
type FromClause = diesel::internal::table_macro::StaticQueryFragmentInstance<table>;
type DefaultSelection = <Self as diesel::Table>::AllColumns;
type DefaultSelection = <Self as diesel::query_source::View>::AllColumns;

fn from_clause(&self) -> Self::FromClause {
diesel::internal::table_macro::StaticQueryFragmentInstance::new()
@@ -287,17 +289,20 @@ pub(crate) fn expand(input: TableDecl) -> TokenStream {
}
}

impl diesel::query_source::View for table {
type AllColumns = (#(#column_names,)*);

fn all_columns() -> Self::AllColumns {
(#(#column_names,)*)
}
}

impl diesel::Table for table {
type PrimaryKey = #primary_key;
type AllColumns = (#(#column_names,)*);

fn primary_key(&self) -> Self::PrimaryKey {
#primary_key
}

fn all_columns() -> Self::AllColumns {
(#(#column_names,)*)
}
}

impl diesel::associations::HasTable for table {
@@ -349,7 +354,7 @@ pub(crate) fn expand(input: TableDecl) -> TokenStream {
impl<S, C> diesel::internal::table_macro::FieldAliasMapperAssociatedTypesDisjointnessTrick<table, S, C> for table
where
S: diesel::query_source::AliasSource<Target = table> + ::std::clone::Clone,
C: diesel::query_source::Column<Table = table>,
C: diesel::query_source::Column<Source = table>,
{
type Out = diesel::query_source::AliasedField<S, C>;

@@ -830,7 +835,7 @@ fn expand_column_def(column_def: &ColumnDef) -> TokenStream {
}

impl diesel::query_source::Column for #column_name {
type Table = super::table;
type Source = super::table;

const NAME: &'static str = #sql_name;
}
15 changes: 10 additions & 5 deletions diesel_dynamic_schema/src/table.rs
Original file line number Diff line number Diff line change
@@ -72,18 +72,23 @@ where
}
}

impl<T, U> diesel::Table for Table<T, U>
impl<T, U> diesel::query_source::View for Table<T, U>
where
Self: QuerySource + AsQuery,
{
type PrimaryKey = DummyExpression;
type AllColumns = DummyExpression;

fn primary_key(&self) -> Self::PrimaryKey {
fn all_columns() -> Self::AllColumns {
DummyExpression::new()
}
}

fn all_columns() -> Self::AllColumns {
impl<T, U> diesel::Table for Table<T, U>
where
Self: QuerySource + AsQuery,
{
type PrimaryKey = DummyExpression;

fn primary_key(&self) -> Self::PrimaryKey {
DummyExpression::new()
}
}