* Import a foreign schema */
| 5236 | * Import a foreign schema |
| 5237 | */ |
| 5238 | static List * |
| 5239 | postgresImportForeignSchema(ImportForeignSchemaStmt *stmt, Oid serverOid) |
| 5240 | { |
| 5241 | List *commands = NIL; |
| 5242 | bool import_collate = true; |
| 5243 | bool import_default = false; |
| 5244 | bool import_generated = true; |
| 5245 | bool import_not_null = true; |
| 5246 | ForeignServer *server; |
| 5247 | UserMapping *mapping; |
| 5248 | PGconn *conn; |
| 5249 | StringInfoData buf; |
| 5250 | PGresult *volatile res = NULL; |
| 5251 | int numrows, |
| 5252 | i; |
| 5253 | ListCell *lc; |
| 5254 | |
| 5255 | /* Parse statement options */ |
| 5256 | foreach(lc, stmt->options) |
| 5257 | { |
| 5258 | DefElem *def = (DefElem *) lfirst(lc); |
| 5259 | |
| 5260 | if (strcmp(def->defname, "import_collate") == 0) |
| 5261 | import_collate = defGetBoolean(def); |
| 5262 | else if (strcmp(def->defname, "import_default") == 0) |
| 5263 | import_default = defGetBoolean(def); |
| 5264 | else if (strcmp(def->defname, "import_generated") == 0) |
| 5265 | import_generated = defGetBoolean(def); |
| 5266 | else if (strcmp(def->defname, "import_not_null") == 0) |
| 5267 | import_not_null = defGetBoolean(def); |
| 5268 | else |
| 5269 | ereport(ERROR, |
| 5270 | (errcode(ERRCODE_FDW_INVALID_OPTION_NAME), |
| 5271 | errmsg("invalid option \"%s\"", def->defname))); |
| 5272 | } |
| 5273 | |
| 5274 | /* |
| 5275 | * Get connection to the foreign server. Connection manager will |
| 5276 | * establish new connection if necessary. |
| 5277 | */ |
| 5278 | server = GetForeignServer(serverOid); |
| 5279 | mapping = GetUserMapping(GetUserId(), server->serverid); |
| 5280 | conn = GetConnection(mapping, false, NULL); |
| 5281 | |
| 5282 | /* Don't attempt to import collation if remote server hasn't got it */ |
| 5283 | if (PQserverVersion(conn) < 90100) |
| 5284 | import_collate = false; |
| 5285 | |
| 5286 | /* Create workspace for strings */ |
| 5287 | initStringInfo(&buf); |
| 5288 | |
| 5289 | /* In what follows, do not risk leaking any PGresults. */ |
| 5290 | PG_TRY(); |
| 5291 | { |
| 5292 | /* Check that the schema really exists */ |
| 5293 | appendStringInfoString(&buf, "SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname = "); |
| 5294 | deparseStringLiteral(&buf, stmt->remote_schema); |
| 5295 |
nothing calls this directly
no test coverage detected