Skip to content

Commit

Permalink
Fix: still proceed with indexed access (fast path) if some parts were…
Browse files Browse the repository at this point in the history
… deleted from LCSC
  • Loading branch information
gyohng committed Apr 22, 2024
1 parent 7b715b5 commit 56d38ec
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions library.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,17 +359,26 @@ def get_part_details(self, lcsc):
with contextlib.closing(sqlite3.connect(self.partsdb_file)) as con, con as cur:
numbers = ",".join([f'"{n}"' for n in lcsc])

# try retrieving from the cached index first (LCSC Part indexing from FTS5 parts is sloooooow)
try:
rows = cur.execute(f'SELECT partsId FROM parts_by_lcsc where lcsc IN ({numbers})').fetchall()
if len(rows) == len(lcsc):
numbers = ",".join([f'"{r[0]}"' for r in rows])
return cur.execute(
f'SELECT "LCSC Part", "Stock", "Library Type" FROM parts where rowid IN ({numbers})'
).fetchall()
rows = cur.execute(f'SELECT lcsc, partsId FROM parts_by_lcsc where lcsc IN ({numbers})').fetchall()

# orphaned parts found
if len(rows) != len(lcsc):
rowid_by_lcsc = dict(rows)
for lc in lcsc:
if lc not in rowid_by_lcsc:
self.logger.debug(f"LCSC Part `{lc}` not found in the database.")

numbers = ",".join([f'"{r[0]}"' for r in rows])
return cur.execute(
f'SELECT "LCSC Part", "Stock", "Library Type" FROM parts where rowid IN ({numbers})'
).fetchall()
except Exception as e:
self.logger.debug(f"{e}")
pass

# fall back to the direct approach
try:
return cur.execute(
f'SELECT "LCSC Part", "Stock", "Library Type" FROM parts where "LCSC Part" IN ({numbers})'
Expand Down

0 comments on commit 56d38ec

Please sign in to comment.