Datum lexize(PG_FUNCTION_ARGS) { text *in = PG_GETARG_TEXT_P(1); DictInfo *dict; TSLexeme *res, *ptr; Datum *da; ArrayType *a; SET_FUNCOID(); dict = finddict(PG_GETARG_OID(0)); ptr = res = (TSLexeme *) DatumGetPointer( FunctionCall3(&(dict->lexize_info), PointerGetDatum(dict->dictionary), PointerGetDatum(VARDATA(in)), Int32GetDatum(VARSIZE(in) - VARHDRSZ) ) ); PG_FREE_IF_COPY(in, 1); if (!res) { if (PG_NARGS() > 2) PG_RETURN_POINTER(NULL); else PG_RETURN_NULL(); } while (ptr->lexeme) ptr++; da = (Datum *) palloc(sizeof(Datum) * (ptr - res + 1)); ptr = res; while (ptr->lexeme) { da[ptr - res] = PointerGetDatum(char2text(ptr->lexeme)); ptr++; } a = construct_array( da, ptr - res, TEXTOID, -1, false, 'i' ); ptr = res; while (ptr->lexeme) { pfree(DatumGetPointer(da[ptr - res])); pfree(ptr->lexeme); ptr++; } pfree(res); pfree(da); PG_RETURN_POINTER(a); }
int get_currcfg(void) { Oid arg[1] = {TEXTOID}; const char *curlocale; Datum pars[1]; bool isnull; int stat; char buf[1024]; char *nsp; void *plan; if (current_cfg_id > 0) return current_cfg_id; nsp = get_namespace(TSNSP_FunctionOid); SPI_connect(); sprintf(buf, "select oid from %s.pg_ts_cfg where locale = $1 ", nsp); pfree(nsp); plan = SPI_prepare(buf, 1, arg); if (!plan) /* internal error */ elog(ERROR, "SPI_prepare() failed"); curlocale = setlocale(LC_CTYPE, NULL); pars[0] = PointerGetDatum(char2text((char *) curlocale)); stat = SPI_execp(plan, pars, " ", 1); if (stat < 0) /* internal error */ elog(ERROR, "SPI_execp return %d", stat); if (SPI_processed > 0) current_cfg_id = DatumGetObjectId(SPI_getbinval(SPI_tuptable->vals[0], SPI_tuptable->tupdesc, 1, &isnull)); else ereport(ERROR, (errcode(ERRCODE_CONFIG_FILE_ERROR), errmsg("could not find tsearch config by locale"))); pfree(DatumGetPointer(pars[0])); SPI_freeplan(plan); SPI_finish(); return current_cfg_id; }
Datum token_type_current(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; Datum result; SET_FUNCOID(); if (SRF_IS_FIRSTCALL()) { funcctx = SRF_FIRSTCALL_INIT(); if (current_parser_id == InvalidOid) current_parser_id = name2id_prs(char2text("default")); setup_firstcall(fcinfo, funcctx, current_parser_id); } funcctx = SRF_PERCALL_SETUP(); if ((result = process_call(funcctx)) != (Datum) 0) SRF_RETURN_NEXT(funcctx, result); SRF_RETURN_DONE(funcctx); }
Datum parse_current(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; Datum result; SET_FUNCOID(); if (SRF_IS_FIRSTCALL()) { text *txt = PG_GETARG_TEXT_P(0); funcctx = SRF_FIRSTCALL_INIT(); if (current_parser_id == InvalidOid) current_parser_id = name2id_prs(char2text("default")); prs_setup_firstcall(fcinfo, funcctx, current_parser_id, txt); PG_FREE_IF_COPY(txt, 0); } funcctx = SRF_PERCALL_SETUP(); if ((result = prs_process_call(funcctx)) != (Datum) 0) SRF_RETURN_NEXT(funcctx, result); SRF_RETURN_DONE(funcctx); }
Datum spell_init(PG_FUNCTION_ARGS) { DictISpell *d; Map *cfg, *pcfg; text *in; bool affloaded = false, dictloaded = false, stoploaded = false; if (PG_ARGISNULL(0) || PG_GETARG_POINTER(0) == NULL) ereport(ERROR, (errcode(ERRCODE_CONFIG_FILE_ERROR), errmsg("ISpell confguration error"))); d = (DictISpell *) malloc(sizeof(DictISpell)); if (!d) ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("out of memory"))); memset(d, 0, sizeof(DictISpell)); d->stoplist.wordop = lowerstr; in = PG_GETARG_TEXT_P(0); parse_cfgdict(in, &cfg); PG_FREE_IF_COPY(in, 0); pcfg = cfg; while (pcfg->key) { if (pg_strcasecmp("DictFile", pcfg->key) == 0) { if (dictloaded) { freeDictISpell(d); ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("dictionary already loaded"))); } if (NIImportDictionary(&(d->obj), pcfg->value)) { freeDictISpell(d); ereport(ERROR, (errcode(ERRCODE_CONFIG_FILE_ERROR), errmsg("could not load dictionary file \"%s\"", pcfg->value))); } dictloaded = true; } else if (pg_strcasecmp("AffFile", pcfg->key) == 0) { if (affloaded) { freeDictISpell(d); ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("affixes already loaded"))); } if (NIImportAffixes(&(d->obj), pcfg->value)) { freeDictISpell(d); ereport(ERROR, (errcode(ERRCODE_CONFIG_FILE_ERROR), errmsg("could not load affix file \"%s\"", pcfg->value))); } affloaded = true; } else if (pg_strcasecmp("StopFile", pcfg->key) == 0) { text *tmp = char2text(pcfg->value); if (stoploaded) { freeDictISpell(d); ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("stop words already loaded"))); } readstoplist(tmp, &(d->stoplist)); sortstoplist(&(d->stoplist)); pfree(tmp); stoploaded = true; } else { freeDictISpell(d); ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("unrecognized option: %s => %s", pcfg->key, pcfg->value))); } pfree(pcfg->key); pfree(pcfg->value); pcfg++; } pfree(cfg); if (affloaded && dictloaded) { NISortDictionary(&(d->obj)); NISortAffixes(&(d->obj)); } else if (!affloaded) { freeDictISpell(d); ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("no affixes"))); } else { freeDictISpell(d); ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("no dictionary"))); } PG_RETURN_POINTER(d); }