Moved string matching to sqlite

Probably (definitely more efficient)
This commit is contained in:
2025-08-20 21:07:37 +01:00
parent 6ff0204189
commit 3f7cd6353a
4 changed files with 51 additions and 23 deletions

View File

@@ -1,8 +1,8 @@
use deunicode::deunicode;
use rusqlite;
use std::fmt;
use std::fs;
use std::path::PathBuf;
use std::str::SplitWhitespace;
use super::deser::ScryfallCard;
use super::utils::get_local_cache_folder;
@@ -46,13 +46,14 @@ pub fn update_db_with_file(file: PathBuf) -> bool {
let tx = conn.transaction().unwrap();
for card in ac {
for word in card.name.split_whitespace() {
let word = deunicode(word);
let res = tx.execute(
"INSERT INTO magic_words (word) VALUES (?1)
ON CONFLICT (word) DO NOTHING;",
[word.replace(",", "")],
);
}
let lowercase_name = card.name.to_lowercase();
let lowercase_name = deunicode(&card.name.to_lowercase());
let power_toughness = match card.power {
Some(p) => Some(format!("{}/{}", p, card.toughness.unwrap())),
None => None,
@@ -107,6 +108,7 @@ impl fmt::Display for DbCard {
#[derive(Debug)]
pub struct DbCard {
pub name: String,
pub lowercase_name: String,
pub type_line: String,
pub oracle_text: Option<String>,
pub power_toughness: Option<String>,
@@ -125,11 +127,11 @@ pub fn get_card_by_name(name: &str, name_type: GetNameType) -> Option<DbCard> {
let conn = rusqlite::Connection::open(sqlite_file).unwrap();
let sql = match name_type {
GetNameType::Name => {
"SELECT name, type_line, oracle_text, power_toughness, loyalty, mana_cost, scryfall_uri
"SELECT name, lowercase_name, type_line, oracle_text, power_toughness, loyalty, mana_cost, scryfall_uri
FROM cards WHERE name = (?1)"
}
GetNameType::LowercaseName => {
"SELECT name, type_line, oracle_text, power_toughness, loyalty, mana_cost, scryfall_uri
"SELECT name, lowercase_name, type_line, oracle_text, power_toughness, loyalty, mana_cost, scryfall_uri
FROM cards WHERE lowercase_name = (?1)"
}
};
@@ -138,17 +140,48 @@ pub fn get_card_by_name(name: &str, name_type: GetNameType) -> Option<DbCard> {
match rows.next().unwrap() {
Some(row) => Some(DbCard {
name: row.get(0).unwrap(),
type_line: row.get(1).unwrap(),
oracle_text: row.get(2).unwrap(),
power_toughness: row.get(3).unwrap(),
loyalty: row.get(4).unwrap(),
mana_cost: row.get(5).unwrap(),
scryfall_uri: row.get(6).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(),
}),
None => None,
}
}
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();
// There must be something better than this - although I don't think it's possible with a str
let mut name = name.to_string();
name.push('%');
name.insert(0, '%');
let mut stmt = conn
.prepare(
"SELECT name, lowercase_name, type_line, oracle_text, power_toughness, loyalty, mana_cost, scryfall_uri
FROM cards WHERE lowercase_name LIKE (?1)",
)
.unwrap();
stmt.query_map([name], |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()
}
const CREATE_CARDS_TABLE_SQL: &str = "
CREATE TABLE cards (
name TEXT NOT NULL UNIQUE,