Move then select-then-ingest logic into BrickRecord and allow context to be passed to select()
This commit is contained in:
parent
a01d38ee7a
commit
4e1bf08139
@ -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
|
||||
|
@ -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
|
||||
|
@ -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] = {}
|
||||
|
@ -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
|
||||
|
@ -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()
|
||||
|
||||
|
@ -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()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user