so this one is stuck on a very old ticket that has a similar one for MySQL 
also:  http://www.sqlalchemy.org/trac/ticket/959

we have of course added these various extensions over time, such as UPDATE FROM 
and all that, so if you were working on USING it might be nice to actually 
build this as a feature.

the Delete construct does subclass the UpdateBase and we can pretty much 
imitate a simple version of what Update does, it has a @property “_extra_froms” 
we can just reuse (or we can re-implement) that searches the WHERE clause for 
more tables.  Then we just tack them on, not a big deal:

from sqlalchemy import *
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.sql.expression import Delete, Update
import re

@compiles(Delete)
def compile_delete(element, compiler, **kw):
    text = compiler.visit_delete(element, **kw)
    extra_froms = Update._extra_froms.__get__(element)
    if extra_froms:
        text = re.sub(
                    r"(FROM \S+)",
                    lambda m: "%s USING %s" % (
                        m.group(1),
                        ", ".join(
                            compiler.process(fr, asfrom=True, **kw)
                            for fr in extra_froms
                        )
                    ),
                    text
                )
    return text

m = MetaData()

t1 = Table('t1', m, Column('x', Integer))
t2 = Table('t2', m, Column('y', Integer))
t3 = Table('t3', m, Column('z', Integer))

print t1.delete().where(t1.c.x == t2.c.y)

# does USING support multiple tables?
print t1.delete().where(t1.c.x == t2.c.y).where(t2.c.y == t3.c.z)



On Nov 15, 2013, at 4:14 PM, Jon Nelson <jnel...@jamponi.net> wrote:

> How might one extend a Delete clause such that I can use USING in the 
> statement?
> Something like this (given t2 is a Table (or selectable?)):
> 
> Turn this (assuming SomeObject is tied to t1):
> 
> q = sess.query(SomeObject)
> q = q.filter(...)
> q = q.filter(SomeObject.some_column==t2.c.some_other_column)
> q.delete().with_hint('USING', [t2]))
> 
> DELETE
> FROM t1
> USING t2
> WHERE t1.some_column == t2.some_other_column
> ;
> 
> What if there are multiple Tables (or selectables) in to render?
> 
> 
> -- 
> Jon
> Software Blacksmith
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/groups/opt_out.

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to