Skip to content

Commit 2d3c691

Browse files
author
Michael Meskes
committed
Fixed auto allocation for binary data types.
1 parent 77ca045 commit 2d3c691

File tree

6 files changed

+157
-85
lines changed

6 files changed

+157
-85
lines changed

src/interfaces/ecpg/ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2409,6 +2409,7 @@ Wed, 17 Dec 2008 17:49:11 +0100
24092409
Mon, 02 Feb 2009 16:34:53 +0100
24102410

24112411
- Fixed bug in handling of "%s" pattern in PGTYPEStimestamp_defmt_asc().
2412+
- Fixed auto allocation for binary data types.
24122413
- Set pgtypes library version to 3.1.
24132414
- Set compat library version to 3.1.
24142415
- Set ecpg library version to 6.2.

src/interfaces/ecpg/ecpglib/execute.c

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/execute.c,v 1.79 2009/01/15 11:52:55 petere Exp $ */
1+
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/execute.c,v 1.80 2009/02/02 16:14:06 meskes Exp $ */
22

33
/*
44
* The aim is to get a simpler inteface to the database routines.
@@ -353,40 +353,45 @@ ecpg_store_result(const PGresult *results, int act_field,
353353
{
354354
int len = 0;
355355

356-
switch (var->type)
356+
if (!PQfformat(results, act_field))
357357
{
358-
case ECPGt_char:
359-
case ECPGt_unsigned_char:
360-
if (!var->varcharsize && !var->arrsize)
361-
{
362-
/* special mode for handling char**foo=0 */
363-
for (act_tuple = 0; act_tuple < ntuples; act_tuple++)
364-
len += strlen(PQgetvalue(results, act_tuple, act_field)) + 1;
365-
len *= var->offset; /* should be 1, but YMNK */
366-
len += (ntuples + 1) * sizeof(char *);
367-
}
368-
else
369-
{
370-
var->varcharsize = 0;
371-
/* check strlen for each tuple */
372-
for (act_tuple = 0; act_tuple < ntuples; act_tuple++)
358+
switch (var->type)
359+
{
360+
case ECPGt_char:
361+
case ECPGt_unsigned_char:
362+
if (!var->varcharsize && !var->arrsize)
363+
{
364+
/* special mode for handling char**foo=0 */
365+
for (act_tuple = 0; act_tuple < ntuples; act_tuple++)
366+
len += strlen(PQgetvalue(results, act_tuple, act_field)) + 1;
367+
len *= var->offset; /* should be 1, but YMNK */
368+
len += (ntuples + 1) * sizeof(char *);
369+
}
370+
else
373371
{
374-
int len = strlen(PQgetvalue(results, act_tuple, act_field)) + 1;
372+
var->varcharsize = 0;
373+
/* check strlen for each tuple */
374+
for (act_tuple = 0; act_tuple < ntuples; act_tuple++)
375+
{
376+
int len = strlen(PQgetvalue(results, act_tuple, act_field)) + 1;
375377

376-
if (len > var->varcharsize)
377-
var->varcharsize = len;
378+
if (len > var->varcharsize)
379+
var->varcharsize = len;
380+
}
381+
var->offset *= var->varcharsize;
382+
len = var->offset * ntuples;
378383
}
379-
var->offset *= var->varcharsize;
384+
break;
385+
case ECPGt_varchar:
386+
len = ntuples * (var->varcharsize + sizeof(int));
387+
break;
388+
default:
380389
len = var->offset * ntuples;
381-
}
382-
break;
383-
case ECPGt_varchar:
384-
len = ntuples * (var->varcharsize + sizeof(int));
385-
break;
386-
default:
387-
len = var->offset * ntuples;
388-
break;
390+
break;
391+
}
389392
}
393+
else
394+
len = PQgetlength(results, act_tuple, act_field);
390395
ecpg_log("ecpg_store_result on line %d: allocating memory for %d tuples\n", stmt->lineno, ntuples);
391396
var->value = (char *) ecpg_alloc(len, stmt->lineno);
392397
if (!var->value)

src/interfaces/ecpg/test/expected/sql-binary.c

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,26 @@ main (void)
5151
/* exec sql begin declare section */
5252

5353

54+
5455

