Skip to content

Commit

Permalink
feat: implement PartialEq for DynIden, SimpleExpr and related t…
Browse files Browse the repository at this point in the history
…ypes (#620)

* feat: implement `PartialEq` for `SimpleExpr` and its related types

* ship our own `SeaRc`; old `SeaRc` is being re-export as `RcOrArc`

* fixup

* fmt

* bad idea to add `any::Any` as the super trait of `Iden`

* experiment

* assert two `dyn Iden` are of same type by comparing the memory address of vtable

* fmt

* more tests
  • Loading branch information
billy1624 authored Apr 11, 2023
1 parent 8db47be commit a2ab409
Show file tree
Hide file tree
Showing 17 changed files with 134 additions and 51 deletions.
2 changes: 1 addition & 1 deletion src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,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)]
#[derive(Debug, Clone, PartialEq)]
pub enum SimpleExpr {
Column(ColumnRef),
Tuple(Vec<SimpleExpr>),
Expand Down
2 changes: 1 addition & 1 deletion src/extension/postgres/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::{expr::*, func::*};

/// Functions
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum PgFunction {
ToTsquery,
ToTsvector,
Expand Down
4 changes: 2 additions & 2 deletions src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{expr::*, types::*};
pub use crate::extension::postgres::{PgFunc, PgFunction};

/// Functions
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum Function {
Max,
Min,
Expand All @@ -29,7 +29,7 @@ pub enum Function {
}

/// Function call.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct FunctionCall {
pub(crate) func: Function,
pub(crate) args: Vec<SimpleExpr>,
Expand Down
4 changes: 2 additions & 2 deletions src/query/case.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::{Condition, IntoCondition, SimpleExpr};

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct CaseStatementCondition {
pub(crate) condition: Condition,
pub(crate) result: SimpleExpr,
}

#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone, Default, PartialEq)]
pub struct CaseStatement {
pub(crate) when: Vec<CaseStatementCondition>,
pub(crate) r#else: Option<SimpleExpr>,
Expand Down
8 changes: 4 additions & 4 deletions src/query/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub enum ConditionType {
}

/// Represents the value of an [`Condition::any`] or [`Condition::all`]: a set of disjunctive or conjunctive conditions.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct Condition {
pub(crate) negate: bool,
pub(crate) condition_type: ConditionType,
Expand All @@ -23,21 +23,21 @@ pub type Cond = Condition;
/// Represents anything that can be passed to an [`Condition::any`] or [`Condition::all`]'s [`Condition::add`] method.
///
/// The arguments are automatically converted to the right enum.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum ConditionExpression {
Condition(Condition),
SimpleExpr(SimpleExpr),
}

#[derive(Default, Debug, Clone)]
#[derive(Default, Debug, Clone, PartialEq)]
pub enum ConditionHolderContents {
#[default]
Empty,
Chain(Vec<LogicalChainOper>),
Condition(Condition),
}

#[derive(Default, Debug, Clone)]
#[derive(Default, Debug, Clone, PartialEq)]
pub struct ConditionHolder {
pub contents: ConditionHolderContents,
}
Expand Down
2 changes: 1 addition & 1 deletion src/query/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use inherent::inherent;
/// r#"DELETE FROM "glyph" WHERE "id" < 1 OR "id" > 10"#
/// );
/// ```
#[derive(Default, Debug, Clone)]
#[derive(Default, Debug, Clone, PartialEq)]
pub struct DeleteStatement {
pub(crate) table: Option<Box<TableRef>>,
pub(crate) r#where: ConditionHolder,
Expand Down
4 changes: 2 additions & 2 deletions src/query/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use inherent::inherent;
///
/// [`InsertValueSource`] is a node in the expression tree and can represent a raw value set
/// ('VALUES') or a select query.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum InsertValueSource {
Values(Vec<Vec<SimpleExpr>>),
Select(Box<SelectStatement>),
Expand Down Expand Up @@ -42,7 +42,7 @@ pub(crate) enum InsertValueSource {
/// r#"INSERT INTO "glyph" ("aspect", "image") VALUES (5.15, '12A'), (4.21, '123')"#
/// );
/// ```
#[derive(Debug, Default, Clone)]
#[derive(Debug, Default, Clone, PartialEq)]
pub struct InsertStatement {
pub(crate) replace: bool,
pub(crate) table: Option<Box<TableRef>>,
Expand Down
2 changes: 1 addition & 1 deletion src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub enum QueryStatement {
Delete(DeleteStatement),
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum SubQueryStatement {
SelectStatement(SelectStatement),
InsertStatement(InsertStatement),
Expand Down
8 changes: 4 additions & 4 deletions src/query/on_conflict.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{ConditionHolder, DynIden, IntoCondition, IntoIden, SimpleExpr};

#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone, Default, PartialEq)]
pub struct OnConflict {
pub(crate) target: Option<OnConflictTarget>,
pub(crate) target_where: ConditionHolder,
Expand All @@ -9,14 +9,14 @@ pub struct OnConflict {
}

/// Represents ON CONFLICT (upsert) targets
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum OnConflictTarget {
/// A list of columns with unique constraint
ConflictColumns(Vec<DynIden>),
}

/// Represents ON CONFLICT (upsert) actions
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum OnConflictAction {
/// Do nothing
DoNothing,
Expand All @@ -25,7 +25,7 @@ pub enum OnConflictAction {
}

/// Represents strategies to update column in ON CONFLICT (upsert) actions
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum OnConflictUpdate {
/// Update column value of existing row with inserting value
Column(DynIden),
Expand Down
2 changes: 1 addition & 1 deletion src/query/returning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{ColumnRef, IntoColumnRef, SimpleExpr};
/// * SQLite
/// - SQLite version >= 3.35.0
/// - **Note that sea-query won't try to enforce either of these constraints**
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq)]
pub enum ReturningClause {
All,
Columns(Vec<ColumnRef>),
Expand Down
12 changes: 6 additions & 6 deletions src/query/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use inherent::inherent;
/// r#"SELECT "character", "font"."name" FROM "character" LEFT JOIN "font" ON "character"."font_id" = "font"."id" WHERE "size_w" IN (3, 4) AND "character" LIKE 'A%'"#
/// );
/// ```
#[derive(Default, Debug, Clone)]
#[derive(Default, Debug, Clone, PartialEq)]
pub struct SelectStatement {
pub(crate) distinct: Option<SelectDistinct>,
pub(crate) selects: Vec<SelectExpr>,
Expand All @@ -57,7 +57,7 @@ pub struct SelectStatement {
}

/// List of distinct keywords that can be used in select statement
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum SelectDistinct {
All,
Distinct,
Expand All @@ -66,7 +66,7 @@ pub enum SelectDistinct {
}

/// Window type in [`SelectExpr`]
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum WindowSelectType {
/// Name in [`SelectStatement`]
Name(DynIden),
Expand All @@ -75,15 +75,15 @@ pub enum WindowSelectType {
}

/// Select expression used in select statement
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct SelectExpr {
pub expr: SimpleExpr,
pub alias: Option<DynIden>,
pub window: Option<WindowSelectType>,
}

/// Join expression used in select statement
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct JoinExpr {
pub join: JoinType,
pub table: Box<TableRef>,
Expand All @@ -109,7 +109,7 @@ pub enum LockBehavior {
SkipLocked,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct LockClause {
pub(crate) r#type: LockType,
pub(crate) tables: Vec<TableRef>,
Expand Down
2 changes: 1 addition & 1 deletion src/query/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use inherent::inherent;
/// r#"UPDATE "glyph" SET "aspect" = 1.23, "image" = '123' WHERE "id" = 1"#
/// );
/// ```
#[derive(Default, Debug, Clone)]
#[derive(Default, Debug, Clone, PartialEq)]
pub struct UpdateStatement {
pub(crate) table: Option<Box<TableRef>>,
pub(crate) values: Vec<(DynIden, Box<SimpleExpr>)>,
Expand Down
8 changes: 4 additions & 4 deletions src/query/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub trait OverStatement {
}

/// frame_start or frame_end clause
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum Frame {
UnboundedPreceding,
Preceding(u32),
Expand All @@ -50,14 +50,14 @@ pub enum Frame {
}

/// Frame type
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum FrameType {
Range,
Rows,
}

/// Frame clause
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct FrameClause {
pub(crate) r#type: FrameType,
pub(crate) start: Frame,
Expand All @@ -71,7 +71,7 @@ pub struct FrameClause {
/// 1. <https://dev.mysql.com/doc/refman/8.0/en/window-function-descriptions.html>
/// 2. <https://www.sqlite.org/windowfunctions.html>
/// 3. <https://www.postgresql.org/docs/current/tutorial-window.html>
#[derive(Default, Debug, Clone)]
#[derive(Default, Debug, Clone, PartialEq)]
pub struct WindowStatement {
pub(crate) partition_by: Vec<SimpleExpr>,
pub(crate) order_by: Vec<OrderExpr>,
Expand Down
12 changes: 6 additions & 6 deletions src/query/with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use std::ops::Deref;
/// DELETE FROM table WHERE table.a = cte_name.a)
///
/// It is mandatory to set the [Self::table_name] and the [Self::query].
#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone, Default, PartialEq)]
pub struct CommonTableExpression {
pub(crate) table_name: Option<DynIden>,
pub(crate) cols: Vec<DynIden>,
Expand Down Expand Up @@ -193,7 +193,7 @@ impl CommonTableExpression {

/// For recursive [WithQuery] [WithClause]s the traversing order can be specified in some databases
/// that support this functionality.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub enum SearchOrder {
/// Breadth first traversal during the execution of the recursive query.
BREADTH,
Expand All @@ -211,7 +211,7 @@ pub enum SearchOrder {
///
/// Setting [Self::order] and [Self::expr] is mandatory. The [SelectExpr] used must specify an alias
/// which will be the name that you can use to order the result of the [CommonTableExpression].
#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Search {
pub(crate) order: Option<SearchOrder>,
pub(crate) expr: Option<SelectExpr>,
Expand Down Expand Up @@ -269,7 +269,7 @@ impl Search {
/// A query can have both SEARCH and CYCLE clauses.
///
/// Setting [Self::set], [Self::expr] and [Self::using] is mandatory.
#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Cycle {
pub(crate) expr: Option<SimpleExpr>,
pub(crate) set_as: Option<DynIden>,
Expand Down Expand Up @@ -433,7 +433,7 @@ impl Cycle {
/// r#"WITH RECURSIVE "cte_traversal" ("id", "depth", "next", "value") AS (SELECT "id", 1, "next", "value" FROM "table" UNION ALL SELECT "id", "depth" + 1, "next", "value" FROM "table" INNER JOIN "cte_traversal" ON "cte_traversal"."next" = "table"."id") SELECT * FROM "cte_traversal""#
/// );
/// ```
#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone, Default, PartialEq)]
pub struct WithClause {
pub(crate) recursive: bool,
pub(crate) search: Option<Search>,
Expand Down Expand Up @@ -533,7 +533,7 @@ impl WithClause {
/// DELETE FROM table WHERE table.a = cte_name.a)
///
/// It is mandatory to set the [Self::cte] and the [Self::query].
#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone, Default, PartialEq)]
pub struct WithQuery {
pub(crate) with_clause: WithClause,
pub(crate) query: Option<Box<SubQueryStatement>>,
Expand Down
4 changes: 2 additions & 2 deletions src/table/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub enum ColumnType {
name: DynIden,
variants: Vec<DynIden>,
},
Array(SeaRc<ColumnType>),
Array(RcOrArc<ColumnType>),
Cidr,
Inet,
MacAddr,
Expand Down Expand Up @@ -555,7 +555,7 @@ impl ColumnDef {
/// Set column type as an array with a specified element type.
/// This is only supported on Postgres.
pub fn array(&mut self, elem_type: ColumnType) -> &mut Self {
self.types = Some(ColumnType::Array(SeaRc::new(elem_type)));
self.types = Some(ColumnType::Array(RcOrArc::new(elem_type)));
self
}

Expand Down
Loading

0 comments on commit a2ab409

Please sign in to comment.