On Aug 31, 2010, at 8:51 PM, Petr Kobalíček wrote:
> I solved everything by overriding against()
>
> _DDL_MATCHER = re.compile(ur"\$\{(_\w+)\:{0,1}([^\}]*)\}")
>
> class CustomDDL(DDL):
> def __init__(self, statement, on=None, context=None, bind=None):
> super(CustomDDL, self).__init__(statement, on, context, bind)
>
> def against(self, target):
> global _DDL_MATCHER
> def repl(match):
> func = match.group(1)
> args = match.group(2)
> return getattr(self, func)(target, args)
> return _DDL_MATCHER.sub(repl, self.statement);
>
> This works for me.
>
> I'm getting engine from target.metadata.bind and I hope that it's correct way.
Its not the best way since the creation sequence runs on a single connection,
which could potentially be on a single transaction. Working with "bind"
pulls you out of that state which would be problematic if you were using
transactions on a transactional DDL backend like Postgresql.
>
> On Wed, Sep 1, 2010 at 2:15 AM, Petr Kobalíček <[email protected]>
> wrote:
>> Hi,
>>
>> I have problem to understand which way should be used to write custom
>> DDL class. I'm generating some stored procedures for my tables. The
>> procedures are very similar and I'd like to use some generic way. Now
>> I'm nearly done, but I need some way how to extract some information
>> from table to use it in my DDL.
>>
>> I discovered that for this I need the engine where DDL runs.
>>
>> My custom class may look like this:
>>
>> class CustomDDL(DDL):
>> def against(self, target):
>> # Is this the method I should generate the DDL? How can I access
>> engine from here?
>> self.target = target
>>
>> There is also possibility to create a __call__ method where the engine
>> is, but I'm not sure if this is the right place.
>>
>> So my questions:
>>
>> - which method I should override to make my custom DDL substitution
>> - do I need to base class on DDL or DDLElement? I can create my own,
>> but I'm not sure if I can add it.
>>
>>
>>
>> Currently I'm using something like this to inject some triggers/functions:
>>
>> _DDL_MATCHER = re.compile(ur"\$\{(_\w+)\:{0,1}([^\}]*)\}")
>>
>> class CustomDDL(object):
>> def __init__(self, event, ddl):
>> self.event = event
>> self.ddl = ddl
>>
>> def inject(self, table):
>> DDL(self.compile(table)).execute_at(self.event, table)
>>
>> def compile(self, table):
>> global _DDL_MATCHER
>> def repl(match):
>> func = match.group(1)
>> args = match.group(2)
>> return getattr(self, func)(table, args)
>> return _DDL_MATCHER.sub(repl, self.ddl);
>>
>> def _TABLE(self, table, args):
>> return table.name + args
>>
>> def _PREPARE_DECLARATION(self, table, args):
>> result = u""
>> if hasattr(table, "area"):
>> for column in table.area:
>> #result += u"condition_" + unicode(column.name) + u" " + \
>> # unicode(column.type.get_dbapi_type(engine.dialect.dbapi)) + u"; "
>> pass
>> return result
>>
>> def _PREPARE_CONDITION(self, table, args):
>> return ""
>>
>> def _WHERE(self, table, args):
>> if args:
>> return u"WHERE " + args
>> else:
>> return u""
>>
>> This works for me, except I need to access the engine.
>>
>> Thanks for any info about this
>>
>> Best regards
>> Petr
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/sqlalchemy?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.