Skip to content

Commit d19669e

Browse files
author
Michael Meskes
committed
Fixed auto-prepare to not try preparing statements that are not preparable. Bug
found and solved by Boszormenyi Zoltan <zb@cybertec.at>, some small adjustments by me.
1 parent dd4cd55 commit d19669e

13 files changed

+262
-135
lines changed

src/interfaces/ecpg/preproc/ecpg.addons

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/ecpg.addons,v 1.11 2009/11/27 16:07:22 meskes Exp $ */
1+
/* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/ecpg.addons,v 1.12 2009/12/16 10:15:06 meskes Exp $ */
22
ECPG: stmtClosePortalStmt block
33
{
44
if (INFORMIX_MODE)
@@ -26,13 +26,14 @@ ECPG: stmtDeallocateStmt block
2626
}
2727
ECPG: stmtDeclareCursorStmt block
2828
{ output_simple_statement($1); }
29-
ECPG: stmtDeleteStmt block
3029
ECPG: stmtDiscardStmt block
3130
ECPG: stmtFetchStmt block
31+
{ output_statement($1, 1, ECPGst_normal); }
32+
ECPG: stmtDeleteStmt block
3233
ECPG: stmtInsertStmt block
3334
ECPG: stmtSelectStmt block
3435
ECPG: stmtUpdateStmt block
35-
{ output_statement($1, 1, ECPGst_normal); }
36+
{ output_statement($1, 1, ECPGst_prepnormal); }
3637
ECPG: stmtExecuteStmt block
3738
{ output_statement($1, 1, ECPGst_execute); }
3839
ECPG: stmtPrepareStmt block
@@ -133,7 +134,7 @@ ECPG: stmtViewStmt rule
133134
if ((ptr = add_additional_variables($1, true)) != NULL)
134135
{
135136
connection = ptr->connection ? mm_strdup(ptr->connection) : NULL;
136-
output_statement(mm_strdup(ptr->command), 0, 0);
137+
output_statement(mm_strdup(ptr->command), 0, ECPGst_normal);
137138
ptr->opened = true;
138139
}
139140
}

src/interfaces/ecpg/preproc/output.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/output.c,v 1.25 2009/06/11 14:49:13 momjian Exp $ */
1+
/* $PostgreSQL: pgsql/src/interfaces/ecpg/preproc/output.c,v 1.26 2009/12/16 10:15:06 meskes Exp $ */
22

33
#include "postgres_fe.h"
44

@@ -105,23 +105,31 @@ hashline_number(void)
105105
return EMPTY;
106106
}
107107

