Skip to content

Instantly share code, notes, and snippets.

@gregglind
Created November 11, 2010 16:19
Show Gist options
  • Save gregglind/672712 to your computer and use it in GitHub Desktop.
Save gregglind/672712 to your computer and use it in GitHub Desktop.
use native pg 'copy' to or from...
def pgsql_copy(session,tablename,string_buffer,direction="from"):
"""
Args:
session: a SA session that with a started transaction.
tablename: the table to write into
string_buffer: handle to a filelike, with seek set to 0
direction: from|to, which way it should go.
sele
Returns:
True on success, raises on errror
Note:
1. This isn't really containable in a session, given pgcopy.
2. If you want to use a selectable in 'to' copying,
wrap it in parens as shown in example below.
3. 'from' means from string_buffer to db
'to' is from db to string_buffer
example:
## hack: selectables for 'to' need to be wrapped in parens...
# psql_copy(session, "(select 1)", buf, 'to') # does....
# > COPY (select 1) TO STDOUT WITH DELIMITER E'\t';
"""
if direction not in ('from','to'):
raise ValueError("direction must be one of: from|to")
if direction == 'from':
SQL = '''COPY %(tablename)s FROM STDIN USING DELIMITERS E'\t' ''' \
% (dict(tablename=tablename))
else:
SQL = '''COPY %(tablename)s TO STDOUT WITH DELIMITER E'\t';''' \
% (dict(tablename=tablename))
connection = session.connection()
# We need the raw psycopg-cursor, hidden deep within SA's abstractions.
cursor = connection.connection.cursor().cursor
cursor.copy_expert(SQL, string_buffer)
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment