Binary COPY for psql - Mailing list pgsql-patches

From Andreas Pflug
Subject Binary COPY for psql
Date
Msg-id 4475CCB8.3050505@pse-consulting.de
Whole thread Raw
Responses Re: Binary COPY for psql
List pgsql-patches
The attached patch enables psql to copy binary data in and out.

Regards,
Andreas
Index: src/bin/psql/copy.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/bin/psql/copy.c,v
retrieving revision 1.60
diff -u -r1.60 copy.c
--- src/bin/psql/copy.c    5 Mar 2006 15:58:51 -0000    1.60
+++ src/bin/psql/copy.c    25 May 2006 15:17:58 -0000
@@ -284,9 +284,10 @@

             fetch_next = true;

-            /* someday allow BINARY here */
             if (pg_strcasecmp(token, "oids") == 0)
                 result->oids = true;
+            else if (pg_strcasecmp(token, "binary") == 0)
+                result->binary = true;
             else if (pg_strcasecmp(token, "csv") == 0)
                 result->csv_mode = true;
             else if (pg_strcasecmp(token, "header") == 0)
@@ -442,8 +443,6 @@
     initPQExpBuffer(&query);

     printfPQExpBuffer(&query, "COPY ");
-    if (options->binary)
-        appendPQExpBuffer(&query, "BINARY ");

     appendPQExpBuffer(&query, "%s ", options->table);

@@ -480,6 +479,9 @@
             appendPQExpBuffer(&query, " WITH NULL AS '%s'", options->null);
     }

+    if (options->binary)
+        appendPQExpBuffer(&query, " BINARY");
+
     if (options->csv_mode)
         appendPQExpBuffer(&query, " CSV");

@@ -622,7 +624,7 @@

         if (buf)
         {
-            fputs(buf, copystream);
+            fwrite(buf, 1, ret, copystream);
             PQfreemem(buf);
         }
     }
@@ -686,6 +688,21 @@
     else
         prompt = NULL;

+    if (!prompt)
+    {
+        int buflen;
+
+        while ((buflen = fread(buf, 1, COPYBUFSIZ, copystream)) > 0)
+        {
+            if (PQputCopyData(conn, buf, buflen) <= 0)
+            {
+                OK = false;
+                copydone = true;
+                break;
+            }
+        }
+    }
+    else
     while (!copydone)
     {                            /* for each input line ... */
         if (prompt)

pgsql-patches by date:

Previous
From: Andrew Dunstan
Date:
Subject: Re: plperl - put schema-name in $_TD
Next
From: Tom Lane
Date:
Subject: Re: plperl - put schema-name in $_TD