108+
static char *ecpg_statement_type_name[] = {
109+
"ECPGst_normal",
110+
"ECPGst_execute",
111+
"ECPGst_exec_immediate",
112+
"ECPGst_prepnormal"
113+
};
114+
108115
void
109116
output_statement(char *stmt, int whenever_mode, enum ECPG_statement_type st)
110117
{
111-
112118
fprintf(yyout, "{ ECPGdo(__LINE__, %d, %d, %s, %d, ", compat, force_indicator, connection ? connection : "NULL", questionmarks);
113-
if (st == ECPGst_normal)
119+
if (st == ECPGst_execute || st == ECPGst_exec_immediate)
120+
{
121+
fprintf(yyout, "%s, %s, ", ecpg_statement_type_name[st], stmt);
122+
}
123+
else
114124
{
115-
if (auto_prepare)
125+
if (st == ECPGst_prepnormal && auto_prepare)
116126
fputs("ECPGst_prepnormal, \"", yyout);
117127
else
118128
fputs("ECPGst_normal, \"", yyout);
119129

120130
output_escaped_str(stmt, false);
121131
fputs("\", ", yyout);
122132
}
123-
else
124-
fprintf(yyout, "%d, %s, ", st, stmt);
125133

126134
/* dump variables to C file */
127135
dump_variables(argsinsert, 1);

src/interfaces/ecpg/test/expected/preproc-autoprep.c

Lines changed: 116 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -26,118 +26,114 @@
2626
int main() {
2727
/* exec sql begin declare section */
2828

29+
30+
2931

3032
#line 10 "autoprep.pgc"
3133
int item [ 4 ] , ind [ 4 ] , i = 1 ;
32-
/* exec sql end declare section */
34+
3335
#line 11 "autoprep.pgc"
36+
int item1 , ind1 ;
37+
38+
#line 12 "autoprep.pgc"
39+
char sqlstr [ 64 ] = "SELECT item2 FROM T ORDER BY item2 NULLS LAST" ;
40+
/* exec sql end declare section */
41+
#line 13 "autoprep.pgc"
3442

3543

3644
ECPGdebug(1, stderr);
3745
{ ECPGconnect(__LINE__, 0, "regress1" , NULL, NULL , NULL, 0); }
38-
#line 14 "autoprep.pgc"
46+
#line 16 "autoprep.pgc"
3947

4048

4149
/* exec sql whenever sql_warning sqlprint ; */
42-
#line 16 "autoprep.pgc"
50+
#line 18 "autoprep.pgc"
4351

4452
/* exec sql whenever sqlerror sqlprint ; */
45-
#line 17 "autoprep.pgc"
53+
#line 19 "autoprep.pgc"
4654

4755

48-
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_prepnormal, "create table T ( Item1 int , Item2 int )", ECPGt_EOIT, ECPGt_EORT);
49-
#line 19 "autoprep.pgc"
56+
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "create table T ( Item1 int , Item2 int )", ECPGt_EOIT, ECPGt_EORT);
57+
#line 21 "autoprep.pgc"
5058

5159
if (sqlca.sqlwarn[0] == 'W') sqlprint();
52-
#line 19 "autoprep.pgc"
60+
#line 21 "autoprep.pgc"
5361

5462
if (sqlca.sqlcode < 0) sqlprint();}
55-
#line 19 "autoprep.pgc"
63+
#line 21 "autoprep.pgc"
5664

5765

5866
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_prepnormal, "insert into T values ( 1 , null )", ECPGt_EOIT, ECPGt_EORT);
59-
#line 21 "autoprep.pgc"
67+
#line 23 "autoprep.pgc"
6068

6169
if (sqlca.sqlwarn[0] == 'W') sqlprint();
62-
#line 21 "autoprep.pgc"
70+
#line 23 "autoprep.pgc"
6371

6472
if (sqlca.sqlcode < 0) sqlprint();}
65-
#line 21 "autoprep.pgc"
73+
#line 23 "autoprep.pgc"
6674

6775
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_prepnormal, "insert into T values ( 1 , $1 )",
6876
ECPGt_int,&(i),(long)1,(long)1,sizeof(int),
6977
ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);
70-
#line 22 "autoprep.pgc"
78+
#line 24 "autoprep.pgc"
7179

7280
if (sqlca.sqlwarn[0] == 'W') sqlprint();
73-
#line 22 "autoprep.pgc"
81+
#line 24 "autoprep.pgc"
7482

7583
if (sqlca.sqlcode < 0) sqlprint();}
76-
#line 22 "autoprep.pgc"
84+
#line 24 "autoprep.pgc"
7785

7886
i++;
7987
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_prepnormal, "insert into T values ( 1 , $1 )",
8088
ECPGt_int,&(i),(long)1,(long)1,sizeof(int),
8189
ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);
82-
#line 24 "autoprep.pgc"
90+
#line 26 "autoprep.pgc"
8391

8492
if (sqlca.sqlwarn[0] == 'W') sqlprint();
85-
#line 24 "autoprep.pgc"
93+
#line 26 "autoprep.pgc"
8694

8795
if (sqlca.sqlcode < 0) sqlprint();}
88-
#line 24 "autoprep.pgc"
96+
#line 26 "autoprep.pgc"
8997

