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):
"""
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"
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
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."""