From ea3c75245aa633e80a5a03d54b601cfde2dfecf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Murta?= Date: Sun, 9 Oct 2022 23:09:37 +0100 Subject: [PATCH] Use the DbTransaction type in the manager MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- pfbudget/db/client.py | 2 +- pfbudget/db/schema.py | 18 ++++++++---------- pfbudget/utils/converters.py | 2 +- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/pfbudget/db/client.py b/pfbudget/db/client.py index c122126..90191e1 100644 --- a/pfbudget/db/client.py +++ b/pfbudget/db/client.py @@ -96,7 +96,7 @@ class DatabaseClient: def insert_transactions(self, transactions: Q.DbTransactions): 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): logger.info(f"Update {transaction} category") diff --git a/pfbudget/db/schema.py b/pfbudget/db/schema.py index 78909d0..1b5eb24 100644 --- a/pfbudget/db/schema.py +++ b/pfbudget/db/schema.py @@ -1,6 +1,5 @@ from dataclasses import dataclass from decimal import Decimal -import sqlite3 CREATE_TRANSACTIONS_TABLE = """ CREATE TABLE IF NOT EXISTS "transactions" ( @@ -25,15 +24,14 @@ class DbTransaction: original: str comments: str - def __conform__(self, protocol): - if protocol is sqlite3.PrepareProtocol: - return ( - self.date, - self.description, - self.bank, - self.value, - self.category, - ) + def tuple(self) -> tuple: + return ( + self.date, + self.description, + self.bank, + self.value, + self.category, + ) DbTransactions = list[DbTransaction] diff --git a/pfbudget/utils/converters.py b/pfbudget/utils/converters.py index 3622e49..106b65d 100644 --- a/pfbudget/utils/converters.py +++ b/pfbudget/utils/converters.py @@ -13,7 +13,7 @@ def convert(t): @convert.register 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