#include "executor/spi.h" #include "postgres.h" #include text *sts_strcatex (char **str_pointer, char *str, text *txt, long *saved_chars, long *allocated_free, long page_size) { long l = strlen (str); if (l > page_size) elog (ERROR, "Retezec je vetsi nez velikost stranky"); if (*allocated_free < l) { text *txtn; long ma = ((*saved_chars + l) / page_size + 1) * page_size; elog (NOTICE, "before alloc %d", ma); txtn = (text *) palloc (ma); elog (NOTICE, "after alloc"); elog (NOTICE, "Alokovan %d", txtn); memcpy ((void *) VARDATA(txtn), (void *) VARDATA(txt), *saved_chars); elog (NOTICE, "after memcpy"); *allocated_free = ma - *saved_chars; *str_pointer = VARDATA(txtn) + *saved_chars; elog (NOTICE, "befor pfree"); pfree (txt); txt = txtn; elog (NOTICE, "after pfree"); } strcpy (*str_pointer, str); *str_pointer += l; *saved_chars += l; *allocated_free -= l; VARATT_SIZEP (txt) = *saved_chars + VARHDRSZ; return txt; } PG_FUNCTION_INFO_V1 (html_list); Datum html_list (PG_FUNCTION_ARGS) { long ret, sloupec, radek, saved_chars, allocated_free, page_size, max; char *str_pointer, *query, *column_name; text *txt; if (PG_ARGISNULL (0) | PG_ARGISNULL (1)) elog (ERROR, "Parametrs have'nt to be NULL"); if ((ret = SPI_connect()) < 0) elog (ERROR, "SPI_connect returned %d", ret); query = PG_GETARG_CSTRING (0); column_name = PG_GETARG_CSTRING (1); max = PG_GETARG_INT32 (2); page_size = PG_GETARG_INT32 (3); if ((ret = SPI_exec (query, max)) < 0) elog (ERROR, "SPI_exec returned %d", ret); sloupec = SPI_fnumber(SPI_tuptable->tupdesc, column_name); if (sloupec <= 0) elog (ERROR, "SPI_ERROR_NOATTRIBUTE"); saved_chars = 0; allocated_free = page_size; txt = (text*) palloc (VARHDRSZ + allocated_free); str_pointer = VARDATA(txt); txt = sts_strcatex (&str_pointer, "", txt, &saved_chars, &allocated_free, page_size); elog (NOTICE,"BEFORE finish"); SPI_finish (); elog (NOTICE, "AFTER FINISH"); PG_RETURN_TEXT_P(txt); }