5556
#line 20 "binary.pgc"
5657
struct TBempl empl ;
5758

5859
#line 21 "binary.pgc"
60+
char * pointer = NULL ;
61+
62+
#line 22 "binary.pgc"
5963
char * data = "\\001\\155\\000\\212" ;
6064
/* exec sql end declare section */
61-
#line 22 "binary.pgc"
65+
#line 23 "binary.pgc"
6266

6367
int i;
6468

6569
ECPGdebug (1, stderr);
6670

6771
empl.idnum = 1;
6872
{ ECPGconnect(__LINE__, 0, "regress1" , NULL, NULL , NULL, 0); }
69-
#line 28 "binary.pgc"
73+
#line 29 "binary.pgc"
7074

7175
if (sqlca.sqlcode)
7276
{
@@ -75,7 +79,7 @@ main (void)
7579
}
7680

7781
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "create table empl ( idnum integer , name char ( 20 ) , accs smallint , byte bytea )", ECPGt_EOIT, ECPGt_EORT);}
78-
#line 36 "binary.pgc"
82+
#line 37 "binary.pgc"
7983

8084
if (sqlca.sqlcode)
8185
{
@@ -86,7 +90,7 @@ main (void)
8690
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into empl values ( 1 , 'first user' , 320 , $1 )",
8791
ECPGt_char,&(data),(long)0,(long)1,(1)*sizeof(char),
8892
ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);}
89-
#line 43 "binary.pgc"
93+
#line 44 "binary.pgc"
9094

9195
if (sqlca.sqlcode)
9296
{
@@ -95,12 +99,12 @@ main (void)
9599
}
96100

97101
/* declare C cursor for select name , accs , byte from empl where idnum = $1 */
98-
#line 50 "binary.pgc"
102+
#line 51 "binary.pgc"
99103

100104
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "declare C cursor for select name , accs , byte from empl where idnum = $1 ",
101105
ECPGt_long,&(empl.idnum),(long)1,(long)1,sizeof(long),
102106
ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);}
103-
#line 51 "binary.pgc"
107+
#line 52 "binary.pgc"
104108

105109
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "fetch C", ECPGt_EOIT,
106110
ECPGt_char,(empl.name),(long)21,(long)1,(21)*sizeof(char),
@@ -109,7 +113,7 @@ main (void)
109113
ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L,
110114
ECPGt_char,(empl.byte),(long)20,(long)1,(20)*sizeof(char),
111115
ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
112-
#line 52 "binary.pgc"
116+
#line 53 "binary.pgc"
113117

114118
if (sqlca.sqlcode)
115119
{
@@ -119,15 +123,13 @@ main (void)
119123

120124
printf ("name=%s, accs=%d byte=%s\n", empl.name, empl.accs, empl.byte);
121125

122-
memset(empl.name, 0, 21L);
123-
memset(empl.byte, '#', 20L);
124126
/* declare B binary cursor for select name , accs , byte from empl where idnum = $1 */
125-
#line 63 "binary.pgc"
127+
#line 62 "binary.pgc"
126128

127129
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "declare B binary cursor for select name , accs , byte from empl where idnum = $1 ",
128130
ECPGt_long,&(empl.idnum),(long)1,(long)1,sizeof(long),
129131
ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);}
130-
#line 64 "binary.pgc"
132+
#line 63 "binary.pgc"
131133

132134
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "fetch B", ECPGt_EOIT,
133135
ECPGt_char,(empl.name),(long)21,(long)1,(21)*sizeof(char),
@@ -136,7 +138,7 @@ main (void)
136138
ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L,
137139
ECPGt_char,(empl.byte),(long)20,(long)1,(20)*sizeof(char),
138140
ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
139-
#line 65 "binary.pgc"
141+
#line 64 "binary.pgc"
140142

141143
if (sqlca.sqlcode)
142144
{
@@ -145,20 +147,46 @@ main (void)
145147
}
146148

147149
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "close B", ECPGt_EOIT, ECPGt_EORT);}
148-
#line 72 "binary.pgc"
150+
#line 71 "binary.pgc"
149151

150152

