--- trunk/pgswish.c 2005/02/19 20:59:17 14 +++ trunk/pgswish.c 2005/02/20 21:51:56 16 @@ -25,6 +25,8 @@ #include "fmgr.h" #include "funcapi.h" #include "utils/builtins.h" +#include "utils/array.h" +#include "miscadmin.h" #include #define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str)) @@ -182,7 +184,7 @@ values[3] = (char *) palloc(16 * sizeof(char)); snprintf(values[3], 16, "%d", 4); */ - + /* build a tuple */ tuple = BuildTupleFromCStrings(attinmeta, values); @@ -211,22 +213,88 @@ } } -Datum swtextprop(PG_FUNCTION_ARGS) { - char *prop; - char *val; +/* work in progress */ +PG_FUNCTION_INFO_V1(pgswish2); +Datum pgswish2(PG_FUNCTION_ARGS) +{ + int ncols = 2; + 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 rsinfo_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; + rsinfo_ncols = tupdesc->natts; + + /* + * The requested tuple description better match up with the array + * we were given. + */ + elog(INFO, "rsinfo_ncols = %d, ncols = %d", rsinfo_ncols, ncols); + + /* 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); - prop = _textout(PG_GETARG_TEXT_P(0)); - elog(INFO, "pgswish: swextprop(%s)", prop); + /* now store it */ + tuplestore_puttuple(tupstore, tuple); + } - val = prop2int( sw_res, prop ); - error_or_abort( swish_handle ); + tuplestore_donestoring(tupstore); + rsinfo->setResult = tupstore; - elog(INFO, "pgswish: swextprop(%s) = '%s'", prop, val ); + /* + * 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); - PG_FREE_IF_COPY(prop, 0); - PG_RETURN_TEXT_P( _textin(val) ); + return (Datum) 0; } + /* make text var prom property */ char *prop2text(SW_RESULT sw_res, char *propname) { char *val;