395 raise |
395 raise |
396 finally: |
396 finally: |
397 cnx.commit() |
397 cnx.commit() |
398 cu.close() |
398 cu.close() |
399 |
399 |
400 def _create_copyfrom_buffer(data, columns, encoding='utf-8'): |
400 def _create_copyfrom_buffer(data, columns, encoding='utf-8', replace_sep=None): |
401 """ |
401 """ |
402 Create a StringIO buffer for 'COPY FROM' command. |
402 Create a StringIO buffer for 'COPY FROM' command. |
403 Deals with Unicode, Int, Float, Date... |
403 Deals with Unicode, Int, Float, Date... |
404 """ |
404 """ |
405 # Create a list rather than directly create a StringIO |
405 # Create a list rather than directly create a StringIO |
418 value = 'NULL' |
418 value = 'NULL' |
419 elif isinstance(value, (long, int, float)): |
419 elif isinstance(value, (long, int, float)): |
420 value = str(value) |
420 value = str(value) |
421 elif isinstance(value, (str, unicode)): |
421 elif isinstance(value, (str, unicode)): |
422 # Remove separators used in string formatting |
422 # Remove separators used in string formatting |
423 if u'\t' in value or u'\r' in value or u'\n' in value: |
423 for _char in (u'\t', u'\r', u'\n'): |
424 return |
424 if _char in value: |
|
425 # If a replace_sep is given, replace |
|
426 # the separator instead of returning None |
|
427 # (and thus avoid empty buffer) |
|
428 if replace_sep: |
|
429 value = value.replace(_char, replace_sep) |
|
430 else: |
|
431 return |
425 value = value.replace('\\', r'\\') |
432 value = value.replace('\\', r'\\') |
426 if not value: |
433 if value is None: |
427 return |
434 return |
428 if isinstance(value, unicode): |
435 if isinstance(value, unicode): |
429 value = value.encode(encoding) |
436 value = value.encode(encoding) |
430 elif isinstance(value, (date, datetime)): |
437 elif isinstance(value, (date, datetime)): |
431 # Do not use strftime, as it yields issue |
438 # Do not use strftime, as it yields issue |