151153
/* do not print a.accs because big/little endian will have different outputs here */
152154
printf ("name=%s, byte=", empl.name);
153-
for (i=0; i<20; i++)
154-
{
155-
if (empl.byte[i] == '#')
156-
break;
155+
for (i=0; i<4; i++)
157156
printf("(%o)", (unsigned char)empl.byte[i]);
158-
}
159157
printf("\n");
158+
159+
/* declare A binary cursor for select byte from empl where idnum = $1 */
160+
#line 79 "binary.pgc"
161+
162+
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "declare A binary cursor for select byte from empl where idnum = $1 ",
163+
ECPGt_long,&(empl.idnum),(long)1,(long)1,sizeof(long),
164+
ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);}
165+
#line 80 "binary.pgc"
166+
167+
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "fetch A", ECPGt_EOIT,
168+
ECPGt_char,&(pointer),(long)0,(long)1,(1)*sizeof(char),
169+
ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
170+
#line 81 "binary.pgc"
171+
172+
if (sqlca.sqlcode)
173+
{
174+
printf ("fetch error = %ld\n", sqlca.sqlcode);
175+
exit (sqlca.sqlcode);
176+
}
177+
178+
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "close A", ECPGt_EOIT, ECPGt_EORT);}
179+
#line 88 "binary.pgc"
180+
181+
182+
printf ("pointer=");
183+
for (i=0; i<4; i++)
184+
printf("(%o)", (unsigned char)pointer[i]);
185+
printf("\n");
186+
free(pointer);
187+
160188
{ ECPGdisconnect(__LINE__, "CURRENT");}
161-
#line 83 "binary.pgc"
189+
#line 96 "binary.pgc"
162190

163191
exit (0);
164192
}

