mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-25 18:34:32 +02:00
Initial implementation of table.get(...)
This commit is contained in:
parent
f70e35c9bb
commit
a9fd1e785f
2 changed files with 41 additions and 0 deletions
|
|
@ -78,6 +78,9 @@ class NoObviousTable(Exception):
|
|||
class BadPrimaryKey(Exception):
|
||||
pass
|
||||
|
||||
class NotFoundError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class Database:
|
||||
def __init__(self, filename_or_conn):
|
||||
|
|
@ -382,6 +385,30 @@ class Table:
|
|||
def pks(self):
|
||||
return [column.name for column in self.columns if column.is_pk]
|
||||
|
||||
def get(self, pk_values):
|
||||
if not isinstance(pk_values, (list, tuple)):
|
||||
pk_values = [pk_values]
|
||||
pks = self.pks
|
||||
pk_names = []
|
||||
if len(pks) == 0:
|
||||
# rowid table
|
||||
pk_names = ["rowid"]
|
||||
last_pk = pk_values[0]
|
||||
elif len(pks) == 1:
|
||||
pk_names = [pks[0]]
|
||||
last_pk = pk_values[0]
|
||||
elif len(pks) > 1:
|
||||
pk_names = pks
|
||||
last_pk = pk_values
|
||||
wheres = ["[{}] = ?".format(pk_name) for pk_name in pk_names]
|
||||
rows = self.rows_where(' and '.join(wheres), pk_values)
|
||||
try:
|
||||
row = list(rows)[0]
|
||||
self.last_pk = last_pk
|
||||
return row
|
||||
except IndexError:
|
||||
raise NotFoundError
|
||||
|
||||
@property
|
||||
def foreign_keys(self):
|
||||
fks = []
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue