Skip to content

Commit a6092ba

Browse files
authored
impl From<Condition> and From<ConditionExpression> for SimpleExpr (#886)
1 parent 2f400ae commit a6092ba

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/backend/query_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1389,7 +1389,7 @@ pub trait QueryBuilder:
13891389
#[doc(hidden)]
13901390
/// Translate part of a condition to part of a "WHERE" clause.
13911391
fn prepare_condition_where(&self, condition: &Condition, sql: &mut dyn SqlWriter) {
1392-
let simple_expr = condition.to_simple_expr();
1392+
let simple_expr = condition.clone().into();
13931393
self.prepare_simple_expr(&simple_expr, sql);
13941394
}
13951395

src/query/condition.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -260,39 +260,50 @@ impl Condition {
260260
pub fn len(&self) -> usize {
261261
self.conditions.len()
262262
}
263+
}
263264

264-
pub(crate) fn to_simple_expr(&self) -> SimpleExpr {
265+
impl From<Condition> for SimpleExpr {
266+
fn from(cond: Condition) -> Self {
265267
let mut inner_exprs = vec![];
266-
for ce in &self.conditions {
268+
for ce in cond.conditions {
267269
inner_exprs.push(match ce {
268-
ConditionExpression::Condition(c) => c.to_simple_expr(),
269-
ConditionExpression::SimpleExpr(e) => e.clone(),
270+
ConditionExpression::Condition(c) => c.into(),
271+
ConditionExpression::SimpleExpr(e) => e,
270272
});
271273
}
272274
let mut inner_exprs_into_iter = inner_exprs.into_iter();
273275
let expr = if let Some(first_expr) = inner_exprs_into_iter.next() {
274276
let mut out_expr = first_expr;
275277
for e in inner_exprs_into_iter {
276-
out_expr = match self.condition_type {
278+
out_expr = match cond.condition_type {
277279
ConditionType::Any => out_expr.or(e),
278280
ConditionType::All => out_expr.and(e),
279281
};
280282
}
281283
out_expr
282284
} else {
283-
SimpleExpr::Constant(match self.condition_type {
285+
SimpleExpr::Constant(match cond.condition_type {
284286
ConditionType::Any => false.into(),
285287
ConditionType::All => true.into(),
286288
})
287289
};
288-
if self.negate {
290+
if cond.negate {
289291
expr.not()
290292
} else {
291293
expr
292294
}
293295
}
294296
}
295297

298+
impl From<ConditionExpression> for SimpleExpr {
299+
fn from(ce: ConditionExpression) -> Self {
300+
match ce {
301+
ConditionExpression::Condition(c) => c.into(),
302+
ConditionExpression::SimpleExpr(e) => e,
303+
}
304+
}
305+
}
306+
296307
impl From<Condition> for ConditionExpression {
297308
fn from(condition: Condition) -> Self {
298309
ConditionExpression::Condition(condition)

0 commit comments

Comments
 (0)