1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
#include <locale.h>
#include <string.h>
#include <stdlib.h>
EXEC SQL WHENEVER SQLERROR SQLPRINT;
EXEC SQL INCLUDE sqlca;
EXEC SQL INCLUDE ../regression;
#define ARRAY_SIZE 2
void execute_test(void);
void commitTable(void);
void reset(void);
void printResult(char *tc_name, int loop);
EXEC SQL BEGIN DECLARE SECTION;
int f1[ARRAY_SIZE];
int f2[ARRAY_SIZE];
char f3[ARRAY_SIZE][20];
EXEC SQL END DECLARE SECTION;
int main(void)
{
setlocale(LC_ALL, "C");
ECPGdebug(1, stderr);
EXEC SQL CONNECT TO REGRESSDB1 AS con1;
EXEC SQL CONNECT TO REGRESSDB2 AS con2;
EXEC SQL AT con1 CREATE TABLE source(f1 integer, f2 integer, f3 varchar(20));
EXEC SQL AT con2 CREATE TABLE source(f1 integer, f2 integer, f3 varchar(20));
EXEC SQL AT con1 INSERT INTO source VALUES(1, 10, 'db on con1');
EXEC SQL AT con1 INSERT INTO source VALUES(2, 20, 'db on con1');
EXEC SQL AT con2 INSERT INTO source VALUES(1, 10, 'db on con2');
EXEC SQL AT con2 INSERT INTO source VALUES(2, 20, 'db on con2');
commitTable();
execute_test();
EXEC SQL AT con1 DROP TABLE IF EXISTS source;
EXEC SQL AT con2 DROP TABLE IF EXISTS source;
commitTable();
EXEC SQL DISCONNECT ALL;
return 0;
}
/*
* default connection: con2
* Non-default connection: con1
*
*/
void execute_test(void)
{
EXEC SQL BEGIN DECLARE SECTION;
int i, count, length;
char *selectString = "SELECT f1,f2,f3 FROM source";
EXEC SQL END DECLARE SECTION;
/*
* testcase1. using DECLARE STATEMENT without using AT clause,
* using PREPARE and CURSOR statement without using AT clause
*/
reset();
EXEC SQL DECLARE stmt_1 STATEMENT;
EXEC SQL PREPARE stmt_1 FROM :selectString;
EXEC SQL DECLARE cur_1 CURSOR FOR stmt_1;
EXEC SQL OPEN cur_1;
EXEC SQL WHENEVER NOT FOUND DO BREAK;
i = 0;
while (1)
{
EXEC SQL FETCH cur_1 INTO :f1[i], :f2[i], :f3[i];
i++;
}
EXEC SQL CLOSE cur_1;
EXEC SQL DEALLOCATE PREPARE stmt_1;
EXEC SQL WHENEVER NOT FOUND CONTINUE;
printResult("testcase1", 2);
/*
* testcase2. using DECLARE STATEMENT at con1,
* using PREPARE and CURSOR statement without using AT clause
*/
reset();
EXEC SQL AT con1 DECLARE stmt_2 STATEMENT;
EXEC SQL PREPARE stmt_2 FROM :selectString;
EXEC SQL DECLARE cur_2 CURSOR FOR stmt_2;
EXEC SQL OPEN cur_2;
EXEC SQL WHENEVER NOT FOUND DO BREAK;
i = 0;
while (1)
{
EXEC SQL FETCH cur_2 INTO :f1[i], :f2[i], :f3[i];
i++;
}
EXEC SQL CLOSE cur_2;
EXEC SQL DEALLOCATE PREPARE stmt_2;
EXEC SQL WHENEVER NOT FOUND CONTINUE;
printResult("testcase2", 2);
/*
* testcase3. using DECLARE STATEMENT without using AT clause,
* using PREPARE and EXECUTE statement without using AT clause
*/
reset();
EXEC SQL DECLARE stmt_3 STATEMENT;
EXEC SQL PREPARE stmt_3 FROM :selectString;
EXEC SQL EXECUTE stmt_3 INTO :f1, :f2, :f3;
EXEC SQL DEALLOCATE PREPARE stmt_3;
printResult("testcase3", 2);
/*
* testcase4. using DECLARE STATEMENT without using AT clause,
* using PREPARE and CURSOR statement at con2
*/
reset();
EXEC SQL DECLARE stmt_4 STATEMENT;
EXEC SQL AT con2 PREPARE stmt_4 FROM :selectString;
EXEC SQL AT con2 DECLARE cur_4 CURSOR FOR stmt_4;
EXEC SQL AT con2 OPEN cur_4;
EXEC SQL WHENEVER NOT FOUND DO BREAK;
i = 0;
while (1)
{
EXEC SQL AT con2 FETCH cur_4 INTO :f1[i], :f2[i], :f3[i];
i++;
}
EXEC SQL AT con2 CLOSE cur_4;
EXEC SQL AT con2 DEALLOCATE PREPARE stmt_4;
EXEC SQL WHENEVER NOT FOUND CONTINUE;
printResult("testcase4", 2);
/*
* DESCRIBE statement is also supported.
*/
EXEC SQL AT con1 DECLARE stmt_desc STATEMENT;
EXEC SQL PREPARE stmt_desc FROM :selectString;
EXEC SQL DECLARE cur_desc CURSOR FOR stmt_desc;
EXEC SQL OPEN cur_desc;
/* descriptor can be used for describe statement */
EXEC SQL AT con1 ALLOCATE DESCRIPTOR desc_for_describe;
EXEC SQL DESCRIBE stmt_desc INTO SQL DESCRIPTOR desc_for_describe;
EXEC SQL AT con1 GET DESCRIPTOR desc_for_describe :count = COUNT;
EXEC SQL AT con1 GET DESCRIPTOR desc_for_describe VALUE 3 :length = LENGTH;
EXEC SQL AT con1 DEALLOCATE DESCRIPTOR desc_for_describe;
/* for fetch statement */
EXEC SQL AT con1 ALLOCATE DESCRIPTOR desc_for_fetch;
EXEC SQL FETCH cur_desc INTO SQL DESCRIPTOR desc_for_fetch;
EXEC SQL AT con1 GET DESCRIPTOR desc_for_fetch VALUE 3 :f3[0] = DATA;
EXEC SQL AT con1 DEALLOCATE DESCRIPTOR desc_for_fetch;
EXEC SQL CLOSE cur_desc;
EXEC SQL DEALLOCATE stmt_desc;
printf("****descriptor results****\n");
printf("count: %d, length: %d, data: %s\n", count, length, f3[0]);
}
void commitTable()
{
EXEC SQL AT con1 COMMIT;
EXEC SQL AT con2 COMMIT;
}
/*
* reset all the output variables
*/
void reset()
{
memset(f1, 0, sizeof(f1));
memset(f2, 0, sizeof(f2));
memset(f3, 0, sizeof(f3));
}
void printResult(char *tc_name, int loop)
{
int i;
if (tc_name)
printf("****%s test results:****\n", tc_name);
for (i = 0; i < loop; i++)
printf("f1=%d, f2=%d, f3=%s\n", f1[i], f2[i], f3[i]);
printf("\n");
}
|