Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3f8f0ac

Browse files
committed
Bug fixes and enhances to psql submitted by Masaaki Sakaida
1. Fix problems of PAGER and \? command 2. Add -E option that shows actual queries sent by \dt and friends 3. Add version number in startup banners for psql
1 parent 0000a0c commit 3f8f0ac

File tree

1 file changed

+58
-10
lines changed

1 file changed

+58
-10
lines changed

src/bin/psql/psql.c

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.173 1999/03/24 06:55:14 ishii Exp $
10+
* $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.174 1999/03/30 05:00:42 ishii Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -89,7 +89,7 @@ char *__progname = "psql";
8989

9090
#ifdef MULTIBYTE
9191
/* flag to indicate if PGCLIENTENCODING has been set by a user */
92-
static int has_client_encoding;
92+
static char *has_client_encoding = 0;
9393
#endif
9494

9595
/* This prompt string is assumed to have at least 3 characters by code in MainLoop().
@@ -133,6 +133,7 @@ typedef struct _psqlSettings
133133
bool notty; /* input or output is not a tty */
134134
bool pipe; /* queryFout is from a popen() */
135135
bool echoQuery; /* echo the query before sending it */
136+
bool echoAllQueries; /* echo all queries before sending it*/
136137
bool quiet; /* run quietly, no messages, no promt */
137138
bool singleStep; /* prompt before for each query */
138139
bool singleLineMode; /* query terminated by newline */
@@ -178,6 +179,8 @@ static int
178179
static int MainLoop(PsqlSettings *pset, char *query, FILE *source);
179180
static FILE *setFout(PsqlSettings *pset, char *fname);
180181

