PostgreSQL - libpq - bytea and PQexecParams - c

In my C++ code I use libpq for PostgreSQL.
If I run the following queries directly with PQexec, I got the correct results (1 | {{15,20},{78,96}})
WITH bar AS(
SELECT ST_FromGDALRaster(E'\\x89504e470d0a1a0a0000000d494844520000000200000002080000000057dd52f80000000e49444154089963e46765b11702000107006b50183daf0000000049454e44ae426082'::bytea) AS rast
)
SELECT (ST_DumpValues(rast)).*
FROM bar
or
WITH bar AS(
SELECT ST_FromGDALRaster('\x89504e470d0a1a0a0000000d494844520000000200000002080000000057dd52f80000000e49444154089963e46765b11702000107006b50183daf0000000049454e44ae426082'::bytea) AS rast
)
SELECT (ST_DumpValues(rast)).*
FROM bar
However, when I do:
const char* paramValues[1];
int paramLengths[1];
int paramFormats[1];
const char * q = R"(
WITH bar AS (
SELECT ST_FromGDALRaster($1::bytea) AS rast
)
SELECT (ST_DumpValues(rast)).*
FROM bar
)";
paramValues[0] = "\\x89504e470d0a1a0a0000000d494844520000000200000002080000000057dd52f80000000e49444154089963e46765b11702000107006b50183daf0000000049454e44ae426082";
paramLengths[0] = 72;
paramFormats[0] = 1; /* parameters are passed in a binary format */
PGresult* res = PQexecParams(conn,
q,
1, /* one param */
NULL, /* let the backend deduce param type */
paramValues,
paramLengths,
paramFormats,
1); /* ask for binary results */
auto r1 = PQgetvalue(res, 0, 0);
auto r2 = PQgetvalue(res, 0, 0);
Values r1 and r2 are nullptr and no error is returned from the query itself.
If I convert paramValues[0] from hexadeximal values to "byte array", I got data starting with chars ‰PNG. In this case, result is not nullptr, but empty string, which is not correct as well.
Any idea what is wrong with this code?

The issue was the type of returned result type
PGresult* res = PQexecParams(conn,
q,
1, /* one param */
NULL, /* let the backend deduce param type */
paramValues,
paramLengths,
paramFormats,
0); /* ask for text results */
The result should be text (0) instead of binary (1).
As input, byte array is used without escaped symbols.

Related

Can't transmate string through MariaDB connect/c Prepared Statement

I'm using "MariaDB Connector/C" for my homework, but I got a problem: I always get an empty string when I pass in a string parameter, the db table is:
MariaDB none#(none):test> SELECT * FROM t3
a
b
0
abc
1
bcd
2
af
3 rows in set
Time: 0.010s
MariaDB none#(none):test> DESC t3
Field
Type
Null
Key
Default
Extra
a
int(11)
NO
PRI
b
char(10)
YES
2 rows in set
Time: 0.011s
And the code I use to test:
#include <mysql/mysql.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
MYSQL *mysql;
mysql = mysql_init(NULL);
if (!mysql_real_connect(mysql,NULL , "none", "linux", "test", 0,"/tmp/mariadb.sock",0)){
printf( "Error connecting to database: %s",mysql_error(mysql));
} else
printf("Connected...\n");
if(mysql_real_query(mysql,"SET CHARACTER SET utf8",(unsigned int)sizeof("SET CHARACTER SET utf8"))){
printf("Failed to set Encode!\n");
}
char query_stmt_2[]="select * from t3 where b=?";
MYSQL_STMT *stmt2 = mysql_stmt_init(mysql);
if(mysql_stmt_prepare(stmt2, query_stmt_2, -1))
{
printf("STMT2 prepare failed.\n");
}
MYSQL_BIND instr_bind;
char instr[50]="abc";
my_bool in_is_null = 0;
my_bool in_error = 0;
instr_bind.buffer_type = MYSQL_TYPE_STRING;
instr_bind.buffer = &instr[0];
char in_ind = STMT_INDICATOR_NTS;
instr_bind.u.indicator = &in_ind;
unsigned long instr_len=sizeof(instr);
// instr_bind.length = &instr_len;
// instr_bind.buffer_length=instr_len;
instr_bind.is_null = &in_is_null;
instr_bind.error = &in_error;
MYSQL_BIND out_bind[2];
memset(out_bind, 0, sizeof(out_bind));
int out_int[2];
char outstr[50];
my_bool out_int_is_null[2]={0,0};
my_bool out_int_error[2]={0,0};
unsigned long out_int_length[2]={0,0};
out_bind[0].buffer = out_int+0;
out_bind[0].buffer_type = MYSQL_TYPE_LONG;
out_bind[0].is_null = out_int_is_null+0;
out_bind[0].error = out_int_error+0;
out_bind[0].length = out_int_length+0;
out_bind[1].buffer = outstr;
out_bind[1].buffer_type = MYSQL_TYPE_STRING;
out_bind[1].buffer_length = 50;
out_bind[1].is_null = out_int_is_null+1;
out_bind[1].error = out_int_error+1;
out_bind[1].length = out_int_length+1;
if(mysql_stmt_bind_param(stmt2, &instr_bind) ||
mysql_stmt_bind_result(stmt2, out_bind)){
printf("Bind error\n");
}
if(mysql_stmt_execute(stmt2))
{
printf("Exec error: %s",mysql_stmt_error(stmt2));
}
if(mysql_stmt_store_result(stmt2)){
printf("Store result error!\n");
printf("%s\n",mysql_stmt_error(stmt2));
}
while(!mysql_stmt_fetch(stmt2))
{
printf("%d\t%s\n", out_int[0], outstr);
}
mysql_stmt_close(stmt2);
end:
mysql_close(mysql);
}
I only got an empty result:
❯ ./Exec/test/stmt_test
Connected...
I have been in trouble with this for two days, and tomorrow is the deadline, I'm very anxious. Can you help? Thanks a lot!
1) General
Avoid "it was hard to write, so it should be hard to read" code
add variable declarations at the beginning of the function, not in the middle of code (Wdeclaration-after-statement)
don't use c++ comments in C
set character set with api function mysql_set_character_set()
write proper error handling, including mysql_error/mysql_stmt_error results and don't continue executing subsequent code after error.
always initialize MYSQL_BIND
2) input bind buffer
u.indicator is used for bulk operations and doesn't make sense here
bind.is_null is not required, since you specified a valid buffer address
buffer_length is not set (in comments)
3) Output bind buffer
Always bind output parameters after mysql_stmt_execute(), since mysql_stmt_prepare can't always determine the number of parameters, e.g. when calling a stored procedure: In this case mysql_stmt_bind_param will return an error.
binding an error indicator doesn't make much sense without setting MYSQL_REPORT_DATA_TRUNCATION (mysql_optionsv)
For some examples how to deal with prepared statements check the file ps.c of MariaDB Connector/C unit tests

