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
133133bool notty ;/* input or output is not a tty */
134134bool pipe ;/* queryFout is from a popen() */
135135bool echoQuery ;/* echo the query before sending it */
136+ bool echoAllQueries ;/* echo all queries before sending it*/
136137bool quiet ;/* run quietly, no messages, no promt */
137138bool singleStep ;/* prompt before for each query */
138139bool singleLineMode ;/* query terminated by newline */
@@ -178,6 +179,8 @@ static int
178179static int MainLoop (PsqlSettings * pset ,char * query ,FILE * source );
179180static 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)
191194fprintf (stderr ,"\t -c query run single query (slash commands too)\n" );
192195fprintf (stderr ,"\t -d dbName specify database name\n" );
193196fprintf (stderr ,"\t -e echo the query sent to the backend\n" );
197+ fprintf (stderr ,"\t -E echo all queries sent to the backend\n" );
194198fprintf (stderr ,"\t -f filename use file as a source of queries\n" );
195199fprintf (stderr ,"\t -F sep set the field separator (default is '|')\n" );
196200fprintf (stderr ,"\t -h host set database server host\n" );
@@ -242,7 +246,7 @@ slashUsage(PsqlSettings *pset)
242246if (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{
248252usePipe = 1 ;
@@ -300,6 +304,13 @@ PSQLexec(PsqlSettings *pset, char *query)
300304{
301305PGresult * res ;
302306
307+ if (pset -> echoAllQueries )
308+ {
309+ fprintf (stderr ,"QUERY: %s\n" ,query );
310+ fprintf (stderr ,"\n" );
311+ fflush (stderr );
312+ }
313+
303314res = PQexec (pset -> db ,query );
304315if (!res )
305316fputs (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 ));
494505fprintf (fout ," +------------------+----------------------------------+----------+\n" );
495506fprintf (fout ," | Owner | Relation | Type |\n" );
496507fprintf (fout ," +------------------+----------------------------------+----------+\n" );
@@ -511,6 +522,7 @@ tableList(PsqlSettings *pset, bool deep_tablelist, char info_type,
511522fprintf (fout ,"\n" );
512523}
513524fprintf (fout ," +------------------+----------------------------------+----------+\n" );
525+ fprintf (fout ,"\n" ) ;
514526PQclear (res );
515527}
516528if (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 ));
618630fprintf (fout ," +" );
619631emitNtimes (fout ,"-" ,maxCol1Len + 2 );
620632fprintf (fout ,"+" );
@@ -780,12 +792,12 @@ tableDesc(PsqlSettings *pset, char *table, FILE *fout)
780792if (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 );
786798fprintf (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}
790802PQclear (res2 );
791803
@@ -889,6 +901,7 @@ tableDesc(PsqlSettings *pset, char *table, FILE *fout)
889901fprintf (fout ,"%s\n" ,PQgetvalue (res ,i ,0 ));
890902else
891903fprintf (fout ," %s\n" ,PQgetvalue (res ,i ,0 ));
904+ fprintf (fout ,"\n" );
892905}
893906PQclear (res );
894907}
@@ -2815,6 +2828,7 @@ main(int argc, char **argv)
28152828int c ;
28162829
28172830char * home = NULL ;/* Used to store $HOME */
2831+ char * version = NULL ;/* PostgreSQL version */
28182832
28192833MemSet (& settings ,0 ,sizeof settings );
28202834settings .opt .align = 1 ;
@@ -2845,7 +2859,7 @@ main(int argc, char **argv)
28452859has_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{
28502864switch (c )
28512865{
@@ -2868,6 +2882,10 @@ main(int argc, char **argv)
28682882case 'e' :
28692883settings .echoQuery = 1 ;
28702884break ;
2885+ case 'E' :
2886+ settings .echoAllQueries = 1 ;
2887+ settings .echoQuery = 1 ;
2888+ break ;
28712889case 'f' :
28722890qfilename = optarg ;
28732891break ;
@@ -2956,7 +2974,12 @@ main(int argc, char **argv)
29562974{
29572975printf ("Welcome to the POSTGRESQL interactive sql monitor:\n" );
29582976printf (" 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" );
29602983printf (" type \\? for help on slash commands\n" );
29612984printf (" type \\q to quit\n" );
29622985printf (" type \\g or terminate with semicolon to execute query\n" );
@@ -3230,3 +3253,28 @@ prompt_for_password(char *username, char *password)
32303253
32313254printf ("\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+ }