22
22
*
23
23
*
24
24
* IDENTIFICATION
25
- * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.245 2002/04/05 00:31:31 tgl Exp $
25
+ * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.246 2002/04/05 11:51:12 momjian Exp $
26
26
*
27
27
*-------------------------------------------------------------------------
28
28
*/
@@ -77,6 +77,7 @@ typedef enum _formatLiteralOptions
77
77
static void dumpComment (Archive * fout , const char * target , const char * oid ,
78
78
const char * classname , int subid ,
79
79
const char * ((* deps )[]));
80
+ static void dumpOneDomain (Archive * fout , TypeInfo * tinfo );
80
81
static void dumpSequence (Archive * fout , TableInfo tbinfo , const bool schemaOnly , const bool dataOnly );
81
82
static void dumpACL (Archive * fout , TableInfo tbinfo );
82
83
static void dumpTriggers (Archive * fout , const char * tablename ,
@@ -91,6 +92,7 @@ static Oid findLastBuiltinOid_V71(const char *);
91
92
static Oid findLastBuiltinOid_V70 (void );
92
93
static void setMaxOid (Archive * fout );
93
94
95
+
94
96
static void AddAcl (char * aclbuf , const char * keyword );
95
97
static char * GetPrivileges (Archive * AH , const char * s );
96
98
@@ -1352,6 +1354,7 @@ getTypes(int *numTypes)
1352
1354
int i_typisdefined ;
1353
1355
int i_usename ;
1354
1356
int i_typedefn ;
1357
+ int i_typtype ;
1355
1358
1356
1359
/* find all base types */
1357
1360
@@ -1368,7 +1371,7 @@ getTypes(int *numTypes)
1368
1371
"typinput, typoutput, typreceive, typsend, typelem, typdelim, "
1369
1372
"typdefault, typrelid, typalign, 'p'::char as typstorage, typbyval, typisdefined, "
1370
1373
"(select usename from pg_user where typowner = usesysid) as usename, "
1371
- "typname as typedefn "
1374
+ "typname as typedefn, typtype "
1372
1375
"from pg_type" );
1373
1376
}
1374
1377
else
@@ -1377,7 +1380,7 @@ getTypes(int *numTypes)
1377
1380
"typinput, typoutput, typreceive, typsend, typelem, typdelim, "
1378
1381
"typdefault, typrelid, typalign, typstorage, typbyval, typisdefined, "
1379
1382
"(select usename from pg_user where typowner = usesysid) as usename, "
1380
- "format_type(pg_type.oid, NULL) as typedefn "
1383
+ "format_type(pg_type.oid, NULL) as typedefn, typtype "
1381
1384
"from pg_type" );
1382
1385
}
1383
1386
@@ -1412,6 +1415,7 @@ getTypes(int *numTypes)
1412
1415
i_typisdefined = PQfnumber (res , "typisdefined" );
1413
1416
i_usename = PQfnumber (res , "usename" );
1414
1417
i_typedefn = PQfnumber (res , "typedefn" );
1418
+ i_typtype = PQfnumber (res , "typtype" );
1415
1419
1416
1420
for (i = 0 ; i < ntups ; i ++ )
1417
1421
{
@@ -1435,6 +1439,7 @@ getTypes(int *numTypes)
1435
1439
tinfo [i ].typstorage = strdup (PQgetvalue (res , i , i_typstorage ));
1436
1440
tinfo [i ].usename = strdup (PQgetvalue (res , i , i_usename ));
1437
1441
tinfo [i ].typedefn = strdup (PQgetvalue (res , i , i_typedefn ));
1442
+ tinfo [i ].typtype = strdup (PQgetvalue (res , i , i_typtype ));
1438
1443
1439
1444
if (strlen (tinfo [i ].usename ) == 0 )
1440
1445
write_msg (NULL , "WARNING: owner of data type %s appears to be invalid\n" ,
@@ -3188,6 +3193,88 @@ dumpDBComment(Archive *fout)
3188
3193
destroyPQExpBuffer (query );
3189
3194
}
3190
3195
3196
+ /*
3197
+ * dumpOneDomain
3198
+ * wites out to fout the queries to recrease a user-defined domains
3199
+ * as requested by dumpTypes
3200
+ */
3201
+ void
3202
+ dumpOneDomain (Archive * fout , TypeInfo * tinfo )
3203
+ {
3204
+ PQExpBuffer q = createPQExpBuffer ();
3205
+ PQExpBuffer delq = createPQExpBuffer ();
3206
+
3207
+ PGresult * res ;
3208
+ PQExpBuffer query = createPQExpBuffer ();
3209
+ int ntups ;
3210
+ const char * ((* deps )[]);
3211
+ int depIdx = 0 ;
3212
+
3213
+
3214
+ deps = malloc (sizeof (char * ) * 10 );
3215
+
3216
+ /* Fetch domain specific details */
3217
+ resetPQExpBuffer (query );
3218
+ appendPQExpBuffer (query , "SELECT typnotnull, "
3219
+ "format_type(typbasetype, typtypmod) as typdefn, "
3220
+ "typbasetype "
3221
+ "FROM pg_type "
3222
+ "WHERE typname = '%s'" ,
3223
+ tinfo -> typname );
3224
+
3225
+ res = PQexec (g_conn , query -> data );
3226
+ if (!res ||
3227
+ PQresultStatus (res ) != PGRES_TUPLES_OK )
3228
+ {
3229
+ write_msg (NULL , "query to obtain domain information failed: %s" , PQerrorMessage (g_conn ));
3230
+ exit_nicely ();
3231
+ }
3232
+
3233
+ /* Expecting a single result only */
3234
+ ntups = PQntuples (res );
3235
+ if (ntups != 1 )
3236
+ write_msg (NULL , "Domain %s non-existant." , fmtId (tinfo -> typname , force_quotes ));
3237
+
3238
+
3239
+ /* Drop the old copy */
3240
+ resetPQExpBuffer (delq );
3241
+ appendPQExpBuffer (delq , "DROP DOMAIN %s RESTRICT;\n" , fmtId (tinfo -> typname , force_quotes ));
3242
+
3243
+ resetPQExpBuffer (q );
3244
+ appendPQExpBuffer (q ,
3245
+ "CREATE DOMAIN %s AS %s" ,
3246
+ fmtId (tinfo -> typname , force_quotes ),
3247
+ PQgetvalue (res , 0 , PQfnumber (res , "typdefn" ))
3248
+ );
3249
+
3250
+ /* Depends on the base type */
3251
+ (* deps )[depIdx ++ ] = strdup (PQgetvalue (res , 0 , PQfnumber (res , "typbasetype" )));
3252
+
3253
+ if (PQgetvalue (res , 0 , PQfnumber (res , "typnotnull" ))[0 ] == 't' )
3254
+ appendPQExpBuffer (q , " NOT NULL" );
3255
+
3256
+ if (tinfo -> typdefault )
3257
+ {
3258
+ appendPQExpBuffer (q ,
3259
+ " DEFAULT %s" ,
3260
+ tinfo -> typdefault );
3261
+ }
3262
+
3263
+ appendPQExpBuffer (q , ";\n" );
3264
+
3265
+
3266
+ (* deps )[depIdx ++ ] = NULL ; /* End of List */
3267
+
3268
+ ArchiveEntry (fout , tinfo -> oid , tinfo -> typname , "DOMAIN" , deps ,
3269
+ q -> data , delq -> data , "" , tinfo -> usename , NULL , NULL );
3270
+
3271
+ /*** Dump Domain Comments ***/
3272
+ resetPQExpBuffer (q );
3273
+
3274
+ appendPQExpBuffer (q , "DOMAIN %s" , fmtId (tinfo -> typname , force_quotes ));
3275
+ dumpComment (fout , q -> data , tinfo -> oid , "pg_type" , 0 , NULL );
3276
+ }
3277
+
3191
3278
/*
3192
3279
* dumpTypes
3193
3280
* writes out to fout the queries to recreate all the user-defined types
@@ -3223,6 +3310,13 @@ dumpTypes(Archive *fout, FuncInfo *finfo, int numFuncs,
3223
3310
(strcmp (tinfo [i ].typinput , "array_in" ) == 0 ))
3224
3311
continue ;
3225
3312
3313
+ /* Dump out domains as we run across them */
3314
+ if (strcmp (tinfo [i ].typtype , "d" ) == 0 ) {
3315
+ dumpOneDomain (fout , & tinfo [i ]);
3316
+ continue ;
3317
+ }
3318
+
3319
+
3226
3320
deps = malloc (sizeof (char * ) * 10 );
3227
3321
depIdx = 0 ;
3228
3322
@@ -3318,6 +3412,8 @@ dumpTypes(Archive *fout, FuncInfo *finfo, int numFuncs,
3318
3412
ArchiveEntry (fout , tinfo [i ].oid , tinfo [i ].typname , "TYPE" , deps ,
3319
3413
q -> data , delq -> data , "" , tinfo [i ].usename , NULL , NULL );
3320
3414
3415
+
3416
+
3321
3417
/*** Dump Type Comments ***/
3322
3418
3323
3419
resetPQExpBuffer (q );
@@ -4294,6 +4390,36 @@ dumpTables(Archive *fout, TableInfo *tblinfo, int numTables,
4294
4390
tblinfo [i ].check_expr [k ]);
4295
4391
}
4296
4392
4393
+ /* Primary Key */
4394
+ if (tblinfo [i ].pkIndexOid != NULL )
4395
+ {
4396
+ PQExpBuffer consDef ;
4397
+
4398
+ /* Find the corresponding index */
4399
+ for (k = 0 ; k < numIndexes ; k ++ )
4400
+ {
4401
+ if (strcmp (indinfo [k ].indexreloid ,
4402
+ tblinfo [i ].pkIndexOid ) == 0 )
4403
+ break ;
4404
+ }
4405
+
4406
+ if (k >= numIndexes )
4407
+ {
4408
+ write_msg (NULL , "dumpTables(): failed sanity check, could not find index (%s) for primary key constraint\n" ,
4409
+ tblinfo [i ].pkIndexOid );
4410
+ exit_nicely ();
4411
+ }
4412
+
4413
+ consDef = getPKconstraint (& tblinfo [i ], & indinfo [k ]);
4414
+
4415
+ if ((actual_atts + tblinfo [i ].ncheck ) > 0 )
4416
+ appendPQExpBuffer (q , ",\n\t" );
4417
+
4418
+ appendPQExpBuffer (q , "%s" , consDef -> data );
4419
+
4420
+ destroyPQExpBuffer (consDef );
4421
+ }
4422
+
4297
4423
/*
4298
4424
* Primary Key: In versions of PostgreSQL prior to 7.2, we
4299
4425
* needed to include the primary key in the table definition.
0 commit comments