def process_row(self, table, row): """Examines row data from MySQL and alters the values when necessary to be compatible with sending to PostgreSQL via the copy command """ for index, column in enumerate(table.columns): hash_key = hash(frozenset(column.items())) column_type = self.column_types[ hash_key] if hash_key in self.column_types else self.column_type( column) if row[index] == None and ('timestamp' not in column_type or not column['default']): row[index] = '\N' elif row[index] == None and column['default']: if self.tz: row[index] = '1970-01-01T00:00:00.000000' + self.tz_offset else: row[index] = '1970-01-01 00:00:00' elif 'bit' in column_type: row[index] = bin(ord(row[index]))[2:] elif isinstance(row[index], (str, unicode, basestring)): if column_type == 'boolean': row[index] = 't' if row[index] == '\x01' else 'f' if row[ index] == '\x00' or row[index] == '' else row[index] elif column_type == 'bytea': row[index] = Binary(row[index]).getquoted( )[1:-8] if row[index] else row[index] elif 'text[' in column_type: row[index] = '{%s}' % ','.join( '"%s"' % v.replace('"', r'\"') for v in row[index].split(',')) else: row[index] = row[index].replace('\\', r'\\').replace( '\n', r'\n').replace('\t', r'\t').replace('\r', r'\r').replace('\0', '') elif column_type == 'boolean': # We got here because you used a tinyint(1), if you didn't want a bool, don't use that type row[index] = 't' if row[index] not in ( None, 0) else 'f' if row[index] == 0 else row[index] elif isinstance(row[index], (date, datetime)): if isinstance(row[index], datetime) and self.tz: try: if row[index].tzinfo: row[index] = row[index].astimezone( self.tz).isoformat() else: row[index] = datetime(*row[index].timetuple()[:6], tzinfo=self.tz).isoformat() except Exception as e: print e.message else: row[index] = row[index].isoformat() elif isinstance(row[index], timedelta): row[index] = datetime.utcfromtimestamp( row[index].total_seconds()).time().isoformat() else: row[index] = AsIs(row[index]).getquoted()
def handle_wiki_content_versions(pgconn, table_name, columns, record): """ :param pgconn: :param table_name: :param columns: :param record: :return: """ data = dict(record) log.debug("wiki_content_versions handler: {}".format(data)) data['data'] = Binary(data['data'].encode('utf-8')) return handle_standard_table(pgconn, table_name, columns, data)
def process_row(self, table, row): """Examines row data from MySQL and alters the values when necessary to be compatible with sending to PostgreSQL via the copy command """ for index, column in enumerate(table.columns): column_type = self.column_type(column) if row[index] == None and ('timestamp' not in column_type or not column['default']): row[index] = '\N' elif row[index] == None and column['default']: row[index] = '1970-01-01 00:00:00' elif 'bit' in column_type: row[index] = bin(ord(row[index]))[2:] elif row[index].__class__ in (str, unicode): if column_type == 'bytea': row[index] = Binary(row[index]).getquoted( )[1:-8] if row[index] else row[index] elif 'text[' in column_type: row[index] = '{%s}' % ','.join( '"%s"' % v.replace('"', r'\"') for v in row[index].split(',')) else: row[index] = row[index].replace('\\', r'\\').replace( '\n', r'\n').replace('\t', r'\t').replace('\r', r'\r').replace('\0', '') elif column_type == 'boolean': row[index] = 't' if row[ index] == 1 else 'f' if row[index] == 0 else row[index] elif row[index].__class__ in (date, datetime): row[index] = row[index].isoformat() elif row[index].__class__ is timedelta: row[index] = datetime.utcfromtimestamp( row[index].total_seconds()).time().isoformat() else: row[index] = AsIs(row[index]).getquoted()
def python_prewrite(self, geom): return Binary(geom.to_ewkb()) if geom is not None else None