7
7
*
8
8
*
9
9
* 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 $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
@@ -89,7 +89,7 @@ char *__progname = "psql";
89
89
90
90
#ifdef MULTIBYTE
91
91
/* flag to indicate if PGCLIENTENCODING has been set by a user */
92
- static int has_client_encoding ;
92
+ static char * has_client_encoding = 0 ;
93
93
#endif
94
94
95
95
/* This prompt string is assumed to have at least 3 characters by code in MainLoop().
@@ -133,6 +133,7 @@ typedef struct _psqlSettings
133
133
bool notty ; /* input or output is not a tty */
134
134
bool pipe ; /* queryFout is from a popen() */
135
135
bool echoQuery ; /* echo the query before sending it */
136
+ bool echoAllQueries ; /* echo all queries before sending it*/
136
137
bool quiet ; /* run quietly, no messages, no promt */
137
138
bool singleStep ; /* prompt before for each query */
138
139
bool singleLineMode ; /* query terminated by newline */
@@ -178,6 +179,8 @@ static int
178
179
static int MainLoop (PsqlSettings * pset , char * query , FILE * source );
179
180
static FILE * setFout (PsqlSettings * pset , char * fname );
180
181
182
+ static char * selectVersion (PsqlSettings * pset );
183
+
181
184
/*
182
185
* usage print out usage for command line arguments
183
186
*/
@@ -191,6 +194,7 @@ usage(char *progname)
191
194
fprintf (stderr , "\t -c query run single query (slash commands too)\n" );
192
195
fprintf (stderr , "\t -d dbName specify database name\n" );
193
196
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" );
194
198
fprintf (stderr , "\t -f filename use file as a source of queries\n" );
195
199
fprintf (stderr , "\t -F sep set the field separator (default is '|')\n" );
196
200
fprintf (stderr , "\t -h host set database server host\n" );
@@ -242,7 +246,7 @@ slashUsage(PsqlSettings *pset)
242
246
if (pset -> notty == 0 &&
243
247
(pagerenv = getenv ("PAGER" )) &&
244
248
(pagerenv [0 ] != '\0' ) &&
245
- screen_size .ws_row <= 28 &&
249
+ screen_size .ws_row <= 35 &&
246
250
(fout = popen (pagerenv , "w" )))
247
251
{
248
252
usePipe = 1 ;
@@ -300,6 +304,13 @@ PSQLexec(PsqlSettings *pset, char *query)
300
304
{
301
305
PGresult * res ;
302
306
307
+ if (pset -> echoAllQueries )
308
+ {
309
+ fprintf (stderr , "QUERY: %s\n" , query );
310
+ fprintf (stderr , "\n" );
311
+ fflush (stderr );
312
+ }
313
+
303
314
res = PQexec (pset -> db , query );
304
315
if (!res )
305
316
fputs (PQerrorMessage (pset -> db ), stderr );
@@ -490,7 +501,7 @@ tableList(PsqlSettings *pset, bool deep_tablelist, char info_type,
490
501
{
491
502
/* Display the information */
492
503
493
- fprintf (fout , "\nDatabase = %s\n" , PQdb (pset -> db ));
504
+ fprintf (fout , "Database = %s\n" , PQdb (pset -> db ));
494
505
fprintf (fout , " +------------------+----------------------------------+----------+\n" );
495
506
fprintf (fout , " | Owner | Relation | Type |\n" );
496
507
fprintf (fout , " +------------------+----------------------------------+----------+\n" );
@@ -511,6 +522,7 @@ tableList(PsqlSettings *pset, bool deep_tablelist, char info_type,
511
522
fprintf (fout , "\n" );
512
523
}
513
524
fprintf (fout , " +------------------+----------------------------------+----------+\n" );
525
+ fprintf (fout , "\n" ) ;
514
526
PQclear (res );
515
527
}
516
528
if (usePipe )
@@ -614,7 +626,7 @@ rightsList(PsqlSettings *pset)
614
626
615
627
/* Display the information */
616
628
617
- fprintf (fout , "\nDatabase = %s\n" , PQdb (pset -> db ));
629
+ fprintf (fout , "Database = %s\n" , PQdb (pset -> db ));
618
630
fprintf (fout , " +" );
619
631
emitNtimes (fout , "-" , maxCol1Len + 2 );
620
632
fprintf (fout , "+" );
@@ -780,12 +792,12 @@ tableDesc(PsqlSettings *pset, char *table, FILE *fout)
780
792
if (PQntuples (res2 )) {
781
793
/*
782
794
* display the query.
783
- * -Ryan 2/14/99
795
+ o * -Ryan 2/14/99
784
796
*/
785
- fprintf (fout , "\nView = %s\n" , table );
797
+ fprintf (fout , "View = %s\n" , table );
786
798
fprintf (fout , "Query = %s\n" , PQgetvalue (res2 , 0 , 1 ));
787
799
} else {
788
- fprintf (fout , "\nTable = %s\n" , table );
800
+ fprintf (fout , "Table = %s\n" , table );
789
801
}
790
802
PQclear (res2 );
791
803
@@ -889,6 +901,7 @@ tableDesc(PsqlSettings *pset, char *table, FILE *fout)
889
901
fprintf (fout , "%s\n" , PQgetvalue (res , i , 0 ));
890
902
else
891
903
fprintf (fout , " %s\n" , PQgetvalue (res , i , 0 ));
904
+ fprintf (fout , "\n" );
892
905
}
893
906
PQclear (res );
894
907
}
@@ -2815,6 +2828,7 @@ main(int argc, char **argv)
2815
2828
int c ;
2816
2829
2817
2830
char * home = NULL ; /* Used to store $HOME */
2831
+ char * version = NULL ; /* PostgreSQL version */
2818
2832
2819
2833
MemSet (& settings , 0 , sizeof settings );
2820
2834
settings .opt .align = 1 ;
@@ -2845,7 +2859,7 @@ main(int argc, char **argv)
2845
2859
has_client_encoding = getenv ("PGCLIENTENCODING" );
2846
2860
#endif
2847
2861
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 )
2849
2863
{
2850
2864
switch (c )
2851
2865
{
@@ -2868,6 +2882,10 @@ main(int argc, char **argv)
2868
2882
case 'e' :
2869
2883
settings .echoQuery = 1 ;
2870
2884
break ;
2885
+ case 'E' :
2886
+ settings .echoAllQueries = 1 ;
2887
+ settings .echoQuery = 1 ;
2888
+ break ;
2871
2889
case 'f' :
2872
2890
qfilename = optarg ;
2873
2891
break ;
@@ -2956,7 +2974,12 @@ main(int argc, char **argv)
2956
2974
{
2957
2975
printf ("Welcome to the POSTGRESQL interactive sql monitor:\n" );
2958
2976
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" );
2960
2983
printf (" type \\? for help on slash commands\n" );
2961
2984
printf (" type \\q to quit\n" );
2962
2985
printf (" type \\g or terminate with semicolon to execute query\n" );
@@ -3230,3 +3253,28 @@ prompt_for_password(char *username, char *password)
3230
3253
3231
3254
printf ("\n\n" );
3232
3255
}
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