Skip site navigation (1) Skip section navigation (2)

Peripheral Links

Header And Logo

PostgreSQL
| The world's most advanced open source database.

Site Navigation

Search archives
  Advanced Search

Update psql and pg_dump for new COPY api


  • From: Christopher Kings-Lynne <chriskl(at)familyhealth(dot)com(dot)au>
  • To: pgsql-patches(at)postgresql(dot)org
  • Subject: Update psql and pg_dump for new COPY api
  • Date: Fri, 25 Mar 2005 13:45:37 +0800 (WST)
  • Message-id: <20050325134452.V77464@houston.internal> <text/plain>

This patch updates psql and pg_dump to use the new copy api. Probably needs some review.

I have tested it with dos and unix newlines, etc.

Chris
? src/bin/initdb/.deps
? src/bin/initdb/initdb
? src/bin/ipcclean/ipcclean
? src/bin/pg_config/.deps
? src/bin/pg_config/pg_config
? src/bin/pg_controldata/.deps
? src/bin/pg_controldata/pg_controldata
? src/bin/pg_ctl/.deps
? src/bin/pg_ctl/pg_ctl
? src/bin/pg_dump/.deps
? src/bin/pg_dump/file
? src/bin/pg_dump/pg_dump
? src/bin/pg_dump/pg_dumpall
? src/bin/pg_dump/pg_restore
? src/bin/pg_resetxlog/.deps
? src/bin/pg_resetxlog/pg_resetxlog
? src/bin/psql/.deps
? src/bin/psql/dos.sql
? src/bin/psql/dump.sql
? src/bin/psql/dump2.sql
? src/bin/psql/file.sql
? src/bin/psql/psql
? src/bin/psql/test.sql
? src/bin/scripts/.deps
? src/bin/scripts/clusterdb
? src/bin/scripts/createdb
? src/bin/scripts/createlang
? src/bin/scripts/createuser
? src/bin/scripts/dropdb
? src/bin/scripts/droplang
? src/bin/scripts/dropuser
? src/bin/scripts/vacuumdb
Index: src/bin/pg_dump/pg_dump.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v
retrieving revision 1.404
diff -c -r1.404 pg_dump.c
*** src/bin/pg_dump/pg_dump.c	14 Mar 2005 18:57:33 -0000	1.404
--- src/bin/pg_dump/pg_dump.c	25 Mar 2005 05:44:27 -0000
***************
*** 776,783 ****
   *	  to be dumped.
   */
  
- #define COPYBUFSIZ		8192
- 
  static int
  dumpTableData_copy(Archive *fout, void *dcontext)
  {
--- 776,781 ----
***************
*** 790,796 ****
  	PGresult   *res;
  	int			ret;
  	bool		copydone;
! 	char		copybuf[COPYBUFSIZ];
  	const char *column_list;
  
  	if (g_verbose)
--- 788,794 ----
  	PGresult   *res;
  	int			ret;
  	bool		copydone;
! 	char		*copybuf;
  	const char *column_list;
  
  	if (g_verbose)
***************
*** 836,863 ****
  
  	while (!copydone)
  	{
! 		ret = PQgetline(g_conn, copybuf, COPYBUFSIZ);
! 
! 		if (copybuf[0] == '\\' &&
! 			copybuf[1] == '.' &&
! 			copybuf[2] == '\0')
! 		{
! 			copydone = true;	/* don't print this... */
! 		}
! 		else
! 		{
! 			archputs(copybuf, fout);
! 			switch (ret)
! 			{
! 				case EOF:
! 					copydone = true;
! 					/* FALLTHROUGH */
! 				case 0:
! 					archputs("\n", fout);
! 					break;
! 				case 1:
! 					break;
! 			}
  		}
  
  		/*
--- 834,855 ----
  
  	while (!copydone)
  	{
! 		ret = PQgetCopyData(g_conn, &copybuf, false);
! 		switch (ret) {
! 			case -1:
! 				copydone = true;
! 				break;
! 			case 0:
! 			case -2:
! 				write_msg(NULL, "SQL command to dump the contents of table \"%s\" failed: PQgetCopyData() failed.\n", classname);
! 				write_msg(NULL, "Error message from server: %s", PQerrorMessage(g_conn));
! 				write_msg(NULL, "The command was: %s\n", q->data);
! 				exit_nicely();
! 				break;
! 			default:
! 				archputs(copybuf, fout);
! 				PQfreemem(copybuf);
! 				break;
  		}
  
  		/*
***************
*** 903,917 ****
  	}
  	archprintf(fout, "\\.\n\n\n");
  
- 	ret = PQendcopy(g_conn);
- 	if (ret != 0)
- 	{
- 		write_msg(NULL, "SQL command to dump the contents of table \"%s\" failed: PQendcopy() failed.\n", classname);
- 		write_msg(NULL, "Error message from server: %s", PQerrorMessage(g_conn));
- 		write_msg(NULL, "The command was: %s\n", q->data);
- 		exit_nicely();
- 	}
- 
  	PQclear(res);
  	destroyPQExpBuffer(q);
  	return 1;
--- 895,900 ----
Index: src/bin/psql/copy.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/bin/psql/copy.c,v
retrieving revision 1.56
diff -c -r1.56 copy.c
*** src/bin/psql/copy.c	22 Feb 2005 04:40:54 -0000	1.56
--- src/bin/psql/copy.c	25 Mar 2005 05:44:28 -0000
***************
*** 580,589 ****
  	return success;
  }
  
- 
- #define COPYBUFSIZ 8192			/* size doesn't matter */
- 
- 
  /*
   * handleCopyOut
   * receives data as a result of a COPY ... TO stdout command
--- 580,585 ----
***************
*** 598,639 ****
  handleCopyOut(PGconn *conn, FILE *copystream)
  {
  	bool		copydone = false;		/* haven't started yet */
! 	char		copybuf[COPYBUFSIZ];
  	int			ret;
  
  	while (!copydone)
  	{
! 		ret = PQgetline(conn, copybuf, COPYBUFSIZ);
! 
! 		if (copybuf[0] == '\\' &&
! 			copybuf[1] == '.' &&
! 			copybuf[2] == '\0')
! 		{
! 			copydone = true;	/* we're at the end */
! 		}
! 		else
! 		{
! 			fputs(copybuf, copystream);
! 			switch (ret)
! 			{
! 				case EOF:
! 					copydone = true;
! 					/* FALLTHROUGH */
! 				case 0:
! 					fputc('\n', copystream);
! 					break;
! 				case 1:
! 					break;
! 			}
  		}
  	}
  	fflush(copystream);
- 	ret = !PQendcopy(conn);
  	ResetCancelConn();
! 	return ret;
  }
  
! 
  
  /*
   * handleCopyIn
--- 594,627 ----
  handleCopyOut(PGconn *conn, FILE *copystream)
  {
  	bool		copydone = false;		/* haven't started yet */
! 	char		*copybuf;
  	int			ret;
  
  	while (!copydone)
  	{
! 		ret = PQgetCopyData(conn, &copybuf, false);
! 		switch (ret) {
! 			case -1:
! 				copydone = true;
! 				break;
! 			case 0:
! 			case -2:
! 				fflush(copystream);
! 				ResetCancelConn();
! 				return false;
! 				break;
! 			default:
! 				fputs(copybuf, copystream);
! 				PQfreemem(copybuf);
! 				break;
  		}
  	}
  	fflush(copystream);
  	ResetCancelConn();
! 	return true;
  }
  
! #define COPYBUFSIZ 8192                       /* size doesn't matter */
  
  /*
   * handleCopyIn
***************
*** 656,661 ****
--- 644,650 ----
  	bool		saw_cr = false;
  	char		copybuf[COPYBUFSIZ];
  	char	   *s;
+ 	int			len;
  	int			bufleft;
  	int			c = 0;
  	int			ret;
***************
*** 686,691 ****
--- 675,681 ----
  		{						/* for each bufferload in line ... */
  			/* Fetch string until \n, EOF, or buffer full */
  			s = copybuf;
+ 			len = 0;
  			for (bufleft = COPYBUFSIZ - 1; bufleft > 0; bufleft--)
  			{
  				c = getc(copystream);
***************
*** 695,702 ****
--- 685,694 ----
  					break;
  				}
  				*s++ = c;
+ 				len++;
  				if (c == '\n')
  				{
+ 					if (saw_cr) len--;
  					linedone = true;
  					break;
  				}
***************
*** 704,709 ****
--- 696,702 ----
  					saw_cr = true;
  			}
  			*s = '\0';
+ 
  			/* EOF with empty line-so-far? */
  			if (c == EOF && s == copybuf && firstload)
  			{
***************
*** 711,727 ****
  				 * We are guessing a little bit as to the right
  				 * line-ending here...
  				 */
- 				if (saw_cr)
- 					PQputline(conn, "\\.\r\n");
- 				else
- 					PQputline(conn, "\\.\n");
  				copydone = true;
  				if (pset.cur_cmd_interactive)
  					puts("\\.");
  				break;
  			}
  			/* No, so pass the data to the backend */
! 			PQputline(conn, copybuf);
  			/* Check for line consisting only of \. */
  			if (firstload)
  			{
--- 704,718 ----
  				 * We are guessing a little bit as to the right
  				 * line-ending here...
  				 */
  				copydone = true;
  				if (pset.cur_cmd_interactive)
  					puts("\\.");
  				break;
  			}
  			/* No, so pass the data to the backend */
! 			ret = PQputCopyData(conn, copybuf, len);
! 			if (ret != 1)
! 				return false;
  			/* Check for line consisting only of \. */
  			if (firstload)
  			{
***************
*** 736,742 ****
  		}
  		linecount++;
  	}
! 	ret = !PQendcopy(conn);
  	pset.lineno += linecount;
  	return ret;
  }
--- 727,733 ----
  		}
  		linecount++;
  	}
! 	ret = (PQputCopyEnd(conn, NULL) == 1);
  	pset.lineno += linecount;
  	return ret;
  }


Home | Main Index | Thread Index

Privacy Policy | PostgreSQL Archives hosted by Command Prompt, Inc. | Designed by tinysofa
Copyright © 1996 – 2008 PostgreSQL Global Development Group