/[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 12 by dpavlin, Sat Feb 19 15:09:05 2005 UTC revision 18 by dpavlin, Sun Feb 20 22:58:25 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 36  Line 38 
38  SW_HANDLE   swish_handle = NULL;/* Database handle */  SW_HANDLE   swish_handle = NULL;/* Database handle */
39  SW_SEARCH   search = NULL;      /* search handle -- holds search parameters */  SW_SEARCH   search = NULL;      /* search handle -- holds search parameters */
40  SW_RESULTS  swish_results = NULL;       /* results handle -- holds list of results */  SW_RESULTS  swish_results = NULL;       /* results handle -- holds list of results */
41    SW_RESULT   *sw_res = NULL;     /* one row from swish-e results */
42    
43  /* define PostgreSQL v1 function */  /* define PostgreSQL v1 function */
44  PG_FUNCTION_INFO_V1(pgswish);  PG_FUNCTION_INFO_V1(pgswish);
# Line 114  Datum pgswish(PG_FUNCTION_ARGS) { Line 117  Datum pgswish(PG_FUNCTION_ARGS) {
117                  funcctx->attinmeta = attinmeta;                  funcctx->attinmeta = attinmeta;
118    
119                  MemoryContextSwitchTo(oldcontext);                  MemoryContextSwitchTo(oldcontext);
120    
121                    elog(INFO, "SRF_IS_FIRSTCALL done");
122          }          }
123    
124          /* stuff done on every call of the function */          /* stuff done on every call of the function */
# Line 128  Datum pgswish(PG_FUNCTION_ARGS) { Line 133  Datum pgswish(PG_FUNCTION_ARGS) {
133                  char            **values;                  char            **values;
134                  HeapTuple       tuple;                  HeapTuple       tuple;
135                  Datum           result;                  Datum           result;
                 SW_RESULT       *sw_res;        /* one row from swish-e results */  
136    
137                  elog(INFO, "pgswish: in loop %d", call_cntr);                  elog(INFO, "pgswish: loop count %d", call_cntr);
138    
139                  if (! swish_results) {                  if (! swish_results) {
140                          elog(ERROR, "pgswish: no swish-e results");                          elog(ERROR, "pgswish: no swish-e results");
141                          SRF_RETURN_DONE(funcctx);                          SRF_RETURN_DONE(funcctx);
142                  }                  }
143                                    
144                  elog(INFO, "pgswish: check for swish-e error");                  elog(DEBUG1, "pgswish: check for swish-e error");
145                  error_or_abort( swish_handle );                  error_or_abort( swish_handle );
146    
147                  /*                  /*
# Line 145  Datum pgswish(PG_FUNCTION_ARGS) { Line 149  Datum pgswish(PG_FUNCTION_ARGS) {
149                   * This should be an array of C strings which will                   * This should be an array of C strings which will
150                   * be processed later by the type input functions.                   * be processed later by the type input functions.
151                   */                   */
                 values = (char **) palloc(4 * sizeof(char *));  
