--- trunk/pgswish.c 2005/02/20 22:58:25 18 +++ trunk/pgswish.c 2005/03/06 21:13:39 19 @@ -6,6 +6,8 @@ * TODO: * - check null input using PG_ARGISNULL before using PG_GETARG_xxxx * - support composite type arguments + * - split error_or_abort + * - use getResultPropValue not SwishResultPropertyStr * * NOTES: * - clear structures with memset to support hash indexes (who whould like @@ -34,7 +36,6 @@ #define GET_STR(textp) DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(textp))) #define GET_TEXT(cstrp) DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(cstrp))) - SW_HANDLE swish_handle = NULL;/* Database handle */ SW_SEARCH search = NULL; /* search handle -- holds search parameters */ SW_RESULTS swish_results = NULL; /* results handle -- holds list of results */ @@ -81,15 +82,15 @@ SRF_RETURN_DONE(funcctx); } - error_or_abort( swish_handle ); + if (error_or_abort( swish_handle )) SRF_RETURN_DONE(funcctx); /* set ranking scheme. default is 0 */ SwishRankScheme( swish_handle, 0 ); - error_or_abort( swish_handle ); + if (error_or_abort( swish_handle )) SRF_RETURN_DONE(funcctx); elog(INFO, "pgswish: SwishQuery(%s)", query); /* Here's a short-cut to searching that creates a search object and searches at the same time */ swish_results = SwishQuery( swish_handle, query); - error_or_abort( swish_handle ); + if (error_or_abort( swish_handle )) SRF_RETURN_DONE(funcctx); /* total number of tuples to be returned */ funcctx->max_calls = SwishHits( swish_results ); @@ -142,7 +143,7 @@ } elog(DEBUG1, "pgswish: check for swish-e error"); - error_or_abort( swish_handle ); + if (error_or_abort( swish_handle )) SRF_RETURN_DONE(funcctx); /* * Prepare a values array for storage in our slot. @@ -153,6 +154,8 @@ sw_res = SwishNextResult( swish_results ); if (! sw_res) { elog(ERROR, "pgswish: swish-e sort result list: %d rows expected %d", call_cntr, max_calls - 1); + Free_Results_Object( swish_results ); + Free_Search_Object( search ); SRF_RETURN_DONE(funcctx); } @@ -292,7 +295,7 @@ } -/* make text var prom property */ +/* make text var from property */ char *prop2text(SW_RESULT sw_res, char *propname) { char *val; char *prop; @@ -301,7 +304,7 @@ elog(DEBUG2, "prop2text(%s)", propname); prop = SwishResultPropertyStr( sw_res, propname ); - error_or_abort( swish_handle ); + if (error_or_abort( swish_handle )) return NULL; len = strlen(prop); elog(DEBUG1, "prop2text(%s) = '%s' %d bytes", propname, prop, len); @@ -330,7 +333,7 @@ elog(DEBUG2, "prop2int(%s)", propname); prop = SwishResultPropertyULong( sw_res, propname ); - error_or_abort( swish_handle ); + if (error_or_abort( swish_handle )) return NULL; elog(DEBUG1, "prop2int(%s) = %lu", propname, prop); @@ -351,9 +354,9 @@ /* * check if swish has returned error, and elog it. */ -static void error_or_abort( SW_HANDLE swish_handle ) { +static int error_or_abort( SW_HANDLE swish_handle ) { if ( !SwishError( swish_handle ) ) - return; + return 0; /* print a message */ elog(ERROR, @@ -362,7 +365,10 @@ SwishErrorString( swish_handle ), SwishLastErrorMsg( swish_handle ) ); + if ( swish_results ) Free_Results_Object( swish_results ); if ( search ) Free_Search_Object( search ); SwishClose( swish_handle ); + + return 1; }