Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
Add Comment
Please, Sign In to add comment