From 75c879645873250e53ba2399bf5a15fd1d94b7db Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 9 Apr 2021 11:02:59 +0200 Subject: Simplify implementation of subcommand "db jobs" This patch simplifies the implementation by using the `.into_boxed()` function from the diesel query builder and appling the filters from the CLI one-by-one. Signed-off-by: Matthias Beyer --- src/commands/db.rs | 90 +++++++++++++++++++----------------------------------- 1 file changed, 31 insertions(+), 59 deletions(-) diff --git a/src/commands/db.rs b/src/commands/db.rs index 0f11837..8b51570 100644 --- a/src/commands/db.rs +++ b/src/commands/db.rs @@ -290,8 +290,6 @@ fn submits(conn_cfg: DbConnectionConfig, matches: &ArgMatches) -> Result<()> { } fn jobs(conn_cfg: DbConnectionConfig, matches: &ArgMatches) -> Result<()> { - use crate::schema::jobs::dsl; - let csv = matches.is_present("csv"); let hdrs = crate::commands::util::mk_header(vec![ "id", @@ -310,67 +308,41 @@ fn jobs(conn_cfg: DbConnectionConfig, matches: &ArgMatches) -> Result<()> { .map(crate::util::env::parse_to_env) .transpose()?; - let jobs = matches + let sel = schema::jobs::table + .inner_join(schema::submits::table) + .inner_join(schema::endpoints::table) + .inner_join(schema::packages::table) + .left_outer_join(schema::job_envs::table.inner_join(schema::envvars::table)) + .into_boxed(); + + let sel = if let Some(submit_uuid) = matches .value_of("submit_uuid") .map(uuid::Uuid::parse_str) .transpose()? - .map(|submit_uuid| { - let sel = schema::jobs::table - .inner_join(schema::submits::table) - .inner_join(schema::endpoints::table) - .inner_join(schema::packages::table) - .left_outer_join(schema::job_envs::table.inner_join(schema::envvars::table)) - .filter(schema::submits::uuid.eq(&submit_uuid)) - .into_boxed(); - - let sel = if let Some((name, val)) = env_filter_tpl.as_ref() { - use crate::diesel::BoolExpressionMethods; - - sel.filter({ - schema::envvars::dsl::name.eq(name.as_ref()) - .and(schema::envvars::dsl::value.eq(val)) - }) - } else { - sel - }; - - sel.load::<( - models::Job, - models::Submit, - models::Endpoint, - models::Package, - Option<(models::JobEnv, models::EnvVar)>, - )>(&conn) - .map_err(Error::from) + { + sel.filter(schema::submits::uuid.eq(submit_uuid)) + } else { + sel + }; + + let sel = if let Some((name, val)) = env_filter_tpl.as_ref() { + use crate::diesel::BoolExpressionMethods; + + sel.filter({ + schema::envvars::dsl::name.eq(name.as_ref()) + .and(schema::envvars::dsl::value.eq(val)) }) - .unwrap_or_else(|| { - let sel = dsl::jobs - .inner_join(crate::schema::submits::table) - .inner_join(crate::schema::endpoints::table) - .inner_join(crate::schema::packages::table) - .left_outer_join(schema::job_envs::table.inner_join(schema::envvars::table)) - .into_boxed(); - - let sel = if let Some((name, val)) = env_filter_tpl.as_ref() { - use crate::diesel::BoolExpressionMethods; - - sel.filter({ - schema::envvars::dsl::name.eq(name.as_ref()) - .and(schema::envvars::dsl::value.eq(val)) - }) - } else { - sel - }; - - sel.load::<( - models::Job, - models::Submit, - models::Endpoint, - models::Package, - Option<(models::JobEnv, models::EnvVar)>, - )>(&conn) - .map_err(Error::from) - })?; + } else { + sel + }; + + let jobs = sel.load::<( + models::Job, + models::Submit, + models::Endpoint, + models::Package, + Option<(models::JobEnv, models::EnvVar)>, + )>(&conn)?; let data = jobs .iter() -- cgit v1.2.3