Pre: add an interpose macro for SQL output.

This commit is contained in:
Richard Newman 2017-03-07 10:31:28 -08:00
parent 7bcf311db9
commit 1961815acd

View file

@ -172,6 +172,19 @@ impl QueryFragment for Op {
} }
} }
macro_rules! interpose {
( $name: ident, $across: expr, $body: block, $inter: block ) => {
let mut seq = $across.iter();
if let Some($name) = seq.next() {
$body;
for $name in seq {
$inter;
$body;
}
}
}
}
impl QueryFragment for Constraint { impl QueryFragment for Constraint {
fn push_sql(&self, out: &mut QueryBuilder) -> BuildQueryResult { fn push_sql(&self, out: &mut QueryBuilder) -> BuildQueryResult {
use self::Constraint::*; use self::Constraint::*;
@ -208,12 +221,9 @@ impl QueryFragment for TableList {
return Ok(()); return Ok(());
} }
source_alias_push_sql(out, &self.0[0])?; interpose!(sa, self.0,
{ source_alias_push_sql(out, sa)? },
for sa in self.0.iter().skip(1) { { out.push_sql(", ") });
out.push_sql(", ");
source_alias_push_sql(out, sa)?;
}
Ok(()) Ok(())
} }
} }
@ -258,12 +268,9 @@ impl QueryFragment for SelectQuery {
} }
out.push_sql(" WHERE "); out.push_sql(" WHERE ");
self.constraints[0].push_sql(out)?; interpose!(constraint, self.constraints,
{ constraint.push_sql(out)? },
for constraint in self.constraints[1..].iter() { { out.push_sql(" AND ") });
out.push_sql(" AND ");
constraint.push_sql(out)?;
}
// Guaranteed to be positive: u64. // Guaranteed to be positive: u64.
if let Some(limit) = self.limit { if let Some(limit) = self.limit {