forked from joeferner/node-oracle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutparams.js
230 lines (203 loc) · 6.86 KB
/
outparams.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
/*
tests-settings.json:
{
"hostname": "localhost",
"user": "test",
"password": "test"
}
Database:
CREATE OR REPLACE PROCEDURE procNumericOutParam(param1 IN VARCHAR2, outParam1 OUT NUMBER)
IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello '|| param1);
outParam1 := 42;
END;
/
CREATE OR REPLACE PROCEDURE procStringOutParam(param1 IN VARCHAR2, outParam1 OUT STRING)
IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello '|| param1);
outParam1 := 'Hello ' || param1;
END;
/
CREATE OR REPLACE PROCEDURE procVarChar2OutParam(param1 IN VARCHAR2, outParam1 OUT VARCHAR2)
IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello '|| param1);
outParam1 := 'Hello ' || param1;
END;
/
CREATE OR REPLACE PROCEDURE procDoubleOutParam(param1 IN VARCHAR2, outParam1 OUT DOUBLE PRECISION)
IS
BEGIN
outParam1 := -43.123456789012;
END;
/
CREATE OR REPLACE PROCEDURE procFloatOutParam(param1 IN VARCHAR2, outParam1 OUT FLOAT)
IS
BEGIN
outParam1 := 43;
END;
/
CREATE OR REPLACE PROCEDURE procTwoOutParams(param1 IN VARCHAR2, outParam1 OUT NUMBER, outParam2 OUT STRING)
IS
BEGIN
outParam1 := 42;
outParam2 := 'Hello ' || param1;
END;
/
CREATE OR REPLACE PROCEDURE procCursorOutParam(outParam OUT SYS_REFCURSOR)
IS
BEGIN
open outParam for
select * from person;
END;
/
CREATE OR REPLACE PROCEDURE procCLOBOutParam(outParam OUT CLOB)
IS
BEGIN
outParam := 'IAMCLOB';
END;
/
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE basic_lob_table';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE != -942 THEN
RAISE;
END IF;
END;
/
create table basic_lob_table (x varchar2 (30), b blob, c clob);
insert into basic_lob_table values('one', '010101010101010101010101010101', 'onetwothreefour');
select * from basic_lob_table where x='one' and ROWNUM = 1;
CREATE OR REPLACE PROCEDURE ReadBasicBLOB (outBlob OUT BLOB)
IS
BEGIN
SELECT b INTO outBlob FROM basic_lob_table where x='one' and ROWNUM = 1;
END;
/
CREATE OR REPLACE procedure doSquareInteger(z IN OUT Integer)
is
begin
z := z * z;
end;
/
*/
var nodeunit = require("nodeunit");
var oracle = require("../");
var settings = JSON.parse(require('fs').readFileSync('./tests-settings.json','utf8'));
exports['OutParamsTest'] = nodeunit.testCase({
setUp: function(callback) {
var self = this;
oracle.connect(settings, function(err, connection) {
if(err) { callback(err); return; }
self.connection = connection;
callback();
});
},
tearDown: function(callback) {
if(this.connection) {
this.connection.close();
}
callback();
},
"stored procedures - multiple out params": function(test){
var self = this;
self.connection.execute("call procTwoOutParams(:1,:2,:3)", ["node", new oracle.OutParam(oracle.OCCIINT), new oracle.OutParam(oracle.OCCISTRING)], function(err, results){
if(err) { console.error(err); return; }
test.equal(results.returnParam, 42);
test.equal(results.returnParam1, "Hello node");
test.done();
});
},
"stored procedures - numeric out param - occiint": function(test){
var self = this;
self.connection.execute("call procNumericOutParam(:1,:2)", ["node", new oracle.OutParam()], function(err, results){
if(err) { console.error(err); return; }
test.equal(results.returnParam, 42);
test.done();
});
},
"stored procedures - numeric out param - occinumber": function(test){
var self = this;
self.connection.execute("call procNumericOutParam(:1,:2)", ["node", new oracle.OutParam(oracle.OCCINUMBER)], function(err, results){
if(err) { console.error(err); return; }
test.equal(results.returnParam, 42);
test.done();
});
},
"stored procedures - string out param": function(test){
var self = this;
self.connection.execute("call procStringOutParam(:1,:2)", ["node", new oracle.OutParam(oracle.OCCISTRING)], function(err, results){
if(err) { console.error(err); return; }
test.equal(results.returnParam, "Hello node");
test.done();
});
},
"stored procedures - varchar2 out param": function(test){
var self = this;
self.connection.execute("call procVarChar2OutParam(:1,:2)", ["node", new oracle.OutParam(oracle.OCCISTRING, {size: 40})], function(err, results){
if(err) { console.error(err); return; }
test.equal(results.returnParam, "Hello node");
test.done();
});
},
"stored procedures - double out param": function(test){
var self = this;
self.connection.execute("call procDoubleOutParam(:1,:2)", ["node", new oracle.OutParam(oracle.OCCIDOUBLE)], function(err, results){
if(err) { console.error(err); return; }
test.equal(results.returnParam, -43.123456789012);
test.done();
});
},
"stored procedures - float out param": function(test){
var self = this;
self.connection.execute("call procFloatOutParam(:1,:2)", ["node", new oracle.OutParam(oracle.OCCIFLOAT)], function(err, results){
if(err) { console.error(err); return; }
// purposely commented, gotta love floats in javasctipt: http://stackoverflow.com/questions/588004/is-javascripts-floating-point-math-broken
// test.equal(results.returnParam, 43);
test.done();
});
},
"stored procedures - cursor out param": function(test){
var self = this;
self.connection.execute("call procCursorOutParam(:1)", [new oracle.OutParam(oracle.OCCICURSOR)], function(err, results){
if(err) { console.error(err); return; }
test.equal(results.returnParam.length, 0);
test.done();
});
},
"stored procedures - clob out param": function(test){
var self = this;
self.connection.execute("call procCLOBOutParam(:1)", [new oracle.OutParam(oracle.OCCICLOB)], function(err, results){
if(err) { console.error(err); return; }
test.equal(results.returnParam, "IAMCLOB");
test.done();
});
},
"stored procedures - date timestamp out param": function(test){
var self = this;
self.connection.execute("call procDateTimeOutParam(:1, :2)", [new oracle.OutParam(oracle.OCCIDATE), new oracle.OutParam(oracle.OCCITIMESTAMP)], function(err, results){
if(err) { console.error(err); return; }
var d = new Date();
test.equal(results.returnParam.getFullYear(), d.getFullYear());
test.done();
});
},
"stored procedures - blob out param": function(test){
var self = this;
self.connection.execute("call ReadBasicBLOB(:1)", [new oracle.OutParam(oracle.OCCIBLOB)], function(err, results){
if(err) { console.error(err); return; }
test.done();
});
},
"stored procedures - in out params": function(test){
var self = this;
self.connection.execute("call doSquareInteger(:1)", [new oracle.OutParam(oracle.OCCIINT, {in: 5})], function(err, results){
if(err) { console.error(err); return; }
test.equal(results.returnParam, 25);
test.done();
});
}
});