*** pg_dump.c.orig	Thu May  9 15:07:32 1996
--- pg_dump.c	Thu May  9 15:07:05 1996
***************
*** 47,52 ****
--- 47,53 ----
  int g_last_builtin_oid; /* value of the last builtin oid */
  FILE *g_fout;     /* the script file */
  PGconn *g_conn;   /* the database connection */
+ int dumpData; /* dump data using proper insert strings */
  
  char g_opaque_type[10]; /* name for the opaque type */
  
***************
*** 60,65 ****
--- 61,67 ----
  {
      fprintf(stderr, "usage:  %s [options] [dbname]\n",progname);
      fprintf(stderr, "\t -f filename \t\t script output filename\n");
+     fprintf(stderr, "\t -d          \t\t dump data as proper insert strings\n");
      fprintf(stderr, "\t -H hostname \t\t server host name\n");
      fprintf(stderr, "\t -p port     \t\t server port number\n");
      fprintf(stderr, "\t -v          \t\t verbose\n");
***************
*** 103,113 ****
      g_comment_end[0] = '\0';
      strcpy(g_opaque_type, "opaque");
  
!     schemaOnly = 0;
  
      progname = *argv;
  
!     while ((c = getopt(argc, argv,"f:H:p:vSD")) != EOF) {
  	switch(c) {
  	case 'f': /* output file name */
  	    filename = optarg;
--- 105,115 ----
      g_comment_end[0] = '\0';
      strcpy(g_opaque_type, "opaque");
  
!     schemaOnly = dumpData = 0;
  
      progname = *argv;
  
!     while ((c = getopt(argc, argv,"f:H:p:vSDd")) != EOF) {
  	switch(c) {
  	case 'f': /* output file name */
  	    filename = optarg;
***************
*** 124,129 ****
--- 126,134 ----
  	case 'S': /* dump schema only */
  	    schemaOnly = 1;
  	    break;
+         case 'd': /* dump data as proper insert strings */
+             dumpData = 1;
+             break;
  	default:
  	    usage(progname);
  	    break;
***************
*** 1277,1282 ****
--- 1282,1289 ----
      PGresult *res;
      int i;
      int ret;
+     int field;
+     int tuple;
      int copydone;
  
      for(i = 0; i < numTables; i++) {
***************
*** 1286,1324 ****
  	if (isArchiveName(classname))
  	    continue;
  
! 	fprintf(fout, "COPY %s from stdin;\n", classname);
! 	sprintf(query, "COPY %s to stdout;\n", classname);
! 	res = PQexec(g_conn, query);
! 	if (!res || 
! 	    PQresultStatus(res) != PGRES_COPY_OUT) {
! 	    fprintf(stderr,"dumpClasses(): COPY to stdout failed");
! 	    exit_nicely(g_conn);
! 	}
! 	copydone = 0;
! 	while (!copydone) {
! 	    ret = PQgetline(res->conn, copybuf, COPYBUFSIZ);
  	
! 	    if (copybuf[0] == '.' && copybuf[1] =='\0') {
! 		copydone = true;	/* don't print this... */
! 	    } else {
! 		fputs(copybuf, stdout);
! 		switch (ret) {
! 		case EOF:
! 		    copydone = true;
! 		    /*FALLTHROUGH*/
! 		case 0:
! 		    fputc('\n', stdout);
! 		    break;
! 		case 1:
! 		    break;
! 		}
  	    }
  	}
- 	fprintf(fout, ".\n");
- 	PQclear(res);
- 	PQendcopy(res->conn);
      }
-     
  }
  
  /*
--- 1293,1365 ----
  	if (isArchiveName(classname))
  	    continue;
  
!         if(!dumpData) {
! 	    fprintf(fout, "COPY %s from stdin;\n", classname);
! 	    sprintf(query, "COPY %s to stdout;\n", classname);
! 	    res = PQexec(g_conn, query);
! 	    if (!res || 
! 	        PQresultStatus(res) != PGRES_COPY_OUT) {
! 	        fprintf(stderr,"dumpClasses(): COPY to stdout failed");
! 	        exit_nicely(g_conn);
! 	    }
! 	    copydone = 0;
! 	    while (!copydone) {
! 	        ret = PQgetline(res->conn, copybuf, COPYBUFSIZ);
  	
! 	        if (copybuf[0] == '.' && copybuf[1] =='\0') {
! 	  	    copydone = true;	/* don't print this... */
! 	        } else {
! 	    	    fputs(copybuf, stdout);
! 		    switch (ret) {
! 		    case EOF:
! 		        copydone = true;
! 		        /*FALLTHROUGH*/
! 		    case 0:
! 		        fputc('\n', stdout);
! 		        break;
! 		    case 1:
! 		        break;
! 	    	    }
! 	        }
! 	    }
! 	    fprintf(fout, ".\n");
! 	    PQclear(res);
! 	    PQendcopy(res->conn);
!         } else {
! 	    sprintf(query, "select * from %s;\n", classname);
! 	    res = PQexec(g_conn, query);
! 	    if (!res ||
! 		PQresultStatus(res) != PGRES_TUPLES_OK) {
! 		fprintf(stderr,"dumpClasses(): command failed");
! 		exit_nicely(g_conn);
! 	    }
! 	    tuple=0;
! 	    while(tuple < PQntuples(res)) {
! 		fprintf(fout, "insert into %s values (", classname);
! 		field=0;
! 		do {
! 		    switch(PQftype(res,field)) {
! 			case 21: case 22: case 23: /* int types */
! 			case 810: case 910: /* oldint types */
! 			case 1005: case 1006: case 1007: /* _int types */
! 			case 700: case 701: /* float types */
! 			case 1021: case 1022: /* _float types */
! 			    fprintf(fout, "%s", PQgetvalue(res,tuple,field));
! 			    break;
! 		        default:  
! 			    fprintf(fout, "'%s'", PQgetvalue(res,tuple,field));
! 			    break;
! 		    }
! 		    field++;
! 		    if(field != PQnfields(res))
! 			fprintf(fout, ", ");
! 		} while(field < PQnfields(res));
! 		fprintf(fout, ");\n");
! 		tuple++;
  	    }
+ 	    PQclear(res);
  	}
      }
  }
  
  /*
