diesel/query_source/mod.rs
1//! Types related to describing schema, and interactions between tables.
2//!
3//! Most traits in this module are derived or generated by [`table!`].
4//!
5//! [`table!`]: ../macro.table.html
6#[doc(hidden)]
7pub mod joins;
8mod peano_numbers;
9
10use expression::{Expression, NonAggregate, SelectableExpression};
11use query_builder::*;
12
13pub use self::joins::JoinTo;
14pub use self::peano_numbers::*;
15
16#[cfg(feature = "with-deprecated")]
17#[deprecated(since = "1.1.0", note = "Use `deserialize::Queryable` instead")]
18pub use deserialize::Queryable;
19#[cfg(feature = "with-deprecated")]
20#[deprecated(since = "1.1.0", note = "Use `deserialize::QueryableByName` instead")]
21pub use deserialize::QueryableByName;
22
23/// Represents a type which can appear in the `FROM` clause. Apps should not
24/// need to concern themselves with this trait.
25///
26/// Types which implement this trait include:
27/// - Tables generated by the `table!` macro
28/// - Internal structs which represent joins
29/// - A select statement which has had no query builder methods called on it,
30/// other than those which can affect the from clause.
31pub trait QuerySource {
32 /// The type returned by `from_clause`
33 type FromClause;
34 /// The type returned by `default_selection`
35 type DefaultSelection: SelectableExpression<Self>;
36
37 /// The actual `FROM` clause of this type. This is typically only called in
38 /// `QueryFragment` implementations.
39 fn from_clause(&self) -> Self::FromClause;
40 /// The default select clause of this type, which should be used if no
41 /// select clause was explicitly specified. This should always be a tuple of
42 /// all the desired columns, not `star`
43 fn default_selection(&self) -> Self::DefaultSelection;
44}
45
46/// A column on a database table. Types which implement this trait should have
47/// been generated by the [`table!` macro](../macro.table.html).
48pub trait Column: Expression {
49 /// The table which this column belongs to
50 type Table: Table;
51
52 /// The name of this column
53 const NAME: &'static str;
54}
55
56/// A SQL database table. Types which implement this trait should have been
57/// generated by the [`table!` macro](../macro.table.html).
58pub trait Table: QuerySource + AsQuery + Sized {
59 /// The type returned by `primary_key`
60 type PrimaryKey: SelectableExpression<Self> + NonAggregate;
61 /// The type returned by `all_columns`
62 type AllColumns: SelectableExpression<Self> + NonAggregate;
63
64 /// Returns the primary key of this table.
65 ///
66 /// If the table has a composite primary key, this will be a tuple.
67 fn primary_key(&self) -> Self::PrimaryKey;
68 /// Returns a tuple of all columns belonging to this table.
69 fn all_columns() -> Self::AllColumns;
70}
71
72/// Determines how many times `Self` appears in `QS`
73///
74/// This trait is primarily used to determine whether or not a column is
75/// selectable from a given from clause. A column can be selected if its table
76/// appears in the from clause *exactly once*.
77///
78/// We do not allow the same table to appear in a query multiple times in any
79/// context where referencing that table would be ambiguous (depending on the
80/// context and backend being used, this may or may not be something that would
81/// otherwise result in a runtime error).
82pub trait AppearsInFromClause<QS> {
83 /// How many times does `Self` appear in `QS`?
84 type Count;
85}