* Construct a "CREATE OR REPLACE ..." command that describes the specified * database object. If successful, the result is stored in buf. */
| 5105 | * database object. If successful, the result is stored in buf. |
| 5106 | */ |
| 5107 | static bool |
| 5108 | get_create_object_cmd(EditableObjectType obj_type, Oid oid, |
| 5109 | PQExpBuffer buf) |
| 5110 | { |
| 5111 | bool result = true; |
| 5112 | PQExpBuffer query = createPQExpBuffer(); |
| 5113 | PGresult *res; |
| 5114 | |
| 5115 | switch (obj_type) |
| 5116 | { |
| 5117 | case EditableFunction: |
| 5118 | printfPQExpBuffer(query, |
| 5119 | "SELECT pg_catalog.pg_get_functiondef(%u)", |
| 5120 | oid); |
| 5121 | break; |
| 5122 | |
| 5123 | case EditableView: |
| 5124 | |
| 5125 | /* |
| 5126 | * pg_get_viewdef() just prints the query, so we must prepend |
| 5127 | * CREATE for ourselves. We must fully qualify the view name to |
| 5128 | * ensure the right view gets replaced. Also, check relation kind |
| 5129 | * to be sure it's a view. |
| 5130 | * |
| 5131 | * Starting with 9.2, views may have reloptions (security_barrier) |
| 5132 | * and from 9.4 onwards they may also have WITH [LOCAL|CASCADED] |
| 5133 | * CHECK OPTION. These are not part of the view definition |
| 5134 | * returned by pg_get_viewdef() and so need to be retrieved |
| 5135 | * separately. Materialized views (introduced in 9.3) may have |
| 5136 | * arbitrary storage parameter reloptions. |
| 5137 | */ |
| 5138 | if (pset.sversion >= 90400) |
| 5139 | { |
| 5140 | printfPQExpBuffer(query, |
| 5141 | "SELECT nspname, relname, relkind, " |
| 5142 | "pg_catalog.pg_get_viewdef(c.oid, true), " |
| 5143 | "pg_catalog.array_remove(pg_catalog.array_remove(c.reloptions,'check_option=local'),'check_option=cascaded') AS reloptions, " |
| 5144 | "CASE WHEN 'check_option=local' = ANY (c.reloptions) THEN 'LOCAL'::text " |
| 5145 | "WHEN 'check_option=cascaded' = ANY (c.reloptions) THEN 'CASCADED'::text ELSE NULL END AS checkoption " |
| 5146 | "FROM pg_catalog.pg_class c " |
| 5147 | "LEFT JOIN pg_catalog.pg_namespace n " |
| 5148 | "ON c.relnamespace = n.oid WHERE c.oid = %u", |
| 5149 | oid); |
| 5150 | } |
| 5151 | else if (pset.sversion >= 90200) |
| 5152 | { |
| 5153 | printfPQExpBuffer(query, |
| 5154 | "SELECT nspname, relname, relkind, " |
| 5155 | "pg_catalog.pg_get_viewdef(c.oid, true), " |
| 5156 | "c.reloptions AS reloptions, " |
| 5157 | "NULL AS checkoption " |
| 5158 | "FROM pg_catalog.pg_class c " |
| 5159 | "LEFT JOIN pg_catalog.pg_namespace n " |
| 5160 | "ON c.relnamespace = n.oid WHERE c.oid = %u", |
| 5161 | oid); |
| 5162 | } |
| 5163 | else |
| 5164 | { |
no test coverage detected