Query condition simplification incoming processing #148
-
I used PHP and Java to develop before. Recently, I was studying trust development. I saw the high quality of the code you wrote. Thank you for your hard work and dedication. let (sql, values) = Query::select()
.columns(vec![
Character::Id,
Character::Character,
Character::FontSize,
])
.from(Character::Table)
.condition_all(map) //batch incoming
.order_by(Character::Id, Order::Desc)
.limit(1)
.build(PostgresQueryBuilder); The above is just what I want to express, which means that there is a simplified method of importing conditional values, so that developers can handle development work more efficiently and quickly. Everyone has different needs, but you can define a standard for everyone to use. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
Hi and welcome. The https://docs.rs/sea-orm/0.2.x/sea_orm/query/struct.Condition.html let mut conditions = Condition::all();
if let Some(name) = params.name {
conditions = conditions.add(cake::Column::Name.contains(name));
}
// .. other conditions
cake::Entity::find().filter(conditions); Let me know what you think about it? |
Beta Was this translation helpful? Give feedback.
-
Thank you. |
Beta Was this translation helpful? Give feedback.
-
Edit: the 'add' method takes ownership of self, so it has to be mut when defined. Oh and there is also an add_option which can simplify the above code: struct Input {
name: Option<String>,
}
let input = Input { name: Some("cheese".to_owned()) };
assert_eq!(
cake::Entity::find()
.filter(
Condition::all()
.add_option(input.name.map(|n| cake::Column::Name.contains(&n)))
)
.build(DbBackend::MySql)
.to_string(),
"SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`name` LIKE '%cheese%'"
); |
Beta Was this translation helpful? Give feedback.
-
Oh I thought this belongs to SeaORM. I guess it also applies? |
Beta Was this translation helpful? Give feedback.
-
Okay this is the final one use sea_query::{*, tests_cfg::*};
let query = Query::select()
.column(Glyph::Image)
.from(Glyph::Table)
.cond_where(
Cond::all()
.add_option(Some(Expr::tbl(Glyph::Table, Glyph::Image).like("A%")))
.add_option(None::<SimpleExpr>)
)
.to_owned();
assert_eq!(
query.to_string(MysqlQueryBuilder),
r#"SELECT `image` FROM `glyph` WHERE `glyph`.`image` LIKE 'A%'"#
); |
Beta Was this translation helpful? Give feedback.
Okay this is the final one