add separate search_notes_only()

This commit is contained in:
Damien Elmes 2020-04-16 16:53:02 +10:00
parent 04f0ea8599
commit 7c23deb562

View file

@ -8,14 +8,31 @@ use crate::notes::NoteID;
use crate::search::parser::parse; use crate::search::parser::parse;
impl Collection { impl Collection {
pub(crate) fn search_notes(&mut self, search: &str) -> Result<Vec<NoteID>> { /// This supports card queries as well, but is slower.
pub fn search_notes(&mut self, search: &str) -> Result<Vec<NoteID>> {
self.search_notes_inner(search, |sql| {
format!(
"select distinct n.id from cards c, notes n where c.nid=n.id and {}",
sql
)
})
}
/// This only supports note-related search terms.
pub fn search_notes_only(&mut self, search: &str) -> Result<Vec<NoteID>> {
self.search_notes_inner(search, |sql| {
format!("select n.id from notes n where {}", sql)
})
}
fn search_notes_inner<F>(&mut self, search: &str, build_sql: F) -> Result<Vec<NoteID>>
where
F: FnOnce(String) -> String,
{
let top_node = Node::Group(parse(search)?); let top_node = Node::Group(parse(search)?);
let (sql, args) = node_to_sql(self, &top_node)?; let (sql, args) = node_to_sql(self, &top_node)?;
let sql = format!( let sql = build_sql(sql);
"select n.id from cards c, notes n where c.nid=n.id and {}",
sql
);
let mut stmt = self.storage.db.prepare(&sql)?; let mut stmt = self.storage.db.prepare(&sql)?;
let ids: Vec<_> = stmt let ids: Vec<_> = stmt