On Dec 29, 2009, at 11:02 PM, Douglas Alan wrote:

> 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?

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 sqlalch...@googlegroups.com.
> To unsubscribe from this group, send email to 
> sqlalchemy+unsubscr...@googlegroups.com.
> 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 sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.


Reply via email to