Error in SPI_execute_plan (when inserting JSON from C code first { simboll is lost)

From: Арсен Арутюнян <arutar(at)bk(dot)ru>
To: pgsql-bugs(at)postgresql(dot)org
Subject: Error in SPI_execute_plan (when inserting JSON from C code first { simboll is lost)
Date: 2017-06-26 22:21:04
Message-ID: 1498515664.231254063@f472.i.mail.ru
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

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

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Andres Freund 2017-06-26 22:35:36 Re: Error in SPI_execute_plan (when inserting JSON from C code first { simboll is lost)
Previous Message Caio Parolin 2017-06-26 19:51:33 RES: Problems installation