Re: PostgreSQL C Language Extension with C++ Code

Поиск
Список
Период
Сортировка
От TalGloz
Тема Re: PostgreSQL C Language Extension with C++ Code
Дата
Msg-id 1534107330015-0.post@n3.nabble.com
обсуждение исходный текст
Ответ на Re: PostgreSQL C Language Extension with C++ Code  (Tom Lane <tgl@sss.pgh.pa.us>)
Список pgsql-general
OK It worked. This is how I did it, hopefully it is right

extern "C" {
#include <postgres.h>
#include <utils/rel.h>
#include <fmgr.h>
#include <utils/array.h>
#include <utils/builtins.h>
#include <catalog/pg_type.h>
#include <stdlib.h>
#include <stdint.h>

PG_MODULE_MAGIC;
}

#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <seal/seal.h> // external compiled c++ library linked on running
'make'


extern "C" {
Datum sum_of_numbers(PG_FUNCTION_ARGS){
        std::vector<int> numbers {23, 445, 64};
                int sum = 0;
                for (auto &item : numbers){
                        sum += item;
                }
                return sum;
};
PG_FUNCTION_INFO_V1(sum_of_numbers);
}

I've managed to create and execute the function in my PostgreSQL database.

So basically I can execute any CPP code as long as I declare my functions
like this:

extern "C" {
    Datum function_name(PG_FUNCTION_ARGS){
         // CPP code here
    };

    PG_FUNCTION_INFO_V1(function_name);
}

In addition tho thath, all the C headers should be * inside a extern "C" {
}* block and all the CPP headers *outside the extern "C" { }* block, did I
get it right? 

Thanks,
Tal



--
Sent from: http://www.postgresql-archive.org/PostgreSQL-general-f1843780.html


В списке pgsql-general по дате отправления:

Предыдущее
От: Adrian Klaver
Дата:
Сообщение: Re: Replication failure, slave requesting old segments
Следующее
От: Stephen Frost
Дата:
Сообщение: Re: Replication failure, slave requesting old segments