GCC C and passing integer to PostgreSQL

I know that there probably was plenty on that but after several days of searching I am unable to find how to do one simple passing of integer and char in one go to PostgreSQL from C under Linux.
In PHP it is easy, like 123, and in C using libpq it seem to be like something out of ordinary.
I had a look at PQexecParams but is seem to be not helping. Examples on the net are not helping as well and it seems to be an impossible mission.
Would someone be kind enough to translate this simple PHP statement to C and show me how to pass multiple vars of different types in one INSERT query.
col1 is INT
col2 is CHAR
$int1 = 1;
$char1 = 'text';
$query = "INSERT INTO table (col1, col2) values ('$int1',$char1)";
$result = ibase_query($query);
This would show what I am trying to do (please mind the code is very wrong):
void insert_CommsDb(PGconn *conn, PGresult *pgres, int csrv0) { const char * params[1];
params[0] = csrv0;
pgres = PQexecParams(conn, "INSERT INTO comms_db (srv0::int) values ($1)",
1,
NULL,
params,
1,
NULL,
0);
if (PQresultStatus(pgres) != PGRES_COMMAND_OK)
{
fprintf(stderr, "INSERT failed: %s", PQerrorMessage(conn));
exit_nicely(conn,pgres);
}
PQclear(pgres);
}
https://www.postgresql.org/docs/current/static/libpq-exec.html
As #joop commented above:
If the paramTypes argument is NULL, all the params are assumed to be strings.
So, you should transform your int argument to a string.
void insert_CommsDb(PGconn *conn, int csrv0)
{
PGresult *pgres;
char * params[1];
char buff[12];
sprintf(buff, "%d", csrv0);
params[0] = buff;
pgres = PQexecParams(conn
, "INSERT INTO comms_db (srv0::int) values ($1)" // The query (we dont need the cast here)
, 1 // number of params
, NULL // array with types, or NULL
, params // array with parameter values
, NULL // ARRAY with parameter lenghts
, NULL // array with per-param flags indicating binary/non binary
, 0 // set to 1 if we want BINARY results, 0 for txt
);
if (PQrresultStatus(pgres) != PGRES_COMMAND_OK)
{
fprintf(stderr, "INSERT failed: %s", PQerrorMessage(conn));
exit_nicely(conn,pgres);
}
PQclear(pgres);
}
wildplasser's answer shows the way in general.
Since you explicitly asked about several parameters, I'll add an example for that.
If you are not happy to convert integers to strings, the alternative would be to use the external binary format of the data type in question. That requires inside knowledge and probably reading the PostgreSQL source. For some data types, it can also depend on the hardware.
PGresult *res;
PGconn *conn;
Oid types[2];
char * values[2];
int lengths[2], formats[2];
int arg0;
/* connect to the database */
/*
* The first argument is in binary format.
* Apart from having to use the "external binary
* format" for the data, we have to specify
* type and length.
*/
arg0 = htonl(42); /* external binary format: network byte order */
types[0] = 23; /* OID of "int4" */
values[0] = (char *) &arg0;
lengths[0] = sizeof(int);
formats[0] = 1;
/* second argument is in text format */
types[1] = 0;
values[1] = "something";
lengths[1] = 0;
formats[1] = 0;
res = PQexecParams(
conn,
"INSERT INTO mytab (col1, col2) values ($1, $2)",
2,
types,
(const char * const *)values,
lengths,
formats,
0 /* results in text format */
);
I'd recommend that you use the text format for most data types.
The notable exception is bytea, where it usually is an advantage to use the binary format, as it saves space and CPU power. In this case, the external binary format is simply the bytes.
VS C++ not liking htonl(42):
arg0 = htonl(42); /* external binary format: network byte order */

