aboutsummaryrefslogtreecommitdiff
path: root/src/bin/pg_dump/common.c
blob: 96ea7f7295145a81371bb45720c83fac82014815 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*-------------------------------------------------------------------------
 *
 * common.c--
 *    common routines between pg_dump and pg4_dump
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *    $Header: /cvsroot/pgsql/src/bin/pg_dump/common.c,v 1.1.1.1 1996/07/09 06:22:14 scrappy Exp $
 *
 *-------------------------------------------------------------------------
 */


#include <stdlib.h>
#include <stdio.h>
#include <sys/param.h>	/* for MAXHOSTNAMELEN on most */
#ifdef PORTNAME_sparc_solaris
#include <netdb.h>	/* for MAXHOSTNAMELEN on some */
#endif

#include "postgres.h"
#include "libpq-fe.h"

#include "pg_dump.h"

/* dupstr : copies a string, while allocating space for it. 
   the CALLER is responsible for freeing the space
   returns NULL if the argument is NULL*/
char* 
dupstr(char *s)
{
  char* result;

  if (s == NULL)
    return NULL;

  result = (char*)malloc(strlen(s)+1);
  strcpy(result, s);
  return result;
}


/*
 * findTypeByOid 
 *    given an oid of a type, return its typename
 *
 * if oid is "0", return "opaque" -- this is a special case
 *
 * NOTE:  should hash this, but just do linear search for now
 */

char*
findTypeByOid(TypeInfo* tinfo, int numTypes, char* oid)
{
    int i;

    if (strcmp(oid, "0") == 0) return g_opaque_type;

    for (i=0;i<numTypes;i++) {
	if (strcmp(tinfo[i].oid, oid) == 0)
	    return tinfo[i].typname;
    }

    /* should never get here */
    fprintf(stderr,"failed sanity check,  type with oid %s was not found\n",
	    oid);
    exit(2);
}

/*
 * findOprByOid
 *    given the oid of an operator, return the name of the operator
 *
 *
 * NOTE:  should hash this, but just do linear search for now
 * 
 */
char*
findOprByOid(OprInfo *oprinfo, int numOprs, char *oid)
{
    int i;
    for (i=0;i<numOprs;i++) {
	if (strcmp(oprinfo[i].oid, oid) == 0)
	    return oprinfo[i].oprname;
    }

    /* should never get here */
    fprintf(stderr,"failed sanity check,  opr with oid %s was not found\n",
	    oid);
    exit(2);
}


/*
 * findParentsByOid --
 *    given the oid of a class, return the names of its parent classes
 * and assign the number of parents to the last argument.
 *
 *
 * returns NULL if none
 */

char** 
findParentsByOid(TableInfo* tblinfo, int numTables,
		 InhInfo* inhinfo, int numInherits, char *oid,
		 int *numParentsPtr)
{
    int i,j;
    int parentInd;
    char** result;
    int numParents;

    numParents = 0;
    for (i=0;i<numInherits;i++) {
	if ( strcmp(inhinfo[i].inhrel, oid) == 0) {
	    numParents++;
	}
    }

    *numParentsPtr = numParents;

    if (numParents > 0) {
	result = (char**)malloc(sizeof(char*) * numParents);
	j = 0;
	for (i=0;i<numInherits;i++) {
	    if ( strcmp(inhinfo[i].inhrel, oid) == 0) {
		parentInd = findTableByOid(tblinfo, numTables, 
					   inhinfo[i].inhparent);
		result[j++] = tblinfo[parentInd].relname;
	    }
	}
	return result;
    }
    else 
	return NULL;
}

/*
 * parseArgTypes
 *    parse a string of eight numbers delimited by spaces
 * into a character array
 */

void 
parseArgTypes(char **argtypes, char* str)
{
    int j, argNum;
    char temp[100];
    char s;

    argNum = 0;
    j = 0;
    while ( (s = *str) != '\0') {
	if (s == ' ') {
	    temp[j] = '\0';
	    argtypes[argNum] = dupstr(temp);
	    argNum++;
	    j = 0;
	} else {
	    temp[j] = s;
	    j++;
	}
	str++;
    }
    if (j != 0)  {
	temp[j] = '\0';
        argtypes[argNum] = dupstr(temp);
    }
    
}


/*
 * strInArray:
 *    takes in a string and a string array and the number of elements in the 
 * string array.  
 *    returns the index if the string is somewhere in the array, -1 otherwise
 *
 */

int 
strInArray(char* pattern, char** arr, int arr_size)
{
    int i;
    for (i=0;i<arr_size;i++) {
	if (strcmp(pattern, arr[i]) == 0) 
	    return i;
    }
    return -1;
}

/*
 * dumpSchema:
 *    we have a valid connection, we are now going to dump the schema
 * into the file
 *
 */

