diff --git a/src/backend/foreign_key_builder.rs b/src/backend/foreign_key_builder.rs index f0aaedd30..ae7bc9883 100644 --- a/src/backend/foreign_key_builder.rs +++ b/src/backend/foreign_key_builder.rs @@ -1,6 +1,7 @@ use crate::*; #[derive(Debug, PartialEq, Eq)] +#[non_exhaustive] pub enum Mode { Creation, Alter, diff --git a/src/expr.rs b/src/expr.rs index a765fc4e8..c88ca8c8c 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -20,6 +20,7 @@ pub struct Expr { /// [`SimpleExpr`] is a node in the expression tree and can represent identifiers, function calls, /// various operators and sub-queries. #[derive(Debug, Clone, PartialEq)] +#[non_exhaustive] pub enum SimpleExpr { Column(ColumnRef), Tuple(Vec), diff --git a/src/extension/mysql/column.rs b/src/extension/mysql/column.rs index 232f8a989..87337419e 100644 --- a/src/extension/mysql/column.rs +++ b/src/extension/mysql/column.rs @@ -1,6 +1,7 @@ use crate::Iden; #[derive(Debug, Copy, Clone)] +#[non_exhaustive] pub enum MySqlType { TinyBlob, MediumBlob, diff --git a/src/extension/mysql/index.rs b/src/extension/mysql/index.rs index c38ff073d..4bec5830f 100644 --- a/src/extension/mysql/index.rs +++ b/src/extension/mysql/index.rs @@ -10,6 +10,7 @@ pub struct IndexHint { } #[derive(Debug, Clone, Copy, PartialEq)] +#[non_exhaustive] pub enum IndexHintType { Use, Ignore, @@ -17,6 +18,7 @@ pub enum IndexHintType { } #[derive(Debug, Clone, Copy, PartialEq)] +#[non_exhaustive] pub enum IndexHintScope { Join, OrderBy, diff --git a/src/extension/postgres/func.rs b/src/extension/postgres/func.rs index 0780c0520..d79cdb22d 100644 --- a/src/extension/postgres/func.rs +++ b/src/extension/postgres/func.rs @@ -8,6 +8,7 @@ use crate::{expr::*, func::*, PgDateTruncUnit}; /// /// If something is not supported, you can use [`Function::Custom`]. #[derive(Debug, Clone, PartialEq)] +#[non_exhaustive] pub enum PgFunction { ToTsquery, ToTsvector, diff --git a/src/extension/postgres/mod.rs b/src/extension/postgres/mod.rs index 9b1660ca6..4ba07d725 100644 --- a/src/extension/postgres/mod.rs +++ b/src/extension/postgres/mod.rs @@ -19,6 +19,7 @@ pub(crate) mod types; /// /// For all supported operators (including the standard ones), see [`BinOper`]. #[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[non_exhaustive] pub enum PgBinOper { ILike, NotILike, diff --git a/src/extension/postgres/select.rs b/src/extension/postgres/select.rs index 080156fca..f0c3e8758 100644 --- a/src/extension/postgres/select.rs +++ b/src/extension/postgres/select.rs @@ -8,6 +8,7 @@ pub struct TableSample { } #[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[non_exhaustive] pub enum SampleMethod { BERNOULLI, SYSTEM, diff --git a/src/extension/postgres/types.rs b/src/extension/postgres/types.rs index 1b8bed7a4..5334a24b3 100644 --- a/src/extension/postgres/types.rs +++ b/src/extension/postgres/types.rs @@ -5,6 +5,7 @@ use crate::{prepare::*, types::*, QueryBuilder, QuotedBuilder}; pub struct Type; #[derive(Clone, Debug)] +#[non_exhaustive] pub enum TypeRef { Type(DynIden), SchemaType(DynIden, DynIden), @@ -59,6 +60,7 @@ pub struct TypeCreateStatement { } #[derive(Debug, Clone)] +#[non_exhaustive] pub enum TypeAs { // Composite, Enum, @@ -81,12 +83,14 @@ pub struct TypeAlterStatement { } #[derive(Debug, Clone)] +#[non_exhaustive] pub enum TypeDropOpt { Cascade, Restrict, } #[derive(Debug, Clone)] +#[non_exhaustive] pub enum TypeAlterOpt { Add { value: DynIden, @@ -98,6 +102,7 @@ pub enum TypeAlterOpt { } #[derive(Debug, Clone)] +#[non_exhaustive] pub enum TypeAlterAddOpt { Before(DynIden), After(DynIden), diff --git a/src/extension/sqlite/mod.rs b/src/extension/sqlite/mod.rs index 22bcbcb5b..d8f3d5cd5 100644 --- a/src/extension/sqlite/mod.rs +++ b/src/extension/sqlite/mod.rs @@ -8,6 +8,7 @@ mod expr; /// /// For all supported operators (including the standard ones), see [`BinOper`]. #[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[non_exhaustive] pub enum SqliteBinOper { /// `GLOB` Glob, diff --git a/src/foreign_key/common.rs b/src/foreign_key/common.rs index c93f15e5e..597fd2b03 100644 --- a/src/foreign_key/common.rs +++ b/src/foreign_key/common.rs @@ -14,6 +14,7 @@ pub struct TableForeignKey { /// Foreign key on update & on delete actions #[derive(Debug, Clone, Copy)] +#[non_exhaustive] pub enum ForeignKeyAction { Restrict, Cascade, @@ -22,6 +23,20 @@ pub enum ForeignKeyAction { SetDefault, } +impl ForeignKeyAction { + #[doc(hidden)] + /// Return the PascalCase name of the action + pub fn variant_name(&self) -> &'static str { + match self { + Self::Restrict => "Restrict", + Self::Cascade => "Cascade", + Self::SetNull => "SetNull", + Self::NoAction => "NoAction", + Self::SetDefault => "SetDefault", + } + } +} + impl TableForeignKey { /// Construct a new foreign key pub fn new() -> Self { diff --git a/src/foreign_key/mod.rs b/src/foreign_key/mod.rs index 38812809b..918e09dd3 100644 --- a/src/foreign_key/mod.rs +++ b/src/foreign_key/mod.rs @@ -19,6 +19,7 @@ pub struct ForeignKey; /// All available types of foreign key statement #[derive(Debug, Clone)] +#[non_exhaustive] pub enum ForeignKeyStatement { Create(ForeignKeyCreateStatement), Drop(ForeignKeyDropStatement), diff --git a/src/func.rs b/src/func.rs index f98fbb24a..ebca19398 100644 --- a/src/func.rs +++ b/src/func.rs @@ -9,6 +9,7 @@ pub use crate::extension::postgres::{PgFunc, PgFunction}; /// /// If something is not supported here, you can use [`Function::Custom`]. #[derive(Debug, Clone, PartialEq)] +#[non_exhaustive] pub enum Function { Max, Min, diff --git a/src/index/common.rs b/src/index/common.rs index e6597a49d..1281f806d 100644 --- a/src/index/common.rs +++ b/src/index/common.rs @@ -9,6 +9,7 @@ pub struct TableIndex { } #[derive(Debug, Clone)] +#[non_exhaustive] pub enum IndexColumn { TableColumn(IndexColumnTableColumn), Expr(IndexColumnExpr), @@ -37,6 +38,7 @@ impl IndexColumn { } #[derive(Debug, Clone)] +#[non_exhaustive] pub enum IndexOrder { Asc, Desc, diff --git a/src/index/create.rs b/src/index/create.rs index 1d58209ed..579ac6fc2 100644 --- a/src/index/create.rs +++ b/src/index/create.rs @@ -222,6 +222,7 @@ pub struct IndexCreateStatement { /// Specification of a table index #[derive(Debug, Clone)] +#[non_exhaustive] pub enum IndexType { BTree, FullText, diff --git a/src/index/mod.rs b/src/index/mod.rs index 87646bb6c..18d28b229 100644 --- a/src/index/mod.rs +++ b/src/index/mod.rs @@ -19,6 +19,7 @@ pub struct Index; /// All available types of index statement #[derive(Debug, Clone)] +#[non_exhaustive] pub enum IndexStatement { Create(IndexCreateStatement), Drop(IndexDropStatement), diff --git a/src/query/select.rs b/src/query/select.rs index 400509cf0..7b3de89d3 100644 --- a/src/query/select.rs +++ b/src/query/select.rs @@ -63,6 +63,7 @@ pub struct SelectStatement { /// List of distinct keywords that can be used in select statement #[derive(Debug, Clone, PartialEq)] +#[non_exhaustive] pub enum SelectDistinct { All, Distinct, @@ -72,6 +73,7 @@ pub enum SelectDistinct { /// Window type in [`SelectExpr`] #[derive(Debug, Clone, PartialEq)] +#[non_exhaustive] pub enum WindowSelectType { /// Name in [`SelectStatement`] Name(DynIden), @@ -98,6 +100,7 @@ pub struct JoinExpr { /// List of lock types that can be used in select statement #[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[non_exhaustive] pub enum LockType { /// Exclusive lock Update, @@ -109,6 +112,7 @@ pub enum LockType { /// List of lock behavior can be used in select statement #[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[non_exhaustive] pub enum LockBehavior { Nowait, SkipLocked, @@ -123,6 +127,7 @@ pub struct LockClause { /// List of union types that can be used in union clause #[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[non_exhaustive] pub enum UnionType { Intersect, Distinct, diff --git a/src/query/with.rs b/src/query/with.rs index 8fc4d13fd..9226db276 100644 --- a/src/query/with.rs +++ b/src/query/with.rs @@ -187,6 +187,7 @@ impl CommonTableExpression { /// For recursive [WithQuery] [WithClause]s the traversing order can be specified in some databases /// that support this functionality. #[derive(Debug, Clone, PartialEq)] +#[non_exhaustive] pub enum SearchOrder { /// Breadth first traversal during the execution of the recursive query. BREADTH, diff --git a/src/schema.rs b/src/schema.rs index 7e03c455f..d8c328f7c 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -3,6 +3,7 @@ use crate::{backend::SchemaBuilder, ForeignKeyStatement, IndexStatement, TableStatement}; #[derive(Debug, Clone)] +#[non_exhaustive] pub enum SchemaStatement { TableStatement(TableStatement), IndexStatement(IndexStatement), diff --git a/src/table/alter.rs b/src/table/alter.rs index 58915fb2f..4a7ce540c 100644 --- a/src/table/alter.rs +++ b/src/table/alter.rs @@ -44,6 +44,7 @@ pub struct AddColumnOption { /// All available table alter options #[derive(Debug, Clone)] +#[non_exhaustive] pub enum TableAlterOption { AddColumn(AddColumnOption), ModifyColumn(ColumnDef), diff --git a/src/table/column.rs b/src/table/column.rs index 97677c524..041a44f16 100644 --- a/src/table/column.rs +++ b/src/table/column.rs @@ -171,6 +171,7 @@ impl ColumnType { /// All column specification keywords #[derive(Debug, Clone)] +#[non_exhaustive] pub enum ColumnSpec { Null, NotNull, @@ -187,6 +188,7 @@ pub enum ColumnSpec { // All interval fields #[derive(Debug, Clone, Eq, PartialEq)] +#[non_exhaustive] pub enum PgInterval { Year, Month, @@ -205,6 +207,7 @@ pub enum PgInterval { // All possible inputs to DATE_TRUNC (https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC) #[derive(Debug, Clone, Eq, PartialEq)] +#[non_exhaustive] pub enum PgDateTruncUnit { Microseconds, Milliseconds, diff --git a/src/table/create.rs b/src/table/create.rs index 7a2f51115..b711e3de4 100644 --- a/src/table/create.rs +++ b/src/table/create.rs @@ -96,6 +96,7 @@ pub struct TableCreateStatement { /// All available table options #[derive(Debug, Clone)] +#[non_exhaustive] pub enum TableOpt { Engine(String), Collate(String), @@ -104,6 +105,7 @@ pub enum TableOpt { /// All available table partition options #[derive(Debug, Clone)] +#[non_exhaustive] pub enum TablePartition {} impl TableCreateStatement { diff --git a/src/table/drop.rs b/src/table/drop.rs index 42421a395..e4274e02d 100644 --- a/src/table/drop.rs +++ b/src/table/drop.rs @@ -36,6 +36,7 @@ pub struct TableDropStatement { /// All available table drop options #[derive(Debug, Clone)] +#[non_exhaustive] pub enum TableDropOpt { Restrict, Cascade, diff --git a/src/table/mod.rs b/src/table/mod.rs index fa2b0d8b1..5e51413cb 100644 --- a/src/table/mod.rs +++ b/src/table/mod.rs @@ -30,6 +30,7 @@ pub struct Table; /// All available types of table statement #[derive(Debug, Clone)] +#[non_exhaustive] pub enum TableStatement { Create(TableCreateStatement), Alter(TableAlterStatement), diff --git a/src/token.rs b/src/token.rs index 1d0ee9714..955ffdfc1 100644 --- a/src/token.rs +++ b/src/token.rs @@ -10,6 +10,7 @@ pub struct Tokenizer { } #[derive(Debug, PartialEq, Eq)] +#[non_exhaustive] pub enum Token { Quoted(String), Unquoted(String), diff --git a/src/types.rs b/src/types.rs index 9fcb0b5ff..6cec85d3c 100644 --- a/src/types.rs +++ b/src/types.rs @@ -131,6 +131,7 @@ impl fmt::Debug for dyn Iden { /// Column references #[derive(Debug, Clone, PartialEq)] +#[non_exhaustive] pub enum ColumnRef { Column(DynIden), TableColumn(DynIden, DynIden), @@ -139,6 +140,20 @@ pub enum ColumnRef { TableAsterisk(DynIden), } +impl ColumnRef { + #[doc(hidden)] + /// Returns the column name if it's not an asterisk. + pub fn column(&self) -> Option<&DynIden> { + match self { + ColumnRef::Column(column) => Some(column), + ColumnRef::TableColumn(_, column) => Some(column), + ColumnRef::SchemaTableColumn(_, _, column) => Some(column), + ColumnRef::Asterisk => None, + ColumnRef::TableAsterisk(_) => None, + } + } +} + pub trait IntoColumnRef { fn into_column_ref(self) -> ColumnRef; } @@ -146,6 +161,7 @@ pub trait IntoColumnRef { /// Table references #[allow(clippy::large_enum_variant)] #[derive(Debug, Clone, PartialEq)] +#[non_exhaustive] pub enum TableRef { /// Table identifier without any schema / database prefix Table(DynIden), @@ -167,12 +183,45 @@ pub enum TableRef { FunctionCall(FunctionCall, DynIden), } +impl TableRef { + #[doc(hidden)] + pub fn table(&self) -> &DynIden { + match self { + TableRef::Table(tbl) + | TableRef::SchemaTable(_, tbl) + | TableRef::DatabaseSchemaTable(_, _, tbl) + | TableRef::TableAlias(tbl, _) + | TableRef::SchemaTableAlias(_, tbl, _) + | TableRef::DatabaseSchemaTableAlias(_, _, tbl, _) + | TableRef::SubQuery(_, tbl) + | TableRef::ValuesList(_, tbl) + | TableRef::FunctionCall(_, tbl) => tbl, + } + } + + #[doc(hidden)] + pub fn table_alias(&self) -> Option<&DynIden> { + match self { + TableRef::Table(_) + | TableRef::SchemaTable(_, _) + | TableRef::DatabaseSchemaTable(_, _, _) + | TableRef::SubQuery(_, _) + | TableRef::ValuesList(_, _) => None, + TableRef::TableAlias(_, alias) + | TableRef::SchemaTableAlias(_, _, alias) + | TableRef::DatabaseSchemaTableAlias(_, _, _, alias) + | TableRef::FunctionCall(_, alias) => Some(alias), + } + } +} + pub trait IntoTableRef { fn into_table_ref(self) -> TableRef; } /// Unary operators. #[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[non_exhaustive] pub enum UnOper { Not, } @@ -181,6 +230,7 @@ pub enum UnOper { /// /// If something is not supported here, you can use [`BinOper::Custom`]. #[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[non_exhaustive] pub enum BinOper { And, Or, @@ -251,6 +301,7 @@ pub struct OrderExpr { /// Join on types #[derive(Debug, Clone, PartialEq)] +#[non_exhaustive] pub enum JoinOn { Condition(Box), Columns(Vec), @@ -258,6 +309,7 @@ pub enum JoinOn { /// Ordering options #[derive(Debug, Clone, PartialEq)] +#[non_exhaustive] pub enum Order { Asc, Desc, @@ -334,6 +386,7 @@ pub struct Asterisk; /// /// If something is not supported here, you can use [`Keyword::Custom`]. #[derive(Debug, Clone, PartialEq)] +#[non_exhaustive] pub enum Keyword { Null, CurrentDate, @@ -355,6 +408,7 @@ pub trait IntoLikeExpr { /// SubQuery operators #[derive(Debug, Copy, Clone, PartialEq)] +#[non_exhaustive] pub enum SubQueryOper { Exists, Any, diff --git a/src/value.rs b/src/value.rs index 5e7c6c3ae..0b5722b3a 100644 --- a/src/value.rs +++ b/src/value.rs @@ -35,6 +35,7 @@ use crate::{ColumnType, CommonSqlQueryBuilder, QueryBuilder, StringLen}; /// [`Value`] types variant for Postgres array #[derive(Clone, Debug, Eq, PartialEq, Hash)] +#[non_exhaustive] pub enum ArrayType { Bool, TinyInt, @@ -124,6 +125,7 @@ pub enum ArrayType { /// implementation of NaN != NaN. #[derive(Clone, Debug)] #[cfg_attr(not(feature = "hashable-value"), derive(PartialEq))] +#[non_exhaustive] pub enum Value { Bool(Option), TinyInt(Option), @@ -259,6 +261,7 @@ pub struct Values(pub Vec); #[derive(Clone, Debug, PartialEq)] #[cfg_attr(feature = "hashable-value", derive(Hash, Eq))] +#[non_exhaustive] pub enum ValueTuple { One(Value), Two(Value, Value),