]> git.ipfire.org Git - pbs.git/commitdiff
base: Add new functions to create objetcs easier
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 20 May 2023 09:38:03 +0000 (09:38 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 20 May 2023 09:38:03 +0000 (09:38 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/buildservice/__init__.py
src/buildservice/base.py
src/buildservice/database.py

index 4ac4770b31f2ab3c256ac2b2c1c1402d2d413116..e6045f39b6535bc0e2c6830ef9b9b36773edbf26 100644 (file)
@@ -114,7 +114,7 @@ class Backend(object):
 
                log.debug("Connecting to database %s @ %s" % (name, hostname))
 
-               return database.Connection(hostname, name, user=user, password=password)
+               return database.Connection(self, hostname, name, user=user, password=password)
 
        def _create_tmp_path(self):
                """
index da7a04de5cbd2e57eaa2de41488afd91fdc955e3..f2a920e84999d56c1aecb3856a0741964dd4286b 100644 (file)
@@ -48,12 +48,16 @@ class DataObject(Object):
        def __hash__(self):
                return hash(self.id)
 
-       def init(self, id, data=None):
+       def init(self, id, data=None, **kwargs):
                self.id = id
 
                if data:
                        self.data = data
 
+               # Set any extra arguments (to populate the cache)
+               for arg in kwargs:
+                       setattr(self, arg, kwargs[arg])
+
        @lazy_property
        def data(self):
                assert self.table, "Table name is not set"
index 27db380664e7dddfc570ad3cfec2908a1b4bf031..1ba98a5406d6ad1f1818ab1d9f2bee8c128eb5ac 100644 (file)
@@ -33,7 +33,8 @@ class Connection(object):
                We explicitly set the timezone to UTC and the character encoding to
                UTF-8 on all connections to avoid time zone and encoding errors.
        """
-       def __init__(self, host, database, user=None, password=None):
+       def __init__(self, backend, host, database, user=None, password=None):
+               self.backend = backend
                self.host = host
                self.database = database
 
@@ -194,6 +195,25 @@ class Connection(object):
        def transaction(self):
                return Transaction(self)
 
+       def fetch_one(self, cls, query, *args, **kwargs):
+               """
+                       Takes a class and a query and will return one object of that class
+               """
+               # Execute the query
+               res = self.get(query, *args)
+
+               # Return an object (if possible)
+               if res:
+                       return cls(self.backend, res.id, res, **kwargs)
+
+       def fetch_many(self, cls, query, *args, **kwargs):
+               # Execute the query
+               res = self.query(query, *args)
+
+               # Return a generator with objects
+               for row in res:
+                       yield cls(self.backend, row.id, row, **kwargs)
+
 
 class Row(dict):
        """A dict that allows for object-like property access syntax."""