-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathtest-sp-resultset-execute.js
269 lines (249 loc) · 9.69 KB
/
test-sp-resultset-execute.js
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Test case to test the OUT parameters and Result set returned by
// Stored Procedure when Async and Sync forms of Prepare and Execute
// APIs are used.
var common = require("./common")
, ibmdb = require("../")
, assert = require("assert")
, isZOS = common.isZOS
, schema = common.connectionObject.CURRENTSCHEMA;
var proc1 = "create or replace procedure " + schema + ".PROC1 ( IN v1 int, INOUT v2 varchar(30) ) dynamic result sets 2 language sql begin declare cr1 cursor with return for select c1, c2 from " + schema + ".mytab1; declare cr2 cursor with return for select c2 from " + schema + ".mytab1; open cr1; open cr2; set v2 = 'success'; end";
var proc2 = "create or replace procedure " + schema + ".PROC2 ( IN v1 int, INOUT v2 varchar(30) ) language sql begin set v2 = 'success'; end";
var proc3 = "create or replace procedure " + schema + ".PROC3 ( IN v1 int, IN v2 varchar(30) ) dynamic result sets 2 language sql begin declare cr1 cursor with return for select c1, c2 from " + schema + ".mytab1; declare cr2 cursor with return for select c2 from " + schema + ".mytab1; open cr1; open cr2; end";
var query1 = "call " + schema + ".PROC1(?, ?)";
var query2 = "call " + schema + ".PROC2(?, ?)";
var query3 = "call " + schema + ".PROC3(?, ?)";
var dropProc1 = "drop procedure " + schema + ".PROC1";
var dropProc2 = "drop procedure " + schema + ".PROC2";
var dropProc3 = "drop procedure " + schema + ".PROC3";
if (isZOS) {
proc1 = common.sanitizeSP(proc1);
proc2 = common.sanitizeSP(proc2);
proc3 = common.sanitizeSP(proc3);
} else {
dropProc1 = "drop procedure " + schema + ".PROC1 ( INT, VARCHAR(30) )";
dropProc2 = "drop procedure " + schema + ".PROC2 ( INT, VARCHAR(30) )";
dropProc3 = "drop procedure " + schema + ".PROC3 ( INT, VARCHAR(30) )";
}
var param2 = {ParamType:"INOUT", DataType:1, Data:"abc", Length:50};
var conn, stmt, result, data, err;
async function setupTestEnv() {
try {
conn = ibmdb.openSync(common.connectionString, {fetchMode : 3});
} catch (err) {
console.log(err);
process.exit(-1);
}
await conn.query("drop table " + schema + ".mytab1").catch((e) => {});
await conn.query(dropProc1).catch((e) => {});
await conn.query(dropProc2).catch((e) => {});
await conn.query(dropProc3).catch((e) => {});
try {
conn.querySync({"sql":"create table " + schema + ".mytab1 (c1 int, c2 varchar(20))", "noResults":true});
conn.querySync("insert into " + schema + ".mytab1 values (2, 'bimal')");
conn.querySync("insert into " + schema + ".mytab1 values (3, 'kumar')");
} catch(err) {
console.log(err);
process.exit(-1);
}
// Create SP with INOUT param and 2 Result Set.
await conn.query(proc1).catch((e) => {console.log(e);});
// Create SP with only OUT param and no resultset.
await conn.query(proc2).catch((e) => {console.log(e);});
// Create SP with only Result Set and no OUT or INPUT param.
await conn.query(proc3).catch((e) => {console.log(e);});
}
function closeStmt() {
result.closeSync();
stmt.closeSync();
result = "";
stmt = "";
data = "";
}
// Call SP Synchronously.
function syncTestSP() {
//==> Test 1 <== *******************************************************
stmt = conn.prepareSync(query1);
//console.log(stmt.bindSync(['1', param2]));
// Passing '1' throws SQL0420N on windows, so use 1.
result = stmt.executeSync([1, param2]);
console.log("\nTest 1: Result for Sync call of PROC1 (1 OUT param and 2 Result Sets) ==>");
if(Array.isArray(result))
{
// Print INOUT and OUT param values for SP.
console.log(result[1]);
assert.deepEqual(result[1], ['success']);
result = result[0];
}
data = result.fetchAllSync();
console.log(data);
assert.equal(data.length, 2);
while(result.moreResultsSync()) {
data = result.fetchAllSync();
console.log(data);
}
closeStmt();
//==> Test 2 <== *******************************************************
stmt = conn.prepareSync(query2);
result = stmt.executeSync([1, param2]);
console.log("\nTest 2: Result for Sync call of PROC2 (1 OUT param and no Result Set) ==>");
if(Array.isArray(result))
{
// Print INOUT and OUT param values for SP.
console.log(result[1]);
assert.deepEqual(result[1], ['success']);
result = result[0];
}
data = result.fetchAllSync();
if(data.length) console.log(data);
closeStmt();
//==> Test 3 <== *******************************************************
stmt = conn.prepareSync(query3);
result = stmt.executeSync([1, 'abc']);
console.log("\nTest 3: Result for Sync call of PROC3 (only 2 Result Sets) ==>");
if(Array.isArray(result))
{
// Print INOUT and OUT param values for SP.
console.log(result[1]);
assert.deepEqual(result[1], null);
result = result[0];
}
data = result.fetchAllSync();
console.log(data);
assert.equal(data.length, 2);
while(result.moreResultsSync()) {
data = result.fetchAllSync();
console.log(data);
}
closeStmt();
}
// Call SP Asynchronously using async await.
async function awaitTestSP() {
//==> Test 4 <== *******************************************************
stmt = await conn.prepare(query1);
result = await stmt.execute([1, param2]);
console.log("\nTest 4: Result for AWAIT call of PROC1 (1 OUT param and 2 Result Sets) ==>");
if(Array.isArray(result))
{
// Print INOUT and OUT param values for SP.
console.log(result[1]);
assert.deepEqual(result[1], ['success']);
result = result[0];
}
data = await result.fetchAll();
console.log(data);
assert.equal(data.length, 2);
while(result.moreResultsSync()) {
data = await result.fetchAll();
console.log(data);
}
closeStmt();
//==> Test 5 <== *******************************************************
stmt = await conn.prepare(query2);
result = await stmt.execute([1, param2]);
console.log("\nTest 5: Result for Sync call of PROC2 (1 OUT param and no Result Set) ==>");
if(Array.isArray(result))
{
// Print INOUT and OUT param values for SP.
console.log(result[1]);
assert.deepEqual(result[1], ['success']);
result = result[0];
}
data = await result.fetchAll();
if(data.length) console.log(data);
closeStmt();
//==> Test 6 <== *******************************************************
stmt = await conn.prepare(query3);
result = await stmt.execute([1, 'abc']);
console.log("\nTest 6: Result for Sync call of PROC3 (only 2 Result Sets) ==>");
if(Array.isArray(result))
{
// Print INOUT and OUT param values for SP.
console.log(result[1]);
assert.deepEqual(result[1], null);
result = result[0];
}
data = await result.fetchAll();
console.log(data);
assert.equal(data.length, 2);
while(result.moreResultsSync()) {
data = await result.fetchAll();
console.log(data);
}
closeStmt();
}
// Call SP Asynchronously using callback function.
function callbackTestSP() {
//==> Test 7 <== *******************************************************
conn.prepare(query1, function (err, stmt) {
if (err) console.log(err);
stmt.execute([1, param2], function(err, result, outparams) {
if( err ) console.log(err);
else {
data = result.fetchAllSync();
console.log("\nTest 7: Result for Async call of PROC1 (1 OUT param and 2 Result Sets) ==>");
console.log(outparams);
assert.deepEqual(outparams, ['success']);
console.log(data);
assert.equal(data.length, 2);
while(result.moreResultsSync()) {
data = result.fetchAllSync();
console.log(data);
}
result.closeSync();
}
stmt.closeSync();
//==> Test 8 <== *******************************************************
conn.prepare(query2, function (err, stmt) {
if (err) console.log(err);
stmt.execute([1, param2], function(err, result, outparams) {
if( err ) console.log(err);
else {
result.closeSync();
console.log("\nTest 8: Result for Async call of PROC2 (1 OUT param and no Result Set) ==>");
console.log(outparams);
assert.deepEqual(outparams, ['success']);
}
stmt.closeSync();
//==> Test 9 <== *******************************************************
conn.prepare(query3, function (err, stmt) {
if (err) console.log(err);
stmt.execute([1, 'abc'], function(err, result, outparams) {
if( err ) console.log(err);
else {
data = result.fetchAllSync();
console.log("\nTest 9: Result for Async call of PROC3 (only 2 Result Sets) ==>");
console.log(data);
assert.equal(data.length, 2);
while(result.moreResultsSync()) {
data = result.fetchAllSync();
console.log(data);
}
result.closeSync();
}
stmt.closeSync();
cleanupTestEnv();
});
});
});
});
});
});
}
function cleanupTestEnv() {
// Do Cleanup.
try {
conn.querySync(dropProc1);
conn.querySync(dropProc2);
conn.querySync(dropProc3);
conn.querySync("drop table " + schema + ".mytab1");
} catch(e) {};
// Close connection in last only.
conn.close(function (err) { console.log("done.");});
}
async function main() {
await setupTestEnv();
syncTestSP();
await awaitTestSP();
callbackTestSP();
}
main();