182+
static char *selectVersion(PsqlSettings *pset);
183+
181184
/*
182185
* usage print out usage for command line arguments
183186
*/
@@ -191,6 +194,7 @@ usage(char *progname)
191194
fprintf(stderr, "\t -c query run single query (slash commands too)\n");
192195
fprintf(stderr, "\t -d dbName specify database name\n");
193196
fprintf(stderr, "\t -e echo the query sent to the backend\n");
197+
fprintf(stderr, "\t -E echo all queries sent to the backend\n");
194198
fprintf(stderr, "\t -f filename use file as a source of queries\n");
195199
fprintf(stderr, "\t -F sep set the field separator (default is '|')\n");
196200
fprintf(stderr, "\t -h host set database server host\n");
@@ -242,7 +246,7 @@ slashUsage(PsqlSettings *pset)
242246
if (pset->notty == 0 &&
243247
(pagerenv = getenv("PAGER")) &&
244248
(pagerenv[0] != '\0') &&
245-
screen_size.ws_row <= 28 &&
249+
screen_size.ws_row <= 35 &&
246250
(fout = popen(pagerenv, "w")))
247251
{
248252
usePipe = 1;
@@ -300,6 +304,13 @@ PSQLexec(PsqlSettings *pset, char *query)
300304
{
301305
PGresult *res;
302306

307+
if (pset->echoAllQueries)
308+
{
309+
fprintf(stderr, "QUERY: %s\n", query);
310+
fprintf(stderr, "\n");
311+
fflush(stderr);
312+
}
313+
303314
res = PQexec(pset->db, query);
304315
if (!res)
305316
fputs(PQerrorMessage(pset->db), stderr);
@@ -490,7 +501,7 @@ tableList(PsqlSettings *pset, bool deep_tablelist, char info_type,
490501
{
491502
/* Display the information */
492503

493-
fprintf(fout, "\nDatabase = %s\n", PQdb(pset->db));
504+
fprintf(fout, "Database = %s\n", PQdb(pset->db));
494505
fprintf(fout, " +------------------+----------------------------------+----------+\n");
495506
fprintf(fout, " | Owner | Relation | Type |\n");
496507
fprintf(fout, " +------------------+----------------------------------+----------+\n");
@@ -511,6 +522,7 @@ tableList(PsqlSettings *pset, bool deep_tablelist, char info_type,
511522
fprintf(fout, "\n");
512523
}
513524
fprintf(fout, " +------------------+----------------------------------+----------+\n");
525+
fprintf(fout, "\n") ;
514526
PQclear(res);
515527
}
516528
if (usePipe)
@@ -614,7 +626,7 @@ rightsList(PsqlSettings *pset)
614626

615627
/* Display the information */
616628

617-
fprintf(fout, "\nDatabase = %s\n", PQdb(pset->db));
629+
fprintf(fout, "Database = %s\n", PQdb(pset->db));
618630
fprintf(fout, " +");
619631
emitNtimes(fout, "-", maxCol1Len+2);
620632
fprintf(fout, "+");
@@ -780,12 +792,12 @@ tableDesc(PsqlSettings *pset, char *table, FILE *fout)
780792
if(PQntuples(res2)) {
781793
/*
782794
* display the query.
783-
* -Ryan 2/14/99
795+
o * -Ryan 2/14/99
784796
*/
785-
fprintf(fout, "\nView = %s\n", table);
797+
fprintf(fout, "View = %s\n", table);
786798
fprintf(fout, "Query = %s\n", PQgetvalue(res2, 0, 1));
787799
} else {
788-
fprintf(fout, "\nTable = %s\n", table);
800+
fprintf(fout, "Table = %s\n", table);
789801
}
790802
PQclear(res2);
791803

@@ -889,6 +901,7 @@ tableDesc(PsqlSettings *pset, char *table, FILE *fout)
889901
fprintf(fout, "%s\n", PQgetvalue(res, i, 0));
890902
else
891903
fprintf(fout, " %s\n", PQgetvalue(res, i, 0));
904+
fprintf(fout, "\n");
892905
}
893906
PQclear(res);
894907
}
@@ -2815,6 +2828,7 @@ main(int argc, char **argv)
28152828
int c;
28162829

28172830
char *home = NULL; /* Used to store $HOME */
2831+
char *version = NULL; /* PostgreSQL version */
28182832

28192833
MemSet(&settings, 0, sizeof settings);
28202834
settings.opt.align = 1;
@@ -2845,7 +2859,7 @@ main(int argc, char **argv)
28452859
has_client_encoding = getenv("PGCLIENTENCODING");
28462860
#endif
28472861

2848-
while ((c = getopt(argc, argv, "Aa:c:d:ef:F:lh:Hnso:p:qStT:ux")) != EOF)
2862+
while ((c = getopt(argc, argv, "Aa:c:d:eEf:F:lh:Hnso:p:qStT:ux")) != EOF)
28492863
{
28502864
switch (c)
28512865
{
@@ -2868,6 +2882,10 @@ main(int argc, char **argv)
28682882
case 'e':
28692883
settings.echoQuery = 1;
28702884
break;
2885+
case 'E':
2886+
settings.echoAllQueries = 1;
2887+
settings.echoQuery = 1;
2888+
break;
28712889
case 'f':
28722890
qfilename = optarg;
28732891
break;
@@ -2956,7 +2974,12 @@ main(int argc, char **argv)
29562974
{
29572975
printf("Welcome to the POSTGRESQL interactive sql monitor:\n");
29582976
printf(" Please read the file COPYRIGHT for copyright terms "
2959-
"of POSTGRESQL\n\n");
2977+
"of POSTGRESQL\n");
2978+
2979+
if ( (version = selectVersion(&settings)) != NULL )
2980+
printf("[%s]\n", version);
2981+
2982+
printf("\n");
29602983
printf(" type \\? for help on slash commands\n");
29612984
printf(" type \\q to quit\n");
29622985
printf(" type \\g or terminate with semicolon to execute query\n");
@@ -3230,3 +3253,28 @@ prompt_for_password(char *username, char *password)
32303253

32313254
printf("\n\n");
32323255
}
3256+
3257+
static char *
3258+
selectVersion(PsqlSettings *pset)
3259+
{
3260+
#define PGVERSIONBUFSZ 128
3261+
static char version[PGVERSIONBUFSZ+1];
3262+
PGresult *res;
3263+
char *query = "select version();";
3264+
3265+
if (!(res = PQexec(pset->db, query))) return(NULL);
3266+
3267+
if (PQresultStatus(res) == PGRES_COMMAND_OK ||
3268+
PQresultStatus(res) == PGRES_TUPLES_OK )
3269+
{
3270+
strncpy(version, PQgetvalue(res,0,0), PGVERSIONBUFSZ);
3271+
version[PGVERSIONBUFSZ] = '\0';
3272+
PQclear(res);
3273+
return(version);
3274+
}
3275+
else
3276+
{
3277+
PQclear(res);
3278+
return(NULL);
3279+
}
3280+
}

0 commit comments

Comments
 (0)