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
|
# Save the parameters to the fields
|
||||||
self.fields.fig_num = fig_num
|
self.fields.fig_num = fig_num
|
||||||
|
|
||||||
record = self.select(override_query=self.generic_query)
|
if not self.select(override_query=self.generic_query):
|
||||||
|
|
||||||
if record is None:
|
|
||||||
raise NotFoundException(
|
raise NotFoundException(
|
||||||
'Minifigure with number {number} was not found in the database'.format( # noqa: E501
|
'Minifigure with number {number} was not found in the database'.format( # noqa: E501
|
||||||
number=self.fields.fig_num,
|
number=self.fields.fig_num,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
# Ingest the record
|
|
||||||
self.ingest(record)
|
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
# Select a specific minifigure (with a set and an number)
|
# Select a specific minifigure (with a set and an number)
|
||||||
@ -82,9 +77,7 @@ class BrickMinifigure(BrickRecord):
|
|||||||
self.brickset = brickset
|
self.brickset = brickset
|
||||||
self.fields.fig_num = fig_num
|
self.fields.fig_num = fig_num
|
||||||
|
|
||||||
record = self.select()
|
if not self.select():
|
||||||
|
|
||||||
if record is None:
|
|
||||||
raise NotFoundException(
|
raise NotFoundException(
|
||||||
'Minifigure with number {number} from set {set} was not found in the database'.format( # noqa: E501
|
'Minifigure with number {number} from set {set} was not found in the database'.format( # noqa: E501
|
||||||
number=self.fields.fig_num,
|
number=self.fields.fig_num,
|
||||||
@ -92,9 +85,6 @@ class BrickMinifigure(BrickRecord):
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
# Ingest the record
|
|
||||||
self.ingest(record)
|
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
# Return a dict with common SQL parameters for a minifigure
|
# 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.color_id = color_id
|
||||||
self.fields.element_id = element_id
|
self.fields.element_id = element_id
|
||||||
|
|
||||||
record = self.select(override_query=self.generic_query)
|
if not self.select(override_query=self.generic_query):
|
||||||
|
|
||||||
if record is None:
|
|
||||||
raise NotFoundException(
|
raise NotFoundException(
|
||||||
'Part with number {number}, color ID {color} and element ID {element} was not found in the database'.format( # noqa: E501
|
'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,
|
number=self.fields.part_num,
|
||||||
@ -103,9 +101,6 @@ class BrickPart(BrickRecord):
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
# Ingest the record
|
|
||||||
self.ingest(record)
|
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
# Select a specific part (with a set and an id, and option. a minifigure)
|
# 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.minifigure = minifigure
|
||||||
self.fields.id = id
|
self.fields.id = id
|
||||||
|
|
||||||
record = self.select()
|
if not self.select():
|
||||||
|
|
||||||
if record is None:
|
|
||||||
raise NotFoundException(
|
raise NotFoundException(
|
||||||
'Part with ID {id} from set {set} was not found in the database'.format( # noqa: E501
|
'Part with ID {id} from set {set} was not found in the database'.format( # noqa: E501
|
||||||
id=self.fields.id,
|
id=self.fields.id,
|
||||||
@ -132,9 +125,6 @@ class BrickPart(BrickRecord):
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
# Ingest the record
|
|
||||||
self.ingest(record)
|
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
# Return a dict with common SQL parameters for a part
|
# Return a dict with common SQL parameters for a part
|
||||||
|
@ -54,17 +54,33 @@ class BrickRecord(object):
|
|||||||
return self.fields.__dict__.items()
|
return self.fields.__dict__.items()
|
||||||
|
|
||||||
# Get from the database using the query
|
# 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:
|
if override_query:
|
||||||
query = override_query
|
query = override_query
|
||||||
else:
|
else:
|
||||||
query = self.select_query
|
query = self.select_query
|
||||||
|
|
||||||
return BrickSQL().fetchone(
|
record = BrickSQL().fetchone(
|
||||||
query,
|
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
|
# Generic SQL parameters from fields
|
||||||
def sql_parameters(self, /) -> dict[str, Any]:
|
def sql_parameters(self, /) -> dict[str, Any]:
|
||||||
parameters: dict[str, Any] = {}
|
parameters: dict[str, Any] = {}
|
||||||
|
@ -36,6 +36,7 @@ class BrickRecordList(Generic[T]):
|
|||||||
override_query: str | None = None,
|
override_query: str | None = None,
|
||||||
order: str | None = None,
|
order: str | None = None,
|
||||||
limit: int | None = None,
|
limit: int | None = None,
|
||||||
|
**context: Any,
|
||||||
) -> list[Row]:
|
) -> list[Row]:
|
||||||
# Select the query
|
# Select the query
|
||||||
if override_query:
|
if override_query:
|
||||||
@ -48,6 +49,7 @@ class BrickRecordList(Generic[T]):
|
|||||||
parameters=self.sql_parameters(),
|
parameters=self.sql_parameters(),
|
||||||
order=order,
|
order=order,
|
||||||
limit=limit,
|
limit=limit,
|
||||||
|
**context
|
||||||
)
|
)
|
||||||
|
|
||||||
# Generic SQL parameters from fields
|
# Generic SQL parameters from fields
|
||||||
|
@ -106,18 +106,13 @@ class BrickSet(BrickRecord):
|
|||||||
self.fields.u_id = u_id
|
self.fields.u_id = u_id
|
||||||
|
|
||||||
# Load from database
|
# Load from database
|
||||||
record = self.select()
|
if not self.select():
|
||||||
|
|
||||||
if record is None:
|
|
||||||
raise NotFoundException(
|
raise NotFoundException(
|
||||||
'Set with ID {id} was not found in the database'.format(
|
'Set with ID {id} was not found in the database'.format(
|
||||||
id=self.fields.u_id,
|
id=self.fields.u_id,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
# Ingest the record
|
|
||||||
self.ingest(record)
|
|
||||||
|
|
||||||
# Resolve the theme
|
# Resolve the theme
|
||||||
self.resolve_theme()
|
self.resolve_theme()
|
||||||
|
|
||||||
|
@ -46,18 +46,13 @@ class BrickWish(BrickSet):
|
|||||||
self.fields.set_num = set_num
|
self.fields.set_num = set_num
|
||||||
|
|
||||||
# Load from database
|
# Load from database
|
||||||
record = self.select()
|
if not self.select():
|
||||||
|
|
||||||
if record is None:
|
|
||||||
raise NotFoundException(
|
raise NotFoundException(
|
||||||
'Wish with number {number} was not found in the database'.format( # noqa: E501
|
'Wish with number {number} was not found in the database'.format( # noqa: E501
|
||||||
number=self.fields.set_num,
|
number=self.fields.set_num,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
# Ingest the record
|
|
||||||
self.ingest(record)
|
|
||||||
|
|
||||||
# Resolve the theme
|
# Resolve the theme
|
||||||
self.resolve_theme()
|
self.resolve_theme()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user