From: Michael Tremer Date: Wed, 31 Dec 2025 13:11:44 +0000 (+0000) Subject: database: Add a custom SELECT method X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b4f6f473629b96941dd7696733a06a6a26c45cf1;p=dbl.git database: Add a custom SELECT method This is necessary because SQLModel only spits out the first column of any custom queries. Signed-off-by: Michael Tremer --- diff --git a/src/dnsbl/database.py b/src/dnsbl/database.py index 0eaff3b..2b165d5 100644 --- a/src/dnsbl/database.py +++ b/src/dnsbl/database.py @@ -18,6 +18,7 @@ # # ############################################################################### +import collections import functools import logging import sqlalchemy.orm @@ -120,6 +121,22 @@ class Database(object): # Return the object return object + def select(self, stmt, batch_size=128): + """ + Selects custom queries + """ + result = self.execute(stmt) + + # Process the result in batches + if batch_size: + result = result.yield_per(batch_size) + + # Return all objects + for object in result.mappings(): + row = collections.namedtuple("Row", object.keys()) + + yield row(**object) + def fetch(self, stmt, batch_size=128): """ Fetches objects of the given type