1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
|
/*-------------------------------------------------------------------------
*
* ogr_fdw_info.c
* Commandline utility to read an OGR layer and output a
* SQL "create table" statement.
*
* Copyright (c) 2014-2015, Paul Ramsey <pramsey@cleverelephant.ca>
*
*-------------------------------------------------------------------------
*/
/* postgresql */
#include "pg_config_manual.h"
/* getopt */
#include <unistd.h>
/*
* OGR library API
*/
#include "ogr_fdw_gdal.h"
#include "ogr_fdw_common.h"
static void usage();
static OGRErr ogrListLayers(const char* source);
static OGRErr ogrFindLayer(const char* source, int layerno, const char** layer);
static OGRErr ogrGenerateSQL(const char* server, const char* layer, const char* table, const char* source, const char* options);
static int reserved_word(const char* pgcolumn);
static char *
ogr_fdw_strupr(char* str)
{
int i;
for (i = 0; i < strlen(str); i++) {
str[i] = toupper(str[i]);
}
return str;
}
static char *
strip_spaces(char* str)
{
unsigned char *cur = (unsigned char *)str;
unsigned char *head = cur;
while (*head != '\0') {
if (*head != ' ') {
*cur = *head;
++cur;
}
++head;
}
*cur = '\0';
return str;
}
/* Define this no-op here, so that code */
/* in the ogr_fdw_common module works */
const char* quote_identifier(const char* ident);
static char identifier[NAMEDATALEN+3];
const char*
quote_identifier(const char* ident)
{
int len = (int)MIN(strlen(ident), NAMEDATALEN - 1);
if (reserved_word(ident))
{
sprintf(identifier,"\"%*s\"", len, ident);
}
else
{
sprintf(identifier,"%*s", len, ident);
}
return identifier;
}
static char config_options[STR_MAX_LEN] = {0};
static void
formats()
{
int i;
GDALAllRegister();
printf("Supported Formats:\n");
for (i = 0; i < GDALGetDriverCount(); i++)
{
GDALDriverH ogr_dr = GDALGetDriver(i);
int vector = FALSE;
int createable = TRUE;
const char* tmpl;
#if GDAL_VERSION_MAJOR >= 2
char** papszMD = GDALGetMetadata(ogr_dr, NULL);
vector = CSLFetchBoolean(papszMD, GDAL_DCAP_VECTOR, FALSE);
createable = CSLFetchBoolean(papszMD, GDAL_DCAP_CREATE, FALSE);
#else
createable = GDALDatasetTestCapability(ogr_dr, ODrCCreateDataSource);
#endif
/* Skip raster data sources */
if (! vector)
{
continue;
}
/* Report sources w/ create capability as r/w */
if (createable)
{
tmpl = " -> \"%s\" (read/write)\n";
}
else
{
tmpl = " -> \"%s\" (readonly)\n";
}
printf(tmpl, GDALGetDriverShortName(ogr_dr));
}
exit(0);
}
static void
usage()
{
printf(
"usage: ogr_fdw_info -s <ogr datasource> -l <ogr layer name> -i <ogr layer index (numeric)> -t <output table name> -n <output server name> -o <config options>\n"
" ogr_fdw_info -s <ogr datasource>\n"
"usage: ogr_fdw_info -f\n"
" Show what input file formats are supported.\n"
"\n");
printf(
"note (1): You can specify either -l (layer name) or -i (layer index)\n"
" if you specify both -l will be used\n"
"note (2): config options are specified as a comma deliminated list without the OGR_<driver>_ prefix\n"
" so OGR_XLSX_HEADERS = FORCE OGR_XLSX_FIELD_TYPES = STRING would become:\n"
" \"HEADERS = FORCE,FIELD_TYPES = STRING\""
"\n");
exit(0);
}
int
main(int argc, char** argv)
{
int ch;
char* source = NULL;
const char* layer = NULL, *server = NULL, *table = NULL, *options = NULL;
int layer_index = -1;
OGRErr err = OGRERR_NONE;
/* If no options are specified, display usage */
if (argc == 1)
{
usage();
}
while ((ch = getopt(argc, argv, "hfs:l:t:n:i:o:")) != -1)
{
switch (ch)
{
case 's':
source = optarg;
break;
case 'l':
layer = optarg;
break;
case 'f':
formats();
break;
case 't':
table = optarg;
break;
case 'n':
server = optarg;
break;
case 'i':
layer_index = atoi(optarg) - 1;
break;
case 'o':
options = optarg;
break;
case '?':
case 'h':
default:
usage();
break;
}
}
if (source && ! layer && layer_index == -1)
{
err = ogrListLayers(source);
}
else if (source && (layer || layer_index > -1))
{
if (! layer)
{
err = ogrFindLayer(source, layer_index, &layer);
}
if (err == OGRERR_NONE)
{
err = ogrGenerateSQL(server, layer, table, source, options);
}
}
else if (! source && ! layer)
{
usage();
}
if (err != OGRERR_NONE)
{
printf("OGR Error: %s\n\n", CPLGetLastErrorMsg());
exit(1);
}
OGRCleanupAll();
exit(0);
}
static OGRErr
ogrListLayers(const char* source)
{
GDALDatasetH ogr_ds = NULL;
int i;
GDALAllRegister();
#if GDAL_VERSION_MAJOR < 2
ogr_ds = OGROpen(source, FALSE, NULL);
#else
ogr_ds = GDALOpenEx(source,
GDAL_OF_VECTOR | GDAL_OF_READONLY,
NULL, NULL, NULL);
#endif
if (! ogr_ds)
{
CPLError(CE_Failure, CPLE_AppDefined, "Could not connect to source '%s'", source);
return OGRERR_FAILURE;
}
printf("Format: %s\n\n", GDALGetDriverShortName(GDALGetDatasetDriver(ogr_ds)));
printf("Layers:\n");
for (i = 0; i < GDALDatasetGetLayerCount(ogr_ds); i++)
{
OGRLayerH ogr_lyr = GDALDatasetGetLayer(ogr_ds, i);
if (! ogr_lyr)
{
return OGRERR_FAILURE;
}
printf(" %s\n", OGR_L_GetName(ogr_lyr));
}
printf("\n");
GDALClose(ogr_ds);
return OGRERR_NONE;
}
static OGRErr
ogrGenerateSQL(const char* server, const char* layer, const char* table, const char* source, const char* options)
{
OGRErr err;
GDALDatasetH ogr_ds = NULL;
GDALDriverH ogr_dr = NULL;
OGRLayerH ogr_lyr = NULL;
char server_name[NAMEDATALEN];
stringbuffer_t buf;
char **option_iter;
char **option_list;
GDALAllRegister();
#if GDAL_VERSION_MAJOR < 2
ogr_ds = OGROpen(source, FALSE, &ogr_dr);
#else
ogr_ds = GDALOpenEx(source,
GDAL_OF_VECTOR | GDAL_OF_READONLY,
NULL, NULL, NULL);
#endif
if (! ogr_ds)
{
CPLError(CE_Failure, CPLE_AppDefined, "Could not connect to source '%s'", source);
return OGRERR_FAILURE;
}
if (! ogr_dr)
ogr_dr = GDALGetDatasetDriver(ogr_ds);
strcpy(server_name, server == NULL ? "myserver" : server);
if (options != NULL) {
char *p;
char stripped_config_options[STR_MAX_LEN] = {0};
char option[NAMEDATALEN];
const char *short_name = GDALGetDriverShortName(ogr_dr);
strncpy(stripped_config_options, options, STR_MAX_LEN - 1);
p = strtok(strip_spaces(stripped_config_options), ",");
while (p != NULL) {
if (strcmp(short_name, "XLSX") == 0 || strcmp(short_name, "XLSX") == 0 || strcmp(short_name, "ODS") == 0)
{
/* Unify the handling of the options of spreadsheet file options as they are all the same except they have their
* Driver Short Name included in the option
*/
sprintf(option, "OGR_%s_%s ", short_name, ogr_fdw_strupr(p));
}
else {
sprintf(option, "%s ", ogr_fdw_strupr(p));
}
strcat(config_options, option);
p = strtok(NULL, ",");
}
}
option_list = CSLTokenizeString(config_options);
for ( option_iter = option_list; option_iter && *option_iter; option_iter++ )
{
char *key;
const char *value;
value = CPLParseNameValue(*option_iter, &key);
if (! (key && value))
CPLError(CE_Failure, CPLE_AppDefined, "bad config option string '%s'", config_options);
CPLSetConfigOption(key, value);
CPLFree(key);
}
CSLDestroy( option_list );
ogr_lyr = GDALDatasetGetLayerByName(ogr_ds, layer);
if (! ogr_lyr)
{
CPLError(CE_Failure, CPLE_AppDefined, "Could not find layer '%s' in source '%s'", layer, source);
return OGRERR_FAILURE;
}
/* Output SERVER definition */
printf("\nCREATE SERVER %s\n"
" FOREIGN DATA WRAPPER ogr_fdw\n"
" OPTIONS (\n"
" datasource '%s',\n"
" format '%s'",
quote_identifier(server_name), source, GDALGetDriverShortName(ogr_dr));
if (strlen(config_options) > 0)
{
printf(",\n config_options '%s');\n", config_options);
}
else
{
printf(");\n");
}
stringbuffer_init(&buf);
err = ogrLayerToSQL(ogr_lyr,
server_name,
TRUE, /* launder table names */
TRUE, /* launder column names */
table,/* output table name */
TRUE, /* use postgis geometry */
&buf);
GDALClose(ogr_ds);
if (err != OGRERR_NONE)
{
return err;
}
printf("\n%s\n", stringbuffer_getstring(&buf));
stringbuffer_release(&buf);
return OGRERR_NONE;
}
static OGRErr
ogrFindLayer(const char *source, int layerno, const char** layer)
{
GDALDatasetH ogr_ds = NULL;
int i;
char **option_iter;
char **option_list;
GDALAllRegister();
option_list = CSLTokenizeString(config_options);
for (option_iter = option_list; option_iter && *option_iter; option_iter++)
{
char *key;
const char *value;
value = CPLParseNameValue(*option_iter, &key);
if (! (key && value))
CPLError(CE_Failure, CPLE_AppDefined, "bad config option string '%s'", config_options);
CPLSetConfigOption(key, value);
CPLFree(key);
}
CSLDestroy(option_list);
#if GDAL_VERSION_MAJOR < 2
ogr_ds = OGROpen(source, FALSE, NULL);
#else
ogr_ds = GDALOpenEx(source,
GDAL_OF_VECTOR | GDAL_OF_READONLY,
NULL, NULL, NULL);
#endif
if (! ogr_ds)
{
CPLError(CE_Failure, CPLE_AppDefined, "Could not connect to source '%s'", source);
return OGRERR_FAILURE;
}
for (i = 0; i < GDALDatasetGetLayerCount(ogr_ds); i++)
{
if (i == layerno) {
OGRLayerH ogr_lyr = GDALDatasetGetLayer(ogr_ds, i);
if (! ogr_lyr)
{
return OGRERR_FAILURE;
}
*layer = OGR_L_GetName(ogr_lyr);
return OGRERR_NONE;
}
}
GDALClose(ogr_ds);
return OGRERR_FAILURE;
}
static int
reserved_word(const char * pgcolumn)
{
char* reserved[] = {
"all", "analyse", "analyze", "and", "any", "array", "as", "asc", "asymmetric", "authorization",
"binary", "both",
"case", "cast", "check", "collate", "collation", "column", "concurrently", "constraint", "create", "cross", "current_catalog", "current_date", "current_role",
"current_schema", "current_time", "current_timestamp", "current_user",
"default", "deferrable", "desc", "distinct", "do",
"else", "end", "except",
"false", "fetch", "for", "foreign", "freeze", "from", "full",
"grant", "group",
"having",
"ilike", "in", "initially", "inner", "intersect", "into", "is", "isnull",
"join",
"lateral", "leading", "left", "like", "limit", "localtime", "localtimestamp",
"natural", "not", "notnull", "null",
"offset", "on", "only", "or", "order", "outer", "overlaps",
"placing", "primary",
"references", "returning", "right",
"select", "session_user", "similar", "some", "symmetric",
"table", "tablesample", "then", "to", "trailing", "true",
"union", "unique", "user", "using",
"variadic", "verbose",
"when", "where", "window", "with"
};
int i;
for (i = 0; i < sizeof(reserved)/sizeof(reserved[0]); i++)
{
if (strcmp(pgcolumn, reserved[i]) == 0)
return 1;
}
return 0;
}
|