Hi

here is proof concept patch

It should be cleaned, but it demonstrates a work well

[pavel@localhost psql]$ ./psql  -C 'select 10 x; select 20 y;'  -C "\l"
postgres
 x
----
 10
(1 row)

 y
----
 20
(1 row)

                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access
privileges
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
=c/postgres          +
           |          |          |             |             |
postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
=c/postgres          +
           |          |          |             |             |
postgres=CTc/postgres
(3 rows)


2015-07-28 18:46 GMT+02:00 Andrew Dunstan <and...@dunslane.net>:

>
> On 07/28/2015 11:52 AM, Pavel Stehule wrote:
>
>>
>>
>> 2015-07-28 15:16 GMT+02:00 Andrew Dunstan <and...@dunslane.net <mailto:
>> and...@dunslane.net>>:
>>
>>
>>     On 07/28/2015 12:08 AM, Pavel Stehule wrote:
>>
>>
>>
>>         2015-07-28 5:24 GMT+02:00 Pavel Stehule
>>         <pavel.steh...@gmail.com <mailto:pavel.steh...@gmail.com>
>>         <mailto:pavel.steh...@gmail.com
>>         <mailto:pavel.steh...@gmail.com>>>:
>>
>>
>>
>>             2015-07-27 21:57 GMT+02:00 Andrew Dunstan
>>         <and...@dunslane.net <mailto:and...@dunslane.net>
>>             <mailto:and...@dunslane.net <mailto:and...@dunslane.net>>>:
>>
>>
>>
>>                 On 07/27/2015 02:53 PM, Pavel Stehule wrote:
>>
>>
>>
>>
>>
>>                     I am trying to run parallel execution
>>
>>                     psql -At -c "select datname from pg_database"
>>         postgres |
>>                     xargs -n 1 -P 3 psql -c "select current_database()"
>>
>>
>>
>>
>>                 I don't think it's going to be a hugely important
>>         feature, but
>>                 I don't see a problem with creating a new option (-C seems
>>                 fine) which would have the same effect as if the arguments
>>                 were contatenated into a file which is then used with
>>         -f. IIRC
>>                 -c has some special characteristics which means it's
>>         probably
>>                 best not to try to extend it for this feature.
>>
>>
>>             ok, I'll try to write patch.
>>
>>
>>         I have a question. Can be -C option multiple?
>>
>>         The SQL is without problem, but what about \x command?
>>
>>         postgres=# \dt \dn select 10;
>>         No relations found.
>>         List of schemas
>>         ┌──────┬───────┐
>>         │ Name │ Owner │
>>         ╞══════╪═══════╡
>>         └──────┴───────┘
>>         (0 rows)
>>
>>         \dn: extra argument "10;" ignored
>>
>>
>>
>>     I don't understand the question.
>>
>>     You should include one sql or psql command per -C option, ISTM. e.g.
>>
>>         psql -C '\dt' -C '\dn' -C 'select 10;'
>>
>>
>>     Isn't that what we're talking about with this whole proposal?
>>
>>
>>
>> I am searching some agreement, how to solve a current "-c" limits. I am
>> 100% for >>> psql -C '\dt' -C '\dn' -C 'select 10;' <<<
>>
>>
>>
> I think you're probably best off leaving -c alone. If there are issues to
> be solved for -c they should be handled separately from the feature we
> agree on.
>
> cheers
>
> andrew
>
>
>
>
diff --git a/src/bin/psql/mainloop.c b/src/bin/psql/mainloop.c
new file mode 100644
index b6cef94..bfd5bf5
*** a/src/bin/psql/mainloop.c
--- b/src/bin/psql/mainloop.c
*************** MainLoop(FILE *source)
*** 43,48 ****
--- 43,49 ----
  	volatile promptStatus_t prompt_status = PROMPT_READY;
  	volatile int count_eof = 0;
  	volatile bool die_on_error = false;
+ 	Commands *cmd = pset.commands;
  
  	/* Save the prior command source */
  	FILE	   *prev_cmd_source;
*************** MainLoop(FILE *source)
*** 135,140 ****
--- 136,156 ----
  				prompt_status = PROMPT_READY;
  			line = gets_interactive(get_prompt(prompt_status));
  		}
