77 *
88 *
99 * IDENTIFICATION
10- * $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.104 1997/11/1405:57:35 momjian Exp $
10+ * $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.105 1997/11/1421:37:41 momjian Exp $
1111 *
1212 *-------------------------------------------------------------------------
1313 */
@@ -135,14 +135,6 @@ HandleSlashCmds(PsqlSettings *pset,
135135char * line ,
136136char * query );
137137static int MainLoop (PsqlSettings * pset ,char * query ,FILE * source );
138-
139- /* probably should move this into libpq */
140- void
141- PQprint (FILE * fp ,
142- PGresult * res ,
143- PQprintOpt * po
144- );
145-
146138static FILE * setFout (PsqlSettings * pset ,char * fname );
147139
148140/*
@@ -224,11 +216,14 @@ slashUsage(PsqlSettings *pset)
224216fprintf (fout ," \\C [<captn>] -- set html3 caption (currently '%s')\n" ,pset -> opt .caption ?pset -> opt .caption :"" );
225217fprintf (fout ," \\connect <dbname|-> <user> -- connect to new database (currently '%s')\n" ,PQdb (pset -> db ));
226218fprintf (fout ," \\copy table {from | to} <fname>\n" );
227- fprintf (fout ," \\d [<table>] -- list tables and indices in database or columns in <table>, * for all\n" );
219+ fprintf (fout ," \\d [<table>] -- list tables and indices, columns in <table>, or * for all\n" );
220+ fprintf (fout ," \\da -- list aggregates\n" );
228221fprintf (fout ," \\dd [<object>]- list comment for table, field, type, function, or operator.\n" );
229- fprintf (fout ," \\di -- list only indices in database\n" );
230- fprintf (fout ," \\ds -- list only sequences in database\n" );
231- fprintf (fout ," \\dt -- list only tables in database\n" );
222+ fprintf (fout ," \\di -- list only indices\n" );
223+ fprintf (fout ," \\do -- list operators\n" );
224+ fprintf (fout ," \\ds -- list only sequences\n" );
225+ fprintf (fout ," \\dt -- list only tables\n" );
226+ fprintf (fout ," \\dT -- list types\n" );
232227fprintf (fout ," \\e [<fname>] -- edit the current query buffer or <fname>\n" );
233228fprintf (fout ," \\E [<fname>] -- edit the current query buffer or <fname>, and execute\n" );
234229fprintf (fout ," \\f [<sep>] -- change field separater (currently '%s')\n" ,pset -> opt .fieldSep );
@@ -751,59 +746,45 @@ objectDescription(PsqlSettings *pset, char *object, FILE *fout)
751746strcat (descbuf ,"FROM pg_type, pg_description " );
752747strcat (descbuf ,"WHERE pg_type.typname = '" );
753748strcat (descbuf ,object );
754- strcat (descbuf ,"'" );
755- strcat (descbuf ,"and pg_type.oid = pg_description.objoid " );
749+ strcat (descbuf ,"' and " );
750+ strcat (descbuf ," pg_type.oid = pg_description.objoid " );
756751if (!(res = PSQLexec (pset ,descbuf )))
757752return -1 ;
758- else if (PQntuples (res ) <=0 )
759753{
760754PQclear (res );
761755descbuf [0 ]= '\0' ;
762756strcat (descbuf ,"SELECT DISTINCT description " );
763- strcat (descbuf ,"FROMpg_type , pg_description " );
764- strcat (descbuf ,"WHEREpg_type.typname = '" );
757+ strcat (descbuf ,"FROMpg_proc , pg_description " );
758+ strcat (descbuf ,"WHEREpg_proc.proname = '" );
765759strcat (descbuf ,object );
766760strcat (descbuf ,"'" );
767- strcat (descbuf ," andpg_type .oid = pg_description.objoid " );
761+ strcat (descbuf ," andpg_proc .oid = pg_description.objoid " );
768762if (!(res = PSQLexec (pset ,descbuf )))
769763return -1 ;
770764else if (PQntuples (res ) <=0 )
771765{
772766PQclear (res );
773767descbuf [0 ]= '\0' ;
774768strcat (descbuf ,"SELECT DISTINCT description " );
775- strcat (descbuf ,"FROMpg_proc , pg_description " );
776- strcat (descbuf ,"WHEREpg_proc.proname = '" );
769+ strcat (descbuf ,"FROMpg_operator , pg_description " );
770+ strcat (descbuf ,"WHEREpg_operator.oprname = '" );
777771strcat (descbuf ,object );
778772strcat (descbuf ,"'" );
779- strcat (descbuf ," andpg_proc .oid = pg_description.objoid " );
773+ strcat (descbuf ," andpg_operator .oid = pg_description.objoid " );
780774if (!(res = PSQLexec (pset ,descbuf )))
781775return -1 ;
782776else if (PQntuples (res ) <=0 )
783777{
784778PQclear (res );
785779descbuf [0 ]= '\0' ;
786780strcat (descbuf ,"SELECT DISTINCT description " );
787- strcat (descbuf ,"FROMpg_operator , pg_description " );
788- strcat (descbuf ,"WHEREpg_operator.oprname = '" );
781+ strcat (descbuf ,"FROMpg_aggregate , pg_description " );
782+ strcat (descbuf ,"WHEREpg_aggregate.aggname = '" );
789783strcat (descbuf ,object );
790784strcat (descbuf ,"'" );
791- strcat (descbuf ," andpg_operator .oid = pg_description.objoid " );
785+ strcat (descbuf ," andpg_aggregate .oid = pg_description.objoid " );
792786if (!(res = PSQLexec (pset ,descbuf )))
793787return -1 ;
794- else if (PQntuples (res ) <=0 )
795- {
796- PQclear (res );
797- descbuf [0 ]= '\0' ;
798- strcat (descbuf ,"SELECT DISTINCT description " );
799- strcat (descbuf ,"FROM pg_aggregate, pg_description " );
800- strcat (descbuf ,"WHERE pg_aggregate.aggname = '" );
801- strcat (descbuf ,object );
802- strcat (descbuf ,"'" );
803- strcat (descbuf ," and pg_aggregate.oid = pg_description.objoid " );
804- if (!(res = PSQLexec (pset ,descbuf )))
805- return -1 ;
806- }
807788}
808789}
809790}
@@ -1584,7 +1565,7 @@ HandleSlashCmds(PsqlSettings *pset,
15841565{
15851566int status = CMD_SKIP_LINE ;
15861567char * optarg ;
1587-
1568+ bool success ;
15881569/*
15891570 * Pointer inside the <cmd> string to the argument of the slash
15901571 * command, assuming it is a one-character slash command. If it's not
@@ -1696,20 +1677,82 @@ HandleSlashCmds(PsqlSettings *pset,
16961677}
16971678}
16981679break ;
1699- case 'd' :/* \d describe tables or columns in a
1700- * table */
1701- if (strncmp (cmd ,"dd" ,2 )== 0 )
1680+ case 'd' :/* \d describe database information */
1681+ if (strncmp (cmd ,"da" ,2 )== 0 )
1682+ /* aggregates */
1683+ SendQuery (& success ,pset ,"\
1684+ SELECTa.aggname AS aggname, \
1685+ t.typname AS typname, \
1686+ obj_description(a.oid) as description \
1687+ FROMpg_aggregate a, pg_type t \
1688+ WHEREa.aggbasetype = t.oid \
1689+ ORDER BY aggname, typname;" ,
1690+ false, false,0 );
1691+ else if (strncmp (cmd ,"dd" ,2 )== 0 )
17021692/* descriptions */
17031693objectDescription (pset ,optarg + 1 ,NULL );
17041694else if (strncmp (cmd ,"di" ,2 )== 0 )
17051695/* only indices */
17061696tableList (pset , false,'i' );
1697+ else if (strncmp (cmd ,"do" ,2 )== 0 )
1698+ {
1699+ /* operators */
1700+ SendQuery (& success ,pset ,"\
1701+ SELECTt0.typname AS result, \
1702+ t1.typname AS left_type, \
1703+ t2.typname AS right_type, \
1704+ o.oprname AS operatr, \
1705+ p.proname AS func_name, \
1706+ obj_description(o.oid) as description \
1707+ FROMpg_proc p, pg_type t0, \
1708+ pg_type t1, pg_type t2, \
1709+ pg_operator o \
1710+ WHEREp.prorettype = t0.oid AND \
1711+ RegprocToOid(o.oprcode) = p.oid AND \
1712+ p.pronargs = 2 AND \
1713+ o.oprleft = t1.oid AND \
1714+ o.oprright = t2.oid \
1715+ ORDER BY result, left_type, right_type, operatr;" ,
1716+ false, false,0 );
1717+ SendQuery (& success ,pset ,"\
1718+ SELECTo.oprname AS left_unary, \
1719+ t.typname AS operand, \
1720+ r.typname AS return_type, \
1721+ obj_description(o.oid) as description \
1722+ FROMpg_operator o, pg_type t, pg_type r \
1723+ WHEREo.oprkind = 'l' AND \
1724+ o.oprright = t.oid AND \
1725+ o.oprresult = r.oid \
1726+ ORDER BY operand;" ,
1727+ false, false,0 );
1728+ SendQuery (& success ,pset ,"\
1729+ SELECTo.oprname AS right_unary, \
1730+ t.typname AS operand, \
1731+ r.typname AS return_type, \
1732+ obj_description(o.oid) as description \
1733+ FROMpg_operator o, pg_type t, pg_type r \
1734+ WHEREo.oprkind = 'r' AND \
1735+ o.oprleft = t.oid AND \
1736+ o.oprresult = r.oid \
1737+ ORDER BY operand;" ,
1738+ false, false,0 );
1739+ }
17071740else if (strncmp (cmd ,"ds" ,2 )== 0 )
17081741/* only sequences */
17091742tableList (pset , false,'S' );
17101743else if (strncmp (cmd ,"dt" ,2 )== 0 )
17111744/* only tables */
17121745tableList (pset , false,'t' );
1746+ else if (strncmp (cmd ,"dT" ,2 )== 0 )
1747+ /* types */
1748+ SendQuery (& success ,pset ,"\
1749+ SELECTtypname AS type, \
1750+ obj_description(oid) as description \
1751+ FROMpg_type \
1752+ WHEREtyprelid = 0 AND \
1753+ typname !~ '^_.*' \
1754+ ORDER BY type;" ,
1755+ false, false,0 );
17131756else if (!optarg )
17141757/* show tables, sequences and indices */
17151758tableList (pset , false,'b' );
@@ -1718,9 +1761,12 @@ HandleSlashCmds(PsqlSettings *pset,
17181761if (tableList (pset , false,'b' )== 0 )
17191762tableList (pset , true,'b' );
17201763}
1721- else
1764+ else if ( strncmp ( cmd , "d " , 2 ) == 0 )
17221765/* describe the specified table */
17231766tableDesc (pset ,optarg ,NULL );
1767+ else
1768+ slashUsage (pset );
1769+
17241770break ;
17251771case 'e' :/* edit */
17261772{