pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
CREATE FUNCTION and LANGUAGE 'C'
2+ messages / 2 participants
[nested] [flat]

* CREATE FUNCTION and LANGUAGE 'C'
@ 2002-02-28 15:44 Céline Rivière <cr@fr.netcentrex.net>
  2002-03-04 22:58 ` Re: CREATE FUNCTION and LANGUAGE 'C' Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 2+ messages in thread

From: Céline Rivière @ 2002-02-28 15:44 UTC (permalink / raw)
  To: pgsql-sql

Hello,

I'm trying to call a C function (C++ in fact but I think, it's not a problem) from a
stored procedure (using CREATE FUNCTION).

-------------------------------------------------------
This is the creation of my stored procedure:

CREATE FUNCTION Get_Length(TEXT) 
RETURNS INTEGER
AS
 '/home/ncxusers/criviere/TOOLS/lib/tools.so', 'Get_Length'
LANGUAGE 'C';


This is the C implementation of GetLength :

#include <stdio.h>
#include "postgres.h"
#include "fmgr.h"

PG_FUNCTION_INFO_V1(file_open);

extern "C" Datum file_open (PG_FUNCTION_ARGS)
{
 char *szName = PG_GETARG_CSTRING(0);
 long lgLength = strlen(szName);
 PG_RETURN_UINT32(lgLength);
}



This is the result of the execution :

TestVersioning=# select file_open('abcdefgh') as v_result;
 v_result 
----------
        2
(1 row)

-------------------------------------------------------

Why does it return 2 and not 8 (length of 'abcdef') ?

Thanks for your help.

Céline

PS:
I think I have a problem with string manipulation. 
Because I also have a 2nd issue when trying to return a variable of type TEXT (or VARCHAR). 
The following error occurs : 

pqReadData() -- backend closed the channel unexpectedly.
        This probably means the backend terminated abnormally
        before or while processing the request.
The connection to the server was lost. Attempting reset: Failed.

^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: CREATE FUNCTION and LANGUAGE 'C'
  2002-02-28 15:44 CREATE FUNCTION and LANGUAGE 'C' Céline Rivière <cr@fr.netcentrex.net>
@ 2002-03-04 22:58 ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 2002-03-04 22:58 UTC (permalink / raw)
  To: Céline Rivière <cr@fr.netcentrex.net>; +Cc: pgsql-sql

=?iso-8859-1?Q?C=E9line_Rivi=E8re?= <cr@fr.netcentrex.net> writes:
> CREATE FUNCTION Get_Length(TEXT)=20
> RETURNS INTEGER
> AS
>  '/home/ncxusers/criviere/TOOLS/lib/tools.so', 'Get_Length'
> LANGUAGE 'C';

> This is the C implementation of GetLength :

> extern "C" Datum file_open (PG_FUNCTION_ARGS)
> {
>  char *szName =3D PG_GETARG_CSTRING(0);
>  long lgLength =3D strlen(szName);

Type TEXT is not the same as a C string.  Look at the builtin functions
that manipulate text arguments.

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2002-03-04 22:58 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-02-28 15:44 CREATE FUNCTION and LANGUAGE 'C' Céline Rivière <cr@fr.netcentrex.net>
2002-03-04 22:58 ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox