def copy_from(self, file, table, sep='\t', null='\N', size=8192, columns=None): """Reads data from a file-like object appending them to a database table (COPY table FROM file syntax). The source file must have both read() and readline() method. TODO: Improve error handling """ if columns: columns_str = '(%s)' % ','.join([column for column in columns]) else: columns_str = '' query = "COPY %s%s FROM stdin WITH DELIMITER AS %s" % ( table, columns_str, util.quote_string(self._conn, sep)) if null: query += " NULL AS %s" % util.quote_string(self._conn, null) self._copysize = size self._copyfile = file self._pq_execute(query) self._copyfile = None self._copysize = None
def copy_to(self, file, table, sep='\t', null='\N', columns=None): """Writes the content of a table to a file-like object (COPY table TO file syntax). The target file must have a write() method. TODO: Improve error handling """ if columns: columns_str = '(%s)' % ','.join([column for column in columns]) else: columns_str = '' query = "COPY %s%s TO stdout WITH DELIMITER AS %s" % ( table, columns_str, util.quote_string(self._conn, sep)) if null: query += " NULL AS %s" % util.quote_string(self._conn, null) self._copyfile = file self._pq_execute(query) self._copyfile = None
def _execute_tpc_command(self, command, xid): cmd = '%s %s' % (command, util.quote_string(self, str(xid))) self._execute_command(cmd) self._mark += 1
def _set_guc(self, name, value): """Set the value of a configuration parameter.""" if value.lower() != 'default': value = util.quote_string(self, value) self._execute_command('SET %s TO %s' % (name, value))