Re: [Twisted-Python] adbapi and multiple queries in single

Phil thanks for the reply. I plan to do something similar. [sorry for not providing code snippet.] I have modified your code (please ignore queries and other errors) However I am not very much convinced with this approach, As you can see validate of ObjectGateway is a public method which also needs to be called from handleLogin. So I have to write two methods one public 'validate' and other private '_validate'. Any comments/suggestions or pointers to any existing applications/architecture documents with similar data access? class TableGateway(object): def _getUserName(self, trans, user_id): result = trans.execute('SELECT username FROM user WHERE id = %s', user_id) return result[0][0] def _sessionUpdateQuery(self, trans, sid, username): trans.execute('UPDATE session SET username = %s WHERE sid = %s', [username, sid]) def validateSid(self, txn, sid): res = txn.execute('select id from user where id=?', (sid,)) return True if res else False class ObjectGateway(object): def __init__(self, pool): self.pool = pool self.tblGw = TableGateway() def _validate(self, txn, sid): return self.tblGw.validateSid(txn, sid) def validate(self, sid): return self.pool.runInteraction(self._validate, sid) def handleLogin(self, sid, user_id): def _loginInteraction(trans): if not self._validate(trans, sid): raise "invalid sid" u = self.tblGw._getUserName(trans, user_id) self.tblGw._sessionUpdateQuery(trans, sid, username) return u return self.pool.runInteraction(_loginInteraction) Regards, vishal Date: Tue, 23 Jun 2009 10:25:16 -0400 From: Phil Christensen <phil@bubblehouse.org> Subject: Re: [Twisted-Python] adbapi and multiple queries in single transaction. To: Twisted general discussion <twisted-python@twistedmatrix.com> Message-ID: <2E70F4C7-5253-44B2-9DB8-79D8817E96F4@bubblehouse.org> Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes On Jun 23, 2009, at 9:45 AM, Vishal Shetye wrote:
Here's a stupidly trivial example:
class ObjectGateway(object): def __init__(self, pool): self.pool = pool def _getUserName(self, trans, user_id): result = trans.execute('SELECT username FROM user WHERE id = %s', user_id); return result[0][0] def _sessionUpdateQuery(self, trans, sid, username): trans.execute('UPDATE session SET username = %s WHERE sid = %s', [username, sid]); def handleLogin(self, sid, user_id): def _loginInteraction(trans): u = self._getUserName(trans, user_id) self._sessionUpdateQuery(trans, sid, username) return u return self.pool.runInteraction(_loginInteraction)
Hope this helps,
-phil DISCLAIMER ========== This e-mail may contain privileged and confidential information which is the property of Persistent Systems Ltd. It is intended only for the use of the individual or entity to which it is addressed. If you are not the intended recipient, you are not authorized to read, retain, copy, print, distribute or use this message. If you have received this communication in error, please notify the sender and delete all copies of this message. Persistent Systems Ltd. does not accept any liability for virus infected mails.
participants (1)
-
Vishal Shetye