Skip to content

Commit b9810a9

Browse files
committed
Fix
1 parent c16df9c commit b9810a9

File tree

9 files changed

+73
-46
lines changed

9 files changed

+73
-46
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ pub struct SeaRc; // new
2828

2929
### Breaking Changes
3030

31+
* Replace `ColumnSpec::Check(Expr)` with `ColumnSpec::Check(Check)` to support named check constraints
32+
3133
* Removed inherent `SimpleExpr` methods that duplicate `ExprTrait`. If you encounter the following error, please add `use sea_query::ExprTrait` in scope https://github.com/SeaQL/sea-query/pull/890
3234
```rust
3335
error[E0599]: no method named `like` found for enum `sea_query::Expr` in the current scope
@@ -125,6 +127,7 @@ impl Iden for Glyph {
125127
If you had custom implementations in your own code, some may no longer compile
126128
and may need to be deleted.
127129

130+
128131
### Upgrades
129132

130133
* Upgraded to Rust Edition 2024 https://github.com/SeaQL/sea-query/pull/885

src/backend/table_builder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,13 @@ pub trait TableBuilder:
196196

197197
/// Translate the check constraint into SQL statement
198198
fn prepare_check_constraint(&self, check: &Check, sql: &mut dyn SqlWriter) {
199-
if let Check::Named(name, _) = &check {
199+
if let Some(name) = &check.name {
200200
write!(sql, "CONSTRAINT ",).unwrap();
201-
self.prepare_iden(&name.into_iden(), sql);
201+
self.prepare_iden(name, sql);
202202
write!(sql, " ",).unwrap();
203203
}
204204
write!(sql, "CHECK (").unwrap();
205-
QueryBuilder::prepare_simple_expr(self, check.expr(), sql);
205+
QueryBuilder::prepare_simple_expr(self, &check.expr, sql);
206206
write!(sql, ")").unwrap();
207207
}
208208

src/table/column.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,7 @@ impl ColumnDef {
687687
///
688688
/// ```
689689
/// use sea_query::{tests_cfg::*, *};
690+
///
690691
/// assert_eq!(
691692
/// Table::create()
692693
/// .table(Glyph::Table)
@@ -699,39 +700,25 @@ impl ColumnDef {
699700
/// .to_string(MysqlQueryBuilder),
700701
/// r#"CREATE TABLE `glyph` ( `id` int NOT NULL CHECK (`id` > 10) )"#,
701702
/// );
702-
/// ```
703-
pub fn check<T>(&mut self, value: T) -> &mut Self
704-
where
705-
T: Into<Expr>,
706-
{
707-
self.spec
708-
.push(ColumnSpec::Check(Check::Unnamed(value.into())));
709-
self
710-
}
711-
712-
/// Set named check constraint
713703
///
714-
/// ```
715-
/// use sea_query::{tests_cfg::*, *};
716704
/// assert_eq!(
717705
/// Table::create()
718706
/// .table(Glyph::Table)
719707
/// .col(
720708
/// ColumnDef::new(Glyph::Id)
721709
/// .integer()
722710
/// .not_null()
723-
/// .check_with_name("positive_id", Expr::col(Glyph::Id).gt(10))
711+
/// .check(("positive_id", Expr::col(Glyph::Id).gt(10)))
724712
/// )
725713
/// .to_string(MysqlQueryBuilder),
726714
/// r#"CREATE TABLE `glyph` ( `id` int NOT NULL CONSTRAINT `positive_id` CHECK (`id` > 10) )"#,
727715
/// );
728716
/// ```
729-
pub fn check_with_name<T>(&mut self, name: &'static str, value: T) -> &mut Self
717+
pub fn check<T>(&mut self, check: T) -> &mut Self
730718
where
731-
T: Into<Expr>,
719+
T: Into<Check>,
732720
{
733-
self.spec
734-
.push(ColumnSpec::Check(Check::Named(name, value.into())));
721+
self.spec.push(ColumnSpec::Check(check.into()));
735722
self
736723
}
737724

src/table/constraint.rs

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,49 @@
1-
use crate::Expr;
1+
use crate::{DynIden, Expr};
22

33
#[derive(Debug, Clone)]
4-
pub enum Check {
5-
Named(&'static str, Expr),
6-
Unnamed(Expr),
4+
pub struct Check {
5+
pub(crate) name: Option<DynIden>,
6+
pub(crate) expr: Expr,
77
}
88

99
impl Check {
10-
pub fn expr(&self) -> &Expr {
11-
match self {
12-
Check::Named(_, expr) => expr,
13-
Check::Unnamed(expr) => expr,
10+
pub fn named<N, E>(name: N, expr: E) -> Self
11+
where
12+
N: Into<DynIden>,
13+
E: Into<Expr>,
14+
{
15+
Self {
16+
name: Some(name.into()),
17+
expr: expr.into(),
1418
}
1519
}
20+
21+
pub fn unnamed<E>(expr: E) -> Self
22+
where
23+
E: Into<Expr>,
24+
{
25+
Self {
26+
name: None,
27+
expr: expr.into(),
28+
}
29+
}
30+
}
31+
32+
impl<E> From<E> for Check
33+
where
34+
E: Into<Expr>,
35+
{
36+
fn from(expr: E) -> Self {
37+
Self::unnamed(expr)
38+
}
39+
}
40+
41+
impl<I, E> From<(I, E)> for Check
42+
where
43+
I: Into<DynIden>,
44+
E: Into<Expr>,
45+
{
46+
fn from((name, expr): (I, E)) -> Self {
47+
Self::named(name, expr)
48+
}
1649
}

src/table/create.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use inherent::inherent;
22

33
use crate::{
4-
ColumnDef, Expr, IntoColumnDef, SchemaStatementBuilder, backend::SchemaBuilder, foreign_key::*,
4+
ColumnDef, IntoColumnDef, SchemaStatementBuilder, backend::SchemaBuilder, foreign_key::*,
55
index::*, table::constraint::Check, types::*,
66
};
77

@@ -146,13 +146,11 @@ impl TableCreateStatement {
146146
self
147147
}
148148

149-
pub fn check(&mut self, value: Expr) -> &mut Self {
150-
self.check.push(Check::Unnamed(value));
151-
self
152-
}
153-
154-
pub fn check_with_name(&mut self, name: &'static str, value: Expr) -> &mut Self {
155-
self.check.push(Check::Named(name, value));
149+
pub fn check<T>(&mut self, value: T) -> &mut Self
150+
where
151+
T: Into<Check>,
152+
{
153+
self.check.push(value.into());
156154
self
157155
}
158156

src/types.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ impl std::fmt::Display for DynIden {
118118
}
119119
}
120120

121+
impl From<&'static str> for DynIden {
122+
fn from(s: &'static str) -> Self {
123+
Self(Cow::Borrowed(s))
124+
}
125+
}
126+
121127
pub trait IntoIden {
122128
fn into_iden(self) -> DynIden;
123129
}

tests/mysql/table.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,9 @@ fn create_with_named_check_constraint() {
439439
ColumnDef::new(Glyph::Id)
440440
.integer()
441441
.not_null()
442-
.check_with_name("positive_id", Expr::col(Glyph::Id).gt(10))
442+
.check(("positive_id", Expr::col(Glyph::Id).gt(10)))
443443
)
444-
.check_with_name("id_range", Expr::col(Glyph::Id).lt(20))
444+
.check(("id_range", Expr::col(Glyph::Id).lt(20)))
445445
.check(Expr::col(Glyph::Id).ne(15))
446446
.to_string(MysqlQueryBuilder),
447447
r"CREATE TABLE `glyph` ( `id` int NOT NULL CONSTRAINT `positive_id` CHECK (`id` > 10), CONSTRAINT `id_range` CHECK (`id` < 20), CHECK (`id` <> 15) )",
@@ -475,7 +475,7 @@ fn alter_with_named_check_constraint() {
475475
.integer()
476476
.not_null()
477477
.default(101)
478-
.check_with_name("positive_aspect", Expr::col(Glyph::Aspect).gt(100))
478+
.check(("positive_aspect", Expr::col(Glyph::Aspect).gt(100)))
479479
)
480480
.to_string(MysqlQueryBuilder),
481481
r#"ALTER TABLE `glyph` ADD COLUMN `aspect` int NOT NULL DEFAULT 101 CONSTRAINT `positive_aspect` CHECK (`aspect` > 100)"#,

tests/postgres/table.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -568,9 +568,9 @@ fn create_with_named_check_constraint() {
568568
ColumnDef::new(Glyph::Id)
569569
.integer()
570570
.not_null()
571-
.check_with_name("positive_id", Expr::col(Glyph::Id).gt(10))
571+
.check(("positive_id", Expr::col(Glyph::Id).gt(10)))
572572
)
573-
.check_with_name("id_range", Expr::col(Glyph::Id).lt(20))
573+
.check(("id_range", Expr::col(Glyph::Id).lt(20)))
574574
.check(Expr::col(Glyph::Id).ne(15))
575575
.to_string(PostgresQueryBuilder),
576576
r#"CREATE TABLE "glyph" ( "id" integer NOT NULL CONSTRAINT "positive_id" CHECK ("id" > 10), CONSTRAINT "id_range" CHECK ("id" < 20), CHECK ("id" <> 15) )"#,
@@ -604,7 +604,7 @@ fn alter_with_named_check_constraint() {
604604
.integer()
605605
.not_null()
606606
.default(101)
607-
.check_with_name("positive_aspect", Expr::col(Glyph::Aspect).gt(100))
607+
.check(("positive_aspect", Expr::col(Glyph::Aspect).gt(100)))
608608
)
609609
.to_string(PostgresQueryBuilder),
610610
r#"ALTER TABLE "glyph" ADD COLUMN "aspect" integer NOT NULL DEFAULT 101 CONSTRAINT "positive_aspect" CHECK ("aspect" > 100)"#,

tests/sqlite/table.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,9 @@ fn create_with_named_check_constraint() {
473473
ColumnDef::new(Glyph::Id)
474474
.integer()
475475
.not_null()
476-
.check_with_name("positive_id", Expr::col(Glyph::Id).gt(10))
476+
.check(("positive_id", Expr::col(Glyph::Id).gt(10)))
477477
)
478-
.check_with_name("id_range", Expr::col(Glyph::Id).lt(20))
478+
.check(("id_range", Expr::col(Glyph::Id).lt(20)))
479479
.check(Expr::col(Glyph::Id).ne(15))
480480
.to_string(SqliteQueryBuilder),
481481
r#"CREATE TABLE "glyph" ( "id" integer NOT NULL CONSTRAINT "positive_id" CHECK ("id" > 10), CONSTRAINT "id_range" CHECK ("id" < 20), CHECK ("id" <> 15) )"#,
@@ -509,7 +509,7 @@ fn alter_with_named_check_constraint() {
509509
.integer()
510510
.not_null()
511511
.default(101)
512-
.check_with_name("positive_aspect", Expr::col(Glyph::Aspect).gt(100))
512+
.check(("positive_aspect", Expr::col(Glyph::Aspect).gt(100)))
513513
)
514514
.to_string(SqliteQueryBuilder),
515515
r#"ALTER TABLE "glyph" ADD COLUMN "aspect" integer NOT NULL DEFAULT 101 CONSTRAINT "positive_aspect" CHECK ("aspect" > 100)"#,

0 commit comments

Comments
 (0)