[dataimport] Allow to replace escape char in the create_copyfrom_buffer
authorVincent Michel <vincent.michel@logilab.fr>
Wed, 02 Jan 2013 18:17:02 +0100
changeset 8631 1053b9d0fdf7
parent 8627 5096071b7e1c
child 8632 fa044b9157d7
[dataimport] Allow to replace escape char in the create_copyfrom_buffer
dataimport.py
--- a/dataimport.py	Wed Sep 19 17:16:04 2012 +0200
+++ b/dataimport.py	Wed Jan 02 18:17:02 2013 +0100
@@ -397,7 +397,7 @@
         cnx.commit()
         cu.close()
 
-def _create_copyfrom_buffer(data, columns, encoding='utf-8'):
+def _create_copyfrom_buffer(data, columns, encoding='utf-8', replace_sep=None):
     """
     Create a StringIO buffer for 'COPY FROM' command.
     Deals with Unicode, Int, Float, Date...
@@ -420,10 +420,17 @@
                 value = str(value)
             elif isinstance(value, (str, unicode)):
                 # Remove separators used in string formatting
-                if u'\t' in value or u'\r' in value or u'\n' in value:
-                    return
+                for _char in (u'\t', u'\r', u'\n'):
+                    if _char in value:
+                        # If a replace_sep is given, replace
+                        # the separator instead of returning None
+                        # (and thus avoid empty buffer)
+                        if replace_sep:
+                            value = value.replace(_char, replace_sep)
+                        else:
+                            return
                 value = value.replace('\\', r'\\')
-                if not value:
+                if value is None:
                     return
                 if isinstance(value, unicode):
                     value = value.encode(encoding)