Re: C-Functions using SPI - Missing Magic Block - Mailing list pgsql-general

From Saitenheini@web.de
Subject Re: C-Functions using SPI - Missing Magic Block
Date
Msg-id 796242437.490509.1278318030610.JavaMail.fmail@mwmweb050
Whole thread Raw
In response to Re: C-Functions using SPI - Missing Magic Block  (Craig Ringer <craig@postnewspapers.com.au>)
Responses Re: C-Functions using SPI - Missing Magic Block  (Sam Mason <sam@samason.me.uk>)
List pgsql-general
Hi people,

after a two days break:

I could compile the following code with Visual C++ Express 2010 under Windows Server 2003 R2:

/* Use 32-bit timer (provided header file uses 64-bit timer, not
* compatible with Windows postgreSQL versions */
#define _USE_32BIT_TIME_T
#define BUILDING_DLL 1
#include "postgres.h"
#include "fmgr.h" /* PG_MODULE_MAGIC */
#include "executor\spi.h" /* SPI - Server Programming Interface */

PG_MODULE_MAGIC;

PG_FUNCTION_INFO_V1(count_person);

__declspec(dllexport)
Datum count_person(PG_FUNCTION_ARGS) {
int ret;

SPI_connect();
ret = SPI_exec("SELECT count(*) FROM person", 0);
SPI_finish();

PG_RETURN_INT32(ret);
}

 

The steps I did:

- install GnuWin32 (GetText for Windows) and copy libintl.h to ...\PostgreSQL\8.3\include\server\port\win32

- edit "pg_config.h" and replace #define ENABLE_NLS 1 by #undef ENABLE_NLS

- create new empty DLL-Project and add new C++-File
- Rename file into "filename.c"

- in Visual C++ add:

PostgreSQL\8.3\include\server\port\win32

PostgreSQL\8.3\include\server\

PostgreSQL\8.3\bin

postgres.lib

compile as C-Code !!!

see also:

http://www.postgresql.org/docs/8.2/interactive/xfunc-c.html

http://www.postgresql.org/docs/8.0/interactive/xfunc-c.html

http://www.dbforums.com/postgresql/1626445-how-get-conn-ptr-c-lang-external-function.html

 

But I guess I still did something wrong, because no matter how many rows exist in my table "person" the result in
always5. 

Could this be an data type problem? count(*) returns int8

int8 -> int (C-type) -> PG_RETURN_INT32 ??? Could this cause the problem?

 

Thanks for any advise, Max.
___________________________________________________________
WEB.DE DSL ab 19,99 Euro/Monat. Bis zu 150,- Euro Startguthaben und
50,- Euro Geldprämie inklusive! https://freundschaftswerbung.web.de

pgsql-general by date:

Previous
From: Pavel Stehule
Date:
Subject: Re: Unable to run this query
Next
From: Sam Mason
Date:
Subject: Re: C-Functions using SPI - Missing Magic Block