* Export data out of GPDB. */
| 160 | * Export data out of GPDB. |
| 161 | */ |
| 162 | Datum |
| 163 | demoprot_export(PG_FUNCTION_ARGS) |
| 164 | { |
| 165 | extprotocol_t *myData; |
| 166 | char *data; |
| 167 | int datlen; |
| 168 | size_t wrote = 0; |
| 169 | |
| 170 | /* Must be called via the external table format manager */ |
| 171 | if (!CALLED_AS_EXTPROTOCOL(fcinfo)) |
| 172 | elog(ERROR, "extprotocol_export: not called by external protocol manager"); |
| 173 | |
| 174 | /* Get our internal description of the protocol */ |
| 175 | myData = (extprotocol_t *) EXTPROTOCOL_GET_USER_CTX(fcinfo); |
| 176 | |
| 177 | if(EXTPROTOCOL_IS_LAST_CALL(fcinfo)) |
| 178 | { |
| 179 | /* we're done sending data. close our connection */ |
| 180 | if(myData && myData->file) |
| 181 | if(fclose(myData->file)) |
| 182 | ereport(ERROR, |
| 183 | (errcode_for_file_access(), |
| 184 | errmsg("could not close file \"%s\": %m", |
| 185 | myData->filename))); |
| 186 | |
| 187 | PG_RETURN_INT32(0); |
| 188 | } |
| 189 | |
| 190 | if (myData == NULL) |
| 191 | { |
| 192 | /* first call. do any desired init */ |
| 193 | |
| 194 | const char *p_name = "demoprot"; |
| 195 | DemoUri *parsed_url; |
| 196 | char *url = EXTPROTOCOL_GET_URL(fcinfo); |
| 197 | |
| 198 | myData = palloc(sizeof(extprotocol_t)); |
| 199 | |
| 200 | myData->url = pstrdup(url); |
| 201 | parsed_url = ParseDemoUri(myData->url); |
| 202 | myData->filename = pstrdup(parsed_url->path); |
| 203 | |
| 204 | if(strcasecmp(parsed_url->protocol, p_name) != 0) |
| 205 | elog(ERROR, "internal error: demoprot called with a different protocol (%s)", |
| 206 | parsed_url->protocol); |
| 207 | |
| 208 | FreeDemoUri(parsed_url); |
| 209 | |
| 210 | /* open the destination file (or connect to remote server in other cases) */ |
| 211 | myData->file = fopen(myData->filename, "a"); |
| 212 | |
| 213 | if (myData->file == NULL) |
| 214 | ereport(ERROR, |
| 215 | (errcode_for_file_access(), |
| 216 | errmsg("demoprot_export: could not open file \"%s\" for writing: %m", |
| 217 | myData->filename))); |
| 218 | |
| 219 | EXTPROTOCOL_SET_USER_CTX(fcinfo, myData); |
nothing calls this directly
no test coverage detected