Skip to content

Commit df638d2

Browse files
committed
fixes #9
1 parent 5e2c8f7 commit df638d2

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

fastlite/core.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,12 @@ def __call__(
104104
where_args: Iterable|dict|NoneType=None, with_pk:bool=False, order_by: str|None=None,
105105
limit:int|None=None, offset:int|None=None, as_cls:bool=True, **kwargs)->list:
106106
"Shortcut for `rows_where` or `pks_and_rows_where`, depending on `with_pk`"
107-
107+
108108
f = getattr(self, 'pks_and_rows_where' if with_pk else 'rows_where')
109+
xtra = getattr(self, 'xtra_id', {})
110+
if xtra:
111+
xw = ' and '.join(f"[{k}] = {v!r}" for k,v in xtra.items())
112+
where = f'{xw} and {where}' if where else xw
109113
res = f(where=where, where_args=where_args, order_by=order_by, limit=limit, offset=offset, **kwargs)
110114
if as_cls and hasattr(self,'cls'):
111115
if with_pk: res = ((k,self.cls(**v)) for k,v in res)

nbs/00_core.ipynb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,8 +505,12 @@
505505
" where_args: Iterable|dict|NoneType=None, with_pk:bool=False, order_by: str|None=None,\n",
506506
" limit:int|None=None, offset:int|None=None, as_cls:bool=True, **kwargs)->list:\n",
507507
" \"Shortcut for `rows_where` or `pks_and_rows_where`, depending on `with_pk`\"\n",
508-
" \n",
508+
"\n",
509509
" f = getattr(self, 'pks_and_rows_where' if with_pk else 'rows_where')\n",
510+
" xtra = getattr(self, 'xtra_id', {})\n",
511+
" if xtra:\n",
512+
" xw = ' and '.join(f\"[{k}] = {v!r}\" for k,v in xtra.items())\n",
513+
" where = f'{xw} and {where}' if where else xw\n",
510514
" res = f(where=where, where_args=where_args, order_by=order_by, limit=limit, offset=offset, **kwargs)\n",
511515
" if as_cls and hasattr(self,'cls'):\n",
512516
" if with_pk: res = ((k,self.cls(**v)) for k,v in res)\n",
@@ -576,7 +580,7 @@
576580
{
577581
"data": {
578582
"text/plain": [
579-
"{'ArtistId': 1, 'Name': 'AC/DC'}"
583+
"Artist(ArtistId=1, Name='AC/DC')"
580584
]
581585
},
582586
"execution_count": null,

nbs/index.ipynb

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@
491491
"cell_type": "markdown",
492492
"metadata": {},
493493
"source": [
494-
"Pass a truthy value as the first param and you'll get tuples of primary keys and records:"
494+
"Pass a truthy value as `with_pk` and you'll get tuples of primary keys and records:"
495495
]
496496
},
497497
{
@@ -513,7 +513,7 @@
513513
}
514514
],
515515
"source": [
516-
"album(1, limit=2)"
516+
"album(with_pk=1, limit=2)"
517517
]
518518
},
519519
{
@@ -570,6 +570,34 @@
570570
"except NotFoundError: print(\"Not found\")"
571571
]
572572
},
573+
{
574+
"cell_type": "markdown",
575+
"metadata": {},
576+
"source": [
577+
"The same filtering is done when using the table as a callable:"
578+
]
579+
},
580+
{
581+
"cell_type": "code",
582+
"execution_count": null,
583+
"metadata": {},
584+
"outputs": [
585+
{
586+
"data": {
587+
"text/plain": [
588+
"[Album(AlbumId=1, Title='For Those About To Rock We Salute You', ArtistId=1),\n",
589+
" Album(AlbumId=4, Title='Let There Be Rock', ArtistId=1)]"
590+
]
591+
},
592+
"execution_count": null,
593+
"metadata": {},
594+
"output_type": "execute_result"
595+
}
596+
],
597+
"source": [
598+
"album()"
599+
]
600+
},
573601
{
574602
"cell_type": "markdown",
575603
"metadata": {},

0 commit comments

Comments
 (0)