diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index f66fd7e..3724533 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -35,6 +35,7 @@ #include "libpq-fe.h" #include "pqexpbuffer.h" #include "dumputils.h" +#include "dumpfunc.h" #include "common.h" #include "copy.h" @@ -53,7 +54,7 @@ static backslashResult exec_command(const char *cmd, PsqlScanState scan_state, PQExpBuffer query_buf); -static bool do_edit(const char *filename_arg, PQExpBuffer query_buf); +static bool do_edit(const char *, PQExpBuffer, bool *); static bool do_connect(char *dbname, char *user, char *host, char *port); static bool do_shell(const char *command); @@ -432,11 +433,71 @@ exec_command(const char *cmd, expand_tilde(&fname); if (fname) canonicalize_path(fname); - status = do_edit(fname, query_buf) ? PSQL_CMD_NEWEDIT : PSQL_CMD_ERROR; + if (do_edit(fname, query_buf, 0)) + status = PSQL_CMD_NEWEDIT; + else + status = PSQL_CMD_ERROR; free(fname); } } + /* + * \ef -- edit the named function in $EDITOR. + */ + + else if (strcmp(cmd, "ef") == 0) + { + Oid foid; + char *func; + + func = psql_scan_slash_option(scan_state, OT_WHOLE_LINE, NULL, true); + if (!func) + { + psql_error("no function name specified\n"); + status = PSQL_CMD_ERROR; + } + + if (!lookup_function_oid(pset.db, func, &foid)) + { + psql_error(PQerrorMessage(pset.db)); + status = PSQL_CMD_ERROR; + } + else + { + bool edited = false; + + termPQExpBuffer(query_buf); + if (foid) + { + char *s = create_or_replace_function_text(foid); + appendPQExpBufferStr(query_buf, s); + free(s); + } + else + { + printfPQExpBuffer(query_buf, + "CREATE FUNCTION %s%s RETURNS ... AS $$\n" + "...\n" + "$$ LANGUAGE '...'\n", + func, strchr(func,'(') ? "" : "(...)" ); + } + + if (!do_edit(0, query_buf, &edited)) + { + status = PSQL_CMD_ERROR; + } + else if (!edited) + { + printf("No changes\n"); + } + else + { + status = PSQL_CMD_SEND; + } + free(func); + } + } + /* \echo and \qecho */ else if (strcmp(cmd, "echo") == 0 || strcmp(cmd, "qecho") == 0) { @@ -1298,7 +1359,7 @@ editFile(const char *fname) /* call this one */ static bool -do_edit(const char *filename_arg, PQExpBuffer query_buf) +do_edit(const char *filename_arg, PQExpBuffer query_buf, bool *edited) { char fnametmp[MAXPGPATH]; FILE *stream = NULL; @@ -1420,6 +1481,10 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf) psql_error("%s: %s\n", fname, strerror(errno)); error = true; } + else if (edited) + { + *edited = true; + } fclose(stream); }