7
7
*
8
8
*
9
9
* IDENTIFICATION
10
- * $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.122 1997/12/23 21:38:40 momjian Exp $
10
+ * $Header: /cvsroot/pgsql/src/bin/psql/Attic/psql.c,v 1.123 1998/01/05 02: 21:22 momjian Exp $
11
11
*
12
12
*-------------------------------------------------------------------------
13
13
*/
@@ -527,9 +527,11 @@ tableDesc(PsqlSettings *pset, char *table, FILE *fout)
527
527
char descbuf [256 ];
528
528
int nColumns ;
529
529
char * rtype ;
530
+ char * rnotnull ;
531
+ char * rhasdef ;
530
532
int i ;
531
533
int rsize ;
532
- PGresult * res ;
534
+ PGresult * res , * res2 ;
533
535
int usePipe = 0 ;
534
536
char * pagerenv ;
535
537
@@ -564,7 +566,7 @@ tableDesc(PsqlSettings *pset, char *table, FILE *fout)
564
566
}
565
567
566
568
descbuf [0 ]= '\0' ;
567
- strcat (descbuf ,"SELECT a.attnum, a.attname, t.typname, a.attlen, a.attnotnull " );
569
+ strcat (descbuf ,"SELECT a.attnum, a.attname, t.typname, a.attlen, a.attnotnull, a.atthasdef " );
568
570
strcat (descbuf ,"FROM pg_class c, pg_attribute a, pg_type t " );
569
571
strcat (descbuf ,"WHERE c.relname = '" );
570
572
strcat (descbuf ,table );
@@ -594,7 +596,7 @@ tableDesc(PsqlSettings *pset, char *table, FILE *fout)
594
596
fout = stdout ;
595
597
}
596
598
/*
597
- * * Display the information
599
+ * Display the information
598
600
*/
599
601
600
602
fprintf (fout ,"\nTable = %s\n" ,table );
@@ -605,39 +607,59 @@ tableDesc(PsqlSettings *pset, char *table, FILE *fout)
605
607
/* next, print out the instances */
606
608
for (i = 0 ;i < PQntuples (res );i ++ )
607
609
{
610
+ char type_str [33 ];
611
+
608
612
fprintf (fout ,"| %-32.32s | " ,PQgetvalue (res ,i ,1 ));
609
613
rtype = PQgetvalue (res ,i ,2 );
610
614
rsize = atoi (PQgetvalue (res ,i ,3 ));
611
- if (strcmp (rtype ,"text" )== 0 )
615
+ rnotnull = PQgetvalue (res ,i ,4 );
616
+ rhasdef = PQgetvalue (res ,i ,5 );
617
+
618
+ strcpy (type_str ,rtype );
619
+ if (strcmp (rtype ,"bpchar" )== 0 )
620
+ strcpy (type_str ,"char()" );
621
+ else if (strcmp (rtype ,"varchar" )== 0 )
622
+ strcpy (type_str ,"varchar()" );
623
+ else if (rtype [0 ]== '_' )
612
624
{
613
- fprintf (fout ,"%-32.32s |" ,rtype );
614
- fprintf (fout ,"%6s |" ,"var" );
625
+ strcpy (type_str ,rtype + 1 );
626
+ strncat (type_str ,"[]" ,32 - strlen (type_str ));
627
+ type_str [32 ]= '\0' ;
615
628
}
616
- else if (strcmp (rtype ,"bpchar" )== 0 )
629
+
630
+ if (rnotnull [0 ]== 't' )
617
631
{
618
- fprintf ( fout , "%-32.32s | " ,"(bp)char" );
619
- fprintf ( fout , "%6i |" , rsize > 0 ? rsize - VARHDRSZ : 0 ) ;
632
+ strncat ( type_str , " not null " ,32 - strlen ( type_str ) );
633
+ type_str [ 32 ] = '\0' ;
620
634
}
621
- else if (strcmp ( rtype , "varchar" ) == 0 )
635
+ if (rhasdef [ 0 ] == 't' )
622
636
{
623
- fprintf (fout ,"%-32.32s |" ,rtype );
624
- fprintf (fout ,"%6i |" ,rsize > 0 ?rsize - VARHDRSZ :0 );
637
+ descbuf [0 ]= '\0' ;
638
+ strcat (descbuf ,"SELECT d.adsrc " );
639
+ strcat (descbuf ,"FROM pg_attrdef d, pg_class c " );
640
+ strcat (descbuf ,"WHERE c.relname = '" );
641
+ strcat (descbuf ,table );
642
+ strcat (descbuf ,"'" );
643
+ strcat (descbuf ," and c.oid = d.adrelid " );
644
+ strcat (descbuf ," and d.adnum = " );
645
+ strcat (descbuf ,PQgetvalue (res ,i ,0 ));
646
+ if (!(res2 = PSQLexec (pset ,descbuf )))
647
+ return -1 ;
648
+ strcat (type_str ," default '" );
649
+ strncat (type_str ,PQgetvalue (res2 ,0 ,0 ),32 - strlen (type_str ));
650
+ type_str [32 ]= '\0' ;
651
+ strncat (type_str ,"'" ,32 - strlen (type_str ));
652
+ type_str [32 ]= '\0' ;
625
653
}
654
+ fprintf (fout ,"%-32.32s |" ,type_str );
655
+
656
+ if (strcmp (rtype ,"text" )== 0 )
657
+ fprintf (fout ,"%6s |" ,"var" );
658
+ else if (strcmp (rtype ,"bpchar" )== 0 ||
659
+ strcmp (rtype ,"varchar" )== 0 )
660
+ fprintf (fout ,"%6i |" ,rsize > 0 ?rsize - VARHDRSZ :0 );
626
661
else
627
662
{
628
- /* array types start with an underscore */
629
- if (rtype [0 ]!= '_' )
630
- fprintf (fout ,"%-32.32s |" ,rtype );
631
- else
632
- {
633
- char * newname ;
634
-
635
- newname = malloc (strlen (rtype )+ 2 );
636
- strcpy (newname ,rtype + 1 );
637
- strcat (newname ,"[]" );
638
- fprintf (fout ,"%-32.32s |" ,newname );
639
- free (newname );
640
- }
641
663
if (rsize > 0 )
642
664
fprintf (fout ,"%6i |" ,rsize );
643
665
else