diff options
author | Marko Kreen | 2009-11-03 12:21:28 +0000 |
---|---|---|
committer | Marko Kreen | 2009-11-03 12:43:44 +0000 |
commit | 8ea01dcac263f28babf12feec11c0f8435d6e0cf (patch) | |
tree | b0f98184fe343e147ab97ac158f5f7de02310c25 /python/skytools/sqltools.py | |
parent | 14dabdc95bc7bf507d8b8f18ad9163f07baa2d73 (diff) |
londiste: copy expression support
Diffstat (limited to 'python/skytools/sqltools.py')
-rw-r--r-- | python/skytools/sqltools.py | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/python/skytools/sqltools.py b/python/skytools/sqltools.py index 902bf9a6..76d73efa 100644 --- a/python/skytools/sqltools.py +++ b/python/skytools/sqltools.py @@ -373,23 +373,31 @@ class CopyPipe(object): self.buf.seek(0) self.buf.truncate() -def full_copy(tablename, src_curs, dst_curs, column_list = []): +def full_copy(tablename, src_curs, dst_curs, column_list = [], condition = None): """COPY table from one db to another.""" qtable = quote_fqident(tablename) if column_list: - qfields = [quote_ident(f) for f in column_list] - hdr = "%s (%s)" % (qtable, ",".join(qfields)) + qfields = ",".join([quote_ident(f) for f in column_list]) + src = dst = "%s (%s)" % (qtable, qfields) else: - hdr = qtable + qfields = '*' + src = dst = qtable + + if condition: + src = "(SELECT %s FROM %s WHERE %s)" % (qfields, qtable, condition) + if hasattr(src_curs, 'copy_expert'): - sql_to = "COPY %s TO stdout" % hdr - sql_from = "COPY %s FROM stdout" % hdr + sql_to = "COPY %s TO stdout" % src + sql_from = "COPY %s FROM stdin" % dst buf = CopyPipe(dst_curs, sql_from = sql_from) src_curs.copy_expert(sql_to, buf) else: - buf = CopyPipe(dst_curs, hdr) - src_curs.copy_to(buf, hdr) + if condition: + # regular psycopg copy_to generates invalid sql for subselect copy + raise Exception('copy_expert() is needed for conditional copy') + buf = CopyPipe(dst_curs, dst) + src_curs.copy_to(buf, src) buf.flush() return (buf.total_bytes, buf.total_rows) |