src/interfaces/ecpg/test/expected/sql-binary.stderr

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,89 @@
22
[NO_PID]: sqlca: code: 0, state: 00000
33
[NO_PID]: ECPGconnect: opening database regress1 on <DEFAULT> port <DEFAULT>
44
[NO_PID]: sqlca: code: 0, state: 00000
5-
[NO_PID]: ecpg_execute on line 35: query: create table empl ( idnum integer , name char ( 20 ) , accs smallint , byte bytea ); with 0 parameter(s) on connection regress1
5+
[NO_PID]: ecpg_execute on line 36: query: create table empl ( idnum integer , name char ( 20 ) , accs smallint , byte bytea ); with 0 parameter(s) on connection regress1
66
[NO_PID]: sqlca: code: 0, state: 00000
7-
[NO_PID]: ecpg_execute on line 35: using PQexec
7+
[NO_PID]: ecpg_execute on line 36: using PQexec
88
[NO_PID]: sqlca: code: 0, state: 00000
9-
[NO_PID]: ecpg_execute on line 35: OK: CREATE TABLE
9+
[NO_PID]: ecpg_execute on line 36: OK: CREATE TABLE
1010
[NO_PID]: sqlca: code: 0, state: 00000
11-
[NO_PID]: ecpg_execute on line 43: query: insert into empl values ( 1 , 'first user' , 320 , $1 ); with 1 parameter(s) on connection regress1
11+
[NO_PID]: ecpg_execute on line 44: query: insert into empl values ( 1 , 'first user' , 320 , $1 ); with 1 parameter(s) on connection regress1
1212
[NO_PID]: sqlca: code: 0, state: 00000
13-
[NO_PID]: ecpg_execute on line 43: using PQexecParams
13+
[NO_PID]: ecpg_execute on line 44: using PQexecParams
1414
[NO_PID]: sqlca: code: 0, state: 00000
15-
[NO_PID]: free_params on line 43: parameter 1 = \001\155\000\212
15+
[NO_PID]: free_params on line 44: parameter 1 = \001\155\000\212
1616
[NO_PID]: sqlca: code: 0, state: 00000
17-
[NO_PID]: ecpg_execute on line 43: OK: INSERT 0 1
17+
[NO_PID]: ecpg_execute on line 44: OK: INSERT 0 1
1818
[NO_PID]: sqlca: code: 0, state: 00000
19-
[NO_PID]: ecpg_execute on line 51: query: declare C cursor for select name , accs , byte from empl where idnum = $1 ; with 1 parameter(s) on connection regress1
19+
[NO_PID]: ecpg_execute on line 52: query: declare C cursor for select name , accs , byte from empl where idnum = $1 ; with 1 parameter(s) on connection regress1
2020
[NO_PID]: sqlca: code: 0, state: 00000
21-
[NO_PID]: ecpg_execute on line 51: using PQexecParams
21+
[NO_PID]: ecpg_execute on line 52: using PQexecParams
2222
[NO_PID]: sqlca: code: 0, state: 00000
23-
[NO_PID]: free_params on line 51: parameter 1 = 1
23+
[NO_PID]: free_params on line 52: parameter 1 = 1
2424
[NO_PID]: sqlca: code: 0, state: 00000
25-
[NO_PID]: ecpg_execute on line 51: OK: DECLARE CURSOR
25+
[NO_PID]: ecpg_execute on line 52: OK: DECLARE CURSOR
2626
[NO_PID]: sqlca: code: 0, state: 00000
27-
[NO_PID]: ecpg_execute on line 52: query: fetch C; with 0 parameter(s) on connection regress1
27+
[NO_PID]: ecpg_execute on line 53: query: fetch C; with 0 parameter(s) on connection regress1
2828
[NO_PID]: sqlca: code: 0, state: 00000
29-
[NO_PID]: ecpg_execute on line 52: using PQexec
29+
[NO_PID]: ecpg_execute on line 53: using PQexec
3030
[NO_PID]: sqlca: code: 0, state: 00000
31-
[NO_PID]: ecpg_execute on line 52: correctly got 1 tuples with 3 fields
31+
[NO_PID]: ecpg_execute on line 53: correctly got 1 tuples with 3 fields
3232
[NO_PID]: sqlca: code: 0, state: 00000
33-
[NO_PID]: ecpg_get_data on line 52: RESULT: first user offset: -1; array: yes
33+
[NO_PID]: ecpg_get_data on line 53: RESULT: first user offset: -1; array: yes
3434
[NO_PID]: sqlca: code: 0, state: 00000
35-
[NO_PID]: ecpg_get_data on line 52: RESULT: 320 offset: -1; array: yes
35+
[NO_PID]: ecpg_get_data on line 53: RESULT: 320 offset: -1; array: yes
3636
[NO_PID]: sqlca: code: 0, state: 00000
37-
[NO_PID]: ecpg_get_data on line 52: RESULT: \001m\000\212 offset: -1; array: yes
37+
[NO_PID]: ecpg_get_data on line 53: RESULT: \001m\000\212 offset: -1; array: yes
3838
[NO_PID]: sqlca: code: 0, state: 00000
39-
[NO_PID]: ecpg_execute on line 64: query: declare B binary cursor for select name , accs , byte from empl where idnum = $1 ; with 1 parameter(s) on connection regress1
39+
[NO_PID]: ecpg_execute on line 63: query: declare B binary cursor for select name , accs , byte from empl where idnum = $1 ; with 1 parameter(s) on connection regress1
4040
[NO_PID]: sqlca: code: 0, state: 00000
41-
[NO_PID]: ecpg_execute on line 64: using PQexecParams
41+
[NO_PID]: ecpg_execute on line 63: using PQexecParams
4242
[NO_PID]: sqlca: code: 0, state: 00000
43-
[NO_PID]: free_params on line 64: parameter 1 = 1
43+
[NO_PID]: free_params on line 63: parameter 1 = 1
4444
[NO_PID]: sqlca: code: 0, state: 00000
45-
[NO_PID]: ecpg_execute on line 64: OK: DECLARE CURSOR
45+
[NO_PID]: ecpg_execute on line 63: OK: DECLARE CURSOR
4646
[NO_PID]: sqlca: code: 0, state: 00000
47-
[NO_PID]: ecpg_execute on line 65: query: fetch B; with 0 parameter(s) on connection regress1
47+
[NO_PID]: ecpg_execute on line 64: query: fetch B; with 0 parameter(s) on connection regress1
4848
[NO_PID]: sqlca: code: 0, state: 00000
49-
[NO_PID]: ecpg_execute on line 65: using PQexec
49+
[NO_PID]: ecpg_execute on line 64: using PQexec
5050
[NO_PID]: sqlca: code: 0, state: 00000
51-
[NO_PID]: ecpg_execute on line 65: correctly got 1 tuples with 3 fields
51+
[NO_PID]: ecpg_execute on line 64: correctly got 1 tuples with 3 fields
5252
[NO_PID]: sqlca: code: 0, state: 00000
53-
[NO_PID]: ecpg_get_data on line 65: RESULT: BINARY offset: -1; array: yes
53+
[NO_PID]: ecpg_get_data on line 64: RESULT: BINARY offset: -1; array: yes
5454
[NO_PID]: sqlca: code: 0, state: 00000
55-
[NO_PID]: ecpg_get_data on line 65: RESULT: BINARY offset: -1; array: yes
55+
[NO_PID]: ecpg_get_data on line 64: RESULT: BINARY offset: -1; array: yes
5656
[NO_PID]: sqlca: code: 0, state: 00000
57-
[NO_PID]: ecpg_get_data on line 65: RESULT: BINARY offset: -1; array: yes
57+
[NO_PID]: ecpg_get_data on line 64: RESULT: BINARY offset: -1; array: yes
5858
[NO_PID]: sqlca: code: 0, state: 00000
59-
[NO_PID]: ecpg_execute on line 72: query: close B; with 0 parameter(s) on connection regress1
59+
[NO_PID]: ecpg_execute on line 71: query: close B; with 0 parameter(s) on connection regress1
6060
[NO_PID]: sqlca: code: 0, state: 00000
61-
[NO_PID]: ecpg_execute on line 72: using PQexec
61+
[NO_PID]: ecpg_execute on line 71: using PQexec
6262
[NO_PID]: sqlca: code: 0, state: 00000
63-
[NO_PID]: ecpg_execute on line 72: OK: CLOSE CURSOR
63+
[NO_PID]: ecpg_execute on line 71: OK: CLOSE CURSOR
64+
[NO_PID]: sqlca: code: 0, state: 00000
65+
[NO_PID]: ecpg_execute on line 80: query: declare A binary cursor for select byte from empl where idnum = $1 ; with 1 parameter(s) on connection regress1
66+
[NO_PID]: sqlca: code: 0, state: 00000
67+
[NO_PID]: ecpg_execute on line 80: using PQexecParams
68+
[NO_PID]: sqlca: code: 0, state: 00000
69+
[NO_PID]: free_params on line 80: parameter 1 = 1
70+
[NO_PID]: sqlca: code: 0, state: 00000
71+
[NO_PID]: ecpg_execute on line 80: OK: DECLARE CURSOR
72+
[NO_PID]: sqlca: code: 0, state: 00000
73+
[NO_PID]: ecpg_execute on line 81: query: fetch A; with 0 parameter(s) on connection regress1
74+
[NO_PID]: sqlca: code: 0, state: 00000
75+
[NO_PID]: ecpg_execute on line 81: using PQexec
76+
[NO_PID]: sqlca: code: 0, state: 00000
77+
[NO_PID]: ecpg_execute on line 81: correctly got 1 tuples with 1 fields
78+
[NO_PID]: sqlca: code: 0, state: 00000
79+
[NO_PID]: ecpg_store_result on line 81: allocating memory for 1 tuples
80+
[NO_PID]: sqlca: code: 0, state: 00000
81+
[NO_PID]: ecpg_get_data on line 81: RESULT: BINARY offset: -1; array: yes
82+
[NO_PID]: sqlca: code: 0, state: 00000
83+
[NO_PID]: ecpg_execute on line 88: query: close A; with 0 parameter(s) on connection regress1
84+
[NO_PID]: sqlca: code: 0, state: 00000
85+
[NO_PID]: ecpg_execute on line 88: using PQexec
86+
[NO_PID]: sqlca: code: 0, state: 00000
87+
[NO_PID]: ecpg_execute on line 88: OK: CLOSE CURSOR
6488
[NO_PID]: sqlca: code: 0, state: 00000
6589
[NO_PID]: ecpg_finish: connection regress1 closed
6690
[NO_PID]: sqlca: code: 0, state: 00000
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
name=first user , accs=320 byte=\001m\000\212
22
name=first user , byte=(1)(155)(0)(212)
3+
pointer=(1)(155)(0)(212)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy