Skip to content

Commit

Permalink
Use dict_factory for get_part_details (#512)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bouni authored Aug 2, 2024
1 parent b8a88c1 commit 204dcdb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
12 changes: 7 additions & 5 deletions library.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -381,21 +381,23 @@ 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:
# Each number needs to be wrapped in double quotes for exact match in FTS5
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."""
Expand Down
12 changes: 3 additions & 9 deletions mainwindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])):
Expand Down

0 comments on commit 204dcdb

Please sign in to comment.