pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Арсен Арутюнян <arutar@bk.ru>
To: pgsql-bugs@postgresql.org
Subject:  Error in SPI_execute_plan (when inserting JSON from C code first { simboll is lost)
Date: Tue, 27 Jun 2017 01:21:04 +0300
Message-ID: <1498515664.231254063@f472.i.mail.ru> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

Hello 

i've got table with json field.

create table json_test(id serial,Num integer,Obj json);


i'm trying to insert from C function some json data.
i'm using SPI_execute_plan function to do this.

FULL CODE EXAMPLE
###################################################
###################################################
#include <string.h>
#include "postgres.h"
#include "fmgr.h"
#include "executor/spi.h"
#include "utils/geo_decls.h"
#include "utils/builtins.h"
#include "funcapi.h"
SPIPlanPtr m_JsonInsertPlan = NULL;
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
#ifdef _WIN32
PGDLLEXPORT Datum JsonError(PG_FUNCTION_ARGS);
#endif
PG_FUNCTION_INFO_V1(JsonError);
// create table json_test(id serial,Num integer,Obj json);
void InitPlans(){
if (m_JsonInsertPlan == NULL){
Oid * Types = (Oid *)palloc(2 * sizeof(Oid));
Types[0] = 23; //Int32
Types[1] = 114; //JSON
m_JsonInsertPlan = SPI_saveplan(SPI_prepare("insert into json_test(Num,Obj) values ($1,$2)\0", 2, Types));
}
}

Datum JsonError(PG_FUNCTION_ARGS)
{
SPI_connect();
InitPlans();
StringInfo JsonObject = makeStringInfo();
appendStringInfoString(JsonObject, "{\"ObjectName\":\"Bug Json Object\",\"Type\":227,\"Command\":88}");
Datum Values[2];
Values[0] = Int32GetDatum(227);
Values[1] = CStringGetDatum(JsonObject->data);
int ret = SPI_execute_plan(m_JsonInsertPlan, Values, NULL, false, 0);
bool Returner = false;
if (ret > 0 && SPI_processed){
//INSERT DONE
Returner = true;
}

SPI_finish();
PG_RETURN_BOOL(Returner);
}
###################################################
###################################################
rmdatabase=# select JsonError();
jsonerror
-----------
t
(1 row)

rmdatabase=# select JsonError();
jsonerror
-----------
t
(1 row)

rmdatabase=# select * from json_test;
id | num | obj
----+-----+---------------------------------------------------------
9 | 227 | "ObjectName":"Bug Json Object","Type":227,"Command":88}
10 | 227 | "ObjectName":"Bug Json Object","Type":227,"Command":88}
(2 rows)


first { simboll is lost....
first { simboll is lost....
first { simboll is lost....

best 
Arsen

-- 
Арсен Арутюнян

view thread (3+ messages)  latest in thread

Message-ID: <1498515664.231254063@f472.i.mail.ru>
Permalink:  ../1498515664.231254063@f472.i.mail.ru/
Also on:    postgresql.org/message-id/1498515664.231254063@f472.i.mail.ru

 · 

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-bugs@postgresql.org
  Cc: arutar@bk.ru
  Subject: Re: Error in SPI_execute_plan (when inserting JSON from C code first { simboll is lost)
  In-Reply-To: <1498515664.231254063@f472.i.mail.ru>
  References: <1498515664.231254063@f472.i.mail.ru>

* 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