Allow BrickRecord insert to force not being defered, as well as overriding its query

This commit is contained in:
Gregoo 2025-01-24 10:11:15 +01:00
parent b73bd6e99d
commit a01d38ee7a

View File

@ -1,5 +1,5 @@
from sqlite3 import Row from sqlite3 import Row
from typing import Any, ItemsView from typing import Any, ItemsView, Tuple
from .fields import BrickRecordFields from .fields import BrickRecordFields
from .sql import BrickSQL from .sql import BrickSQL
@ -24,17 +24,31 @@ class BrickRecord(object):
# Insert into the database # Insert into the database
# If we do not commit immediately, we defer the execute() call # If we do not commit immediately, we defer the execute() call
def insert(self, /, *, commit=True) -> None: def insert(
self,
/,
*,
commit=True,
no_defer=False,
override_query: str | None = None
) -> Tuple[int, str]:
if override_query:
query = override_query
else:
query = self.insert_query
database = BrickSQL() database = BrickSQL()
rows, q = database.execute( rows, q = database.execute(
self.insert_query, query,
parameters=self.sql_parameters(), parameters=self.sql_parameters(),
defer=not commit, defer=not commit and not no_defer,
) )
if commit: if commit:
database.commit() database.commit()
return rows, q
# Shorthand to field items # Shorthand to field items
def items(self, /) -> ItemsView[str, Any]: def items(self, /) -> ItemsView[str, Any]:
return self.fields.__dict__.items() return self.fields.__dict__.items()