9098
{ ECPGprepare(__LINE__, NULL, 0, "i", " insert into T values ( 1 , 2 ) ");
91-
#line 25 "autoprep.pgc"
99+
#line 27 "autoprep.pgc"
92100

93101
if (sqlca.sqlwarn[0] == 'W') sqlprint();
94-
#line 25 "autoprep.pgc"
102+
#line 27 "autoprep.pgc"
95103

96104
if (sqlca.sqlcode < 0) sqlprint();}
97-
#line 25 "autoprep.pgc"
105+
#line 27 "autoprep.pgc"
98106

99-
{ ECPGdo(__LINE__, 0, 1, NULL, 0, 1, "i", ECPGt_EOIT, ECPGt_EORT);
100-
#line 26 "autoprep.pgc"
107+
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_execute, "i", ECPGt_EOIT, ECPGt_EORT);
108+
#line 28 "autoprep.pgc"
101109

102110
if (sqlca.sqlwarn[0] == 'W') sqlprint();
103-
#line 26 "autoprep.pgc"
111+
#line 28 "autoprep.pgc"
104112

105113
if (sqlca.sqlcode < 0) sqlprint();}
106-
#line 26 "autoprep.pgc"
114+
#line 28 "autoprep.pgc"
107115

108116

109117
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_prepnormal, "select Item2 from T order by Item2 nulls last", ECPGt_EOIT,
110118
ECPGt_int,(item),(long)1,(long)4,sizeof(int),
111119
ECPGt_int,(ind),(long)1,(long)4,sizeof(int), ECPGt_EORT);
112-
#line 28 "autoprep.pgc"
120+
#line 30 "autoprep.pgc"
113121

114122
if (sqlca.sqlwarn[0] == 'W') sqlprint();
115-
#line 28 "autoprep.pgc"
123+
#line 30 "autoprep.pgc"
116124

117125
if (sqlca.sqlcode < 0) sqlprint();}
118-
#line 28 "autoprep.pgc"
126+
#line 30 "autoprep.pgc"
119127

120128

121129
for (i=0; i<4; i++)
122130
printf("item[%d] = %d\n", i, ind[i] ? -1 : item[i]);
123131

124132
/* declare C cursor for select Item1 from T */
125-
#line 33 "autoprep.pgc"
126-
127-
128-
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_prepnormal, "declare C cursor for select Item1 from T", ECPGt_EOIT, ECPGt_EORT);
129-
#line 35 "autoprep.pgc"
130-
131-
if (sqlca.sqlwarn[0] == 'W') sqlprint();
132-
#line 35 "autoprep.pgc"
133-
134-
if (sqlca.sqlcode < 0) sqlprint();}
135133
#line 35 "autoprep.pgc"
136134

137135

138-
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_prepnormal, "fetch 1 in C", ECPGt_EOIT,
139-
ECPGt_int,&(i),(long)1,(long)1,sizeof(int),
140-
ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);
136+
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "declare C cursor for select Item1 from T", ECPGt_EOIT, ECPGt_EORT);
141137
#line 37 "autoprep.pgc"
142138

143139
if (sqlca.sqlwarn[0] == 'W') sqlprint();
@@ -146,19 +142,21 @@ if (sqlca.sqlwarn[0] == 'W') sqlprint();
146142
if (sqlca.sqlcode < 0) sqlprint();}
147143
#line 37 "autoprep.pgc"
148144

149-
printf("i = %d\n", i);
150145

151-
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_prepnormal, "close C", ECPGt_EOIT, ECPGt_EORT);
152-
#line 40 "autoprep.pgc"
146+
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "fetch 1 in C", ECPGt_EOIT,
147+
ECPGt_int,&(i),(long)1,(long)1,sizeof(int),
148+
ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);
149+
#line 39 "autoprep.pgc"
153150

154151
if (sqlca.sqlwarn[0] == 'W') sqlprint();
155-
#line 40 "autoprep.pgc"
152+
#line 39 "autoprep.pgc"
156153

