pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: TalGloz <glozmantal@gmail.com>
To: pgsql-general@postgresql.org
Subject: Returning Vector of Pairs with a PostgreSQL C Extension Function
Date: Sat, 25 Aug 2018 05:54:38 -0700 (MST)
Message-ID: <1535201678396-0.post@n3.nabble.com> (raw)

Hello,

I want to return an vector of pairs using a C extension. This is a simple
code I have:

extern "C" {
Datum pair(PG_FUNCTION_ARGS){

        // For text aka. character varying parameter
        text *t1 = PG_GETARG_TEXT_PP(0);
        text *t2 = PG_GETARG_TEXT_PP(1);
        std::string localT1 = text_to_cstring(t1);
        std::string localT2 = text_to_cstring(t2);

        /* Construct the return value of the C extention to PostgreSQl */
        // Returns Text ponter of casted cstrings to text
        //PG_RETURN_TEXT_P(cstring_to_text_with_len(localT1.c_str(),
localT1.size()));
        //PG_RETURN_TEXT_P(cstring_to_text_with_len(encodedLocalT1.c_str(),
encodedLocalT1.size()));
        //PG_RETURN_TEXT_P(cstring_to_text_with_len(outputParam.c_str(),
outputParam.size()));

        // Return vector of pairs
        std::vector<std::pair&lt;std::string, std::string>> ret;
        ret.emplace_back(encodedLocalT1, encodedLocalT2);
        PG_RETURN_ARRAYTYPE_P(ret);

};
PG_FUNCTION_INFO_V1(pair);
}

But it doesn't work like it. Even using this doesn't work:
ArrayType   *array;
std::vector<std::pair&lt;std::string, std::string>> ret;
ret.emplace_back(encodedLocalT1, encodedLocalT2);
array = ret;
PG_RETURN_POINTER(array);

Is it possible to return a vector of pairs? If not then how can I return 2
strings in an Array?

Best regards,
Tal



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




view thread (9+ messages)  latest in thread

Message-ID: <1535201678396-0.post@n3.nabble.com>
Permalink:  ../1535201678396-0.post@n3.nabble.com/
Also on:    postgresql.org/message-id/1535201678396-0.post@n3.nabble.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: glozmantal@gmail.com
  Subject: Re: Returning Vector of Pairs with a PostgreSQL C Extension Function
  In-Reply-To: <1535201678396-0.post@n3.nabble.com>
  References: <1535201678396-0.post@n3.nabble.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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