Q. Is it possible to set a default .prefix_with() for a table?

95 views
Skip to first unread message

Douglas Alan

unread,
Dec 29, 2009, 11:02:36 PM12/29/09
to sqlalchemy
Hi. I know how to use the .prefix_with() method of an Insert object in
order to, for example, add an Oracle hint (e.g., /*+ append */) to a
table insertion. What I don't know how to do is to make this prefix be
the the default for a table. I want to do this so that when using
SQLAlchemy's ORM and mappers and sessions, etc., rather than issuing
explicit insert() expressions, I can still supply the aforementioned
Oracle hint.

Is there a way to do what I want to do?

Thanks,
|>ouglas

Michael Bayer

unread,
Dec 29, 2009, 11:30:33 PM12/29/09
to sqlal...@googlegroups.com

I'm not sure exactly what hint would be appropriate to add in all inserts, but the general idea can be accompished using this technique (recent 0.5 and 0.6 versions):

from sqlalchemy.ext.compiler import compiles
from sqlalchemy.sql import expression

@compiles(expression.Insert)
def annotated_insert(insert, compiler, **kw):
return compiler.visit_insert(insert.prefix_with('/* foo */'), **kw)


that will register the annotated_insert function as the compiler for the Insert construct, globally for the whole application. Within annotated_insert you'd probably want to add rules such that your prefix_with() is only registered for the tables and conditions that you'd want. Such as, if you'd like to attach an attribute "my_prefixes" to your Table object, it would be detected as such:

@compiles(expression.Insert)
def annotated_insert(insert, compiler, **kw):
if getattr(insert.table, 'my_prefixes', None):
insert = insert.prefix_with('/* %s */' % insert.table.my_prefixes)
return compiler.visit_insert(insert, **kw)

When the ORM inserts rows, it uses the same insert() construct and the prefix logic above will take effect.

>
> Thanks,
> |>ouglas
>
> --
>
> You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
> To post to this group, send email to sqlal...@googlegroups.com.
> To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
>
>

Douglas Alan

unread,
Dec 29, 2009, 11:51:49 PM12/29/09
to sqlalchemy
On Dec 29, 11:30 pm, Michael Bayer <mike...@zzzcomputing.com> wrote:

> I'm not sure exactly what hint would be appropriate to add in all inserts

Ah, I should have been more clear: I want to have a default prefix_with
() for insertions on a specific table, not for insertions on *all*
tables.

I think that the second example that you included should work fine for
that purpose. Thanks!

|>ouglas

Reply all
Reply to author
Forward
0 new messages