*** src/bin/psql/common.c.orig Mon Nov 5 09:46:30 2001 --- src/bin/psql/common.c Thu Apr 25 21:07:06 2002 *************** *** 501,506 **** --- 501,519 ---- if (!QUIET()) fprintf(pset.queryFout, "%s\n", PQcmdStatus(results)); SetVariable(pset.vars, "LASTOID", buf); + + if (GetVariableBool(pset.vars, "NOAUTOCOMMIT")) { + if (strcmp(PQcmdStatus(results), "COMMIT") == 0 || + strcmp(PQcmdStatus(results), "ROLLBACK") == 0) { + if (!QUIET()) + puts("NOTICE: Transaction ended; starting a new one"); + results = PSQLexec("BEGIN"); + if (!results) + success = false; + + } + } + break; } case PGRES_COPY_OUT: *************** *** 521,526 **** --- 534,551 ---- case PGRES_BAD_RESPONSE: success = false; psql_error("%s", PQerrorMessage(pset.db)); + if (GetVariableBool(pset.vars, "NOAUTOCOMMIT")) { + if (!QUIET()) + puts("NOTICE: aborting current transaction and beginning a new one"); + PQclear(results); + results = PSQLexec("ROLLBACK"); + if (!results) + success = false; + PQclear(results); + results = PSQLexec("BEGIN"); + if (!results) + success = false; + } break; } *** src/bin/psql/mainloop.c.orig Thu Dec 27 21:01:05 2001 --- src/bin/psql/mainloop.c Thu Apr 25 21:07:06 2002 *************** *** 51,61 **** volatile int count_eof = 0; const char *var; volatile unsigned int bslash_count = 0; ! int i, prevlen, thislen; /* Save the prior command source */ FILE *prev_cmd_source; bool prev_cmd_interactive; --- 51,64 ---- volatile int count_eof = 0; const char *var; volatile unsigned int bslash_count = 0; ! int i, prevlen, thislen; + /* Get the results from any PSQLexec calls */ + PGresult *res; + /* Save the prior command source */ FILE *prev_cmd_source; bool prev_cmd_interactive; *************** *** 88,94 **** --- 91,105 ---- prev_lineno = pset.lineno; pset.lineno = 0; + /* If psql was run with the -C (no autocommit) option, * + * explicitly begin a transaction */ + if (GetVariableBool(pset.vars, "NOAUTOCOMMIT")) { + if (!(res = PSQLexec("BEGIN"))) + return EXIT_FAILURE; + PQclear(res); + } + /* main loop to get queries and execute them */ while (1) { *** src/bin/psql/startup.c.orig Mon Nov 5 09:46:31 2001 --- src/bin/psql/startup.c Thu Apr 25 21:15:25 2002 *************** *** 335,340 **** --- 335,341 ---- {"echo-all", no_argument, NULL, 'a'}, {"no-align", no_argument, NULL, 'A'}, {"command", required_argument, NULL, 'c'}, + {"no-autocommit", no_argument, NULL, 'C'}, {"dbname", required_argument, NULL, 'd'}, {"echo-queries", no_argument, NULL, 'e'}, {"echo-hidden", no_argument, NULL, 'E'}, *************** *** 374,387 **** memset(options, 0, sizeof *options); #ifdef HAVE_GETOPT_LONG ! while ((c = getopt_long(argc, argv, "aAc:d:eEf:F:h:Hlno:p:P:qR:sStT:uU:v:VWxX?", long_options, &optindex)) != -1) #else /* not HAVE_GETOPT_LONG */ /* * Be sure to leave the '-' in here, so we can catch accidental long * options. */ ! while ((c = getopt(argc, argv, "aAc:d:eEf:F:h:Hlno:p:P:qR:sStT:uU:v:VWxX?-")) != -1) #endif /* not HAVE_GETOPT_LONG */ { switch (c) --- 375,388 ---- memset(options, 0, sizeof *options); #ifdef HAVE_GETOPT_LONG ! while ((c = getopt_long(argc, argv, "aAc:Cd:eEf:F:h:Hlno:p:P:qR:sStT:uU:v:VWxX?", long_options, &optindex)) != -1) #else /* not HAVE_GETOPT_LONG */ /* * Be sure to leave the '-' in here, so we can catch accidental long * options. */ ! while ((c = getopt(argc, argv, "aAc:Cd:eEf:F:h:Hlno:p:P:qR:sStT:uU:v:VWxX?-")) != -1) #endif /* not HAVE_GETOPT_LONG */ { switch (c) *************** *** 402,407 **** --- 403,411 ---- else options->action = ACT_SINGLE_QUERY; break; + case 'C': + SetVariableBool(pset.vars, "NOAUTOCOMMIT"); + break; case 'd': options->dbname = optarg; break; *** src/bin/psql/help.c.orig Wed Oct 24 22:49:54 2001 --- src/bin/psql/help.c Wed Aug 21 17:27:47 2002 *************** *** 82,87 **** --- 82,88 ---- puts(_(" -a Echo all input from script")); puts(_(" -A Unaligned table output mode (-P format=unaligned)")); puts(_(" -c COMMAND Run only single command (SQL or internal) and exit")); + puts(_(" -C Automatically wrap all SQL commands in a transaction")); /* Display default database */ env = getenv("PGDATABASE");