Skip to content

Commit a6e499d

Browse files
committed
Enhancements to ogr info for XSLX sheets in particular, from @mccluskk. Closes pramsey#183
1 parent fd8fcb3 commit a6e499d

File tree

5 files changed

+229
-44
lines changed

5 files changed

+229
-44
lines changed

README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,20 @@ Layers:
5858
5959
# ogr_fdw_info -s /tmp/test -l pt_two
6060
61-
CREATE SERVER myserver
61+
CREATE SERVER "myserver"
6262
FOREIGN DATA WRAPPER ogr_fdw
6363
OPTIONS (
6464
datasource '/tmp/test',
6565
format 'ESRI Shapefile' );
6666
67-
CREATE FOREIGN TABLE pt_two (
67+
CREATE FOREIGN TABLE "pt_two" (
6868
fid integer,
69-
geom geometry(Point, 4326),
70-
name varchar,
71-
age integer,
72-
height real,
73-
birthdate date )
74-
SERVER myserver
69+
"geom" geometry(Point, 4326),
70+
"name" varchar,
71+
"age" integer,
72+
"height" real,
73+
"birthdate" date )
74+
SERVER "myserver"
7575
OPTIONS (layer 'pt_two');
7676
```
7777

ogr_fdw.c

+1
Original file line numberDiff line numberDiff line change
@@ -3016,6 +3016,7 @@ ogrImportForeignSchema(ImportForeignSchemaStmt* stmt, Oid serverOid)
30163016
server->servername,
30173017
launder_table_names,
30183018
launder_column_names,
3019+
NULL,
30193020
ogrGetGeometryOid() != BYTEAOID,
30203021
&buf
30213022
);

ogr_fdw_common.c

+23-14
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "ogr_fdw_gdal.h"
1212
#include "ogr_fdw_common.h"
1313
#include "stringbuffer.h"
14+
#include "pg_config_manual.h"
1415

1516
/* Prototype for function that must be defined in PostgreSQL (it is) */
1617
/* and in ogr_fdw_info (it is) */
@@ -52,8 +53,8 @@ void
5253
ogrStringLaunder(char *str)
5354
{
5455
int i, j = 0;
55-
char tmp[STR_MAX_LEN];
56-
memset(tmp, 0, STR_MAX_LEN);
56+
char tmp[NAMEDATALEN];
57+
memset(tmp, 0, NAMEDATALEN);
5758

5859
for(i = 0; str[i]; i++)
5960
{
@@ -79,10 +80,10 @@ ogrStringLaunder(char *str)
7980
tmp[j++] = c;
8081

8182
/* Avoid mucking with data beyond the end of our stack-allocated strings */
82-
if ( j >= STR_MAX_LEN )
83-
j = STR_MAX_LEN - 1;
83+
if ( j >= NAMEDATALEN - 1)
84+
break;
8485
}
85-
strncpy(str, tmp, STR_MAX_LEN);
86+
strncpy(str, tmp, NAMEDATALEN);
8687

8788
}
8889

@@ -220,10 +221,11 @@ ogrGeomTypeToPgGeomType(stringbuffer_t *buf, OGRwkbGeometryType gtype)
220221
static OGRErr
221222
ogrColumnNameToSQL (const char *ogrcolname, const char *pgtype, int launder_column_names, stringbuffer_t *buf)
222223
{
223-
char pgcolname[STR_MAX_LEN];
224-
strncpy(pgcolname, ogrcolname, STR_MAX_LEN);
224+
char pgcolname[NAMEDATALEN];
225+
strncpy(pgcolname, ogrcolname, NAMEDATALEN);
225226
ogrStringLaunder(pgcolname);
226227

228+
227229
if ( launder_column_names )
228230
{
229231
stringbuffer_aprintf(buf, ",\n %s %s", quote_identifier(pgcolname), pgtype);
@@ -250,10 +252,11 @@ ogrColumnNameToSQL (const char *ogrcolname, const char *pgtype, int launder_colu
250252
OGRErr
251253
ogrLayerToSQL (const OGRLayerH ogr_lyr, const char *fdw_server,
252254
int launder_table_names, int launder_column_names,
255+
const char *fdw_table_name,
253256
int use_postgis_geometry, stringbuffer_t *buf)
254257
{
255258
int geom_field_count, i;
256-
char table_name[STR_MAX_LEN];
259+
char table_name[NAMEDATALEN];
257260
OGRFeatureDefnH ogr_fd = OGR_L_GetLayerDefn(ogr_lyr);
258261
stringbuffer_t gbuf;
259262

@@ -272,9 +275,15 @@ ogrLayerToSQL (const OGRLayerH ogr_lyr, const char *fdw_server,
272275
#endif
273276

274277
/* Process table name */
275-
strncpy(table_name, OGR_L_GetName(ogr_lyr), STR_MAX_LEN);
276-
if (launder_table_names)
277-
ogrStringLaunder(table_name);
278+
if (fdw_table_name == NULL) {
279+
strncpy(table_name, OGR_L_GetName(ogr_lyr), NAMEDATALEN);
280+
281+
if (launder_table_names)
282+
ogrStringLaunder(table_name);
283+
}
284+
else {
285+
strncpy(table_name, fdw_table_name, NAMEDATALEN);
286+
}
278287

279288
/* Create table */
280289
stringbuffer_aprintf(buf, "CREATE FOREIGN TABLE %s (\n", quote_identifier(table_name));
@@ -361,17 +370,17 @@ ogrLayerToSQL (const OGRLayerH ogr_lyr, const char *fdw_server,
361370
/* Write out attribute fields */
362371
for ( i = 0; i < OGR_FD_GetFieldCount(ogr_fd); i++ )
363372
{
364-
char pgtype[128];
373+
char pgtype[NAMEDATALEN];
365374
OGRFieldDefnH ogr_fld = OGR_FD_GetFieldDefn(ogr_fd, i);
366-
ogrTypeToPgType(ogr_fld, pgtype, 128);
375+
ogrTypeToPgType(ogr_fld, pgtype, sizeof(pgtype));
367376
ogrColumnNameToSQL(OGR_Fld_GetNameRef(ogr_fld), pgtype, launder_column_names, buf);
368377
}
369378

370379
/*
371380
* Add server name and layer-level options. We specify remote
372381
* layer name as option
373382
*/
374-
stringbuffer_aprintf(buf, "\n) SERVER %s \nOPTIONS (", quote_identifier(fdw_server));
383+
stringbuffer_aprintf(buf, "\n) SERVER %s\nOPTIONS (", quote_identifier(fdw_server));
375384
stringbuffer_append(buf, "layer ");
376385
ogrDeparseStringLiteral(buf, OGR_L_GetName(ogr_lyr));
377386
stringbuffer_append(buf, ");\n");

ogr_fdw_common.h

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ void ogrStringLaunder(char *str);
2727

2828
OGRErr ogrLayerToSQL (const OGRLayerH ogr_lyr, const char *fwd_server,
2929
int launder_table_names, int launder_column_names,
30+
const char *fdw_table_name,
3031
int use_postgis_geometry, stringbuffer_t *buf);
3132

3233
#endif /* _OGR_FDW_COMMON_H */

0 commit comments

Comments
 (0)