Use the DbTransaction type in the manager

The __conform__ can´t generate a tuple from the class, but it is still
worth to use the DB intermediate types for cleaner code. So add tuple()
method the the DBTransaction and use it when writing to the DB.
This commit is contained in:
Luís Murta 2022-10-09 23:09:37 +01:00
parent daed9c5814
commit ea3c75245a
Signed by: satprog
GPG Key ID: 169EF1BBD7049F94
3 changed files with 10 additions and 12 deletions

View File

@ -96,7 +96,7 @@ class DatabaseClient:
def insert_transactions(self, transactions: Q.DbTransactions): def insert_transactions(self, transactions: Q.DbTransactions):
logger.info(f"Adding {len(transactions)} into {self.db}") logger.info(f"Adding {len(transactions)} into {self.db}")
self.__executemany(Q.ADD_TRANSACTION, transactions) self.__executemany(Q.ADD_TRANSACTION, [t.tuple() for t in transactions])
def update_category(self, transaction: Transaction): def update_category(self, transaction: Transaction):
logger.info(f"Update {transaction} category") logger.info(f"Update {transaction} category")

View File

@ -1,6 +1,5 @@
from dataclasses import dataclass from dataclasses import dataclass
from decimal import Decimal from decimal import Decimal
import sqlite3
CREATE_TRANSACTIONS_TABLE = """ CREATE_TRANSACTIONS_TABLE = """
CREATE TABLE IF NOT EXISTS "transactions" ( CREATE TABLE IF NOT EXISTS "transactions" (
@ -25,15 +24,14 @@ class DbTransaction:
original: str original: str
comments: str comments: str
def __conform__(self, protocol): def tuple(self) -> tuple:
if protocol is sqlite3.PrepareProtocol: return (
return ( self.date,
self.date, self.description,
self.description, self.bank,
self.bank, self.value,
self.value, self.category,
self.category, )
)
DbTransactions = list[DbTransaction] DbTransactions = list[DbTransaction]

View File

@ -13,7 +13,7 @@ def convert(t):
@convert.register @convert.register
def _(t: Transaction) -> DbTransaction: def _(t: Transaction) -> DbTransaction:
return (t.date, t.description, t.bank, t.value, t.category) return DbTransaction(t.date, t.description, t.bank, t.value, t.category, t.original, t.additional_comment)
@convert.register @convert.register