-
Notifications
You must be signed in to change notification settings - Fork 151
/
test-array-insert.js
176 lines (160 loc) · 6.59 KB
/
test-array-insert.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
var common = require("./common")
, ibmdb = require("../")
, assert = require("assert")
, insertCount = 0;
;
//ibmdb.debug(2);
console.log("\n Test 1:\n =======\n");
ibmdb.open(common.connectionString, function(err, conn) {
if(err) {
console.log(err);
return;
}
// Insert array data using querySync API
// =====================================
err = conn.querySync("create table arrtab (c1 int, c2 double, c3 smallint, c4 varchar(10))");
if(err.length) { console.log(err); return; }
err = conn.querySync("insert into arrtab values (9, 4.5, true, 'rocket')");
if(err.length) { console.log(err); return; }
var param1 = {ParamType:"ARRAY", DataType:1, Data:[null,5,6,7,8]};
var param2 = {ParamType:"ARRAY", DataType:"DOUBLE", Data:[4.1,null,6.14,7,8.3]};
var param3 = {ParamType:"ARRAY", DataType:1, Data:[0,1,null,false,true]};
var namearr = ["", "Row 200", null, "Row 4000", "Last Row"];
var param4 = {ParamType:"ARRAY", DataType:1, Data:namearr, Length:9};
var queryOptions = {sql:"insert into arrtab values (?, ?, ?, ?)",
params: [param1, param2, param3, param4],
ArraySize:5};
conn.querySync(queryOptions);
// Insert array data using prepare-bind-execute API
// ================================================
conn.querySync("create table arrtab2 (c1 int, c2 varchar(10))");
var param5 = {ParamType:"ARRAY", DataType:1, Data:[10,20,30,40]};
var namearr = ["Row 10", "Row 2000", "Row 30", "Last Row"];
var param6 = {ParamType:"ARRAY", DataType:1, Data:namearr};
//Note: param4 uses "Length" key, but param6 do not use Length key
var tab2data = [
{ C1: 10, C2: 'Row 10' },
{ C1: 20, C2: 'Row 20' },
{ C1: 30, C2: 'Row 30' },
{ C1: 40, C2: 'Last R' },
{ C1: 10, C2: 'Row 10' },
{ C1: 20, C2: 'Row 20' },
{ C1: 30, C2: 'Row 30' },
{ C1: 40, C2: 'Last R' }
];
var stmt = conn.prepareSync("insert into arrtab2 values (?, ?)");
stmt.bindSync([param5, param6]);
stmt.setAttrSync(ibmdb.SQL_ATTR_PARAMSET_SIZE, 4);
var result = stmt.executeSync();
stmt.closeSync();
// Insert array data using query API
// =====================================
conn.query(queryOptions, function(err, result) {
if(err) console.log("Error in query,", err);
else {
var data = conn.querySync('select c1, c2, c3, c4, length(c4) "C4LEN" from arrtab');
console.log("\nSelected data for table ARRTAB =\n", data);
conn.querySync("drop table arrtab");
// Insert array data using prepare-bind-execute API
// ================================================
conn.prepare("insert into arrtab2 values (?, ?)", function(err, stmt) {
if(err) { console.log(err); stmt.closeSync(); }
else {
stmt.setAttr(ibmdb.SQL_ATTR_PARAMSET_SIZE, 4, function(err, result) {
if(err) { console.log(err); stmt.closeSync(); }
else {
stmt.execute([param5, param6], function(err, result) {
if(err) console.log(err);
stmt.closeSync();
var data = conn.querySync("select * from arrtab2");
console.log("\nSelected data for table ARRTAB2 =\n", data);
conn.querySync("drop table arrtab2");
assert.deepEqual(data, tab2data);
conn.closeSync();
testEmptyArryElement();
});
}
});
}
});
}
});
});
// Test array insert when one of the element in array is empty.
function testEmptyArryElement() {
console.log("\n Test 2:\n =======\n");
ibmdb.open(common.connectionString, function(err, conn) {
if(err) {
console.log(err);
return;
}
// Insert array data using querySync API
// =====================================
err = conn.querySync("create table arrtab3 (c1 varchar(10))");
if(err.length) { console.log(err); return; }
var params1 = [{ParamType:"ARRAY", DataType:1, Data: ["","Hello"], Length:5}];
var params2 = [{ParamType:"ARRAY", DataType:1, Data: ["Hello",""], Length:5}];
var queryOptions1 = {sql:"insert into arrtab3 values (?)",
params: [{ParamType:"ARRAY", DataType:1, Data: ["","Hello"], Length:5}],
ArraySize:2};
var queryOptions2 = {sql:"insert into arrtab3 values (?)",
params: [{ParamType:"ARRAY", DataType:1, Data: ["Hello"," "], Length:5}],
ArraySize:2};
conn.querySync(queryOptions1);
conn.querySync(queryOptions2);
// Insert array data using prepare-bind-execute API
// ================================================
conn.querySync("create table arrtab4 (c1 varchar(10))");
var tab4data = [
{ C1: '' },
{ C1: 'Hello' },
{ C1: 'Hello' },
{ C1: '' }
];
var stmt = conn.prepareSync("insert into arrtab4 values (?)");
stmt.bindSync(params1);
stmt.setAttrSync(ibmdb.SQL_ATTR_PARAMSET_SIZE, 2);
var result = stmt.executeSync();
stmt.closeSync();
// Insert array data using query API
// =====================================
conn.query(queryOptions1, function(err, result) {
if(err) console.log(err);
else {
var data = conn.querySync('select c1, length(c1) "C1LEN" from arrtab3');
console.log("\nSelected data for table ARRTAB3 =\n", data);
console.log("drop table result = ", conn.querySync("drop table arrtab3"));
var expectedData3 = [
{ C1: '', C1LEN: 0 },
{ C1: 'Hello', C1LEN: 5 },
{ C1: 'Hello', C1LEN: 5 },
{ C1: ' ', C1LEN: 1 },
{ C1: '', C1LEN: 0 },
{ C1: 'Hello', C1LEN: 5 }
];
assert.deepEqual(data, expectedData3);
// Insert array data using prepare-bind-execute API
// ================================================
conn.prepare("insert into arrtab4 values (?)", function(err, stmt) {
if(err) { console.log("error in prepare,", err); stmt.closeSync(); }
else {
stmt.setAttr(ibmdb.SQL_ATTR_PARAMSET_SIZE, 2, function(err, result) {
if(err) { console.log("error in setAttr,", err); stmt.closeSync(); }
else {
stmt.execute(params2, function(err, result) {
if(err) console.log("error in execute,", err);
stmt.closeSync();
var data = conn.querySync("select * from arrtab4");
console.log("\nSelected data for table ARRTAB4 =\n", data);
conn.querySync("drop table arrtab4");
assert.deepEqual(data, tab4data);
conn.closeSync();
});
}
});
}
});
}
});
});
}