How to construct a named list (a SEXP) to be returned from the C function called with .Call()?

I call C code via .Call("foo", <args>), where foo calls other C
functions, computes the result and returns it. The result is a list
of length 3 and I would like to name this list. To this end, foo
does this:
/* Construct result list from variables containing the results */
SEXP res = PROTECT(allocVector(VECSXP, 3)); /* list of length 3 */
SET_VECTOR_ELT(res, 0, ScalarReal(a)); /* numeric(1) */
SET_VECTOR_ELT(res, 1, somenumericvector); /* numeric(<some length>) */
SET_VECTOR_ELT(res, 2, ScalarInteger(i)); /* integer(1) */
/* Name components and return */
SEXP nms = PROTECT(allocVector(STRSXP, 3)); /* names as SEXP */
char *nms_ = CHAR(STRING_ELT(nms, 0)); /* pointer to names */
char *names[3] = {"result_numeric", "result_numeric_vector", "result_integer"};
for(i = 0; i < 3; i++) nms_[i] = names[i];
setAttrib(res, R_NamesSymbol, nms);
UNPROTECT(1);
return res;
Is this a/the correct way of constructing a named list of length 3?
The C function indeed returns back to R but once I assign the output to a
variable in R, I immediately get a segmentation fault. What might be wrong? I
can put 'debug statement's' (simple printf("...\n") right before the above 'return
res;' and they are executed fine.
Is there any convenient way to debug C code called from R?
An alternative to BrodieG's answer is to use mkNamed from Rinlinedfuns.h (which contains an example of how to use mkNamed).
/* Construct named result list from variables containing the results */
const char *names[] = {"result_numeric", "result_numeric_vector",
"result_integer", ""}; /* note the null string */
SEXP res = PROTECT(mkNamed(VECSXP, names)); /* list of length 3 */
SET_VECTOR_ELT(res, 0, ScalarReal(a)); /* numeric(1) */
SET_VECTOR_ELT(res, 1, somenumericvector); /* numeric(<some length>) */
SET_VECTOR_ELT(res, 2, ScalarInteger(i)); /* integer(1) */
UNPROTECT(1);
return res;
Since you ask for the plain vanilla way, you need to create an R character vector from the C strings using mkChar and SET_STRING_ELT:
for(i = 0; i < 3; i++) SET_STRING_ELT(nms, i, mkChar(names[i]));
Right now, you are trying to use the raw C string as an R object, which won't work.
Re:debugging, you can use PrintValue in C code to print out R objects.
All this said, unless you have a very specific reason for wanting plain vanilla, you should consider Rcpp.
Per #nrussell's good advice, a solution in a single statement (broken over four lines for legibility)
R> cppFunction('List marius(double a, NumericVector b, int c) \
{ return List::create(Named("resnum")=a,\
Named("resvec")=b, \
Named("resint")=c); }')
R> marius(1.2, sqrt(3:5), 42L)
$resnum
[1] 1.2
$resvec
[1] 1.73205 2.00000 2.23607
$resint
[1] 42
R>

How to define a dynamic string data type in postgres extension?

