* Create the AS clause for a view or materialized view. The semicolon is * stripped because a materialized view must add a WITH NO DATA clause. * * This returns a new buffer which must be freed by the caller. */
| 17630 | * This returns a new buffer which must be freed by the caller. |
| 17631 | */ |
| 17632 | static PQExpBuffer |
| 17633 | createViewAsClause(Archive *fout, const TableInfo *tbinfo) |
| 17634 | { |
| 17635 | PQExpBuffer query = createPQExpBuffer(); |
| 17636 | PQExpBuffer result = createPQExpBuffer(); |
| 17637 | PGresult *res; |
| 17638 | int len; |
| 17639 | |
| 17640 | /* Fetch the view definition */ |
| 17641 | appendPQExpBuffer(query, |
| 17642 | "SELECT pg_catalog.pg_get_viewdef('%u'::pg_catalog.oid) AS viewdef", |
| 17643 | tbinfo->dobj.catId.oid); |
| 17644 | |
| 17645 | res = ExecuteSqlQuery(fout, query->data, PGRES_TUPLES_OK); |
| 17646 | |
| 17647 | if (PQntuples(res) != 1) |
| 17648 | { |
| 17649 | if (PQntuples(res) < 1) |
| 17650 | fatal("query to obtain definition of view \"%s\" returned no data", |
| 17651 | tbinfo->dobj.name); |
| 17652 | else |
| 17653 | fatal("query to obtain definition of view \"%s\" returned more than one definition", |
| 17654 | tbinfo->dobj.name); |
| 17655 | } |
| 17656 | |
| 17657 | len = PQgetlength(res, 0, 0); |
| 17658 | |
| 17659 | if (len == 0) |
| 17660 | fatal("definition of view \"%s\" appears to be empty (length zero)", |
| 17661 | tbinfo->dobj.name); |
| 17662 | |
| 17663 | /* Strip off the trailing semicolon so that other things may follow. */ |
| 17664 | Assert(PQgetvalue(res, 0, 0)[len - 1] == ';'); |
| 17665 | appendBinaryPQExpBuffer(result, PQgetvalue(res, 0, 0), len - 1); |
| 17666 | |
| 17667 | PQclear(res); |
| 17668 | destroyPQExpBuffer(query); |
| 17669 | |
| 17670 | return result; |
| 17671 | } |
| 17672 | |
| 17673 | /* |
| 17674 | * Create a dummy AS clause for a view. This is used when the real view |
no test coverage detected