/[pgswish]/trunk/pgswish.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/pgswish.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 14 by dpavlin, Sat Feb 19 20:59:17 2005 UTC revision 16 by dpavlin, Sun Feb 20 21:51:56 2005 UTC
# Line 25  Line 25 
25  #include "fmgr.h"  #include "fmgr.h"
26  #include "funcapi.h"  #include "funcapi.h"
27  #include "utils/builtins.h"  #include "utils/builtins.h"
28    #include "utils/array.h"
29    #include "miscadmin.h"
30  #include <swish-e.h>  #include <swish-e.h>
31    
32  #define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str))  #define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str))
# Line 182  Datum pgswish(PG_FUNCTION_ARGS) { Line 184  Datum pgswish(PG_FUNCTION_ARGS) {
184                  values[3] = (char *) palloc(16 * sizeof(char));                  values[3] = (char *) palloc(16 * sizeof(char));
185                  snprintf(values[3], 16, "%d", 4);                  snprintf(values[3], 16, "%d", 4);
186  */  */
187                    
188                  /* build a tuple */                  /* build a tuple */
189                  tuple = BuildTupleFromCStrings(attinmeta, values);                  tuple = BuildTupleFromCStrings(attinmeta, values);
190    
# Line 211  Datum pgswish(PG_FUNCTION_ARGS) { Line 213  Datum pgswish(PG_FUNCTION_ARGS) {
213          }          }
214  }  }
215    
216  Datum swtextprop(PG_FUNCTION_ARGS) {  /* work in progress */
217          char    *prop;  PG_FUNCTION_INFO_V1(pgswish2);
218          char    *val;  Datum pgswish2(PG_FUNCTION_ARGS)
219    {
220            int             ncols = 2;
221            int             nrows = 3;
222            int16           typlen;
223            bool            typbyval;
224            char            typalign;
225            ReturnSetInfo   *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
226            AttInMetadata   *attinmeta;
227            TupleDesc       tupdesc;
228            Tuplestorestate *tupstore = NULL;
229            HeapTuple       tuple;
230            MemoryContext   per_query_ctx;
231            MemoryContext   oldcontext;
232            Datum           dvalue;
233            char            **values;
234            int             rsinfo_ncols;
235            int             i, j;
236    
237            /* check to see if caller supports us returning a tuplestore */
238            if (!rsinfo || !(rsinfo->allowedModes & SFRM_Materialize))
239                    ereport(ERROR,
240                                    (errcode(ERRCODE_SYNTAX_ERROR),
241                                     errmsg("materialize mode required, but it is not " \
242                                                    "allowed in this context")));
243    
244            /* get the requested return tuple description */
245            tupdesc = rsinfo->expectedDesc;
246            rsinfo_ncols = tupdesc->natts;
247    
248            /*
249             * The requested tuple description better match up with the array
250             * we were given.
251             */
252            elog(INFO, "rsinfo_ncols = %d, ncols = %d", rsinfo_ncols, ncols);
253    
254            /* OK, use it */
255            attinmeta = TupleDescGetAttInMetadata(tupdesc);
256    
257            /* Now go to work */
258            rsinfo->returnMode = SFRM_Materialize;
259    
260            per_query_ctx = fcinfo->flinfo->fn_mcxt;
261            oldcontext = MemoryContextSwitchTo(per_query_ctx);
262    
263            /* initialize our tuplestore */
264            tupstore = tuplestore_begin_heap(true, false, SortMem);
265    
266            values = (char **) palloc(ncols * sizeof(char *));
267    
268            for (i = 0; i < nrows; i++)
269            {
270                    for (j = 0; j < ncols; j++)
271                    {
272                            values[j] = DatumGetCString( "foo" );
273                    }
274                    /* construct the tuple */
275                    tuple = BuildTupleFromCStrings(attinmeta, values);
276    
277          prop = _textout(PG_GETARG_TEXT_P(0));                  /* now store it */
278          elog(INFO, "pgswish: swextprop(%s)", prop);                  tuplestore_puttuple(tupstore, tuple);
279            }
280    
281          val = prop2int( sw_res, prop );          tuplestore_donestoring(tupstore);
282          error_or_abort( swish_handle );          rsinfo->setResult = tupstore;
283    
284          elog(INFO, "pgswish: swextprop(%s) = '%s'", prop, val );          /*
285             * SFRM_Materialize mode expects us to return a NULL Datum. The actual
286             * tuples are in our tuplestore and passed back through
287             * rsinfo->setResult. rsinfo->setDesc is set to the tuple description
288             * that we actually used to build our tuples with, so the caller can
289             * verify we did what it was expecting.
290             */
291            rsinfo->setDesc = tupdesc;
292            MemoryContextSwitchTo(oldcontext);
293    
294          PG_FREE_IF_COPY(prop, 0);          return (Datum) 0;
         PG_RETURN_TEXT_P( _textin(val) );  
295  }  }
296    
297    
298  /* make text var prom property */  /* make text var prom property */
299  char *prop2text(SW_RESULT sw_res, char *propname) {  char *prop2text(SW_RESULT sw_res, char *propname) {
300          char *val;          char *val;

Legend:
Removed from v.14  
changed lines
  Added in v.16

  ViewVC Help
Powered by ViewVC 1.1.26