3
3
*
4
4
* Copyright (c) 2000-2008, PostgreSQL Global Development Group
5
5
*
6
- * $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.194 2008/09/06 00:01:24 tgl Exp $
6
+ * $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.195 2008/09/06 20:18:08 tgl Exp $
7
7
*/
8
8
#include "postgres_fe.h"
9
9
#include "command.h"
@@ -62,6 +62,7 @@ static bool do_connect(char *dbname, char *user, char *host, char *port);
62
62
static bool do_shell (const char * command );
63
63
static bool lookup_function_oid (PGconn * conn , const char * desc , Oid * foid );
64
64
static bool get_create_function_cmd (PGconn * conn , Oid oid , PQExpBuffer buf );
65
+ static void minimal_error_message (PGresult * res );
65
66
66
67
#ifdef USE_SSL
67
68
static void printSSLInfo (void );
@@ -433,15 +434,15 @@ exec_command(const char *cmd,
433
434
*/
434
435
else if (strcmp (cmd , "e" ) == 0 || strcmp (cmd , "edit" ) == 0 )
435
436
{
436
- char * fname ;
437
-
438
437
if (!query_buf )
439
438
{
440
439
psql_error ("no query buffer\n" );
441
440
status = PSQL_CMD_ERROR ;
442
441
}
443
442
else
444
443
{
444
+ char * fname ;
445
+
445
446
fname = psql_scan_slash_option (scan_state ,
446
447
OT_NORMAL , NULL , true);
447
448
expand_tilde (& fname );
@@ -456,53 +457,59 @@ exec_command(const char *cmd,
456
457
}
457
458
458
459
/*
459
- * \ef -- edit the named function in $EDITOR.
460
+ * \ef -- edit the named function, or present a blank CREATE FUNCTION
461
+ * template if no argument is given
460
462
*/
461
463
else if (strcmp (cmd , "ef" ) == 0 )
462
464
{
463
- char * func ;
464
- Oid foid ;
465
-
466
- func = psql_scan_slash_option (scan_state , OT_WHOLE_LINE , NULL , true);
467
- if (!func )
468
- {
469
- psql_error ("no function name specified\n" );
470
- status = PSQL_CMD_ERROR ;
471
- }
472
- else if (!lookup_function_oid (pset .db , func , & foid ))
473
- {
474
- psql_error (PQerrorMessage (pset .db ));
475
- status = PSQL_CMD_ERROR ;
476
- }
477
- else if (!query_buf )
465
+ if (!query_buf )
478
466
{
479
467
psql_error ("no query buffer\n" );
480
468
status = PSQL_CMD_ERROR ;
481
469
}
482
- else if (!get_create_function_cmd (pset .db , foid , query_buf ))
483
- {
484
- psql_error (PQerrorMessage (pset .db ));
485
- status = PSQL_CMD_ERROR ;
486
- }
487
470
else
488
471
{
489
- bool edited = false;
472
+ char * func ;
473
+ Oid foid ;
490
474
491
- if (!do_edit (0 , query_buf , & edited ))
475
+ func = psql_scan_slash_option (scan_state ,
476
+ OT_WHOLE_LINE , NULL , true);
477
+ if (!func )
492
478
{
479
+ /* set up an empty command to fill in */
480
+ printfPQExpBuffer (query_buf ,
481
+ "CREATE FUNCTION ( )\n"
482
+ " RETURNS \n"
483
+ " LANGUAGE \n"
484
+ " -- common options: IMMUTABLE STABLE STRICT SECURITY DEFINER\n"
485
+ "AS $function$\n"
486
+ "\n$function$\n" );
487
+ }
488
+ else if (!lookup_function_oid (pset .db , func , & foid ))
489
+ {
490
+ /* error already reported */
493
491
status = PSQL_CMD_ERROR ;
494
492
}
495
- else if (!edited )
493
+ else if (!get_create_function_cmd ( pset . db , foid , query_buf ) )
496
494
{
497
- printf ("No changes\n" );
495
+ /* error already reported */
496
+ status = PSQL_CMD_ERROR ;
498
497
}
498
+ if (func )
499
+ free (func );
500
+ }
501
+
502
+ if (status != PSQL_CMD_ERROR )
503
+ {
504
+ bool edited = false;
505
+
506
+ if (!do_edit (0 , query_buf , & edited ))
507
+ status = PSQL_CMD_ERROR ;
508
+ else if (!edited )
509
+ puts (_ ("No changes." ));
499
510
else
500
- {
501
511
status = PSQL_CMD_NEWEDIT ;
502
- }
503
512
}
504
- if (func )
505
- free (func );
506
513
}
507
514
508
515
/* \echo and \qecho */
@@ -1998,7 +2005,10 @@ lookup_function_oid(PGconn *conn, const char *desc, Oid *foid)
1998
2005
if (PQresultStatus (res ) == PGRES_TUPLES_OK && PQntuples (res ) == 1 )
1999
2006
* foid = atooid (PQgetvalue (res , 0 , 0 ));
2000
2007
else
2008
+ {
2009
+ minimal_error_message (res );
2001
2010
result = false;
2011
+ }
2002
2012
2003
2013
PQclear (res );
2004
2014
destroyPQExpBuffer (query );
@@ -2027,10 +2037,42 @@ get_create_function_cmd(PGconn *conn, Oid oid, PQExpBuffer buf)
2027
2037
appendPQExpBufferStr (buf , PQgetvalue (res , 0 , 0 ));
2028
2038
}
2029
2039
else
2040
+ {
2041
+ minimal_error_message (res );
2030
2042
result = false;
2043
+ }
2031
2044
2032
2045
PQclear (res );
2033
2046
destroyPQExpBuffer (query );
2034
2047
2035
2048
return result ;
2036
2049
}
2050
+
2051
+ /*
2052
+ * Report just the primary error; this is to avoid cluttering the output
2053
+ * with, for instance, a redisplay of the internally generated query
2054
+ */
2055
+ static void
2056
+ minimal_error_message (PGresult * res )
2057
+ {
2058
+ PQExpBuffer msg ;
2059
+ const char * fld ;
2060
+
2061
+ msg = createPQExpBuffer ();
2062
+
2063
+ fld = PQresultErrorField (res , PG_DIAG_SEVERITY );
2064
+ if (fld )
2065
+ printfPQExpBuffer (msg , "%s: " , fld );
2066
+ else
2067
+ printfPQExpBuffer (msg , "ERROR: " );
2068
+ fld = PQresultErrorField (res , PG_DIAG_MESSAGE_PRIMARY );
2069
+ if (fld )
2070
+ appendPQExpBufferStr (msg , fld );
2071
+ else
2072
+ appendPQExpBufferStr (msg , "(not available)" );
2073
+ appendPQExpBufferStr (msg , "\n" );
2074
+
2075
+ psql_error (msg -> data );
2076
+
2077
+ destroyPQExpBuffer (msg );
2078
+ }
0 commit comments