[Tutor] get name of calling class at runtime
Kent Johnson
kent37 at tds.net
Sat Aug 22 15:22:01 CEST 2009
On Sat, Aug 22, 2009 at 3:54 AM, Alan Gauld<alan.gauld at btinternet.com> wrote:
> "Kent Johnson" <kent37 at tds.net> wrote
>
>>>> can group a bunch of SQL statements in one place; they're currently
>>>> scattered all over the program and it's getting unwieldy).
>>>
>>> Normally in an OO program the SQL for each class is in the methods for
>>> that
>>> class. That way any changes to the class canbe easily reflected in the
>>> related SQL.
>>
>> But if all the classes need nearly the same SQL, it may make sense to
>> abstract that out to a common location.That is what Serdar is trying
>> to do. Duplication is also a bad smell.
>
> Thats not what he said, he says he wants to collect SQL thats scattered
> into one location. And his pseudo code has the classic procedural
> case statement to select the right SQL depending on class. He is not
> calling a single parameterised piece of SQL... Thats the bad smell.
Ah, right. I was reading more into the OP than was there.
I think it does makes sense to keep SQL confined to well-defined
locations. This can be per class - each class holds its own SQL - or a
single class that does all database access so all SQL and database
dependencies are in one place. But in this case if different clients
needed different functionality I would make different methods for
them. Rather than
class DataSources(object):
def getdata(self, caller):
if caller == 'CallerA':
# execute sql for callerA
elif caller == 'CallerB':
#execute sql for callerB
it is reasonable to have
class DataSources(object):
def getAdata(self):
# execute sql for callerA
def getBdata(self):
#execute sql for callerB
etc.
Kent
More information about the Tutor
mailing list