Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0af9137

Browse files
committed
Add NOT NULL and DEFAULT to \d table.
1 parent0aa9287 commit0af9137

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/2321: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
chardescbuf[256];
528528
intnColumns;
529529
char*rtype;
530+
char*rnotnull;
531+
char*rhasdef;
530532
inti;
531533
intrsize;
532-
PGresult*res;
534+
PGresult*res,*res2;
533535
intusePipe=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+
chartype_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+
elseif (strcmp(rtype,"varchar")==0)
622+
strcpy(type_str,"varchar()");
623+
elseif (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-
elseif (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-
elseif (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+
elseif (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)

[8]ページ先頭

©2009-2025 Movatter.jp