commit bef661b7c822f4fe9f004bf55645a3e47e514bc8 Author: Pavel Stehule Date: Wed Aug 26 13:03:56 2015 +0200 inital diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index f996865..750c4ae 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -223,6 +223,20 @@ EOF + + + + + Specifies that psql is to execute one or + more command strings, commands, + and then exit. This is useful in shell scripts. Start-up files + (psqlrc and ~/.psqlrc) are + ignored with this option. + + + + + diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index 6181a61..277e980 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -2292,7 +2292,6 @@ process_file(char *filename, bool single_txn, bool use_relative_path) int result; char *oldfilename; char relpath[MAXPGPATH]; - PGresult *res; if (!filename) { @@ -2337,37 +2336,8 @@ process_file(char *filename, bool single_txn, bool use_relative_path) oldfilename = pset.inputfile; pset.inputfile = filename; - if (single_txn) - { - if ((res = PSQLexec("BEGIN")) == NULL) - { - if (pset.on_error_stop) - { - result = EXIT_USER; - goto error; - } - } - else - PQclear(res); - } - - result = MainLoop(fd); + result = MainLoop(fd, single_txn); - if (single_txn) - { - if ((res = PSQLexec("COMMIT")) == NULL) - { - if (pset.on_error_stop) - { - result = EXIT_USER; - goto error; - } - } - else - PQclear(res); - } - -error: if (fd != stdin) fclose(fd); @@ -2375,8 +2345,6 @@ error: return result; } - - static const char * _align2string(enum printFormat in) { diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c index d3e3114..55aa423 100644 --- a/src/bin/psql/help.c +++ b/src/bin/psql/help.c @@ -79,6 +79,8 @@ usage(unsigned short int pager) fprintf(output, _(" -c, --command=COMMAND run only single command (SQL or internal) and exit\n")); fprintf(output, _(" -d, --dbname=DBNAME database name to connect to (default: \"%s\")\n"), env); fprintf(output, _(" -f, --file=FILENAME execute commands from file, then exit\n")); + fprintf(output, _(" -g, --group-command=COMMAND\n" + " run more groups of commands (SQL or internal) and exit\n")); fprintf(output, _(" -l, --list list available databases, then exit\n")); fprintf(output, _(" -v, --set=, --variable=NAME=VALUE\n" " set psql variable NAME to VALUE e.g.: -v ON_ERROR_STOP=1\n")); diff --git a/src/bin/psql/mainloop.c b/src/bin/psql/mainloop.c index b6cef94..4147238 100644 --- a/src/bin/psql/mainloop.c +++ b/src/bin/psql/mainloop.c @@ -24,8 +24,8 @@ * This loop is re-entrant. May be called by \i command * which reads input from a file. */ -int -MainLoop(FILE *source) +static int +_MainLoop(FILE *source) { PsqlScanState scan_state; /* lexer working state */ volatile PQExpBuffer query_buf; /* buffer for query being accumulated */ @@ -43,6 +43,7 @@ MainLoop(FILE *source) volatile promptStatus_t prompt_status = PROMPT_READY; volatile int count_eof = 0; volatile bool die_on_error = false; + GroupCommand *cmd = pset.group_commands; /* Save the prior command source */ FILE *prev_cmd_source; @@ -135,6 +136,20 @@ MainLoop(FILE *source) prompt_status = PROMPT_READY; line = gets_interactive(get_prompt(prompt_status)); } + else if (pset.group_commands != NULL) + { + /* Is there some unprocessed group command? */ + if (cmd != NULL) + { + line = cmd->actions; + cmd = cmd->next; + } + else + { + successResult = EXIT_SUCCESS; + break; + } + } else { line = gets_fromFile(source); @@ -451,6 +466,43 @@ MainLoop(FILE *source) return successResult; } /* MainLoop() */ +/* + * Transactional MainLoop + * + * allow to execute all statements in single transaction + */ +int +MainLoop(FILE *source, bool single_txn) +{ + int result; + PGresult *res; + + if (single_txn) + { + if ((res = PSQLexec("BEGIN")) == NULL) + { + if (pset.on_error_stop) + return EXIT_USER; + } + else + PQclear(res); + } + + result = _MainLoop(source); + + if (single_txn) + { + if ((res = PSQLexec("COMMIT")) == NULL) + { + if (pset.on_error_stop) + return EXIT_USER; + } + else + PQclear(res); + } + + return result; +} /* * psqlscan.c is #include'd here instead of being compiled on its own. diff --git a/src/bin/psql/mainloop.h b/src/bin/psql/mainloop.h index 8f1325c..53c9a11 100644 --- a/src/bin/psql/mainloop.h +++ b/src/bin/psql/mainloop.h @@ -10,6 +10,6 @@ #include "postgres_fe.h" -int MainLoop(FILE *source); +int MainLoop(FILE *source, bool single_txn); #endif /* MAINLOOP_H */ diff --git a/src/bin/psql/settings.h b/src/bin/psql/settings.h index d34dc28..d23d67a 100644 --- a/src/bin/psql/settings.h +++ b/src/bin/psql/settings.h @@ -77,6 +77,12 @@ enum trivalue TRI_YES }; +typedef struct _GroupCommand +{ + char *actions; + struct _GroupCommand *next; +} GroupCommand; + typedef struct _psqlSettings { PGconn *db; /* connection to backend */ @@ -129,6 +135,7 @@ typedef struct _psqlSettings const char *prompt2; const char *prompt3; PGVerbosity verbosity; /* current error verbosity level */ + GroupCommand *group_commands; } PsqlSettings; extern PsqlSettings pset; diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c index 28ba75a..4c9383f 100644 --- a/src/bin/psql/startup.c +++ b/src/bin/psql/startup.c @@ -54,7 +54,8 @@ enum _actions ACT_SINGLE_SLASH, ACT_LIST_DB, ACT_SINGLE_QUERY, - ACT_FILE + ACT_FILE, + ACT_GROUP_COMMANDS }; struct adhoc_opts @@ -158,6 +159,8 @@ main(int argc, char *argv[]) SetVariable(pset.vars, "PROMPT2", DEFAULT_PROMPT2); SetVariable(pset.vars, "PROMPT3", DEFAULT_PROMPT3); + pset.group_commands = NULL; + parse_psql_options(argc, argv, &options); /* @@ -326,6 +329,13 @@ main(int argc, char *argv[]) ? EXIT_SUCCESS : EXIT_FAILURE; } + else if (options.action == ACT_GROUP_COMMANDS) + { + pset.notty = true; + SetVariableBool(pset.vars, "SINGLELINE"); + successResult = MainLoop(NULL, options.single_txn); + } + /* * or otherwise enter interactive main loop */ @@ -338,7 +348,7 @@ main(int argc, char *argv[]) if (!pset.quiet) printf(_("Type \"help\" for help.\n\n")); initializeInput(options.no_readline ? 0 : 1); - successResult = MainLoop(stdin); + successResult = MainLoop(stdin, false); } /* clean up */ @@ -370,6 +380,7 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options) {"file", required_argument, NULL, 'f'}, {"field-separator", required_argument, NULL, 'F'}, {"field-separator-zero", no_argument, NULL, 'z'}, + {"group-command", required_argument, NULL, 'g'}, {"host", required_argument, NULL, 'h'}, {"html", no_argument, NULL, 'H'}, {"list", no_argument, NULL, 'l'}, @@ -401,9 +412,12 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options) int optindex; int c; + bool cmd_opt_is_used = false; + bool group_cmd_opt_is_used = false; + 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", + while ((c = getopt_long(argc, argv, "aAbc:d:eEf:F:g:h:HlL:no:p:P:qR:sStT:U:v:VwWxXz?01", long_options, &optindex)) != -1) { switch (c) @@ -418,6 +432,8 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options) SetVariable(pset.vars, "ECHO", "errors"); break; case 'c': + cmd_opt_is_used = true; + options->action_string = pg_strdup(optarg); if (optarg[0] == '\\') { @@ -444,6 +460,27 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options) pset.popt.topt.fieldSep.separator = pg_strdup(optarg); pset.popt.topt.fieldSep.separator_zero = false; break; + case 'g': + { + GroupCommand *cmd = pg_malloc(sizeof(GroupCommand)); + GroupCommand *ptr = pset.group_commands; + + group_cmd_opt_is_used = true; + + if (ptr == NULL) + pset.group_commands = cmd; + else + { + while (ptr->next != NULL) + ptr = ptr->next; + + ptr->next = cmd; + } + cmd->next = NULL; + cmd->actions = pg_strdup(optarg); + options->action = ACT_GROUP_COMMANDS; + } + break; case 'h': options->host = pg_strdup(optarg); break; @@ -600,6 +637,13 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options) } } + if (cmd_opt_is_used && group_cmd_opt_is_used) + { + fprintf(stderr, _("%s: options -c/--command and -g/--group_command cannot be used together\n"), + pset.progname); + exit(EXIT_FAILURE); + } + /* * if we still have arguments, use it as the database name and username */