|
21 | 21 | *
|
22 | 22 | *
|
23 | 23 | * IDENTIFICATION
|
24 |
| - * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.88 1998/10/02 16:43:40 thomas Exp $ |
| 24 | + * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.89 1998/10/06 03:08:59 momjian Exp $ |
25 | 25 | *
|
26 | 26 | * Modifications - 6/10/96 - dave@bensoft.com - version 1.13.dhb
|
27 | 27 | *
|
@@ -1383,7 +1383,7 @@ getFuncs(int *numFuncs)
|
1383 | 1383 | TableInfo *
|
1384 | 1384 | getTables(int *numTables, FuncInfo *finfo, int numFuncs)
|
1385 | 1385 | {
|
1386 |
| - PGresult *res; |
| 1386 | + PGresult *res, *viewres; |
1387 | 1387 | int ntups;
|
1388 | 1388 | int i;
|
1389 | 1389 | char query[MAXQUERYLEN];
|
@@ -1414,6 +1414,8 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
|
1414 | 1414 | }
|
1415 | 1415 | PQclear(res);
|
1416 | 1416 |
|
| 1417 | +/* NOTE, when outer joins are here, change this query to get the |
| 1418 | +view definition all in one go. */ |
1417 | 1419 | sprintf(query,
|
1418 | 1420 | "SELECT pg_class.oid, relname, relkind, relacl, usename, "
|
1419 | 1421 | "relchecks, reltriggers "
|
@@ -1454,6 +1456,39 @@ getTables(int *numTables, FuncInfo *finfo, int numFuncs)
|
1454 | 1456 | tblinfo[i].ncheck = atoi(PQgetvalue(res, i, i_relchecks));
|
1455 | 1457 | tblinfo[i].ntrig = atoi(PQgetvalue(res, i, i_reltriggers));
|
1456 | 1458 |
|
| 1459 | + /* NOTE that at such time as left outer joins become avaliable, |
| 1460 | + then this will no longer be needed, and can be done in the |
| 1461 | + above query. */ |
| 1462 | + |
| 1463 | + sprintf(query, |
| 1464 | + "select definition from pg_views where viewname = '%s';", |
| 1465 | + tblinfo[i].relname); |
| 1466 | + |
| 1467 | + viewres = PQexec(g_conn, query); |
| 1468 | + if (!viewres || |
| 1469 | + PQresultStatus(res) != PGRES_TUPLES_OK) |
| 1470 | + { |
| 1471 | + fprintf(stderr, "getTables(): SELECT for views failed\n"); |
| 1472 | + exit_nicely(g_conn); |
| 1473 | + } |
| 1474 | + |
| 1475 | + /* NOTE: Tryed to use isViewRule here, but it does it's own |
| 1476 | + BEGIN and END so messed things up. |
| 1477 | + This also needs redone should we ever get outer joins. |
| 1478 | + */ |
| 1479 | + if ( PQntuples(viewres) > 0 ) |
| 1480 | + { |
| 1481 | + if ( PQntuples(viewres) != 1 ) |
| 1482 | + { |
| 1483 | + fprintf(stderr, "getTables(): failed to get view definition.\n"); |
| 1484 | + exit_nicely(g_conn); |
| 1485 | + } |
| 1486 | + |
| 1487 | + tblinfo[i].viewdef = strdup(PQgetvalue(viewres, 0, 0)); |
| 1488 | + } |
| 1489 | + |
| 1490 | + PQclear(viewres); |
| 1491 | + |
1457 | 1492 | /* Get CHECK constraints */
|
1458 | 1493 | if (tblinfo[i].ncheck > 0)
|
1459 | 1494 | {
|
@@ -2468,95 +2503,102 @@ dumpTables(FILE *fout, TableInfo *tblinfo, int numTables,
|
2468 | 2503 | if (!tablename || (!strcmp(tblinfo[i].relname, tablename)))
|
2469 | 2504 | {
|
2470 | 2505 |
|
2471 |
| - /* Skip VIEW relations */ |
| 2506 | + /* Dump VIEW relations also !-) */ |
2472 | 2507 | if (isViewRule(tblinfo[i].relname))
|
2473 |
| - continue; |
2474 |
| - |
2475 |
| - parentRels = tblinfo[i].parentRels; |
2476 |
| - numParents = tblinfo[i].numParents; |
2477 |
| - |
2478 |
| - becomeUser(fout, tblinfo[i].usename); |
| 2508 | + { |
| 2509 | + becomeUser(fout, tblinfo[i].usename); |
2479 | 2510 |
|
2480 |
| - sprintf(q, "CREATE TABLE %s (", fmtId(tblinfo[i].relname)); |
2481 |
| - actual_atts = 0; |
2482 |
| - for (j = 0; j < tblinfo[i].numatts; j++) |
| 2511 | + sprintf(q, "CREATE VIEW %s AS %s\n", |
| 2512 | + fmtId(tblinfo[i].relname), |
| 2513 | + tblinfo[i].viewdef); |
| 2514 | + } |
| 2515 | + else |
2483 | 2516 | {
|
2484 |
| - if (tblinfo[i].inhAttrs[j] == 0) |
2485 |
| - { |
| 2517 | + parentRels = tblinfo[i].parentRels; |
| 2518 | + numParents = tblinfo[i].numParents; |
2486 | 2519 |
|
2487 |
| - /* Show lengths on bpchar and varchar */ |
2488 |
| - if (!strcmp(tblinfo[i].typnames[j], "bpchar")) |
2489 |
| - { |
2490 |
| - sprintf(q, "%s%s%s char", |
2491 |
| - q, |
2492 |
| - (actual_atts > 0) ? ", " : "", |
2493 |
| - fmtId(tblinfo[i].attnames[j])); |
| 2520 | + becomeUser(fout, tblinfo[i].usename); |
2494 | 2521 |
|
2495 |
| - sprintf(q, "%s(%d)", |
2496 |
| - q, |
2497 |
| - tblinfo[i].atttypmod[j] - VARHDRSZ); |
2498 |
| - actual_atts++; |
2499 |
| - } |
2500 |
| - else if (!strcmp(tblinfo[i].typnames[j], "varchar")) |
| 2522 | + sprintf(q, "CREATE TABLE %s (", fmtId(tblinfo[i].relname)); |
| 2523 | + actual_atts = 0; |
| 2524 | + for (j = 0; j < tblinfo[i].numatts; j++) |
| 2525 | + { |
| 2526 | + if (tblinfo[i].inhAttrs[j] == 0) |
2501 | 2527 | {
|
2502 |
| - sprintf(q, "%s%s%s %s", |
2503 |
| - q, |
2504 |
| - (actual_atts > 0) ? ", " : "", |
2505 |
| - fmtId(tblinfo[i].attnames[j]), |
2506 |
| - tblinfo[i].typnames[j]); |
2507 | 2528 |
|
2508 |
| - sprintf(q, "%s(%d)", |
2509 |
| - q, |
2510 |
| - tblinfo[i].atttypmod[j] - VARHDRSZ); |
2511 |
| - actual_atts++; |
2512 |
| - } |
2513 |
| - else |
2514 |
| - { |
2515 |
| - strcpy(id1, fmtId(tblinfo[i].attnames[j])); |
2516 |
| - strcpy(id2, fmtId(tblinfo[i].typnames[j])); |
2517 |
| - sprintf(q, "%s%s%s %s", |
2518 |
| - q, |
2519 |
| - (actual_atts > 0) ? ", " : "", |
2520 |
| - id1, |
2521 |
| - id2); |
2522 |
| - actual_atts++; |
| 2529 | + /* Show lengths on bpchar and varchar */ |
| 2530 | + if (!strcmp(tblinfo[i].typnames[j], "bpchar")) |
| 2531 | + { |
| 2532 | + sprintf(q, "%s%s%s char", |
| 2533 | + q, |
| 2534 | + (actual_atts > 0) ? ", " : "", |
| 2535 | + fmtId(tblinfo[i].attnames[j])); |
| 2536 | + |
| 2537 | + sprintf(q, "%s(%d)", |
| 2538 | + q, |
| 2539 | + tblinfo[i].atttypmod[j] - VARHDRSZ); |
| 2540 | + actual_atts++; |
| 2541 | + } |
| 2542 | + else if (!strcmp(tblinfo[i].typnames[j], "varchar")) |
| 2543 | + { |
| 2544 | + sprintf(q, "%s%s%s %s", |
| 2545 | + q, |
| 2546 | + (actual_atts > 0) ? ", " : "", |
| 2547 | + fmtId(tblinfo[i].attnames[j]), |
| 2548 | + tblinfo[i].typnames[j]); |
| 2549 | + |
| 2550 | + sprintf(q, "%s(%d)", |
| 2551 | + q, |
| 2552 | + tblinfo[i].atttypmod[j] - VARHDRSZ); |
| 2553 | + actual_atts++; |
| 2554 | + } |
| 2555 | + else |
| 2556 | + { |
| 2557 | + strcpy(id1, fmtId(tblinfo[i].attnames[j])); |
| 2558 | + strcpy(id2, fmtId(tblinfo[i].typnames[j])); |
| 2559 | + sprintf(q, "%s%s%s %s", |
| 2560 | + q, |
| 2561 | + (actual_atts > 0) ? ", " : "", |
| 2562 | + id1, |
| 2563 | + id2); |
| 2564 | + actual_atts++; |
| 2565 | + } |
| 2566 | + if (tblinfo[i].adef_expr[j] != NULL) |
| 2567 | + sprintf(q, "%s DEFAULT %s", q, tblinfo[i].adef_expr[j]); |
| 2568 | + if (tblinfo[i].notnull[j]) |
| 2569 | + sprintf(q, "%s NOT NULL", q); |
2523 | 2570 | }
|
2524 |
| - if (tblinfo[i].adef_expr[j] != NULL) |
2525 |
| - sprintf(q, "%s DEFAULT %s", q, tblinfo[i].adef_expr[j]); |
2526 |
| - if (tblinfo[i].notnull[j]) |
2527 |
| - sprintf(q, "%s NOT NULL", q); |
2528 | 2571 | }
|
2529 |
| - } |
2530 | 2572 |
|
2531 |
| - /* put the CONSTRAINTS inside the table def */ |
2532 |
| - for (k = 0; k < tblinfo[i].ncheck; k++) |
2533 |
| - { |
2534 |
| - sprintf(q, "%s%s %s", |
2535 |
| - q, |
2536 |
| - (actual_atts + k > 0) ? ", " : "", |
2537 |
| - tblinfo[i].check_expr[k]); |
2538 |
| - } |
2539 |
| - |
2540 |
| - strcat(q, ")"); |
2541 |
| - |
2542 |
| - if (numParents > 0) |
2543 |
| - { |
2544 |
| - sprintf(q, "%s inherits ( ", q); |
2545 |
| - for (k = 0; k < numParents; k++) |
| 2573 | + /* put the CONSTRAINTS inside the table def */ |
| 2574 | + for (k = 0; k < tblinfo[i].ncheck; k++) |
2546 | 2575 | {
|
2547 |
| - sprintf(q, "%s%s%s", |
| 2576 | + sprintf(q, "%s%s %s", |
2548 | 2577 | q,
|
2549 |
| - (k > 0) ? ", " : "", |
2550 |
| - fmtId(parentRels[k])); |
| 2578 | + (actual_atts + k > 0) ? ", " : "", |
| 2579 | + tblinfo[i].check_expr[k]); |
2551 | 2580 | }
|
| 2581 | + |
2552 | 2582 | strcat(q, ")");
|
2553 |
| - } |
2554 | 2583 |
|
2555 |
| - strcat(q, ";\n"); |
| 2584 | + if (numParents > 0) |
| 2585 | + { |
| 2586 | + sprintf(q, "%s inherits ( ", q); |
| 2587 | + for (k = 0; k < numParents; k++) |
| 2588 | + { |
| 2589 | + sprintf(q, "%s%s%s", |
| 2590 | + q, |
| 2591 | + (k > 0) ? ", " : "", |
| 2592 | + fmtId(parentRels[k])); |
| 2593 | + } |
| 2594 | + strcat(q, ")"); |
| 2595 | + } |
| 2596 | + strcat(q, ";\n"); |
| 2597 | + } /* end of if view ... else .... */ |
| 2598 | + |
2556 | 2599 | fputs(q, fout);
|
2557 | 2600 | if (acls)
|
2558 | 2601 | dumpACL(fout, tblinfo[i]);
|
2559 |
| - |
2560 | 2602 | }
|
2561 | 2603 | }
|
2562 | 2604 | }
|
|
0 commit comments