152    
153                  sw_res = SwishNextResult( swish_results );                  sw_res = SwishNextResult( swish_results );
154                  if (! sw_res) {                  if (! sw_res) {
# Line 164  Datum pgswish(PG_FUNCTION_ARGS) { Line 167  Datum pgswish(PG_FUNCTION_ARGS) {
167                          SwishResultPropertyULong ( sw_res, "swishfilenum" )                          SwishResultPropertyULong ( sw_res, "swishfilenum" )
168                  );                  );
169    
170                  elog(INFO, "swishdocpath: %s", prop2text( sw_res, "swishdocpath" ) );                  values = (char **) palloc(4 * sizeof(char *));
171    
172                  values[0] = prop2int( sw_res, "swishrank" );                  values[0] = prop2int( sw_res, "swishrank" );
173                  values[1] = prop2text( sw_res, "swishdocpath" );                  values[1] = prop2text( sw_res, "swishdocpath" );
174                  values[2] = prop2text( sw_res, "swishtitle" );                  values[2] = prop2text( sw_res, "swishtitle" );
175                  values[3] = prop2int( sw_res, "swishdocsize" );                  values[3] = prop2int( sw_res, "swishdocsize" );
176                    
177    /*
178                    values[0] = (char *) palloc(16 * sizeof(char));
179                    snprintf(values[0], 16, "%d", 1);
180                    values[1] = (char *) palloc(16 * sizeof(char));
181                    snprintf(values[1], 16, "%d", 2);
182                    values[2] = (char *) palloc(16 * sizeof(char));
183                    snprintf(values[2], 16, "%d", 3);
184                    values[3] = (char *) palloc(16 * sizeof(char));
185                    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 177  Datum pgswish(PG_FUNCTION_ARGS) { Line 192  Datum pgswish(PG_FUNCTION_ARGS) {
192                  result = TupleGetDatum(slot, tuple);                  result = TupleGetDatum(slot, tuple);
193    
194                  /* clean up ? */                  /* clean up ? */
195                    pfree(values[0]);
196                  elog(INFO, "row: %s|%s|%s|%s",values[0],values[1],values[2],values[3]);                  pfree(values[1]);
197                    pfree(values[2]);
198                    pfree(values[3]);
199                    pfree(values);
200                    
201                    elog(DEBUG1, "row: %s|%s|%s|%s",values[0],values[1],values[2],values[3]);
202                    
203                  SRF_RETURN_NEXT(funcctx, result);                  SRF_RETURN_NEXT(funcctx, result);
204          } else {          } else {
205                    elog(INFO, "loop over");
206    
207                  /* free swish object and close */                  /* free swish object and close */
208                  Free_Search_Object( search );                  Free_Search_Object( search );
209                  SwishClose( swish_handle );                  SwishClose( swish_handle );
# Line 191  Datum pgswish(PG_FUNCTION_ARGS) { Line 213  Datum pgswish(PG_FUNCTION_ARGS) {
213          }          }
214  }  }
215    
216  /*  /* work in progress */
217   * elog errors  PG_FUNCTION_INFO_V1(pgswish2);
218   *  Datum pgswish2(PG_FUNCTION_ARGS)
219   */  {
220            int             nrows = 3;
221            int16           typlen;
222            bool            typbyval;
223            char            typalign;
224            ReturnSetInfo   *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
225            AttInMetadata   *attinmeta;
226            TupleDesc       tupdesc;
227            Tuplestorestate *tupstore = NULL;
228            HeapTuple       tuple;
229            MemoryContext   per_query_ctx;
230            MemoryContext   oldcontext;
231            Datum           dvalue;
232            char            **values;
233            int             ncols;
234            int             i, j;
235    
236            /* check to see if caller supports us returning a tuplestore */
237            if (!rsinfo || !(rsinfo->allowedModes & SFRM_Materialize))
238                    ereport(ERROR,
239                                    (errcode(ERRCODE_SYNTAX_ERROR),
240                                     errmsg("materialize mode required, but it is not " \
241                                                    "allowed in this context")));
242    
243            /* get the requested return tuple description */
244            tupdesc = rsinfo->expectedDesc;
245            ncols = tupdesc->natts;
246    
247            /*
248             * The requested tuple description better match up with the array
249             * we were given.
250             */
251            /* OK, use it */
252            attinmeta = TupleDescGetAttInMetadata(tupdesc);
253    
254            /* Now go to work */
255            rsinfo->returnMode = SFRM_Materialize;
256    
257            per_query_ctx = fcinfo->flinfo->fn_mcxt;
258            oldcontext = MemoryContextSwitchTo(per_query_ctx);
259    
260            /* initialize our tuplestore */
261            tupstore = tuplestore_begin_heap(true, false, SortMem);
262    
263            values = (char **) palloc(ncols * sizeof(char *));
264    
265            for (i = 0; i < nrows; i++)
266            {
267                    for (j = 0; j < ncols; j++)
268                    {
269                            values[j] = DatumGetCString( "foo" );
270                    }
271                    /* construct the tuple */
272                    tuple = BuildTupleFromCStrings(attinmeta, values);
273    
274                    /* now store it */
275                    tuplestore_puttuple(tupstore, tuple);
276            }
277    
278            tuplestore_donestoring(tupstore);
279            rsinfo->setResult = tupstore;
280    
281            /*
282             * SFRM_Materialize mode expects us to return a NULL Datum. The actual
283             * tuples are in our tuplestore and passed back through
284             * rsinfo->setResult. rsinfo->setDesc is set to the tuple description
285             * that we actually used to build our tuples with, so the caller can
286             * verify we did what it was expecting.
287             */
288            rsinfo->setDesc = tupdesc;
289            MemoryContextSwitchTo(oldcontext);
290    
291            return (Datum) 0;
292    }
293    
294    
295    /* make text var prom property */
296  char *prop2text(SW_RESULT sw_res, char *propname) {  char *prop2text(SW_RESULT sw_res, char *propname) {
297          char *val;          char *val;
298          char *prop;          char *prop;
299          int len;          int len;
300    
301          elog(INFO, "prop2text(%s)", propname);          elog(DEBUG2, "prop2text(%s)", propname);
302    
303          prop = SwishResultPropertyStr( sw_res, propname );          prop = SwishResultPropertyStr( sw_res, propname );
304          error_or_abort( swish_handle );          error_or_abort( swish_handle );
305    
306          len = strlen(prop);          len = strlen(prop);
307          elog(INFO, "prop2text(%s) = '%s' %d bytes", propname, prop, len);          elog(DEBUG1, "prop2text(%s) = '%s' %d bytes", propname, prop, len);
308    
309          len++;          len++;
310          len *= sizeof(char);          len *= sizeof(char);
311    
312          elog(INFO, "palloc(%d)", len);          elog(DEBUG2, "palloc(%d)", len);
313    
314          val = palloc(len);          val = palloc(len);
315    
316          memset(val, 0, len);          memset(val, 0, len);
317          strncpy(val, prop, len);          strncpy(val, prop, len);
318    
319          elog(INFO, "val=%s", val);          elog(DEBUG2, "val=%s", val);
320    
321          return val;          return val;
322  }  }
323    
324    /* make integer variable from property */
325  char *prop2int(SW_RESULT sw_res, char *propname) {  char *prop2int(SW_RESULT sw_res, char *propname) {
326          char *val;          char *val;
327          unsigned long prop;          unsigned long prop;
328          int len;          int len;
329    
330          elog(INFO, "prop2int(%s)", propname);          elog(DEBUG2, "prop2int(%s)", propname);
331    
332          prop = SwishResultPropertyULong( sw_res, propname );          prop = SwishResultPropertyULong( sw_res, propname );
333          error_or_abort( swish_handle );          error_or_abort( swish_handle );
334    
335          elog(INFO, "prop2int(%s) = %lu", propname, prop);          elog(DEBUG1, "prop2int(%s) = %lu", propname, prop);
336    
337          len = 128 * sizeof(char);          len = 128 * sizeof(char);
338          elog(INFO, "palloc(%d)", len);          elog(DEBUG2, "palloc(%d)", len);
339    
340          val = palloc(len);          val = palloc(len);
341          memset(val, 0, len);          memset(val, 0, len);
342    
343          snprintf(val, len, "%lu", prop);          snprintf(val, len, "%lu", prop);
344    
345          elog(INFO, "val=%s", val);          elog(DEBUG2, "val=%s", val);
346    
347          return val;          return val;
348  }  }
349    
350    
351    /*
352     * check if swish has returned error, and elog it.
353     */
354  static void error_or_abort( SW_HANDLE swish_handle ) {  static void error_or_abort( SW_HANDLE swish_handle ) {
355          if ( !SwishError( swish_handle ) )          if ( !SwishError( swish_handle ) )
356                  return;                  return;
# Line 263  static void error_or_abort( SW_HANDLE sw Line 364  static void error_or_abort( SW_HANDLE sw
364          );          );
365          if ( search ) Free_Search_Object( search );          if ( search ) Free_Search_Object( search );
366          SwishClose( swish_handle );          SwishClose( swish_handle );
   
         /* do when there is no more left */  
367  }  }
368    

Legend:
Removed from v.12  
changed lines
  Added in v.18

  ViewVC Help
Powered by ViewVC 1.1.26