Hello

now - complete patch

ToDo:
* enhance a documentation (any volunteer?)
* check name for backslash command

Regards
Pavel Stehule
*** ./doc/src/sgml/ref/psql-ref.sgml.orig	2009-10-13 23:04:01.000000000 +0200
--- ./doc/src/sgml/ref/psql-ref.sgml	2009-11-16 22:44:50.530277356 +0100
***************
*** 307,312 ****
--- 307,325 ----
      </varlistentry>
  
      <varlistentry>
+       <term><option>-r</></term>
+       <term><option>--separate-params</></term>
+       <listitem>
+       <para>
+       Separately passing parameters. Any used <application>psql</application> 
+       variables are passing with query separately (as parameters). This is
+       different than default behave, where variables are substituted by value.
+       </para>
+       </listitem>
+     </varlistentry>
+ 
+ 
+     <varlistentry>
        <term><option>-R <replaceable class="parameter">separator</replaceable></></term>
        <term><option>--record-separator <replaceable class="parameter">separator</replaceable></></term>
        <listitem>
***************
*** 1659,1664 ****
--- 1672,1687 ----
        </varlistentry>
  
        <varlistentry>
+         <term><literal>\pexec [ <replaceable class="parameter">on</replaceable> | <replaceable class="parameter">off</replaceable> ]</literal></term>
+         <listitem>
+         <para>
+         Changes mode of using <application>psql</application> variables. 
+         </para>
+         </listitem>
+       </varlistentry>
+ 
+ 
+       <varlistentry>
          <term><literal>\prompt [ <replaceable class="parameter">text</replaceable> ] <replaceable class="parameter">name</replaceable></literal></term>
          <listitem>
          <para>
*** ./src/bin/psql/command.c.orig	2009-11-16 21:39:08.143281729 +0100
--- ./src/bin/psql/command.c	2009-11-16 21:40:12.770279298 +0100
***************
*** 1127,1132 ****
--- 1127,1152 ----
  			free(pattern);
  	}
  
+ 	/* \pexec -- pass parameters separately */
+ 	else if (strcmp(cmd, "pexec") == 0)
+ 	{
+ 		char	   *opt = psql_scan_slash_option(scan_state,
+ 												 OT_NORMAL, NULL, false);
+ 
+ 		if (opt)
+ 			pset.use_parameters = ParseVariableBool(opt);
+ 		else
+ 			pset.use_parameters = !pset.use_parameters;
+ 		if (!pset.quiet)
+ 		{
+ 			if (pset.use_parameters)
+ 				puts(_("Separately passing parameters is on."));
+ 			else
+ 				puts(_("Separately passing parameters is off."));
+ 		}
+ 		free(opt);
+ 	}
+ 
  	/* \! -- shell escape */
  	else if (strcmp(cmd, "!") == 0)
  	{
*** ./src/bin/psql/common.c.orig	2009-11-16 21:39:08.146280462 +0100
--- ./src/bin/psql/common.c	2009-11-16 21:40:12.771276920 +0100
***************
*** 70,75 ****
--- 70,89 ----
  }
  
  void *
