forked from pramsey/pgsql-ogr-fdw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ogr_fdw_func.c
88 lines (73 loc) · 2.13 KB
/
ogr_fdw_func.c
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
/*-------------------------------------------------------------------------
*
* ogr_fdw_func.c
* Helper functions for OGR FDW
*
* Copyright (c) 2020, Paul Ramsey <[email protected]>
*
*-------------------------------------------------------------------------
*/
#include "ogr_fdw.h"
#include "ogr_fdw_gdal.h"
#include <postgres.h>
#include <fmgr.h>
#include <funcapi.h>
// #include <catalog/pg_type_d.h>
#include <utils/array.h>
#include <utils/builtins.h>
/**
*/
Datum ogr_fdw_drivers(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(ogr_fdw_drivers);
Datum ogr_fdw_drivers(PG_FUNCTION_ARGS)
{
/* Array building */
size_t arr_nelems = 0;
Datum *arr_elems;
ArrayType *arr;
Oid elem_type = TEXTOID;
int16 elem_len;
bool elem_byval;
char elem_align;
int num_drivers;
int i;
if (GDALGetDriverCount() <= 0)
GDALAllRegister();
#if (GDAL_VERSION_NUM >= GDAL_COMPUTE_VERSION(1,11,0))
num_drivers = GDALGetDriverCount();
#else
num_drivers = OGRGetDriverCount();
#endif
if (num_drivers < 1)
PG_RETURN_NULL();
arr_elems = palloc0(num_drivers * sizeof(Datum));
get_typlenbyvalalign(elem_type, &elem_len, &elem_byval, &elem_align);
for (i = 0; i < num_drivers; i++) {
#if GDAL_VERSION_MAJOR <= 1
OGRSFDriverH hDriver = OGRGetDriver(i);
text *txtName = cstring_to_text(OGR_Dr_GetName(hDriver));
arr_elems[arr_nelems++] = PointerGetDatum(txtName);
#else
GDALDriverH hDriver = GDALGetDriver(i);
if (GDALGetMetadataItem(hDriver, GDAL_DCAP_VECTOR, NULL) != NULL) {
const char *strName = OGR_Dr_GetName(hDriver);
text *txtName = cstring_to_text(strName);
arr_elems[arr_nelems++] = PointerGetDatum(txtName);
}
#endif
}
arr = construct_array(arr_elems, arr_nelems, elem_type, elem_len, elem_byval, elem_align);
PG_RETURN_ARRAYTYPE_P(arr);
}
/**
*/
Datum ogr_fdw_version(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(ogr_fdw_version);
Datum ogr_fdw_version(PG_FUNCTION_ARGS)
{
const char *gdal_ver = GDAL_RELEASE_NAME;
const char *ogr_fdw_ver = OGR_FDW_RELEASE_NAME;
char ver_str[256];
snprintf(ver_str, sizeof(ver_str), "OGR_FDW=\"%s\" GDAL=\"%s\"", ogr_fdw_ver, gdal_ver);
PG_RETURN_TEXT_P(cstring_to_text(ver_str));
}