Обсуждение: Pointer to structure in ECPG
Hi All,
I tried to use pointer to array to fetch results of a query. The test case test_select.pgc is attached. Changes specific to one's environment are needed before that test can be tried. Otherwise, you may try file pointer_to_struct.pgc in the patch attached, by putting it ecpg/test directory../test_select
SQL error: SQL error -203 on line 40
empno=-163754450, ename=, job=
empno=634004672, ename=�, job=
empno=634004624, ename=�, job=
This happens because of two problems
1. When dumping individual members of structure emp in test_select.pgc, when a pointer to that structure is dumped, its array size is dumped as "1" instead of "0" or a negative number like scalar variable pointers.
2. As discussed in other thread "using arrays within structure in ECPG", the offsets of members of structures are not dumped correctly for an unbounded array.
Problem 1 reason:
-------------------------
When dumping individual members of structure emp in test_select.pgc, when a pointer to that structure is dumped, the call is routed through code path as shown below
#0 ECPGdump_a_type (o=0x6cc4f0, name=0x6d2190 "empno", type=0x6d2130, brace_level=-1, ind_name=0x4c0ae8 "no_indicator", ind_type=0x6c8260, ind_brace_l
evel=-1, prefix=0x6d1a20 "emp2->", ind_prefix=0x0, arr_str_siz=0x6d2070 "0", struct_sizeof=0x6d0c60 "sizeof( struct employee )", ind_struct_sizeof=0x0)
at type.c:247
#1 0x000000000042f21d in ECPGdump_a_struct (o=0x6cc4f0, name=0x6d20b0 "emp2", ind_name=0x435c20 "no_indicator", arrsiz=0x6d2070 "0", type=0x6d20d0, in
d_type=0x6c8260, prefix=0x6d1a20 "emp2->", ind_prefix=0x0) at type.c:572
#2 0x000000000042e2cc in ECPGdump_a_type (o=0x6cc4f0, name=0x6d20b0 "emp2", type=0x6d22b0, brace_level=1, ind_name=0x435c20 "no_indicator", ind_type=0
x6c8260, ind_brace_level=0, prefix=0x0, ind_prefix=0x0, arr_str_siz=0x6d1e60 "0", struct_sizeof=0x0, ind_struct_sizeof=0x0) at type.c:293
#3 0x000000000043335d in dump_variables (list=0x6d2310, mode=1) at variable.c:452
-------------------------
When dumping individual members of structure emp in test_select.pgc, when a pointer to that structure is dumped, the call is routed through code path as shown below
#0 ECPGdump_a_type (o=0x6cc4f0, name=0x6d2190 "empno", type=0x6d2130, brace_level=-1, ind_name=0x4c0ae8 "no_indicator", ind_type=0x6c8260, ind_brace_l
evel=-1, prefix=0x6d1a20 "emp2->", ind_prefix=0x0, arr_str_siz=0x6d2070 "0", struct_sizeof=0x6d0c60 "sizeof( struct employee )", ind_struct_sizeof=0x0)
at type.c:247
#1 0x000000000042f21d in ECPGdump_a_struct (o=0x6cc4f0, name=0x6d20b0 "emp2", ind_name=0x435c20 "no_indicator", arrsiz=0x6d2070 "0", type=0x6d20d0, in
d_type=0x6c8260, prefix=0x6d1a20 "emp2->", ind_prefix=0x0) at type.c:572
#2 0x000000000042e2cc in ECPGdump_a_type (o=0x6cc4f0, name=0x6d20b0 "emp2", type=0x6d22b0, brace_level=1, ind_name=0x435c20 "no_indicator", ind_type=0
x6c8260, ind_brace_level=0, prefix=0x0, ind_prefix=0x0, arr_str_siz=0x6d1e60 "0", struct_sizeof=0x0, ind_struct_sizeof=0x0) at type.c:293
#3 0x000000000043335d in dump_variables (list=0x6d2310, mode=1) at variable.c:452
ECPGdump_a_type() at frame #3, then calls ECPGdump_a_simple() as follows
#0 ECPGdump_a_simple (o=0x6cc4f0, name=0x6d2190 "empno", type=ECPGt_int, varcharsize=0x6d0f70 "1", arrsize=0x6d2360 "-1", siz=0x6d0c60 "sizeof( struct
employee )", prefix=0x6d1a20 "emp2->", counter=0) at type.c:405
#0 ECPGdump_a_simple (o=0x6cc4f0, name=0x6d2190 "empno", type=ECPGt_int, varcharsize=0x6d0f70 "1", arrsize=0x6d2360 "-1", siz=0x6d0c60 "sizeof( struct
employee )", prefix=0x6d1a20 "emp2->", counter=0) at type.c:405
ECPGdump_a_simple() while dumping the member (here empno) dumps with arrsize = "1" instead of "-1" because of code at line
523│ if (atoi(arrsize) < 0)
524│ strcpy(arrsize, "1");
523│ if (atoi(arrsize) < 0)
524│ strcpy(arrsize, "1");
In ECPG run time library when reading this variable, it is read with array size 1 and is falsely interpreted as an array with size 1 instead of an unbounded array (which are read with arrsize < 0). Hence giving error -203.
ECPGdump_a_struct has correctly called ECPGdump_a_type with arrsize "0" to mean an unbounded array, and ECPGdump_a_type() has correctly converted it to "-1" again to differentiate between pointer to struct from pointer to scalar variable. This differentiation is important to dump pointer to struct and pointer to scalar variable differently in ECPGdump_a_simple(). So, in ECPGdump_a_simple() we should dump the variable with arrsize "-1" if it's part of an outer structure.
The patch attached, includes solutions to both the problems and also a testcase preproc/pointer_to_struct.pgc. This test is a copy of test preproc/array_of_struct.pgc with the structure arrays replaced by pointers to structures and memory allocations.The patch is based on development head. Please consider this to be backpatched to 9.3 as well.
--
Best Wishes,
Ashutosh Bapat
EnterpriseDB Corporation
The Postgres Database Company
Ashutosh Bapat
EnterpriseDB Corporation
The Postgres Database Company
Вложения
Thanks Ashutosh, both patches committed and backported to the whole 9.* series. Michael -- Michael Meskes Michael at Fam-Meskes dot De, Michael at Meskes dot (De|Com|Net|Org) Michael at BorussiaFan dot De, 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
<div dir="ltr">Thanks a lot Michael.<br /></div><div class="gmail_extra"><br /><br /><div class="gmail_quote">On Wed, Apr9, 2014 at 3:59 PM, Michael Meskes <span dir="ltr"><<a href="mailto:meskes@postgresql.org" target="_blank">meskes@postgresql.org</a>></span>wrote:<br /><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px#ccc solid;padding-left:1ex">Thanks Ashutosh,<br /><br /> both patches committed and backported to thewhole 9.* series.<br /><span class="HOEnZb"><font color="#888888"><br /> Michael<br /> --<br /> Michael Meskes<br /> Michaelat Fam-Meskes dot De, Michael at Meskes dot (De|Com|Net|Org)<br /> Michael at BorussiaFan dot De, Meskes at (Debian|Postgresql)dot Org<br /> Jabber: michael.meskes at gmail dot com<br /> VfL Borussia! Força Barça! Go SF 49ers! UseDebian GNU/Linux, PostgreSQL<br /></font></span></blockquote></div><br /><br clear="all" /><br />-- <br /><div dir="ltr">BestWishes,<br />Ashutosh Bapat<br />EnterpriseDB Corporation<br />The Postgres Database Company<br /></div></div>