PG UDF vracejici tabulku o jednom sloupci
Zdenek Janis
Zdenek.Janis na brajan.cz
Neděle Říjen 17 12:32:15 CEST 2004
odpovim si sam...
1) Funkce, jenz vraci nekolik radku integeru:
PG_FUNCTION_INFO_V1 (tabinteger);
Datum tabinteger(PG_FUNCTION_ARGS)
{
FuncCallContext *funcctx;
if (SRF_IS_FIRSTCALL()) {
funcctx = SRF_FIRSTCALL_INIT();
funcctx->max_calls = (random() % 50);
}
funcctx = SRF_PERCALL_SETUP();
if (funcctx->max_calls > funcctx->call_cntr) {
SRF_RETURN_NEXT(funcctx, Int32GetDatum(funcctx->call_cntr));
}
else {
SRF_RETURN_DONE(funcctx);
}
}
create or replace function test.tabinteger()
returns setof integer as
'tabinteger.so' language 'C';
2) Funkce, jenz vraci nekolik radku varcharu:
PG_FUNCTION_INFO_V1 (tabvarchar);
Datum tabvarchar(PG_FUNCTION_ARGS)
{
FuncCallContext *funcctx;
if (SRF_IS_FIRSTCALL()) {
MemoryContext oldcontext;
funcctx = SRF_FIRSTCALL_INIT();
oldcontext = MemoryContextSwitchTo(
funcctx->multi_call_memory_ctx);
funcctx->max_calls = (random() % 50); /* Nahodny pocet radku */
funcctx->user_fctx = (void *)palloc(VARHDRSZ + 100);
MemoryContextSwitchTo(oldcontext);
}
funcctx = SRF_PERCALL_SETUP();
if (funcctx->max_calls > funcctx->call_cntr) {
snprintf(VARDATA(funcctx->user_fctx), 99,
"Line: %d", funcctx->call_cntr);
VARATT_SIZEP(funcctx->user_fctx) =
VARHDRSZ + strlen(VARDATA(funcctx->user_fctx));
SRF_RETURN_NEXT(funcctx, (Datum)(funcctx->user_fctx));
}
else {
SRF_RETURN_DONE(funcctx);
}
}
create or replace function test.tabvarchar()
returns setof varchar as
'tabinteger.so' language 'C';
--
Zdenek
Další informace o konferenci Databases