[BUGS] Error in SPI_execute_plan (when inserting JSON from C code first { simboll is lost) - Mailing list pgsql-bugs

From Арсен Арутюнян
Subject [BUGS] Error in SPI_execute_plan (when inserting JSON from C code first { simboll is lost)
Date
Msg-id 1498515664.231254063@f472.i.mail.ru
Whole thread Raw
Responses Re: [BUGS] Error in SPI_execute_plan (when inserting JSON from Ccode first { simboll is lost)  (Andres Freund <andres@anarazel.de>)
Re: [BUGS] Error in SPI_execute_plan (when inserting JSON from C code first { simboll is lost)  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-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

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

pgsql-bugs by date:

Previous
From: "Caio Parolin"
Date:
Subject: RES: [BUGS] Problems installation
Next
From: Andres Freund
Date:
Subject: Re: [BUGS] Error in SPI_execute_plan (when inserting JSON from Ccode first { simboll is lost)