Fix empty queries with filters (#332)

This commit is contained in:
Ellie Huxtable 2022-04-22 22:15:50 +01:00 committed by GitHub
parent 89549b367b
commit b22929222f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -242,7 +242,11 @@ impl Database for Sqlite {
FilterMode::Directory => format!("cwd = '{}'", context.cwd).to_string(), FilterMode::Directory => format!("cwd = '{}'", context.cwd).to_string(),
}; };
let filter = format!("{} {}", join, filter_query); let filter = if filter_query.is_empty() {
"".to_string()
} else {
format!("{} {}", join, filter_query)
};
let limit = if let Some(max) = max { let limit = if let Some(max) = max {
format!("limit {}", max) format!("limit {}", max)
@ -252,9 +256,9 @@ impl Database for Sqlite {
let query = format!( let query = format!(
"select * from history h "select * from history h
{} {} {}
order by timestamp desc order by timestamp desc
{} {}", {}",
query, filter, limit, query, filter, limit,
); );
@ -402,32 +406,41 @@ impl Database for Sqlite {
} }
}; };
let filter_sql = match filter { let filter_base = if query_sql.is_empty() {
FilterMode::Global => String::from(""), "".to_string()
FilterMode::Session => format!("and session = '{}'", context.session), } else {
FilterMode::Directory => format!("and cwd = '{}'", context.cwd), "and".to_string()
FilterMode::Host => format!("and hostname = '{}'", context.hostname),
}; };
let res = query_params let filter_query = match filter {
.iter() FilterMode::Global => String::from(""),
.fold( FilterMode::Session => format!("session = '{}'", context.session),
sqlx::query( FilterMode::Directory => format!("cwd = '{}'", context.cwd),
format!( FilterMode::Host => format!("hostname = '{}'", context.hostname),
"select * from history h };
where {}
{} let filter_sql = if filter_query.is_empty() {
"".to_string()
} else {
format!("{} {}", filter_base, filter_query)
};
let sql = format!(
"select * from history h
where {} {}
group by command group by command
having max(timestamp) having max(timestamp)
order by timestamp desc {}", order by timestamp desc {}",
query_sql.as_str(), query_sql.as_str(),
filter_sql.as_str(), filter_sql.as_str(),
limit.clone() limit.clone()
) );
.as_str(),
), let res = query_params
|query, query_param| query.bind(query_param), .iter()
) .fold(sqlx::query(sql.as_str()), |query, query_param| {
query.bind(query_param)
})
.map(Self::query_history) .map(Self::query_history)
.fetch_all(&self.pool) .fetch_all(&self.pool)
.await?; .await?;