On Wed, 4 May 2016 22:47:59 +0200
Christoph Zwerschke <[email protected]> wrote:
> Am 04.05.2016 um 07:34 schrieb D'Arcy J.M. Cain:
> > If data is None use self for the dictionary.
> >
> > db = DB()
> > db['table_id'] = key
> > db.select('table)
> >
> > Thoughts?
>
> I would find it a bit confusing since the db instance would then
> combine two different responsibilities in one object, namely being
> both a database connection and a table row container. Normally you
> want to have a separation of concerns.
I do have cases where I merge the two into the same class. Of course I
can still do that in my own code.
class myDB(DB, dict):
def __init__(self, database):
DB.__init__(self, database)
self.select = DB.get
dict.__init__(self)
> Also, if you have two different tables, would you merge the values
> together in one dict? And what would the select method return? Would
> it return another DB() instance with the same underlying connection,
No, just like the get method now it would merge into the same dict.
data = ("table1_id": 12345)
db.get("table1", data) # table1 has a reference to table2
db.get("table2", data)
Now data has all the fields in both tables. The difference would be:
db["table1_id"] = 12345
db.select("table1")
db.select("table2")
> or would it return None and just change the existing instance? In the
Right now get changes the dict passed to it and returns the dict. That
actually violates a Pythonism (side effect or return but not both) so
it could be a chance to correct that. I would do something like this
for backwards compatibility:
def select(self, table, data=None):
if data: _data = data
else: _data = self
# ... get row and merge into _data
return data
> latter case, you could only work with one row at the same time, or
> you would need to open another connection. But the former case
> doesn't feel right, either.
My proposal would still allow for a dict to be passed which would be
used instead of self.
It does occur to me now that doing this only makes sense if you are
also creating specialized methods for managing the data so I guess it
doesn't hurt if I keep doing it in my own subclasses. There's no real
benefit to doing this in a raw database connection other than saving a
few keystrokes here and there.
--
D'Arcy J.M. Cain
PyGreSQL Development Group
http://www.PyGreSQL.org IM:[email protected]
_______________________________________________
PyGreSQL mailing list
[email protected]
https://mail.vex.net/mailman/listinfo.cgi/pygresql