? .deps ? .describe.c.swp ? psql Index: command.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/bin/psql/command.c,v retrieving revision 1.88 diff -c -r1.88 command.c *** command.c 2003/01/10 21:57:44 1.88 --- command.c 2003/01/25 01:43:38 *************** *** 392,398 **** break; default: ! status = CMD_UNKNOWN; } if (pattern) --- 392,401 ---- break; default: ! success = describeUnmatched(cmd, pattern); ! ! if (!success) ! status = CMD_UNKNOWN; } if (pattern) Index: describe.c =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/bin/psql/describe.c,v retrieving revision 1.74 diff -c -r1.74 describe.c *** describe.c 2003/01/07 20:56:06 1.74 --- describe.c 2003/01/25 01:44:09 *************** *** 54,60 **** --- 54,165 ---- return tmp; } + /* + * Checks the database for instructions on how to deal with any unmatched commands + * + * Returns true if it found and successfully processed the command. + */ + bool + describeUnmatched(const char *cmd, const char *pattern) + { + PQExpBufferData buf; + PGresult *res; + printQueryOpt myopt = pset.popt; + char esccmd[strlen(cmd) * 2 + 1]; + char escpattern[strlen(cmd) * 2 + 1]; + int nargs = (pattern ? 1 : 0); + char *fTitle; + char *fTabQuery; + char *fInfoQuery; + + /* Clean up the input data */ + PQescapeString(esccmd, cmd, strlen(cmd)); + + if (pattern) + PQescapeString(escpattern, pattern, strlen(pattern)); + + /* Query the DB to see if there is a command matching the request */ + initPQExpBuffer(&buf); + + printfPQExpBuffer(&buf, + "SELECT table_title, table_query, info_query " + "FROM pgtools.psqlcommands " + "WHERE nargs = '%d' AND '%s' ~ cmd_expression " + "ORDER BY match_order LIMIT 1", + nargs, cmd + ); + + res = PSQLexec(buf.data, false); + termPQExpBuffer(&buf); + if (!res) + return false; + + if (!PQntuples(res)) + return false; + + fTitle = PQgetvalue(res, 0, 0); + fTabQuery = PQgetvalue(res, 0, 1); + fInfoQuery = PQgetvalue(res, 0, 2); + + PQclear(res); + + /* Prepare queries */ + printfPQExpBuffer(&buf, "PREPARE pg_psql "); + + if (nargs > 0) + { + int i; + appendPQExpBuffer(&buf, "("); + + for (i = 0; i < nargs - 1; i++) + appendPQExpBuffer(&buf, "text,"); + + appendPQExpBuffer(&buf, "text)"); + } + appendPQExpBuffer(&buf, "AS %s", fTabQuery); + + res = PSQLexec(buf.data, false); + termPQExpBuffer(&buf); + if (!res) + return false; + + /* Run queries, with arguments (cmd / pattern) */ + printfPQExpBuffer(&buf, "EXECUTE pg_psql"); + + if (nargs > 0) + { + int i; + appendPQExpBuffer(&buf, "("); + + for (i = 0; i < nargs - 1; i++) + appendPQExpBuffer(&buf, "'dummy',"); + + appendPQExpBuffer(&buf, "'%s')", escpattern); + } + + res = PSQLexec(buf.data, false); + termPQExpBuffer(&buf); + if (!res) + return false; + + myopt.nullPrint = NULL; + myopt.title = gettext(fTitle); + + printQuery(res, &myopt, pset.queryFout); + + /* Clean-up our prepared query */ + printfPQExpBuffer(&buf, "DEALLOCATE pg_psql"); + + res = PSQLexec(buf.data, false); + termPQExpBuffer(&buf); + if (!res) + return false; + + PQclear(res); + return true; + } + /*---------------- * Handlers for various slash commands displaying some sort of list * of things in the database. *************** *** 62,68 **** * If you add something here, try to format the query to look nice in -E output. *---------------- */ - /* \da * Takes an optional regexp to select particular aggregates --- 167,172 ---- Index: describe.h =================================================================== RCS file: /projects/cvsroot/pgsql-server/src/bin/psql/describe.h,v retrieving revision 1.20 diff -c -r1.20 describe.h *** describe.h 2003/01/07 20:56:07 1.20 --- describe.h 2003/01/25 01:44:09 *************** *** 52,56 **** --- 52,58 ---- /* \dn */ bool listSchemas(const char *pattern); + /* Try all unmatched commands against this */ + bool describeUnmatched(const char *cmd, const char *pattern); #endif /* DESCRIBE_H */