1
0
Fork 0

Use sqlite3.Row row factory.

This commit is contained in:
voussoir 2022-08-13 13:04:07 -07:00
parent 29aeeb0614
commit 09da1bbb30
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB

View file

@ -70,6 +70,7 @@ def init_db():
db_exists = db_path.is_file db_exists = db_path.is_file
sql = sqlite3.connect(db_path.absolute_path) sql = sqlite3.connect(db_path.absolute_path)
sql.row_factory = sqlite3.Row
cur = sql.cursor() cur = sql.cursor()
if not db_exists: if not db_exists:
@ -231,15 +232,15 @@ def insert_item(data):
'id': id, 'id': id,
'deleted': bool(data.get('deleted', False)), 'deleted': bool(data.get('deleted', False)),
'type': data['type'], 'type': data['type'],
'author': data.get('by', existing.get('author', None)), 'author': data.get('by', existing['author']),
'time': int(data['time']), 'time': int(data['time']),
'text': data.get('text', existing.get('text', None)), 'text': data.get('text', existing['text']),
'dead': bool(data.get('dead', False)), 'dead': bool(data.get('dead', False)),
'parent': data.get('parent', None), 'parent': data.get('parent', None),
'poll': data.get('poll', existing.get('poll', None)), 'poll': data.get('poll', existing['poll']),
'url': data.get('url', existing.get('url', None)), 'url': data.get('url', existing['url']),
'score': int_or_none(data.get('score', existing.get('score', None))), 'score': int_or_none(data.get('score', existing['score'])),
'title': data.get('title', existing.get('title', None)), 'title': data.get('title', existing['title']),
'descendants': int_or_none(data.get('descendants', None)), 'descendants': int_or_none(data.get('descendants', None)),
'retrieved': retrieved, 'retrieved': retrieved,
} }
@ -266,9 +267,7 @@ def select_child_items(id):
''' '''
cur.execute('SELECT * FROM items WHERE parent == ?', [id]) cur.execute('SELECT * FROM items WHERE parent == ?', [id])
rows = cur.fetchall() rows = cur.fetchall()
return rows
items = [dict(zip(ITEMS_COLUMNS, row)) for row in rows]
return items
def select_poll_options(id): def select_poll_options(id):
''' '''
@ -276,26 +275,19 @@ def select_poll_options(id):
''' '''
cur.execute('SELECT * FROM items WHERE poll == ?', [id]) cur.execute('SELECT * FROM items WHERE poll == ?', [id])
rows = cur.fetchall() rows = cur.fetchall()
return rows
items = [dict(zip(ITEMS_COLUMNS, row)) for row in rows]
return items
def select_item(id): def select_item(id):
cur.execute('SELECT * FROM items WHERE id == ?', [id]) cur.execute('SELECT * FROM items WHERE id == ?', [id])
row = cur.fetchone() row = cur.fetchone()
return row
if row is None:
return None
item = dict(zip(ITEMS_COLUMNS, row))
return item
def select_latest_id(): def select_latest_id():
cur.execute('SELECT id FROM items ORDER BY id DESC LIMIT 1') cur.execute('SELECT id FROM items ORDER BY id DESC LIMIT 1')
row = cur.fetchone() row = cur.fetchone()
if row is None: if row is None:
return None return None
return row[0] return row['id']
# RENDERING ######################################################################################## # RENDERING ########################################################################################