@ -21,7 +21,6 @@
/*********top interface**********/
/*********top interface**********/
static void * plan_getparser = NULL ;
static Oid current_parser_id = InvalidOid ;
static Oid current_parser_id = InvalidOid ;
void
void
@ -31,20 +30,22 @@ init_prs(Oid id, WParserInfo * prs)
bool isnull ;
bool isnull ;
Datum pars [ 1 ] ;
Datum pars [ 1 ] ;
int stat ;
int stat ;
void * plan ;
char buf [ 1024 ] , * nsp ;
arg [ 0 ] = OIDOID ;
arg [ 0 ] = OIDOID ;
pars [ 0 ] = ObjectIdGetDatum ( id ) ;
pars [ 0 ] = ObjectIdGetDatum ( id ) ;
memset ( prs , 0 , sizeof ( WParserInfo ) ) ;
memset ( prs , 0 , sizeof ( WParserInfo ) ) ;
SPI_connect ( ) ;
SPI_connect ( ) ;
if ( ! plan_getparser )
nsp = get_namespace ( TSNSP_FunctionOid ) ;
{
sprintf ( buf , " select prs_start, prs_nexttoken, prs_end, prs_lextype, prs_headline from %s.pg_ts_parser where oid = $1 " , nsp ) ;
plan_getparser = SPI_saveplan ( SPI_prepare ( " select prs_start, prs_nexttoken, prs_end, prs_lextype, prs_headline from pg_ts_parser where oid = $1 " , 1 , arg ) ) ;
pfree ( nsp ) ;
if ( ! plan_getparser )
plan = SPI_prepare ( buf , 1 , arg ) ;
ts_error ( ERROR , " SPI_prepare() failed " ) ;
if ( ! plan )
}
ts_error ( ERROR , " SPI_prepare() failed " ) ;
stat = SPI_execp ( plan_getparser , pars , " " , 1 ) ;
stat = SPI_execp ( plan , pars , " " , 1 ) ;
if ( stat < 0 )
if ( stat < 0 )
ts_error ( ERROR , " SPI_execp return %d " , stat ) ;
ts_error ( ERROR , " SPI_execp return %d " , stat ) ;
if ( SPI_processed > 0 )
if ( SPI_processed > 0 )
@ -64,6 +65,7 @@ init_prs(Oid id, WParserInfo * prs)
}
}
else
else
ts_error ( ERROR , " No parser with id %d " , id ) ;
ts_error ( ERROR , " No parser with id %d " , id ) ;
SPI_freeplan ( plan ) ;
SPI_finish ( ) ;
SPI_finish ( ) ;
}
}
@ -130,8 +132,6 @@ findprs(Oid id)
return findprs ( id ) ; /* qsort changed order!! */ ;
return findprs ( id ) ; /* qsort changed order!! */ ;
}
}
static void * plan_name2id = NULL ;
Oid
Oid
name2id_prs ( text * name )
name2id_prs ( text * name )
{
{
@ -140,6 +140,8 @@ name2id_prs(text *name)
Datum pars [ 1 ] ;
Datum pars [ 1 ] ;
int stat ;
int stat ;
Oid id = findSNMap_t ( & ( PList . name2id_map ) , name ) ;
Oid id = findSNMap_t ( & ( PList . name2id_map ) , name ) ;
char buf [ 1024 ] , * nsp ;
void * plan ;
arg [ 0 ] = TEXTOID ;
arg [ 0 ] = TEXTOID ;
pars [ 0 ] = PointerGetDatum ( name ) ;
pars [ 0 ] = PointerGetDatum ( name ) ;
@ -148,20 +150,21 @@ name2id_prs(text *name)
return id ;
return id ;
SPI_connect ( ) ;
SPI_connect ( ) ;
if ( ! plan_name2id )
nsp = get_namespace ( TSNSP_FunctionOid ) ;
{
sprintf ( buf , " select oid from %s.pg_ts_parser where prs_name = $1 " , nsp ) ;
plan_name2id = SPI_saveplan ( SPI_prepare ( " select oid from pg_ts_parser where prs_name = $1 " , 1 , arg ) ) ;
pfree ( nsp ) ;
if ( ! plan_name2id )
plan = SPI_prepare ( buf , 1 , arg ) ;
ts_error ( ERROR , " SPI_prepare() failed " ) ;
if ( ! plan )
}
ts_error ( ERROR , " SPI_prepare() failed " ) ;
stat = SPI_execp ( plan_name2id , pars , " " , 1 ) ;
stat = SPI_execp ( plan , pars , " " , 1 ) ;
if ( stat < 0 )
if ( stat < 0 )
ts_error ( ERROR , " SPI_execp return %d " , stat ) ;
ts_error ( ERROR , " SPI_execp return %d " , stat ) ;
if ( SPI_processed > 0 )
if ( SPI_processed > 0 )
id = DatumGetObjectId ( SPI_getbinval ( SPI_tuptable - > vals [ 0 ] , SPI_tuptable - > tupdesc , 1 , & isnull ) ) ;
id = DatumGetObjectId ( SPI_getbinval ( SPI_tuptable - > vals [ 0 ] , SPI_tuptable - > tupdesc , 1 , & isnull ) ) ;
else
else
ts_error ( ERROR , " No parser '%s' " , text2char ( name ) ) ;
ts_error ( ERROR , " No parser '%s' " , text2char ( name ) ) ;
SPI_freeplan ( plan ) ;
SPI_finish ( ) ;
SPI_finish ( ) ;
addSNMap_t ( & ( PList . name2id_map ) , name , id ) ;
addSNMap_t ( & ( PList . name2id_map ) , name , id ) ;
return id ;
return id ;
@ -239,7 +242,7 @@ token_type(PG_FUNCTION_ARGS)
{
{
FuncCallContext * funcctx ;
FuncCallContext * funcctx ;
Datum result ;
Datum result ;
SET_FUNCOID ( ) ;
if ( SRF_IS_FIRSTCALL ( ) )
if ( SRF_IS_FIRSTCALL ( ) )
{
{
funcctx = SRF_FIRSTCALL_INIT ( ) ;
funcctx = SRF_FIRSTCALL_INIT ( ) ;
@ -260,7 +263,7 @@ token_type_byname(PG_FUNCTION_ARGS)
{
{
FuncCallContext * funcctx ;
FuncCallContext * funcctx ;
Datum result ;
Datum result ;
SET_FUNCOID ( ) ;
if ( SRF_IS_FIRSTCALL ( ) )
if ( SRF_IS_FIRSTCALL ( ) )
{
{
text * name = PG_GETARG_TEXT_P ( 0 ) ;
text * name = PG_GETARG_TEXT_P ( 0 ) ;
@ -284,7 +287,7 @@ token_type_current(PG_FUNCTION_ARGS)
{
{
FuncCallContext * funcctx ;
FuncCallContext * funcctx ;
Datum result ;
Datum result ;
SET_FUNCOID ( ) ;
if ( SRF_IS_FIRSTCALL ( ) )
if ( SRF_IS_FIRSTCALL ( ) )
{
{
funcctx = SRF_FIRSTCALL_INIT ( ) ;
funcctx = SRF_FIRSTCALL_INIT ( ) ;
@ -306,6 +309,7 @@ Datum set_curprs(PG_FUNCTION_ARGS);
Datum
Datum
set_curprs ( PG_FUNCTION_ARGS )
set_curprs ( PG_FUNCTION_ARGS )
{
{
SET_FUNCOID ( ) ;
findprs ( PG_GETARG_OID ( 0 ) ) ;
findprs ( PG_GETARG_OID ( 0 ) ) ;
current_parser_id = PG_GETARG_OID ( 0 ) ;
current_parser_id = PG_GETARG_OID ( 0 ) ;
PG_RETURN_VOID ( ) ;
PG_RETURN_VOID ( ) ;
@ -317,7 +321,7 @@ Datum
set_curprs_byname ( PG_FUNCTION_ARGS )
set_curprs_byname ( PG_FUNCTION_ARGS )
{
{
text * name = PG_GETARG_TEXT_P ( 0 ) ;
text * name = PG_GETARG_TEXT_P ( 0 ) ;
SET_FUNCOID ( ) ;
DirectFunctionCall1 (
DirectFunctionCall1 (
set_curprs ,
set_curprs ,
ObjectIdGetDatum ( name2id_prs ( name ) )
ObjectIdGetDatum ( name2id_prs ( name ) )
@ -440,7 +444,7 @@ parse(PG_FUNCTION_ARGS)
{
{
FuncCallContext * funcctx ;
FuncCallContext * funcctx ;
Datum result ;
Datum result ;
SET_FUNCOID ( ) ;
if ( SRF_IS_FIRSTCALL ( ) )
if ( SRF_IS_FIRSTCALL ( ) )
{
{
text * txt = PG_GETARG_TEXT_P ( 1 ) ;
text * txt = PG_GETARG_TEXT_P ( 1 ) ;
@ -464,7 +468,7 @@ parse_byname(PG_FUNCTION_ARGS)
{
{
FuncCallContext * funcctx ;
FuncCallContext * funcctx ;
Datum result ;
Datum result ;
SET_FUNCOID ( ) ;
if ( SRF_IS_FIRSTCALL ( ) )
if ( SRF_IS_FIRSTCALL ( ) )
{
{
text * name = PG_GETARG_TEXT_P ( 0 ) ;
text * name = PG_GETARG_TEXT_P ( 0 ) ;
@ -491,7 +495,7 @@ parse_current(PG_FUNCTION_ARGS)
{
{
FuncCallContext * funcctx ;
FuncCallContext * funcctx ;
Datum result ;
Datum result ;
SET_FUNCOID ( ) ;
if ( SRF_IS_FIRSTCALL ( ) )
if ( SRF_IS_FIRSTCALL ( ) )
{
{
text * txt = PG_GETARG_TEXT_P ( 0 ) ;
text * txt = PG_GETARG_TEXT_P ( 0 ) ;
@ -515,13 +519,17 @@ Datum headline(PG_FUNCTION_ARGS);
Datum
Datum
headline ( PG_FUNCTION_ARGS )
headline ( PG_FUNCTION_ARGS )
{
{
TSCfgInfo * cfg = findcfg ( PG_GETARG_OID ( 0 ) ) ;
text * in = PG_GETARG_TEXT_P ( 1 ) ;
text * in = PG_GETARG_TEXT_P ( 1 ) ;
QUERYTYPE * query = ( QUERYTYPE * ) DatumGetPointer ( PG_DETOAST_DATUM ( PG_GETARG_DATUM ( 2 ) ) ) ;
QUERYTYPE * query = ( QUERYTYPE * ) DatumGetPointer ( PG_DETOAST_DATUM ( PG_GETARG_DATUM ( 2 ) ) ) ;
text * opt = ( PG_NARGS ( ) > 3 & & PG_GETARG_POINTER ( 3 ) ) ? PG_GETARG_TEXT_P ( 3 ) : NULL ;
text * opt = ( PG_NARGS ( ) > 3 & & PG_GETARG_POINTER ( 3 ) ) ? PG_GETARG_TEXT_P ( 3 ) : NULL ;
HLPRSTEXT prs ;
HLPRSTEXT prs ;
text * out ;
text * out ;
WParserInfo * prsobj = findprs ( cfg - > prs_id ) ;
TSCfgInfo * cfg ;
WParserInfo * prsobj ;
SET_FUNCOID ( ) ;
cfg = findcfg ( PG_GETARG_OID ( 0 ) ) ;
prsobj = findprs ( cfg - > prs_id ) ;
memset ( & prs , 0 , sizeof ( HLPRSTEXT ) ) ;
memset ( & prs , 0 , sizeof ( HLPRSTEXT ) ) ;
prs . lenwords = 32 ;
prs . lenwords = 32 ;
@ -557,7 +565,9 @@ headline_byname(PG_FUNCTION_ARGS)
{
{
text * cfg = PG_GETARG_TEXT_P ( 0 ) ;
text * cfg = PG_GETARG_TEXT_P ( 0 ) ;
Datum out = DirectFunctionCall4 (
Datum out ;
SET_FUNCOID ( ) ;
out = DirectFunctionCall4 (
headline ,
headline ,
ObjectIdGetDatum ( name2id_cfg ( cfg ) ) ,
ObjectIdGetDatum ( name2id_cfg ( cfg ) ) ,
PG_GETARG_DATUM ( 1 ) ,
PG_GETARG_DATUM ( 1 ) ,
@ -574,6 +584,7 @@ Datum headline_current(PG_FUNCTION_ARGS);
Datum
Datum
headline_current ( PG_FUNCTION_ARGS )
headline_current ( PG_FUNCTION_ARGS )
{
{
SET_FUNCOID ( ) ;
PG_RETURN_DATUM ( DirectFunctionCall4 (
PG_RETURN_DATUM ( DirectFunctionCall4 (
headline ,
headline ,
ObjectIdGetDatum ( get_currcfg ( ) ) ,
ObjectIdGetDatum ( get_currcfg ( ) ) ,