From 4e1bf08139a012f325dac1a3189fee342d370319 Mon Sep 17 00:00:00 2001 From: Gregoo Date: Fri, 24 Jan 2025 10:21:05 +0100 Subject: [PATCH] Move then select-then-ingest logic into BrickRecord and allow context to be passed to select() --- bricktracker/minifigure.py | 14 ++------------ bricktracker/part.py | 14 ++------------ bricktracker/record.py | 22 +++++++++++++++++++--- bricktracker/record_list.py | 2 ++ bricktracker/set.py | 7 +------ bricktracker/wish.py | 7 +------ 6 files changed, 27 insertions(+), 39 deletions(-) diff --git a/bricktracker/minifigure.py b/bricktracker/minifigure.py index 25db952..e3b01d8 100644 --- a/bricktracker/minifigure.py +++ b/bricktracker/minifigure.py @@ -62,18 +62,13 @@ class BrickMinifigure(BrickRecord): # Save the parameters to the fields self.fields.fig_num = fig_num - record = self.select(override_query=self.generic_query) - - if record is None: + if not self.select(override_query=self.generic_query): raise NotFoundException( 'Minifigure with number {number} was not found in the database'.format( # noqa: E501 number=self.fields.fig_num, ), ) - # Ingest the record - self.ingest(record) - return self # Select a specific minifigure (with a set and an number) @@ -82,9 +77,7 @@ class BrickMinifigure(BrickRecord): self.brickset = brickset self.fields.fig_num = fig_num - record = self.select() - - if record is None: + if not self.select(): raise NotFoundException( 'Minifigure with number {number} from set {set} was not found in the database'.format( # noqa: E501 number=self.fields.fig_num, @@ -92,9 +85,6 @@ class BrickMinifigure(BrickRecord): ), ) - # Ingest the record - self.ingest(record) - return self # Return a dict with common SQL parameters for a minifigure diff --git a/bricktracker/part.py b/bricktracker/part.py index da62943..d6b39a1 100644 --- a/bricktracker/part.py +++ b/bricktracker/part.py @@ -92,9 +92,7 @@ class BrickPart(BrickRecord): self.fields.color_id = color_id self.fields.element_id = element_id - record = self.select(override_query=self.generic_query) - - if record is None: + if not self.select(override_query=self.generic_query): raise NotFoundException( 'Part with number {number}, color ID {color} and element ID {element} was not found in the database'.format( # noqa: E501 number=self.fields.part_num, @@ -103,9 +101,6 @@ class BrickPart(BrickRecord): ), ) - # Ingest the record - self.ingest(record) - return self # Select a specific part (with a set and an id, and option. a minifigure) @@ -122,9 +117,7 @@ class BrickPart(BrickRecord): self.minifigure = minifigure self.fields.id = id - record = self.select() - - if record is None: + if not self.select(): raise NotFoundException( 'Part with ID {id} from set {set} was not found in the database'.format( # noqa: E501 id=self.fields.id, @@ -132,9 +125,6 @@ class BrickPart(BrickRecord): ), ) - # Ingest the record - self.ingest(record) - return self # Return a dict with common SQL parameters for a part diff --git a/bricktracker/record.py b/bricktracker/record.py index 184697d..08651d2 100644 --- a/bricktracker/record.py +++ b/bricktracker/record.py @@ -54,17 +54,33 @@ class BrickRecord(object): return self.fields.__dict__.items() # Get from the database using the query - def select(self, /, *, override_query: str | None = None) -> Row | None: + def select( + self, + /, + *, + override_query: str | None = None, + **context: Any + ) -> bool: if override_query: query = override_query else: query = self.select_query - return BrickSQL().fetchone( + record = BrickSQL().fetchone( query, - parameters=self.sql_parameters() + parameters=self.sql_parameters(), + **context ) + # Ingest the record + if record is not None: + self.ingest(record) + + return True + + else: + return False + # Generic SQL parameters from fields def sql_parameters(self, /) -> dict[str, Any]: parameters: dict[str, Any] = {} diff --git a/bricktracker/record_list.py b/bricktracker/record_list.py index 48775d2..ac82e93 100644 --- a/bricktracker/record_list.py +++ b/bricktracker/record_list.py @@ -36,6 +36,7 @@ class BrickRecordList(Generic[T]): override_query: str | None = None, order: str | None = None, limit: int | None = None, + **context: Any, ) -> list[Row]: # Select the query if override_query: @@ -48,6 +49,7 @@ class BrickRecordList(Generic[T]): parameters=self.sql_parameters(), order=order, limit=limit, + **context ) # Generic SQL parameters from fields diff --git a/bricktracker/set.py b/bricktracker/set.py index 100896e..f07b475 100644 --- a/bricktracker/set.py +++ b/bricktracker/set.py @@ -106,18 +106,13 @@ class BrickSet(BrickRecord): self.fields.u_id = u_id # Load from database - record = self.select() - - if record is None: + if not self.select(): raise NotFoundException( 'Set with ID {id} was not found in the database'.format( id=self.fields.u_id, ), ) - # Ingest the record - self.ingest(record) - # Resolve the theme self.resolve_theme() diff --git a/bricktracker/wish.py b/bricktracker/wish.py index ad8ab80..30643dc 100644 --- a/bricktracker/wish.py +++ b/bricktracker/wish.py @@ -46,18 +46,13 @@ class BrickWish(BrickSet): self.fields.set_num = set_num # Load from database - record = self.select() - - if record is None: + if not self.select(): raise NotFoundException( 'Wish with number {number} was not found in the database'.format( # noqa: E501 number=self.fields.set_num, ), ) - # Ingest the record - self.ingest(record) - # Resolve the theme self.resolve_theme()