pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #13617: ecpg cannot handle boolean field within a structure
3+ messages / 3 participants
[nested] [flat]
* BUG #13617: ecpg cannot handle boolean field within a structure
@ 2015-09-13 16:32 yoonghm@gmail.com
0 siblings, 1 reply; 3+ messages in thread
From: yoonghm@gmail.com @ 2015-09-13 16:32 UTC (permalink / raw)
To: pgsql-bugs
The following bug has been logged on the website:
Bug reference: 13617
Logged by: Yoong Hor Meng
Email address: yoonghm@gmail.com
PostgreSQL version: 9.4.4
Operating system: Linux
Description:
// Compile the source code
#include <stdio.h>
#include <stdlib.h>
#include <ecpglib.h>
/*
psql << "EOF"
#
# Create database 'todo' and table 'items'. Fill the table.
#
CREATE DATABASE todo;
\c todo
CREATE TABLE items
(
id serial PRIMARY KEY,
task VARCHAR(40) NOT NULL,
complete boolean
);
INSERT INTO items (id, task, complete) VALUES (1, 'task1', true);
INSERT INTO items (id, task, complete) VALUES (2, 'task2', true);
INSERT INTO items (id, task, complete) VALUES (3, 'task3', true);
INSERT INTO items (id, task, complete) VALUES (4, 'task4', true);
INSERT INTO items (id, task, complete) VALUES (5, 'task5', false);
INSERT INTO items (id, task, complete) VALUES (6, 'task6', false);
EOF
*/
int
main(void)
{
#if ENABLE_DEBUG
ECPGdebug(1, stderr);
#endif
EXEC SQL WHENEVER SQLERROR sqlprint;
EXEC SQL BEGIN DECLARE SECTION;
/* Use structure as host variable */
typedef struct {
int id;
char task[40];
bool complete;
} item_t;
item_t item;
/* Use individual variables as host variables */
int id;
char task[40];
bool complete;
EXEC SQL END DECLARE SECTION;
memset(&item, 0, sizeof(item_t));
EXEC SQL CONNECT TO todo;
/*
* Use structure as host variable
*/
EXEC SQL DECLARE cur1 CURSOR FOR
SELECT id, task, complete
FROM items;
EXEC SQL OPEN cur1;
printf("sizeof(item_t) = %ld\n", sizeof(item));
printf(" sizeof(item.int) = %ld\n", sizeof(item.id));
printf(" sizeof(item.task) = %ld\n", sizeof(item.task));
printf(" sizeof(item.complete) = %ld\n", sizeof(item.complete));
printf("\n"
"Using structure variable\n"
"------------------------\n");
EXEC SQL WHENEVER NOT FOUND DO BREAK;
while (1)
{
EXEC SQL FETCH FROM cur1 INTO :item;
printf("id=%d, task=%s, complete=%d\n\n",
item.id, item.task, item.complete);
}
EXEC SQL CLOSE cur1;
/*
* Use individual variables as host variables
*/
EXEC SQL DECLARE cur2 CURSOR FOR
SELECT id, task, complete
FROM items;
EXEC SQL OPEN cur2;
printf("sizeof(int) = %ld\n", sizeof(id));
printf("sizeof(task) = %ld\n", sizeof(task));
printf("sizeof(complete) = %ld\n", sizeof(complete));
printf("\n"
"Using individual variables\n"
"--------------------------\n");
EXEC SQL WHENEVER NOT FOUND DO BREAK;
while (1)
{
EXEC SQL FETCH FROM cur2 INTO :id, :task, :complete;
printf("id=%d, task=%s, complete=%d\n",
id, task, complete);
}
EXEC SQL CLOSE cur2;
EXEC SQL DISCONNECT ALL;
return 0;
}
/* Create the database todo and table items as in the source code. Insert
records too.
*/
/*
sizeof(item_t) = 48
sizeof(item.int) = 4
sizeof(item.task) = 40
sizeof(item.complete) = 1
Using structure variable
------------------------
SQL error: could not convert boolean value: size mismatch, on line 80
id=1, task=task1, complete=0
SQL error: could not convert boolean value: size mismatch, on line 80
id=2, task=task2, complete=0
SQL error: could not convert boolean value: size mismatch, on line 80
id=3, task=task3, complete=0
SQL error: could not convert boolean value: size mismatch, on line 80
id=4, task=task4, complete=0
SQL error: could not convert boolean value: size mismatch, on line 80
id=5, task=task5, complete=0
SQL error: could not convert boolean value: size mismatch, on line 80
id=6, task=task6, complete=0
sizeof(int) = 4
sizeof(task) = 40
sizeof(complete) = 1
Using individual variables
--------------------------
id=1, task=task1, complete=1
id=2, task=task2, complete=1
id=3, task=task3, complete=1
id=4, task=task4, complete=1
id=5, task=task5, complete=0
id=6, task=task6, complete=0
*/
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: BUG #13617: ecpg cannot handle boolean field within a structure
@ 2015-09-17 13:46 Michael Meskes <meskes@postgresql.org>
parent: yoonghm@gmail.com
0 siblings, 1 reply; 3+ messages in thread
From: Michael Meskes @ 2015-09-17 13:46 UTC (permalink / raw)
To: yoonghm@gmail.com; +Cc: pgsql-bugs
On Sun, Sep 13, 2015 at 04:32:51PM +0000, yoonghm@gmail.com wrote:
> The following bug has been logged on the website:
> ...
Thanks for spotting. The bug was in an area with really old code, that
shouldn't be needed anymore. I hope the attached patch fixes it. I also
committed this to HEAD. Can you please try and report back?
Michael
--
Michael Meskes
Michael at Fam-Meskes dot De, Michael at Meskes dot (De|Com|Net|Org)
Meskes at (Debian|Postgresql) dot Org
Jabber: michael.meskes at gmail dot com
VfL Borussia! Força Barça! Go SF 49ers! Use Debian GNU/Linux, PostgreSQL
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Attachments:
ecpg.diff (text/x-diff, 2.7K) download
commit 293fd7c77e40cdf06b7a9eb6db95379be8869b04
Author: Michael Meskes <meskes@postgresql.org>
Date: Thu Sep 17 15:41:04 2015 +0200
Let compiler handle size calculation of bool types.
Back in the day this did not work, but modern compilers should handle it themselves.
diff --git a/src/interfaces/ecpg/ecpglib/data.c b/src/interfaces/ecpg/ecpglib/data.c
index 8d36484..82ab4aa 100644
--- a/src/interfaces/ecpg/ecpglib/data.c
+++ b/src/interfaces/ecpg/ecpglib/data.c
@@ -423,27 +423,13 @@ ecpg_get_data(const PGresult *results, int act_tuple, int act_field, int lineno,
case ECPGt_bool:
if (pval[0] == 'f' && pval[1] == '\0')
{
- if (offset == sizeof(char))
- *((char *) (var + offset * act_tuple)) = false;
- else if (offset == sizeof(int))
- *((int *) (var + offset * act_tuple)) = false;
- else
- ecpg_raise(lineno, ECPG_CONVERT_BOOL,
- ECPG_SQLSTATE_DATATYPE_MISMATCH,
- NULL);
+ *((bool *) (var + offset * act_tuple)) = false;
pval++;
break;
}
else if (pval[0] == 't' && pval[1] == '\0')
{
- if (offset == sizeof(char))
- *((char *) (var + offset * act_tuple)) = true;
- else if (offset == sizeof(int))
- *((int *) (var + offset * act_tuple)) = true;
- else
- ecpg_raise(lineno, ECPG_CONVERT_BOOL,
- ECPG_SQLSTATE_DATATYPE_MISMATCH,
- NULL);
+ *((bool *) (var + offset * act_tuple)) = true;
pval++;
break;
}
diff --git a/src/interfaces/ecpg/ecpglib/execute.c b/src/interfaces/ecpg/ecpglib/execute.c
index 9e40f41..3b6eedb 100644
--- a/src/interfaces/ecpg/ecpglib/execute.c
+++ b/src/interfaces/ecpg/ecpglib/execute.c
@@ -752,18 +752,9 @@ ecpg_store_input(const int lineno, const bool force_indicator, const struct vari
{
strcpy(mallocedval, "{");
- if (var->offset == sizeof(char))
- for (element = 0; element < asize; element++)
- sprintf(mallocedval + strlen(mallocedval), "%c,", (((char *) var->value)[element]) ? 't' : 'f');
+ for (element = 0; element < asize; element++)
+ sprintf(mallocedval + strlen(mallocedval), "%c,", (((bool *) var->value)[element]) ? 't' : 'f');
- /*
- * this is necessary since sizeof(C++'s bool)==sizeof(int)
- */
- else if (var->offset == sizeof(int))
- for (element = 0; element < asize; element++)
- sprintf(mallocedval + strlen(mallocedval), "%c,", (((int *) var->value)[element]) ? 't' : 'f');
- else
- ecpg_raise(lineno, ECPG_CONVERT_BOOL, ECPG_SQLSTATE_DATATYPE_MISMATCH, NULL);
strcpy(mallocedval + strlen(mallocedval) - 1, "}");
}
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: BUG #13617: ecpg cannot handle boolean field within a structure
@ 2015-09-17 17:52 Hor Meng Yoong <yoonghm@gmail.com>
parent: Michael Meskes <meskes@postgresql.org>
0 siblings, 0 replies; 3+ messages in thread
From: Hor Meng Yoong @ 2015-09-17 17:52 UTC (permalink / raw)
To: Michael Meskes <meskes@postgresql.org>; +Cc: pgsql-bugs
Dear Michael:
It is working now. Thank.
On Thu, Sep 17, 2015 at 9:46 PM, Michael Meskes <meskes@postgresql.org>
wrote:
> On Sun, Sep 13, 2015 at 04:32:51PM +0000, yoonghm@gmail.com wrote:
> > The following bug has been logged on the website:
> > ...
>
> Thanks for spotting. The bug was in an area with really old code, that
> shouldn't be needed anymore. I hope the attached patch fixes it. I also
> committed this to HEAD. Can you please try and report back?
>
> Michael
> --
> Michael Meskes
> Michael at Fam-Meskes dot De, Michael at Meskes dot (De|Com|Net|Org)
> Meskes at (Debian|Postgresql) dot Org
> Jabber: michael.meskes at gmail dot com
> VfL Borussia! Força Barça! Go SF 49ers! Use Debian GNU/Linux, PostgreSQL
>
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2015-09-17 17:52 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2015-09-13 16:32 BUG #13617: ecpg cannot handle boolean field within a structure yoonghm@gmail.com
2015-09-17 13:46 ` Michael Meskes <meskes@postgresql.org>
2015-09-17 17:52 ` Hor Meng Yoong <yoonghm@gmail.com>
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