20
20
*
21
21
*
22
22
* 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 $
24
24
*
25
- * Modifications - 6/10/96 - dave@bensoft.com
25
+ * Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
26
26
*
27
27
* Applied 'insert string' patch from "Marc G. Fournier" <scrappy@ki.net>
28
28
* Added '-t table' option
29
29
* Added '-a' option
30
30
* Added '-da' option
31
31
*
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
+ *
32
38
*-------------------------------------------------------------------------
33
39
*/
34
40
@@ -66,6 +72,7 @@ char g_comment_end[10];
66
72
static void
67
73
usage (char * progname )
68
74
{
75
+ fprintf (stderr , "%s - version 1.13.dhb.2\n\n" ,progname );
69
76
fprintf (stderr , "usage: %s [options] [dbname]\n" ,progname );
70
77
fprintf (stderr , "\t -f filename \t\t script output filename\n" );
71
78
fprintf (stderr , "\t -d[a] \t\t dump data as proper insert strings\n" );
@@ -745,6 +752,7 @@ getTableAttrs(TableInfo* tblinfo, int numTables)
745
752
char q [MAXQUERYLEN ];
746
753
int i_attname ;
747
754
int i_typname ;
755
+ int i_attlen ;
748
756
PGresult * res ;
749
757
int ntups ;
750
758
@@ -764,7 +772,7 @@ if (g_verbose)
764
772
tblinfo [i ].relname ,
765
773
g_comment_end );
766
774
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 );
768
776
res = PQexec (g_conn , q );
769
777
if (!res ||
770
778
PQresultStatus (res ) != PGRES_TUPLES_OK ) {
@@ -776,16 +784,21 @@ if (g_verbose)
776
784
777
785
i_attname = PQfnumber (res ,"attname" );
778
786
i_typname = PQfnumber (res ,"typname" );
787
+ i_attlen = PQfnumber (res ,"attlen" );
779
788
780
789
tblinfo [i ].numatts = ntups ;
781
790
tblinfo [i ].attnames = (char * * ) malloc ( ntups * sizeof (char * ));
782
791
tblinfo [i ].typnames = (char * * ) malloc ( ntups * sizeof (char * ));
792
+ tblinfo [i ].attlen = (int * ) malloc (ntups * sizeof (int ));
783
793
tblinfo [i ].inhAttrs = (int * ) malloc (ntups * sizeof (int ));
784
794
tblinfo [i ].parentRels = NULL ;
785
795
tblinfo [i ].numParents = 0 ;
786
796
for (j = 0 ;j < ntups ;j ++ ) {
787
797
tblinfo [i ].attnames [j ] = dupstr (PQgetvalue (res ,j ,i_attname ));
788
798
tblinfo [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 ;
789
802
tblinfo [i ].inhAttrs [j ] = 0 ; /* this flag is set in flagInhAttrs()*/
790
803
}
791
804
PQclear (res );
@@ -1194,12 +1207,33 @@ void dumpTables(FILE* fout, TableInfo *tblinfo, int numTables,
1194
1207
actual_atts = 0 ;
1195
1208
for (j = 0 ;j < tblinfo [i ].numatts ;j ++ ) {
1196
1209
if (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
+ }
1203
1237
}
1204
1238
}
1205
1239
@@ -1309,6 +1343,8 @@ dumpClasses(TableInfo *tblinfo, int numTables, FILE *fout, char *onlytable)
1309
1343
char query [255 ];
1310
1344
#define COPYBUFSIZ 8192
1311
1345
char copybuf [COPYBUFSIZ ];
1346
+ char expandbuf [COPYBUFSIZ ];
1347
+ char * expsrc ,* expdest ;
1312
1348
char q [MAXQUERYLEN ];
1313
1349
PGresult * res ;
1314
1350
int i ,j ;
@@ -1397,7 +1433,21 @@ dumpClasses(TableInfo *tblinfo, int numTables, FILE *fout, char *onlytable)
1397
1433
fprintf (fout , "%s" , PQgetvalue (res ,tuple ,field ));
1398
1434
break ;
1399
1435
default :
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 );
1401
1451
break ;
1402
1452
}
1403
1453
field ++ ;
0 commit comments