diff --git a/library.py b/library.py index 112d395..18374e0 100644 --- a/library.py +++ b/library.py @@ -19,7 +19,7 @@ ResetGaugeEvent, UpdateGaugeEvent, ) -from .helpers import PLUGIN_PATH, natural_sort_collation +from .helpers import PLUGIN_PATH, dict_factory, natural_sort_collation from .unzip_parts import unzip_parts @@ -381,13 +381,13 @@ def insert_parts(self, data, cols): con.executemany(query, data) con.commit() - def get_part_details(self, lcsc): + def get_part_details(self, lcsc: list) -> dict: """Get the part details for a list of LCSC numbers using optimized FTS5 querying.""" with contextlib.closing(sqlite3.connect(self.partsdb_file)) as con: - con.row_factory = sqlite3.Row # To access columns by names + con.row_factory = dict_factory cur = con.cursor() results = [] - query = '''SELECT "LCSC Part", "Stock", "Library Type" FROM parts WHERE parts MATCH ?''' + query = '''SELECT "LCSC Part" AS lcsc, "Stock" AS stock, "Library Type" AS type FROM parts WHERE parts MATCH ?''' # Use parameter binding to prevent SQL injection and handle the query more efficiently for number in lcsc: @@ -395,7 +395,9 @@ def get_part_details(self, lcsc): match_query = f'"LCSC Part:{number}"' cur.execute(query, (match_query,)) results.extend(cur.fetchall()) - return results + if results: + return results[0] + return {} def update(self): """Update the sqlite parts database from the JLCPCB CSV.""" diff --git a/mainwindow.py b/mainwindow.py index f95b91c..7595e5a 100644 --- a/mainwindow.py +++ b/mainwindow.py @@ -612,15 +612,9 @@ def populate_footprint_list(self, *_): corrections = self.library.get_all_correction_data() # find rotation correction values for part in parts: - detail = list( - filter( - lambda x, lcsc=part["lcsc"]: x[0] == lcsc, - details, - ) - ) - if detail: - part["type"] = detail[0][2] - part["stock"] = detail[0][1] + if details: + part["type"] = details["type"] + part["stock"] = details["stock"] # First check if the part name mathes for regex, correction in corrections: if re.search(regex, str(part["reference"])):