Skip to content

Commit 0af9137

Browse files
committed
Add NOT NULL and DEFAULT to \d table.
1 parent 0aa9287 commit 0af9137

File tree

1 file changed

+48
-26
lines changed

1 file changed

+48
-26
lines changed

src/bin/psql/psql.c

Lines changed: 48 additions & 26 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.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 $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -527,9 +527,11 @@ tableDesc(PsqlSettings *pset, char *table, FILE *fout)
527527
char descbuf[256];
528528
int nColumns;
529529
char *rtype;
530+
char *rnotnull;
531+
char *rhasdef;
530532
int i;
531533
int rsize;
532-
PGresult *res;
534+
PGresult *res, *res2;
533535
int usePipe = 0;
534536
char *pagerenv;
535537

@@ -564,7 +566,7 @@ tableDesc(PsqlSettings *pset, char *table, FILE *fout)
564566
}
565567

566568
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 ");
568570
strcat(descbuf, "FROM pg_class c, pg_attribute a, pg_type t ");
569571
strcat(descbuf, "WHERE c.relname = '");
570572
strcat(descbuf, table);
@@ -594,7 +596,7 @@ tableDesc(PsqlSettings *pset, char *table, FILE *fout)
594596
fout = stdout;
595597
}
596598
/*
597-
* * Display the information
599+
* Display the information
598600
*/
599601

600602
fprintf(fout,"\nTable = %s\n", table);
@@ -605,39 +607,59 @@ tableDesc(PsqlSettings *pset, char *table, FILE *fout)
605607
/* next, print out the instances */
606608
for (i = 0; i < PQntuples(res); i++)
607609
{
610+
char type_str[33];
611+
608612
fprintf(fout,"| %-32.32s | ", PQgetvalue(res, i, 1));
609613
rtype = PQgetvalue(res, i, 2);
610614
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] == '_')
612624
{
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';
615628
}
616-
else if (strcmp(rtype, "bpchar") == 0)
629+
630+
if (rnotnull[0] == 't')
617631
{
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';
620634
}
621-
else if (strcmp(rtype, "varchar") == 0)
635+
if (rhasdef[0] == 't')
622636
{
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';
625653
}
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);
626661
else
627662
{
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-
}
641663
if (rsize > 0)
642664
fprintf(fout,"%6i |", rsize);
643665
else

0 commit comments

Comments
 (0)