Обсуждение: C-Language function invocation from another one.

Поиск
Список
Период
Сортировка

C-Language function invocation from another one.

От
Ilya Urikh
Дата:
Hi,

What is the best way to call C-Language function from another one?

Now I see only one method - to call SPI_execute("EXECUTE funcName(...);", false, 0).
For example:


PG_FUNCTION_INFO_V1(calculateAccount);
PG_FUNCTION_INFO_V1(calculateAccountService);

Datum calculateAccount(PG_FUNCTION_ARGS);
Datum calculateAccountService(PG_FUNCTION_ARGS);

Datum calculateAccount(PG_FUNCTION_ARGS)
{
// Arguments
int32 accountId = PG_GETARG_INT32(0);
DateADT startDate = PG_GETARG_DATEADT(1);
DateADT endDate = PG_GETARG_DATEADT(2);
char command[1000];
...

SPI_connect();

snprintf(command, sizeof(command), "execute calculateAccountService(%d,
%d);", accountId, serviceId);
SPI_execute(command, false, 0);

SPI_finish();

PG_RETURN_NULL();
}

Datum calculateAccountService(PG_FUNCTION_ARGS)
{
int32 accountId = PG_GETARG_INT32(0);
int32 serviceId = PG_GETARG_INT32(1);

...

PG_RETURN_NULL();


--
Best regards,
Ilya Urikh.

Re: C-Language function invocation from another one.

От
Tom Lane
Дата:
Ilya Urikh <ilya.urikh@gmail.com> writes:
> What is the best way to call C-Language function from another one?

Try DirectFunctionCallN --- there are many examples in the source code.

            regards, tom lane

Re: C-Language function invocation from another one.

От
Ilya Urikh
Дата:
Thanks! That's help.

When I compile functions in the same library they work fine. But if I put one to another shared library function execution failed with error:
can't load library "/usr/lib/pgsql/calculateAccount.so": /usr/lib/pgsql/calculateAccount.so: undefined symbol: calculateAccountService

Details:
------------------------------------------------------
File calculateAccount.c is compiled to calculateAccount.so:

Datum calculateAccount(PG_FUNCTION_ARGS)
{
    ...
   DirectFunctionCall3(calculateAccountService, accountId, atoi(SPI_getvalue(tuple, tupdesc, 1)), startDate);

   PG_RETURN_NULL();
}

-----------------------------------------------------
File calculateAccountService.c is compiled to calculateAccountService.so:

Datum calculateAccountService(PG_FUNCTION_ARGS)
{
    ...

    PG_RETURN_VOID();
}

------------------------------------------
Define the functions to PostgreSQL

CREATE OR REPLACE FUNCTION calculateAccountService(integer, smallint, date)
    RETURNS void AS
    'calculateAccountService.so', 'calculateAccountService'
    LANGUAGE C STRICT;

CREATE OR REPLACE FUNCTION calculateAccount(integer, date, date)
    RETURNS void AS
    'calculateAccount.so', 'calculateAccount'
    LANGUAGE C STRICT;

----------------------------------------
Try to execute:
SELECT calculateAccount(2, '2008-02-23', '2009-04-13');

And receive Error message:
can't load library "/usr/lib/pgsql/calculateAccount.so": /usr/lib/pgsql/calculateAccount.so: undefined symbol: calculateAccountService


Thanks in advance,

Ilya Urikh.


On Thu, May 14, 2009 at 10:44 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Ilya Urikh <ilya.urikh@gmail.com> writes:
> What is the best way to call C-Language function from another one?

Try DirectFunctionCallN --- there are many examples in the source code.

                       regards, tom lane



--
Best regards,
Ilya Urikh.