Scryfall-type search is working
This commit is contained in:
@@ -152,6 +152,45 @@ pub fn get_card_by_name(name: &str, name_type: GetNameType) -> Option<DbCard> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn find_matching_cards_seperate_words(search_strings: &Vec<String>) -> Vec<DbCard> {
|
||||
assert!(!search_strings.is_empty());
|
||||
let sqlite_file = get_local_data_sqlite_file();
|
||||
let conn = rusqlite::Connection::open(sqlite_file).unwrap();
|
||||
let mut percentaged_string = Vec::new();
|
||||
// I know that .clone fixes my problem - I'm not sure why I need to though
|
||||
for mut search_string in search_strings.clone() {
|
||||
search_string.push('%');
|
||||
search_string.insert(0, '%');
|
||||
percentaged_string.push(search_string);
|
||||
}
|
||||
let mut sql: String = "SELECT name, lowercase_name, type_line, oracle_text, power_toughness, loyalty, mana_cost, scryfall_uri
|
||||
FROM cards WHERE".into();
|
||||
for i in 0..search_strings.len() {
|
||||
sql.push_str(&format!(" lowercase_name LIKE (?{}) AND", i + 1));
|
||||
}
|
||||
sql.pop();
|
||||
sql.pop();
|
||||
sql.pop();
|
||||
sql.pop();
|
||||
dbg!(&sql);
|
||||
let mut stmt = conn.prepare(&sql).unwrap();
|
||||
stmt.query_map(rusqlite::params_from_iter(percentaged_string), |row| {
|
||||
Ok(DbCard {
|
||||
name: row.get(0).unwrap(),
|
||||
lowercase_name: row.get(1).unwrap(),
|
||||
type_line: row.get(2).unwrap(),
|
||||
oracle_text: row.get(3).unwrap(),
|
||||
power_toughness: row.get(4).unwrap(),
|
||||
loyalty: row.get(5).unwrap(),
|
||||
mana_cost: row.get(6).unwrap(),
|
||||
scryfall_uri: row.get(7).unwrap(),
|
||||
})
|
||||
})
|
||||
.unwrap()
|
||||
.filter_map(|res| res.ok())
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn find_matching_cards(name: &str) -> Vec<DbCard> {
|
||||
let sqlite_file = get_local_data_sqlite_file();
|
||||
let conn = rusqlite::Connection::open(sqlite_file).unwrap();
|
||||
|
||||
Reference in New Issue
Block a user