+ 		else if (pset.commands != NULL)
+ 		{
+ 			pset.cur_cmd_interactive = false;
+ 
+ 			if (cmd != NULL)
+ 			{
+ 				line = cmd->actions;
+ 				cmd = cmd->next;
+ 			}
+ 			else
+ 			{
+ 				successResult = EXIT_SUCCESS;
+ 				break;
+ 			}
+ 		}
  		else
  		{
  			line = gets_fromFile(source);
diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h
new file mode 100644
index d34dc28..46d2a81
*** a/src/bin/psql/settings.h
--- b/src/bin/psql/settings.h
*************** enum trivalue
*** 77,82 ****
--- 77,88 ----
  	TRI_YES
  };
  
+ typedef struct _Commands
+ {
+ 	char *actions;
+ 	struct _Commands *next;
+ } Commands;
+ 
  typedef struct _psqlSettings
  {
  	PGconn	   *db;				/* connection to backend */
*************** typedef struct _psqlSettings
*** 129,134 ****
--- 135,141 ----
  	const char *prompt2;
  	const char *prompt3;
  	PGVerbosity verbosity;		/* current error verbosity level */
+ 	Commands *commands;
  } PsqlSettings;
  
  extern PsqlSettings pset;
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
new file mode 100644
index 28ba75a..cbceb91
*** a/src/bin/psql/startup.c
--- b/src/bin/psql/startup.c
*************** enum _actions
*** 54,60 ****
  	ACT_SINGLE_SLASH,
  	ACT_LIST_DB,
  	ACT_SINGLE_QUERY,
! 	ACT_FILE
  };
  
  struct adhoc_opts
--- 54,61 ----
  	ACT_SINGLE_SLASH,
  	ACT_LIST_DB,
  	ACT_SINGLE_QUERY,
! 	ACT_FILE,
! 	ACT_COMMANDS
  };
  
  struct adhoc_opts
*************** main(int argc, char *argv[])
*** 158,163 ****
--- 159,166 ----
  	SetVariable(pset.vars, "PROMPT2", DEFAULT_PROMPT2);
  	SetVariable(pset.vars, "PROMPT3", DEFAULT_PROMPT3);
  
+ 	pset.commands = NULL;
+ 
  	parse_psql_options(argc, argv, &options);
  
  	/*
*************** main(int argc, char *argv[])
*** 326,331 ****
--- 329,340 ----
  			? EXIT_SUCCESS : EXIT_FAILURE;
  	}
  
+ 	else if (options.action == ACT_COMMANDS)
+ 	{
+ 		pset.notty = true;
+ 		successResult = MainLoop(stdin);
+ 	}
+ 
  	/*
  	 * or otherwise enter interactive main loop
  	 */
*************** parse_psql_options(int argc, char *argv[
*** 403,409 ****
  
  	memset(options, 0, sizeof *options);
  
! 	while ((c = getopt_long(argc, argv, "aAbc:d:eEf:F:h:HlL:no:p:P:qR:sStT:U:v:VwWxXz?01",
  							long_options, &optindex)) != -1)
  	{
  		switch (c)
--- 412,418 ----
  
  	memset(options, 0, sizeof *options);
  
! 	while ((c = getopt_long(argc, argv, "aAbc:C:d:eEf:F:h:HlL:no:p:P:qR:sStT:U:v:VwWxXz?01",
  							long_options, &optindex)) != -1)
  	{
  		switch (c)
*************** parse_psql_options(int argc, char *argv[
*** 427,432 ****
--- 436,460 ----
  				else
  					options->action = ACT_SINGLE_QUERY;
  				break;
+ 			case 'C':
+ 				{
+ 					Commands *cmd = pg_malloc(sizeof(Commands));
+ 					Commands *ptr = pset.commands;
+ 
+ 					if (ptr == NULL)
+ 						pset.commands = cmd;
+ 					else
+ 					{
+ 						while (ptr->next != NULL)
+ 							ptr =  ptr->next;
+ 
+ 						ptr->next = cmd;
+ 					}
+ 					cmd->next = NULL;
+ 					cmd->actions = pg_strdup(optarg);
+ 					options->action = ACT_COMMANDS;
+ 				}
+ 				break;
  			case 'd':
  				options->dbname = 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