Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use dict_factory for get_part_details #512

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading