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 at bubblehouse.org>
Subject: Re: [Twisted-Python] adbapi and multiple queries in single
transaction.
To: Twisted general discussion <twisted-python at twistedmatrix.com>
Message-ID: <2E70F4C7-5253-44B2-9DB8-79D8817E96F4 at bubblehouse.org>
Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes
On Jun 23, 2009, at 9:45 AM, Vishal Shetye wrote:
>> I initially thought of putting all the queries per operation in a
>> runInteraction. However this results in code-duplication as many
>> queries are shared between different operations.
>It seems like the easiest way to deal with this would be to make
>'private' methods for all the standard queries; these methods/
>functions would accept a transaction object like you said, but the
>methods themselves would only be called from an interaction, which can
>supply the transaction object.
>Then in each public ObjectGateway method you can just define an inner
>function to serve as the interaction, calling each private query
>method in turn, using the transaction object provided to that
>interaction.
>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)
>Other than being careful not to mess around with the instance state
>during those interactions (they are running in a thread, after all),
>this should be pretty straightforward.
>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.