157154
if (sqlca.sqlcode < 0) sqlprint();}
158-
#line 40 "autoprep.pgc"
155+
#line 39 "autoprep.pgc"
159156

157+
printf("i = %d\n", i);
160158

161-
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_prepnormal, "drop table T", ECPGt_EOIT, ECPGt_EORT);
159+
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "close C", ECPGt_EOIT, ECPGt_EORT);
162160
#line 42 "autoprep.pgc"
163161

164162
if (sqlca.sqlwarn[0] == 'W') sqlprint();
@@ -168,7 +166,7 @@ if (sqlca.sqlcode < 0) sqlprint();}
168166
#line 42 "autoprep.pgc"
169167

170168

171-
{ ECPGdisconnect(__LINE__, "ALL");
169+
{ ECPGprepare(__LINE__, NULL, 0, "stmt1", sqlstr);
172170
#line 44 "autoprep.pgc"
173171

174172
if (sqlca.sqlwarn[0] == 'W') sqlprint();
@@ -178,5 +176,76 @@ if (sqlca.sqlcode < 0) sqlprint();}
178176
#line 44 "autoprep.pgc"
179177

180178

179+
/* declare cur1 cursor for $1 */
180+
#line 46 "autoprep.pgc"
181+
182+
183+
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "declare cur1 cursor for $1",
184+
ECPGt_char_variable,(ECPGprepared_statement(NULL, "stmt1", __LINE__)),(long)1,(long)1,(1)*sizeof(char),
185+
ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);
186+
#line 48 "autoprep.pgc"
187+
188+
if (sqlca.sqlwarn[0] == 'W') sqlprint();
189+
#line 48 "autoprep.pgc"
190+
191+
if (sqlca.sqlcode < 0) sqlprint();}
192+
#line 48 "autoprep.pgc"
193+
194+
195+
/* exec sql whenever not found break ; */
196+
#line 50 "autoprep.pgc"
197+
198+
199+
i = 0;
200+
while (1)
201+
{
202+
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "fetch cur1", ECPGt_EOIT,
203+
ECPGt_int,&(item1),(long)1,(long)1,sizeof(int),
204+
ECPGt_int,&(ind1),(long)1,(long)1,sizeof(int), ECPGt_EORT);
205+
#line 55 "autoprep.pgc"
206+
207+
if (sqlca.sqlcode == ECPG_NOT_FOUND) break;
208+
#line 55 "autoprep.pgc"
209+
210+
if (sqlca.sqlwarn[0] == 'W') sqlprint();
211+
#line 55 "autoprep.pgc"
212+
213+
if (sqlca.sqlcode < 0) sqlprint();}
214+
#line 55 "autoprep.pgc"
215+
216+
printf("item[%d] = %d\n", i, ind1 ? -1 : item1);
217+
i++;
218+
}
219+
220+
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "close cur1", ECPGt_EOIT, ECPGt_EORT);
221+
#line 60 "autoprep.pgc"
222+
223+
if (sqlca.sqlwarn[0] == 'W') sqlprint();
224+
#line 60 "autoprep.pgc"
225+
226+
if (sqlca.sqlcode < 0) sqlprint();}
227+
#line 60 "autoprep.pgc"
228+
229+
230+
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "drop table T", ECPGt_EOIT, ECPGt_EORT);
231+
#line 62 "autoprep.pgc"
232+
233+
if (sqlca.sqlwarn[0] == 'W') sqlprint();
234+
#line 62 "autoprep.pgc"
235+
236+
if (sqlca.sqlcode < 0) sqlprint();}
237+
#line 62 "autoprep.pgc"
238+
239+
240+
{ ECPGdisconnect(__LINE__, "ALL");
241+
#line 64 "autoprep.pgc"
242+
243+
if (sqlca.sqlwarn[0] == 'W') sqlprint();
244+
#line 64 "autoprep.pgc"
245+
246+
if (sqlca.sqlcode < 0) sqlprint();}
247+
#line 64 "autoprep.pgc"
248+
249+
181250
return 0;
182251
}

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