From c260909ab47574122beea3570ed7d1adaaf3b076 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Sat, 9 Nov 2024 12:40:51 -0500 Subject: [PATCH] Add Database._sqlite3_function() for convenience --- lib/nntp/tiny/db.py | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/lib/nntp/tiny/db.py b/lib/nntp/tiny/db.py index 318251e..4f4896d 100644 --- a/lib/nntp/tiny/db.py +++ b/lib/nntp/tiny/db.py @@ -117,28 +117,22 @@ class Database(): def get(self, table, values: dict=dict()): return self.query(table, values).fetchone() - def min(self, table, column: str) -> int: - sql = f"select min({column}) as _min from {table.name}" - - row = self.db.execute(sql).fetchone() - - return row[0] if row is not None else None - - def max(self, table, column: str) -> int: - sql = f"select max({column}) as _max from {table.name}" - - row = self.db.execute(sql).fetchone() - - return row[0] if row is not None else None - - def count(self, table, values: dict=dict()): - sql = f"select count(id) as num from {table.name}" + def _sqlite3_function(self, table, fn: str, column: str, values: dict=dict()) -> int: + sql = f"select {fn}({column}) as ret from {table.name}" if len(values) > 0: sql += " where " sql += " and ".join([f"{k} = ?" for k in values]) - cr = self.db.execute(sql, list(values.values())) - row = cr.fetchone() + row = self.db.execute(sql, list(values.values())).fetchone() return row[0] if row is not None else None + + def min(self, table, column: str, values: dict=dict()) -> int: + return self._sqlite3_function(table, 'min', column, values) + + def max(self, table, column: str, values: dict=dict()) -> int: + return self._sqlite3_function(table, 'max', column, values) + + def count(self, table, values: dict=dict()) -> int: + return self._sqlite3_function(table, 'count', table.key, values)