Skip to content

Commit

Permalink
Merge pull request #45 from AnswerDotAI/use-sqlite-minutils-table.res…
Browse files Browse the repository at this point in the history
…ult-attr

Use new `sqlite_minutils.Table.result` attribute
  • Loading branch information
jph00 authored Nov 23, 2024
2 parents 9a8780f + ce40edd commit 57733fa
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions fastlite/kw.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,21 @@ def xtra(self:Table, **kwargs):
self.xtra_id = kwargs

@patch
def get_last(self:Table, as_cls:bool=True):
assert self.last_rowid is not None
row = first(self.rows_where('_rowid_=?', (self.last_rowid,)))
assert row, f"Couldn't find {self.last_rowid}"
def get_last(self:Table,
as_cls:bool=True, # Display as Row object
legacy:bool=True # If True, use last_rowid. If False, use Table.result attribute
):
if legacy:
assert self.last_rowid is not None
row = first(self.rows_where('_rowid_=?', (self.last_rowid,)))
assert row, f"Couldn't find {self.last_rowid}"
else:
row = self.result[-1] if len(self.result) else {}
vals = [row[pk] for pk in self.pks]
self.last_pk = vals[0] if len(vals)==1 else vals
self.last_pk = vals[0] if len(vals)==1 else vals
if as_cls and hasattr(self,'cls'): row = self.cls(**row)
return row
return row


@patch
def ids_and_rows_where(
Expand Down Expand Up @@ -122,15 +129,15 @@ def _process_row(row):

@patch
def update(self:Table, updates: dict|None=None, pk_values: list|tuple|str|int|float|None=None,
alter: bool=False, conversions: dict|None=None, xtra:dict|None=None, **kwargs):
alter: bool=False, conversions: dict|None=None, xtra:dict|None=None, **kwargs) -> Any:
if not updates: updates={}
updates = _process_row(updates)
if not xtra: xtra = getattr(self, 'xtra_id', {})
updates = {**updates, **kwargs, **xtra}
if not updates: return {}
if pk_values is None: pk_values = [updates[o] for o in self.pks]
self._orig_update(pk_values, updates=updates, alter=alter, conversions=conversions)
return self.get_last()
return self.get_last(legacy=False)


@patch
Expand Down Expand Up @@ -180,15 +187,15 @@ def insert(
conversions: Union[Dict[str, str], Default, None]=DEFAULT,
columns: Union[Dict[str, Any], Default, None]=DEFAULT,
strict: opt_bool=DEFAULT,
**kwargs) -> Table:
**kwargs) -> Any:
record = _process_row(record)
record = {**record, **kwargs}
if not record: return {}
self._orig_insert(
record=record, pk=pk, foreign_keys=foreign_keys, column_order=column_order, not_null=not_null,
defaults=defaults, hash_id=hash_id, hash_id_columns=hash_id_columns, alter=alter, ignore=ignore,
replace=replace, extracts=extracts, conversions=conversions, columns=columns, strict=strict)
return self.get_last()
return self.get_last(legacy=False)


@patch
Expand All @@ -205,7 +212,7 @@ def upsert(
conversions: Union[Dict[str, str], Default, None]=DEFAULT,
columns: Union[Dict[str, Any], Default, None]=DEFAULT,
strict: Union[bool, Default]|None=DEFAULT,
**kwargs) -> Table:
**kwargs) -> Any:
record = _process_row(record)
record = {**record, **kwargs}
if not record: return {}
Expand All @@ -218,7 +225,7 @@ def upsert(
record=record, pk=pk, foreign_keys=foreign_keys, column_order=column_order, not_null=not_null,
defaults=defaults, hash_id=hash_id, hash_id_columns=hash_id_columns, alter=alter,
extracts=extracts, conversions=conversions, columns=columns, strict=strict)
return self.get(last_pk)
return self.get_last(legacy=False)


@patch
Expand Down

0 comments on commit 57733fa

Please sign in to comment.