Skip to content

Commit

Permalink
Merge pull request #274 from SeaQL/active-enum-3
Browse files Browse the repository at this point in the history
Enumeration - 3
  • Loading branch information
billy1624 authored Oct 27, 2021
2 parents cf52839 + fac528a commit a1f57ec
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 27 deletions.
8 changes: 2 additions & 6 deletions src/entity/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,7 @@ impl ColumnType {

pub(crate) fn get_enum_name(&self) -> Option<&String> {
match self {
// FIXME: How to get rid of this feature gate?
ColumnType::Enum(s, _) if cfg!(feature = "sqlx-postgres") => Some(s),
ColumnType::Enum(s, _) => Some(s),
_ => None,
}
}
Expand Down Expand Up @@ -300,13 +299,10 @@ impl From<ColumnType> for sea_query::ColumnType {
ColumnType::Money(s) => sea_query::ColumnType::Money(s),
ColumnType::Json => sea_query::ColumnType::Json,
ColumnType::JsonBinary => sea_query::ColumnType::JsonBinary,
ColumnType::Custom(s) => {
ColumnType::Custom(s) | ColumnType::Enum(s, _) => {
sea_query::ColumnType::Custom(sea_query::SeaRc::new(sea_query::Alias::new(&s)))
}
ColumnType::Uuid => sea_query::ColumnType::Uuid,
ColumnType::Enum(s, _) => {
sea_query::ColumnType::Custom(sea_query::SeaRc::new(sea_query::Alias::new(&s)))
}
}
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/query/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
PrimaryKeyTrait, QueryTrait,
};
use core::marker::PhantomData;
use sea_query::{Alias, Expr, Func, InsertStatement, ValueTuple};
use sea_query::{Alias, Expr, InsertStatement, ValueTuple};

#[derive(Debug)]
pub struct Insert<A>
Expand Down Expand Up @@ -125,7 +125,6 @@ where
let av = am.take(col);
let av_has_val = av.is_set() || av.is_unchanged();
let col_def = col.def();
let enum_name = col_def.get_column_type().get_enum_name();

if columns_empty {
self.columns.push(av_has_val);
Expand All @@ -134,11 +133,10 @@ where
}
if av_has_val {
columns.push(col);
let val = av.into_value().unwrap();
let expr = if let Some(enum_name) = enum_name {
Func::cast_as(val, Alias::new(enum_name))
} else {
Expr::val(val).into()
let val = Expr::val(av.into_value().unwrap());
let expr = match col_def.get_column_type().get_enum_name() {
Some(enum_name) => val.as_enum(Alias::new(enum_name)),
None => val.into(),
};
values.push(expr);
}
Expand Down
13 changes: 6 additions & 7 deletions src/query/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{ColumnTrait, EntityTrait, Iterable, QueryFilter, QueryOrder, QuerySe
use core::fmt::Debug;
use core::marker::PhantomData;
pub use sea_query::JoinType;
use sea_query::{Alias, DynIden, Expr, Func, IntoColumnRef, SeaRc, SelectStatement, SimpleExpr};
use sea_query::{Alias, DynIden, Expr, IntoColumnRef, SeaRc, SelectStatement, SimpleExpr};

#[derive(Clone, Debug)]
pub struct Select<E>
Expand Down Expand Up @@ -115,15 +115,14 @@ where

fn column_list(&self) -> Vec<SimpleExpr> {
let table = SeaRc::new(E::default()) as DynIden;
let text_type = SeaRc::new(Alias::new("text")) as DynIden;
E::Column::iter()
.map(|col| {
let col_def = col.def();
let enum_name = col_def.get_column_type().get_enum_name();
let col_expr = Expr::tbl(table.clone(), col);
if enum_name.is_some() {
Func::cast_expr_as(col_expr, Alias::new("text"))
} else {
col_expr.into()
let expr = Expr::tbl(table.clone(), col);
match col_def.get_column_type().get_enum_name() {
Some(_) => expr.as_enum(text_type.clone()),
None => expr.into(),
}
})
.collect()
Expand Down
12 changes: 5 additions & 7 deletions src/query/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
QueryTrait,
};
use core::marker::PhantomData;
use sea_query::{Alias, Expr, Func, IntoIden, SimpleExpr, UpdateStatement};
use sea_query::{Alias, Expr, IntoIden, SimpleExpr, UpdateStatement};

#[derive(Clone, Debug)]
pub struct Update;
Expand Down Expand Up @@ -105,14 +105,12 @@ where
continue;
}
let col_def = col.def();
let enum_name = col_def.get_column_type().get_enum_name();
let av = self.model.get(col);
if av.is_set() {
let val = av.into_value().unwrap();
let expr = if let Some(enum_name) = enum_name {
Func::cast_as(val, Alias::new(enum_name))
} else {
Expr::val(val).into()
let val = Expr::val(av.into_value().unwrap());
let expr = match col_def.get_column_type().get_enum_name() {
Some(enum_name) => val.as_enum(Alias::new(enum_name)),
None => val.into(),
};
self.query.value_expr(col, expr);
}
Expand Down

0 comments on commit a1f57ec

Please sign in to comment.