2020 *
2121 *
2222 * IDENTIFICATION
23- * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.2 1996/07/12 05:39:35 scrappy Exp $
23+ * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.3 1996/07/22 08:36:59 scrappy Exp $
2424 *
25- * Modifications - 6/10/96 - dave@bensoft.com
25+ * Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
2626 *
2727 * Applied 'insert string' patch from "Marc G. Fournier" <scrappy@ki.net>
2828 * Added '-t table' option
2929 * Added '-a' option
3030 * Added '-da' option
3131 *
32+ * Modifications - 6/12/96 - dave@bensoft.com - version 1.13.dhb.2
33+ *
34+ * - Fixed dumpTable output to output lengths for char and varchar types!
35+ * - Added single. quote to twin single quote expansion for 'insert' string
36+ * mode.
37+ *
3238 *-------------------------------------------------------------------------
3339 */
3440
@@ -66,6 +72,7 @@ char g_comment_end[10];
6672static void
6773usage (char * progname )
6874{
75+ fprintf (stderr ,"%s - version 1.13.dhb.2\n\n" ,progname );
6976fprintf (stderr ,"usage: %s [options] [dbname]\n" ,progname );
7077fprintf (stderr ,"\t -f filename \t\t script output filename\n" );
7178fprintf (stderr ,"\t -d[a] \t\t dump data as proper insert strings\n" );
@@ -745,6 +752,7 @@ getTableAttrs(TableInfo* tblinfo, int numTables)
745752char q [MAXQUERYLEN ];
746753int i_attname ;
747754int i_typname ;
755+ int i_attlen ;
748756PGresult * res ;
749757int ntups ;
750758
@@ -764,7 +772,7 @@ if (g_verbose)
764772tblinfo [i ].relname ,
765773g_comment_end );
766774
767- sprintf (q ,"SELECT a.attnum, a.attname, t.typname from pg_attribute a, pg_type t where a.attrelid = '%s'::oid and a.atttypid = t.oid and a.attnum > 0 order by attnum" ,tblinfo [i ].oid );
775+ sprintf (q ,"SELECT a.attnum, a.attname, t.typname, a.attlen from pg_attribute a, pg_type t where a.attrelid = '%s'::oid and a.atttypid = t.oid and a.attnum > 0 order by attnum" ,tblinfo [i ].oid );
768776res = PQexec (g_conn ,q );
769777if (!res ||
770778PQresultStatus (res )!= PGRES_TUPLES_OK ) {
@@ -776,16 +784,21 @@ if (g_verbose)
776784
777785i_attname = PQfnumber (res ,"attname" );
778786i_typname = PQfnumber (res ,"typname" );
787+ i_attlen = PQfnumber (res ,"attlen" );
779788
780789tblinfo [i ].numatts = ntups ;
781790tblinfo [i ].attnames = (char * * )malloc (ntups * sizeof (char * ));
782791tblinfo [i ].typnames = (char * * )malloc (ntups * sizeof (char * ));
792+ tblinfo [i ].attlen = (int * )malloc (ntups * sizeof (int ));
783793tblinfo [i ].inhAttrs = (int * )malloc (ntups * sizeof (int ));
784794tblinfo [i ].parentRels = NULL ;
785795tblinfo [i ].numParents = 0 ;
786796for (j = 0 ;j < ntups ;j ++ ) {
787797tblinfo [i ].attnames [j ]= dupstr (PQgetvalue (res ,j ,i_attname ));
788798tblinfo [i ].typnames [j ]= dupstr (PQgetvalue (res ,j ,i_typname ));
799+ tblinfo [i ].attlen [j ]= atoi (PQgetvalue (res ,j ,i_attlen ));
800+ if (tblinfo [i ].attlen [j ]> 0 )
801+ tblinfo [i ].attlen [j ]= tblinfo [i ].attlen [j ]- 4 ;
789802tblinfo [i ].inhAttrs [j ]= 0 ;/* this flag is set in flagInhAttrs()*/
790803}
791804PQclear (res );
@@ -1194,12 +1207,33 @@ void dumpTables(FILE* fout, TableInfo *tblinfo, int numTables,
11941207actual_atts = 0 ;
11951208for (j = 0 ;j < tblinfo [i ].numatts ;j ++ ) {
11961209if (tblinfo [i ].inhAttrs [j ]== 0 ) {
1197- sprintf (q ,"%s%s%s %s" ,
1198- q ,
1199- (actual_atts > 0 ) ?", " :"" ,
1200- tblinfo [i ].attnames [j ],
1201- tblinfo [i ].typnames [j ]);
1202- actual_atts ++ ;
1210+
1211+ /* Show lengths on bpchar and varchar */
1212+ if (!strcmp (tblinfo [i ].typnames [j ],"bpchar" )) {
1213+ sprintf (q ,"%s%s%s char(%d)" ,
1214+ q ,
1215+ (actual_atts > 0 ) ?", " :"" ,
1216+ tblinfo [i ].attnames [j ],
1217+ tblinfo [i ].attlen [j ]);
1218+ actual_atts ++ ;
1219+ }
1220+ else if (!strcmp (tblinfo [i ].typnames [j ],"varchar" )) {
1221+ sprintf (q ,"%s%s%s %s(%d)" ,
1222+ q ,
1223+ (actual_atts > 0 ) ?", " :"" ,
1224+ tblinfo [i ].attnames [j ],
1225+ tblinfo [i ].typnames [j ],
1226+ tblinfo [i ].attlen [j ]);
1227+ actual_atts ++ ;
1228+ }
1229+ else {
1230+ sprintf (q ,"%s%s%s %s" ,
1231+ q ,
1232+ (actual_atts > 0 ) ?", " :"" ,
1233+ tblinfo [i ].attnames [j ],
1234+ tblinfo [i ].typnames [j ]);
1235+ actual_atts ++ ;
1236+ }
12031237 }
12041238 }
12051239
@@ -1309,6 +1343,8 @@ dumpClasses(TableInfo *tblinfo, int numTables, FILE *fout, char *onlytable)
13091343char query [255 ];
13101344#define COPYBUFSIZ 8192
13111345char copybuf [COPYBUFSIZ ];
1346+ char expandbuf [COPYBUFSIZ ];
1347+ char * expsrc ,* expdest ;
13121348char q [MAXQUERYLEN ];
13131349PGresult * res ;
13141350int i ,j ;
@@ -1397,7 +1433,21 @@ dumpClasses(TableInfo *tblinfo, int numTables, FILE *fout, char *onlytable)
13971433fprintf (fout ,"%s" ,PQgetvalue (res ,tuple ,field ));
13981434break ;
13991435default :
1400- fprintf (fout ,"'%s'" ,PQgetvalue (res ,tuple ,field ));
1436+
1437+ /* Before outputing string value, expand all
1438+ single quotes to twin single quotes -
1439+ dhb - 6/11/96 */
1440+ expsrc = PQgetvalue (res ,tuple ,field );
1441+ expdest = expandbuf ;
1442+ while (* expsrc ) {
1443+ * expdest ++ = * expsrc ;
1444+ if (* expsrc == (char )0x27 )/*sing. quote*/
1445+ * expdest ++ = * expsrc ;
1446+ expsrc ++ ;
1447+ }
1448+ * expdest = * expsrc ;/* null term. */
1449+
1450+ fprintf (fout ,"'%s'" ,expandbuf );
14011451break ;
14021452 }
14031453field ++ ;