+ pg_realloc(void *ptr, size_t size)
+ {
+ 	void	   *tmp;
+ 	
+ 	tmp = realloc(ptr, size);
+ 	if (!tmp)
+ 	{
+ 		psql_error("out of memory\n");
+ 		exit(EXIT_FAILURE);
+ 	}
+ 	return tmp;
+ }
+ 
+ void *
  pg_malloc_zero(size_t size)
  {
  	void	   *tmp;
***************
*** 852,858 ****
  		if (pset.timing)
  			INSTR_TIME_SET_CURRENT(before);
  
! 		results = PQexec(pset.db, query);
  
  		/* these operations are included in the timing result: */
  		ResetCancelConn();
--- 866,891 ----
  		if (pset.timing)
  			INSTR_TIME_SET_CURRENT(before);
  
! 		if (!pset.use_parameters)
! 			results = PQexec(pset.db, query);
! 		else
! 		{
! 			/* use PQexecParams function instead */
! 			results = PQexecParams(pset.db, query, 
! 							    pset.nparameters, 
! 							    NULL, 
! 							    pset.parameters, 
! 							    NULL, 
! 							    NULL, 
! 							    0);
! 			if (pset.nparameters)
! 			{
! 				pset.nparameters = 0;
! 				pset.maxparameters = 0;
! 				free(pset.parameters);
! 				pset.parameters = NULL;
! 			} 
! 		}
  
  		/* these operations are included in the timing result: */
  		ResetCancelConn();
***************
*** 1005,1011 ****
  	appendPQExpBuffer(&buf, "DECLARE _psql_cursor NO SCROLL CURSOR FOR\n%s",
  					  query);
  
! 	results = PQexec(pset.db, buf.data);
  	OK = AcceptResult(results) &&
  		(PQresultStatus(results) == PGRES_COMMAND_OK);
  	PQclear(results);
--- 1038,1064 ----
  	appendPQExpBuffer(&buf, "DECLARE _psql_cursor NO SCROLL CURSOR FOR\n%s",
  					  query);
  
! 	if (!pset.use_parameters)
! 		results = PQexec(pset.db, buf.data);
! 	else
! 	{
! 		/* use PQexecParams function instead */
! 		results = PQexecParams(pset.db, buf.data, 
! 						    pset.nparameters, 
! 						    NULL, 
! 						    pset.parameters, 
! 						    NULL, 
! 						    NULL, 
! 						    0);
! 		if (pset.nparameters)
! 		{
! 			pset.nparameters = 0;
! 			pset.maxparameters = 0;
! 			free(pset.parameters);
! 			pset.parameters = NULL;
! 		} 
! 	}
! 
  	OK = AcceptResult(results) &&
  		(PQresultStatus(results) == PGRES_COMMAND_OK);
  	PQclear(results);
*** ./src/bin/psql/common.h.orig	2009-11-16 21:39:08.148278501 +0100
--- ./src/bin/psql/common.h	2009-11-16 21:40:12.772278454 +0100
***************
*** 30,35 ****
--- 30,36 ----
  extern void *pg_malloc(size_t size);
  extern void *pg_malloc_zero(size_t size);
  extern void *pg_calloc(size_t nmemb, size_t size);
+ extern void *pg_realloc(void *ptr, size_t size);
  
  extern bool setQFout(const char *fname);
  
*** ./src/bin/psql/help.c.orig	2009-11-16 21:39:08.150277657 +0100
--- ./src/bin/psql/help.c	2009-11-16 21:40:12.772278454 +0100
***************
*** 112,117 ****
--- 112,118 ----
  	printf(_("  -n, --no-readline        disable enhanced command line editing (readline)\n"));
  	printf(_("  -o, --output=FILENAME    send query results to file (or |pipe)\n"));
  	printf(_("  -q, --quiet              run quietly (no messages, only query output)\n"));
+ 	printf(_("  -r, --separate-params    separately passing parameters\n"));
  	printf(_("  -s, --single-step        single-step mode (confirm each query)\n"));
  	printf(_("  -S, --single-line        single-line mode (end of line terminates SQL command)\n"));
  
***************
*** 190,195 ****
--- 191,198 ----
  	fprintf(output, _("  \\i FILE                execute commands from file\n"));
  	fprintf(output, _("  \\o [FILE]              send all query results to file or |pipe\n"));
  	fprintf(output, _("  \\qecho [STRING]        write string to query output stream (see \\o)\n"));
+ 	fprintf(output, _("  \\pexec [on|off]        separately passing parameters (currently %s)\n"),
+ 			ON(pset.use_parameters));
  	fprintf(output, "\n");
  
  	fprintf(output, _("Informational\n"));
*** ./src/bin/psql/psqlscan.l.orig	2009-11-16 22:57:37.000000000 +0100
--- ./src/bin/psql/psqlscan.l	2009-11-16 21:40:12.773278869 +0100
***************
*** 693,701 ****
  
  					if (value)
  					{
! 						/* It is a variable, perform substitution */
! 						push_new_buffer(value);
! 						/* yy_scan_string already made buffer active */
  					}
  					else
  					{
--- 693,717 ----
  
  					if (value)
  					{
! 						if (pset.use_parameters)
! 						{
! 							char	buffer[10];
! 						
! 							/* add new parameter */
! 							if (pset.nparameters == pset.maxparameters)
! 							{
! 								pset.maxparameters += 100;
! 								pset.parameters = pg_realloc(pset.parameters,
! 												    sizeof(char *) * pset.maxparameters);
! 							}
! 							pset.parameters[pset.nparameters++] = value;
! 							sprintf(buffer, "$%d", pset.nparameters);
! 							push_new_buffer(buffer);
! 						}
! 						else
! 							/* It is a variable, perform substitution */
! 							push_new_buffer(value);
! 							/* yy_scan_string already made buffer active */
  					}
  					else
  					{
*** ./src/bin/psql/settings.h.orig	2009-11-16 21:39:08.153278066 +0100
--- ./src/bin/psql/settings.h	2009-11-16 21:40:12.773278869 +0100
***************
*** 111,116 ****
--- 111,120 ----
  	const char *prompt2;
  	const char *prompt3;
  	PGVerbosity verbosity;		/* current error verbosity level */
+ 	bool	use_parameters;
+ 	int 	   nparameters;
+ 	int        maxparameters;
+ 	const char **parameters;
  } PsqlSettings;
  
  extern PsqlSettings pset;
*** ./src/bin/psql/startup.c.orig	2009-11-16 21:39:08.155278060 +0100
--- ./src/bin/psql/startup.c	2009-11-16 21:40:12.774277610 +0100
***************
*** 122,127 ****
--- 122,131 ----
  	pset.queryFoutPipe = false;
  	pset.cur_cmd_source = stdin;
  	pset.cur_cmd_interactive = false;
+ 	pset.use_parameters = false;
+ 	pset.parameters = NULL;
+ 	pset.maxparameters = 0;
+ 	pset.nparameters = 0;
  
  	/* We rely on unmentioned fields of pset.popt to start out 0/false/NULL */
  	pset.popt.topt.format = PRINT_ALIGNED;
***************
*** 322,327 ****
--- 326,332 ----
  		{"port", required_argument, NULL, 'p'},
  		{"pset", required_argument, NULL, 'P'},
  		{"quiet", no_argument, NULL, 'q'},
+ 		{"separate-params", no_argument, NULL, 'r'},
  		{"record-separator", required_argument, NULL, 'R'},
  		{"single-step", no_argument, NULL, 's'},
  		{"single-line", no_argument, NULL, 'S'},
***************
*** 346,352 ****
  
  	memset(options, 0, sizeof *options);
  
! 	while ((c = getopt_long(argc, argv, "aAc:d:eEf:F:h:HlL:no:p:P:qR:sStT:U:v:VwWxX?1",
  							long_options, &optindex)) != -1)
  	{
  		switch (c)
--- 351,357 ----
  
  	memset(options, 0, sizeof *options);
  
! 	while ((c = getopt_long(argc, argv, "aAc:d:eEf:F:h:HlL:no:p:P:qrR:sStT:U:v:VwWxX?1",
  							long_options, &optindex)) != -1)
  	{
  		switch (c)
***************
*** 432,437 ****
--- 437,445 ----
  			case 'q':
  				SetVariableBool(pset.vars, "QUIET");
  				break;
+ 			case 'r':
+ 				pset.use_parameters = true;
+ 				break;
  			case 'R':
  				pset.popt.topt.recordSep = pg_strdup(optarg);
  				break;
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to