I am trying to define a custom data type in postgreSQL that takes two arguments: a long long int (int8 in postgres) and a dynamic string (varchar or TEXT in postgres). I am able to get the long long int working but I am having troubles to implement the dynamic string. This is what I have in my c code for the in and out functions:
In function:
Datum object3d_in(PG_FUNCTION_ARGS) {
char* str = PG_GETARG_CSTRING(0);
long long int timeStamp;
char *temp;
Object3d *result;
if (sscanf(str, "(%lli, %s)", &timeStamp, temp) != 2)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("Invalid input syntax for object3d: \"%s\"",
str)));
result = (Object3d *) palloc(sizeof(Object3d));
result->timeStamp = timeStamp;
result->object = (char*) palloc(sizeof(char)*(strlen(temp) + 1));
sscanf(str, "(%lli, %s)", &timeStamp, result->object); //Reload
PG_RETURN_POINTER(result);
}
Out function:
Datum object3d_out(PG_FUNCTION_ARGS) {
Object3d *object3d = (Object3d *) PG_GETARG_POINTER(0);
char *result;
result = (char *) palloc(128);
snprintf(result, 128, "[%lli, %s]", object3d->timeStamp, object3d->object);
PG_RETURN_CSTRING(result);
}
postgres type commands:
CREATE TYPE object3d;
CREATE FUNCTION object3d_in(cstring)
RETURNS object3d
AS 'mdobject.dll'
LANGUAGE C IMMUTABLE STRICT;
CREATE FUNCTION object3d_out(object3d)
RETURNS cstring
AS 'mdobject.dll'
LANGUAGE C IMMUTABLE STRICT;
CREATE TYPE object3d(
INTERNALLENGTH = 128,
input = object3d_in,
output = object3d_out
);
CREATE TABLE listobject3d (id integer, theobject object3d);
INSERT INTO listobject3d VALUES (random()*100, '(203,12 )');
INSERT INTO listobject3d VALUES (random()*100, '(20120202,r )');
INSERT INTO listobject3d VALUES (random()*100, '(20120203,c )');
INSERT INTO listobject3d VALUES (random()*100, '(20120203,triangle )');
Output:
SELECT * FROM listobject3d;
id | theobject
----+-------------------
21 | [203, 12]
42 | [20120202, /\x01]
19 | [20120203, /\x01]
33 | [20120203,     ]
(4 rows)
result data should be in varlena format - etc first four bytes should to carry complete length.
http://www.iphelp.ru/faq/15/ch06lev1sec1.html

Sybase error while fetching columns more then 255 characters

I'm having problems while fetching data from columns which have more than 255 characters
I got such an error message:
Open Client Message:
Message number: LAYER = (1) ORIGIN = (4) SEVERITY = (1) NUMBER = (132)
Message String: ct_fetch(): user api layer: internal common library error: The bind of result set item 3 resulted in truncation.
It fetches only the first 255 rows and truncates the rest.
I have tried to imply below lines before ct_connect but didn't work
CS_BOOL boolv = CS_TRUE;
CS_RETCODE retcode2 = ct_capability ( *connection, CS_GET, CS_CAP_REQUEST, CS_WIDETABLES, &boolv);
here are some part of the code , do you have any suggestion
for (i = 0; i < num_cols; i++) {
/*
** Get the column description. ct_describe() fills the
** datafmt parameter with a description of the column.
*/
retcode = ct_describe(cmd, (i + 1), &datafmt[i]);
if (retcode != CS_SUCCEED) {
ex_error("ex_fetch_data: ct_describe() failed");
break;
}
/*
** update the datafmt structure to indicate that we want the
** results in a null terminated character string.
**
** First, update datafmt.maxlength to contain the maximum
** possible length of the column. To do this, call
** ex_display_len() to determine the number of bytes needed
** for the character string representation, given the
** datatype described above. Add one for the null
** termination character.
*/
datafmt[i].maxlength = ex_display_dlen(&datafmt[i]) + 1;
/*
** Set datatype and format to tell bind we want things
** converted to null terminated strings
*/
datafmt[i].datatype = CS_LONGCHAR_TYPE;
datafmt[i].format = CS_FMT_NULLTERM;
/*
** Allocate memory for the column string
*/
coldata[i].value = (CS_CHAR *) malloc(datafmt[i].maxlength);
if (coldata[i].value == NULL) {
ex_error("ex_fetch_data: malloc() failed");
retcode = CS_MEM_ERROR;
break;
}
/*
** Now bind.
*/
retcode = ct_bind(cmd, (i + 1), &datafmt[i], coldata[i].value,
&coldata[i].valuelen, (CS_SMALLINT *) &coldata[i].indicator);
if (retcode != CS_SUCCEED) {
ex_error("ex_fetch_data: ct_bind() failed");
break;
}
}
.............
.............
.............
/*
** Fetch the rows. Loop while ct_fetch() returns CS_SUCCEED or
** CS_ROW_FAIL
*/
while (((retcode = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED,
&rows_read)) == CS_SUCCEED) || (retcode == CS_ROW_FAIL)) {
Even we have faced problem when we are using Sybase with Uniface,but uniface sybase driver has got a option which will truncate the data and save in some other table but while fetching we have to fetch data from all the tables.
When using the native jconn*.jar drivers you have to set the "?CHARSET=iso_1" parameter on the JDBC driver to connect to a Sybase server with a default charset of Roman 8, otherwise you see this 255 characters truncation issue.
Could this be the problem you are experiencing?

Resources