TableInfo *
dumpSchema(FILE *fout, int *numTablesPtr)
{
    int numTypes;
    int numFuncs;
    int numTables;
    int numInherits;
    int numIndices;
    int numAggregates;
    int numOperators;
    TypeInfo *tinfo;
    FuncInfo *finfo;
    AggInfo *agginfo;
    TableInfo *tblinfo;
    InhInfo *inhinfo;
    IndInfo *indinfo;
    OprInfo *oprinfo;

if (g_verbose) fprintf(stderr,"%s reading user-defined types %s\n",
		       g_comment_start, g_comment_end);
    tinfo = getTypes(&numTypes);

if (g_verbose) fprintf(stderr,"%s reading user-defined functions %s\n",
		       g_comment_start, g_comment_end);
    finfo = getFuncs(&numFuncs);

if (g_verbose) fprintf(stderr,"%s reading user-defined aggregates %s\n",
		       g_comment_start, g_comment_end);
    agginfo = getAggregates(&numAggregates);

if (g_verbose) fprintf(stderr,"%s reading user-defined operators %s\n",
		       g_comment_start, g_comment_end);
    oprinfo = getOperators(&numOperators);

if (g_verbose) fprintf(stderr,"%s reading user-defined tables %s\n",
		       g_comment_start, g_comment_end);
    tblinfo = getTables(&numTables);

if (g_verbose) fprintf(stderr,"%s reading table inheritance information %s\n",
		       g_comment_start, g_comment_end);
    inhinfo = getInherits(&numInherits);

if (g_verbose) fprintf(stderr, "%s finding the attribute names and types for each table %s\n",
		       g_comment_start, g_comment_end);
    getTableAttrs(tblinfo, numTables);

if (g_verbose) fprintf(stderr, "%s flagging inherited attributes in subtables %s\n",
		       g_comment_start, g_comment_end);
    flagInhAttrs(tblinfo, numTables, inhinfo, numInherits);

if (g_verbose) fprintf(stderr,"%s reading indices information %s\n",
		       g_comment_start, g_comment_end);
    indinfo = getIndices(&numIndices);

if (g_verbose) fprintf(stderr,"%s dumping out user-defined types %s\n",
		       g_comment_start, g_comment_end);
    dumpTypes(fout, finfo, numFuncs, tinfo, numTypes);

if (g_verbose) fprintf(stderr,"%s dumping out tables %s\n",
		       g_comment_start, g_comment_end);
    dumpTables(fout, tblinfo, numTables, inhinfo, numInherits,
	       tinfo, numTypes);

if (g_verbose) fprintf(stderr,"%s dumping out user-defined functions %s\n",
		       g_comment_start, g_comment_end);
    dumpFuncs(fout, finfo, numFuncs, tinfo, numTypes);

if (g_verbose) fprintf(stderr,"%s dumping out user-defined functions %s\n",
		       g_comment_start, g_comment_end);
    dumpAggs(fout, agginfo, numAggregates, tinfo, numTypes);

if (g_verbose) fprintf(stderr,"%s dumping out user-defined operators %s\n",
		       g_comment_start, g_comment_end);
    dumpOprs(fout, oprinfo, numOperators, tinfo, numTypes);

if (g_verbose) fprintf(stderr,"%s dumping out indices %s\n",
		       g_comment_start, g_comment_end);
    dumpIndices(fout, indinfo, numIndices, tblinfo, numTables);

    *numTablesPtr = numTables;
    return tblinfo;
}


/* flagInhAttrs -
 *   for each table in tblinfo, flag its inherited attributes
 * so when we dump the table out, we don't dump out the inherited attributes
 *   
 * initializes the parentRels field of each table
 *
 * modifies tblinfo
 *
 */
void
flagInhAttrs(TableInfo* tblinfo, int numTables,
	     InhInfo* inhinfo, int numInherits)
{
    int i,j,k;
    int parentInd;

    /* we go backwards because the tables in tblinfo are in OID
       order, meaning the subtables are after the parent tables
       we flag inherited attributes from child tables first */
    for (i = numTables-1; i >= 0; i--) {
	tblinfo[i].parentRels = findParentsByOid(tblinfo, numTables,
						inhinfo, numInherits,
						tblinfo[i].oid,
						&tblinfo[i].numParents);
	for (k=0;k<tblinfo[i].numParents;k++) {
	    parentInd = findTableByName(tblinfo, numTables, 
					tblinfo[i].parentRels[k]);
	    for (j=0;j<tblinfo[i].numatts;j++) {
		if (strInArray(tblinfo[i].attnames[j],
			       tblinfo[parentInd].attnames,
			       tblinfo[parentInd].numatts) != -1) {
		    tblinfo[i].inhAttrs[j] = 1;
		}
	    }
	}
    }
}


/*
 * findTableByName
 *    finds the index (in tblinfo) of the table with the given relname
 *  returns -1 if not found
 *
 * NOTE:  should hash this, but just do linear search for now
 */

int
findTableByName(TableInfo* tblinfo, int numTables, char* relname)
{
    int i;
    for (i=0;i<numTables;i++) {
	if  (strcmp(tblinfo[i].relname, relname) == 0)
	    return i;
    }
    return -1;
}

/*
 * findTableByOid
 *    finds the index (in tblinfo) of the table with the given oid
 *  returns -1 if not found
 *
 * NOTE:  should hash this, but just do linear search for now
 */

int
findTableByOid(TableInfo* tblinfo, int numTables, char* oid)
{
    int i;
    for (i=0;i<numTables;i++) {
	if  (strcmp(tblinfo[i].oid, oid) == 0)
	    return i;
    }
    return -1;
}


/*
 * findFuncByName
 *    finds the index (in finfo) of the function with the given name
 *  returns -1 if not found
 *
 * NOTE:  should hash this, but just do linear search for now
 */

int
findFuncByName(FuncInfo* finfo, int numFuncs, char* name)
{
    int i;
    for (i=0;i<numFuncs;i++) {
	if  (strcmp(finfo[i].proname, name) == 0)
	    return i;
    }
    return -1;
}

/*
 * isArchiveName
 *
 *   returns true if the relation name is an archive name, false otherwise
 */ 
int
isArchiveName(char* relname)
{
    return (strlen(relname) > 1 && relname[1] == ',');
}