36 |
#define GET_STR(textp) DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(textp))) |
#define GET_STR(textp) DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(textp))) |
37 |
#define GET_TEXT(cstrp) DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(cstrp))) |
#define GET_TEXT(cstrp) DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(cstrp))) |
38 |
|
|
39 |
SW_HANDLE swish_handle = NULL;/* Database handle */ |
/* Globals */ |
40 |
SW_SEARCH search = NULL; /* search handle -- holds search parameters */ |
static SW_HANDLE swish_handle = NULL; /* Database handle */ |
41 |
SW_RESULTS swish_results = NULL; /* results handle -- holds list of results */ |
static SW_SEARCH search = NULL; /* search handle -- search parameters */ |
42 |
SW_RESULT *sw_res = NULL; /* one row from swish-e results */ |
static SW_RESULTS swish_results = NULL; /* results handle -- list of results */ |
43 |
|
static SW_RESULT *sw_res = NULL; /* one row from swish-e results */ |
44 |
|
|
45 |
/* define PostgreSQL v1 function */ |
/* define PostgreSQL v1 function */ |
46 |
PG_FUNCTION_INFO_V1(pgswish); |
PG_FUNCTION_INFO_V1(pgswish); |
54 |
AttInMetadata *attinmeta; |
AttInMetadata *attinmeta; |
55 |
char *index_path; |
char *index_path; |
56 |
char *query; |
char *query; |
57 |
|
FILE *logfh; |
58 |
|
|
59 |
/* stuff done only on the first call of the function */ |
/* stuff done only on the first call of the function */ |
60 |
if (SRF_IS_FIRSTCALL()) { |
if (SRF_IS_FIRSTCALL()) { |
72 |
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); |
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); |
73 |
|
|
74 |
|
|
75 |
/* Send any errors or warnings to stderr (default is stdout) */ |
/* Send any errors or warnings to log, as well as |
76 |
SwishErrorsToStderr(); |
* STDOUT and STDERR (just to be sure) */ |
77 |
|
if ( logfh = fopen("/tmp/pgswish.log", "a") ) { |
78 |
|
set_error_handle( logfh ); |
79 |
|
elog(INFO, "loggin swish-e errors to /tmp/pgswish.log"); |
80 |
|
/* redirect STDOUT and STDERR to log */ |
81 |
|
dup2(1, logfh); |
82 |
|
dup2(2, logfh); |
83 |
|
} else { |
84 |
|
elog(INFO, "can't open /tmp/pgswish.log -- errors from swish-e won't be cought and may result in back-end crashes!"); |
85 |
|
} |
86 |
|
|
87 |
elog(INFO, "pgswish: SwishInit(%s)", index_path); |
elog(INFO, "pgswish: SwishInit(%s)", index_path); |
88 |
|
|
89 |
swish_handle = SwishInit( index_path ); |
swish_handle = SwishInit( index_path ); |
90 |
|
|
91 |
|
if ( SwishError( swish_handle ) ) |
92 |
|
elog(INFO, "pgswish: SwishInit(%s) failed: %s", index_path, SwishErrorString( swish_handle )); |
93 |
|
|
94 |
|
elog(INFO, "handle: %08x", swish_handle); |
95 |
|
|
96 |
if (! swish_handle) { |
if (! swish_handle) { |
97 |
elog(ERROR, "pgswish: can't open %s", index_path); |
elog(ERROR, "pgswish: can't open %s", index_path); |
98 |
SRF_RETURN_DONE(funcctx); |
SRF_RETURN_DONE(funcctx); |
232 |
} |
} |
233 |
} |
} |
234 |
|
|
|
/* work in progress */ |
|
|
PG_FUNCTION_INFO_V1(pgswish2); |
|
|
Datum pgswish2(PG_FUNCTION_ARGS) |
|
|
{ |
|
|
int nrows = 3; |
|
|
int16 typlen; |
|
|
bool typbyval; |
|
|
char typalign; |
|
|
ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo; |
|
|
AttInMetadata *attinmeta; |
|
|
TupleDesc tupdesc; |
|
|
Tuplestorestate *tupstore = NULL; |
|
|
HeapTuple tuple; |
|
|
MemoryContext per_query_ctx; |
|
|
MemoryContext oldcontext; |
|
|
Datum dvalue; |
|
|
char **values; |
|
|
int ncols; |
|
|
int i, j; |
|
|
|
|
|
/* check to see if caller supports us returning a tuplestore */ |
|
|
if (!rsinfo || !(rsinfo->allowedModes & SFRM_Materialize)) |
|
|
ereport(ERROR, |
|
|
(errcode(ERRCODE_SYNTAX_ERROR), |
|
|
errmsg("materialize mode required, but it is not " \ |
|
|
"allowed in this context"))); |
|
|
|
|
|
/* get the requested return tuple description */ |
|
|
tupdesc = rsinfo->expectedDesc; |
|
|
ncols = tupdesc->natts; |
|
|
|
|
|
/* |
|
|
* The requested tuple description better match up with the array |
|
|
* we were given. |
|
|
*/ |
|
|
/* OK, use it */ |
|
|
attinmeta = TupleDescGetAttInMetadata(tupdesc); |
|
|
|
|
|
/* Now go to work */ |
|
|
rsinfo->returnMode = SFRM_Materialize; |
|
|
|
|
|
per_query_ctx = fcinfo->flinfo->fn_mcxt; |
|
|
oldcontext = MemoryContextSwitchTo(per_query_ctx); |
|
|
|
|
|
/* initialize our tuplestore */ |
|
|
tupstore = tuplestore_begin_heap(true, false, SortMem); |
|
|
|
|
|
values = (char **) palloc(ncols * sizeof(char *)); |
|
|
|
|
|
for (i = 0; i < nrows; i++) |
|
|
{ |
|
|
for (j = 0; j < ncols; j++) |
|
|
{ |
|
|
values[j] = DatumGetCString( "foo" ); |
|
|
} |
|
|
/* construct the tuple */ |
|
|
tuple = BuildTupleFromCStrings(attinmeta, values); |
|
|
|
|
|
/* now store it */ |
|
|
tuplestore_puttuple(tupstore, tuple); |
|
|
} |
|
|
|
|
|
tuplestore_donestoring(tupstore); |
|
|
rsinfo->setResult = tupstore; |
|
|
|
|
|
/* |
|
|
* SFRM_Materialize mode expects us to return a NULL Datum. The actual |
|
|
* tuples are in our tuplestore and passed back through |
|
|
* rsinfo->setResult. rsinfo->setDesc is set to the tuple description |
|
|
* that we actually used to build our tuples with, so the caller can |
|
|
* verify we did what it was expecting. |
|
|
*/ |
|
|
rsinfo->setDesc = tupdesc; |
|
|
MemoryContextSwitchTo(oldcontext); |
|
|
|
|
|
return (Datum) 0; |
|
|
} |
|
|
|
|
235 |
|
|
236 |
/* make text var from property */ |
/* make text var from property */ |
237 |
char *prop2text(SW_RESULT sw_res, char *propname) { |
char *prop2text(SW_RESULT sw_res, char *propname) { |