* Import data into GPDB. */
| 71 | * Import data into GPDB. |
| 72 | */ |
| 73 | Datum |
| 74 | demoprot_import(PG_FUNCTION_ARGS) |
| 75 | { |
| 76 | extprotocol_t *myData; |
| 77 | char *data; |
| 78 | int datlen; |
| 79 | size_t nread = 0; |
| 80 | |
| 81 | /* Must be called via the external table format manager */ |
| 82 | if (!CALLED_AS_EXTPROTOCOL(fcinfo)) |
| 83 | elog(ERROR, "extprotocol_import: not called by external protocol manager"); |
| 84 | |
| 85 | /* Get our internal description of the protocol */ |
| 86 | myData = (extprotocol_t *) EXTPROTOCOL_GET_USER_CTX(fcinfo); |
| 87 | |
| 88 | if(EXTPROTOCOL_IS_LAST_CALL(fcinfo)) |
| 89 | { |
| 90 | /* we're done receiving data. close our connection */ |
| 91 | if(myData && myData->file) |
| 92 | if(fclose(myData->file)) |
| 93 | ereport(ERROR, |
| 94 | (errcode_for_file_access(), |
| 95 | errmsg("could not close file \"%s\": %m", |
| 96 | myData->filename))); |
| 97 | |
| 98 | PG_RETURN_INT32(0); |
| 99 | } |
| 100 | |
| 101 | if (myData == NULL) |
| 102 | { |
| 103 | /* first call. do any desired init */ |
| 104 | |
| 105 | const char *p_name = "demoprot"; |
| 106 | DemoUri *parsed_url; |
| 107 | char *url = EXTPROTOCOL_GET_URL(fcinfo); |
| 108 | |
| 109 | myData = palloc(sizeof(extprotocol_t)); |
| 110 | |
| 111 | myData->url = pstrdup(url); |
| 112 | parsed_url = ParseDemoUri(myData->url); |
| 113 | myData->filename = pstrdup(parsed_url->path); |
| 114 | |
| 115 | if(strcasecmp(parsed_url->protocol, p_name) != 0) |
| 116 | ereport(ERROR, |
| 117 | (errcode(ERRCODE_INVALID_PARAMETER_VALUE), |
| 118 | errmsg("internal error: demoprot called with a different protocol (%s)", |
| 119 | parsed_url->protocol))); |
| 120 | |
| 121 | /* An example of checking options */ |
| 122 | check_ext_options(fcinfo); |
| 123 | |
| 124 | FreeDemoUri(parsed_url); |
| 125 | |
| 126 | /* open the destination file (or connect to remote server in other cases) */ |
| 127 | myData->file = fopen(myData->filename, "r"); |
| 128 | |
| 129 | if (myData->file == NULL) |
| 130 | ereport(ERROR, |
nothing calls this directly
no test coverage detected