Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Several fixes to copy methods

\N default for null fixed, better resources handling on error.
  • Loading branch information...
commit c861fc33ffbf881d02dd254cdf3ebb9fc1a35f85 1 parent 4f4e385
Daniele Varrazzo authored October 21, 2011

Showing 1 changed file with 24 additions and 18 deletions. Show diff stats Hide diff stats

  1. 42  psycopg2ct/_impl/cursor.py
42  psycopg2ct/_impl/cursor.py
@@ -404,7 +404,7 @@ def mogrify(self, query, vars=None):
404 404
 
405 405
     @check_closed
406 406
     @check_async
407  
-    def copy_from(self, file, table, sep='\t', null='\N', size=8192,
  407
+    def copy_from(self, file, table, sep='\t', null='\\N', size=8192,
408 408
                   columns=None):
409 409
         """Reads data from a file-like object appending them to a database
410 410
         table (COPY table FROM file syntax).
@@ -419,20 +419,22 @@ def copy_from(self, file, table, sep='\t', null='\N', size=8192,
419 419
         else:
420 420
             columns_str = ''
421 421
 
422  
-        query = "COPY %s%s FROM stdin WITH DELIMITER AS %s" % (
423  
-            table, columns_str, util.quote_string(self._conn, sep))
424  
-        if null:
425  
-            query += " NULL AS %s" % util.quote_string(self._conn, null)
  422
+        query = "COPY %s%s FROM stdin WITH DELIMITER AS %s NULL AS %s" % (
  423
+            table, columns_str,
  424
+            util.quote_string(self._conn, sep),
  425
+            util.quote_string(self._conn, null))
426 426
 
427 427
         self._copysize = size
428 428
         self._copyfile = file
429  
-        self._pq_execute(query)
430  
-        self._copyfile = None
431  
-        self._copysize = None
  429
+        try:
  430
+            self._pq_execute(query)
  431
+        finally:
  432
+            self._copyfile = None
  433
+            self._copysize = None
432 434
 
433 435
     @check_closed
434 436
     @check_async
435  
-    def copy_to(self, file, table, sep='\t', null='\N', columns=None):
  437
+    def copy_to(self, file, table, sep='\t', null='\\N', columns=None):
436 438
         """Writes the content of a table to a file-like object (COPY table
437 439
         TO file syntax).
438 440
 
@@ -446,18 +448,20 @@ def copy_to(self, file, table, sep='\t', null='\N', columns=None):
446 448
         else:
447 449
             columns_str = ''
448 450
 
449  
-        query = "COPY %s%s TO stdout WITH DELIMITER AS %s" % (
450  
-            table, columns_str, util.quote_string(self._conn, sep))
451  
-        if null:
452  
-            query += " NULL AS %s" % util.quote_string(self._conn, null)
  451
+        query = "COPY %s%s TO stdout WITH DELIMITER AS %s NULL AS %s" % (
  452
+            table, columns_str,
  453
+            util.quote_string(self._conn, sep),
  454
+            util.quote_string(self._conn, null))
453 455
 
454 456
         self._copyfile = file
455  
-        self._pq_execute(query)
456  
-        self._copyfile = None
  457
+        try:
  458
+            self._pq_execute(query)
  459
+        finally:
  460
+            self._copyfile = None
457 461
 
458 462
     @check_closed
459 463
     @check_async
460  
-    def copy_expert(self, sql, file, size=8196):
  464
+    def copy_expert(self, sql, file, size=8192):
461 465
         if not sql:
462 466
             return
463 467
 
@@ -466,8 +470,10 @@ def copy_expert(self, sql, file, size=8196):
466 470
                 " COPY FROM; a writeable file-like object for COPY TO.")
467 471
 
468 472
         self._copyfile = file
469  
-        self._pq_execute(sql)
470  
-        self._copyfile = None
  473
+        try:
  474
+            self._pq_execute(sql)
  475
+        finally:
  476
+            self._copyfile = None
471 477
 
472 478
     @check_closed
473 479
     def setinputsizes(self, sizes):

0 notes on commit c861fc3

Please sign in to comment.
Something went wrong with that request. Please try again.