mysql2pgsql/lib/postgres_writer.py
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 == '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(_get_total_seconds(row[index])).time().isoformat()
            else:
                row[index] = AsIs(row[index]).getquoted()
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 == '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(_get_total_seconds(row[index])).time().isoformat()
            else:
                row[index] = AsIs(row[index]).getquoted()
def process_row ( self , table , row ) : 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 == '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 ( _get_total_seconds ( row [ index ] ) ) . time ( ) . isoformat ( ) else : row [ index ] = AsIs ( row [ index ] ) . getquoted ( )
Examines row data from MySQL and alters
        the values when necessary to be compatible with
        sending to PostgreSQL via the copy command
Examines row data from MySQL and alters the values when necessary to be compatible with sending to PostgreSQL via the copy command