Hi list!

I have tried to create a function to return a statement that copies
data between tables. I have come up with this:

def copy_table(from_table, to_table, whereclause = None):
    columns_str = ", ".join(from_table.columns.keys())
    from_select = select([columns_str]).where(whereclause)
    from_select_str = str(from_select)
    return text("""INSERT INTO %(to_table)s (%(columns_str)s)
\n%(from_select_str)s""" % params )

(Don't know if this is mysql-only syntax, but it works for me at
least) If there is a better way of doing this, please enlighten me.

Now I'd like to add support for where-clauses, and I'm a bit stuck. I
tried something like this:

def copy_table(from_table, to_table, whereclause = None):
    columns_str = ", ".join(from_table.columns.keys())
    from_select = select([columns_str]).where(whereclause)
    from_select_str = str(from_select)

    where_str = whereclause.compile()
    params = locals()
    params.update(whereclause.compile().params)
    return text("""INSERT INTO %(to_table)s (%(columns_str)s) \nWHERE
%(where_str)s""" % params )

...which didn't quite work, I end up with %s in my query.

Nicest would be if I could somehow inherit from Insert or
ClauseElement, so generative where-clauses work, but I can't quite
figure out how to combine that with the visitor pattern used for the
different backends. Any hints on how I would accomplish this?

-- Joakim

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

Reply via email to