Skip to content

Commit

Permalink
fixes #5
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Jun 3, 2024
1 parent 0bf0898 commit 82b9d8f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
15 changes: 12 additions & 3 deletions fastlite/kw.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ def xtra(self:Table, **kwargs):
"Set `xtra_id`"
self.xtra_id = kwargs

@patch
def get_last(self:Table, as_cls:bool=True):
row = first(self.rows_where('_rowid_=?', (self.last_rowid,)))
vals = [row[pk] for pk in self.pks]
self.last_pk = vals[0] if len(vals)==1 else vals
if as_cls and hasattr(self,'cls'): row = self.cls(**row)
return row


@patch
def get(self:Table, pk_values: list|tuple|str|int, as_cls:bool=True)->Any:
if not isinstance(pk_values, (list, tuple)): pk_values = [pk_values]
Expand Down Expand Up @@ -90,7 +99,7 @@ def update(self:Table, updates: dict|None=None, pk_values: list|tuple|str|int|fl
updates = {**updates, **kwargs, **xtra}
if not pk_values: pk_values = [updates[o] for o in self.pks]
self._orig_update(pk_values, updates=updates, alter=alter, conversions=conversions)
return self.get(self.last_pk)
return self.get_last()


@patch
Expand Down Expand Up @@ -144,7 +153,7 @@ def 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(self.last_pk)
return self.get_last()


@patch
Expand Down Expand Up @@ -172,7 +181,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(self.last_pk)
return self.get_last()


@patch
Expand Down
10 changes: 9 additions & 1 deletion nbs/index.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,15 @@
"metadata": {},
"outputs": [],
"source": [
"from sqlite_utils import Database\n",
"from sqlite_utils import Database"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from fastlite import *\n",
"from fastcore.utils import *\n",
"from fastcore.net import urlsave"
Expand Down

0 comments on commit 82b9d8f

Please sign in to comment.