|
37 | 37 | 'use strict';
|
38 | 38 |
|
39 | 39 | const oracledb = require('oracledb');
|
40 |
| -const async = require('async'); |
41 |
| -const should = require('should'); |
| 40 | +const assert = require('assert'); |
42 | 41 | const stream = require('stream');
|
43 | 42 | const dbConfig = require('./dbconfig.js');
|
44 | 43 | const assist = require('./dataTypeAssist.js');
|
45 | 44 |
|
46 | 45 | describe('60. clobPlsqlString.js', function() {
|
47 | 46 |
|
48 | 47 | let connection = null;
|
49 |
| - var tableName = "nodb_myclobs"; |
50 |
| - |
51 |
| - before('get one connection, prepare table', function(done) { |
52 |
| - async.series([ |
53 |
| - function(callback) { |
54 |
| - oracledb.getConnection( |
55 |
| - dbConfig, |
56 |
| - function(err, conn) { |
57 |
| - should.not.exist(err); |
58 |
| - connection = conn; |
59 |
| - callback(); |
60 |
| - } |
61 |
| - ); |
62 |
| - }, |
63 |
| - function(callback) { |
64 |
| - assist.createTable(connection, tableName, callback); |
65 |
| - } |
66 |
| - ], done); |
| 48 | + const tableName = `nodb_myclobs`; |
| 49 | + |
| 50 | + before('get one connection, prepare table', async function() { |
| 51 | + |
| 52 | + connection = await oracledb.getConnection(dbConfig); |
| 53 | + await connection.execute(assist.sqlCreateTable(tableName)); |
67 | 54 | }); // before
|
68 | 55 |
|
69 |
| - after('release connection', function(done) { |
70 |
| - async.series([ |
71 |
| - function(callback) { |
72 |
| - connection.execute( |
73 |
| - "DROP TABLE nodb_myclobs purge", |
74 |
| - function(err) { |
75 |
| - should.not.exist(err); |
76 |
| - callback(); |
77 |
| - } |
78 |
| - ); |
79 |
| - }, |
80 |
| - function(callback) { |
81 |
| - connection.close(function(err) { |
82 |
| - should.not.exist(err); |
83 |
| - callback(); |
84 |
| - }); |
85 |
| - } |
86 |
| - ], done); |
| 56 | + after('release connection', async function() { |
| 57 | + |
| 58 | + await connection.execute(`DROP TABLE nodb_myclobs purge`); |
| 59 | + await connection.close(); |
87 | 60 | }); // after
|
88 | 61 |
|
89 | 62 | describe('60.1 BIND OUT as STRING', function() {
|
90 |
| - before('insert data', function(done) { |
91 |
| - connection.execute( |
92 |
| - "INSERT INTO nodb_myclobs (num, content) VALUES (1, 'abcdefghijklmnopqrstuvwxyz')", |
93 |
| - function(err) { |
94 |
| - should.not.exist(err); |
95 |
| - done(); |
96 |
| - } |
97 |
| - ); |
| 63 | + before('insert data', async function() { |
| 64 | + await connection.execute(`INSERT INTO nodb_myclobs (num, content) VALUES (1, 'abcdefghijklmnopqrstuvwxyz')`); |
98 | 65 | }); // before
|
99 | 66 |
|
100 |
| - it('60.1.1 PL/SQL OUT CLOB parameters can also be bound as STRING', function(done) { |
101 |
| - connection.execute( |
102 |
| - "BEGIN SELECT content INTO :cbv FROM nodb_myclobs WHERE num = :id; END;", |
| 67 | + it('60.1.1 PL/SQL OUT CLOB parameters can also be bound as STRING', async function() { |
| 68 | + let result = await connection.execute(`BEGIN SELECT content INTO :cbv FROM nodb_myclobs WHERE num = :id; END;`, |
103 | 69 | {
|
104 | 70 | id: 1,
|
105 | 71 | cbv: { type: oracledb.STRING, dir: oracledb.BIND_OUT}
|
106 |
| - }, |
107 |
| - function(err, result) { |
108 |
| - should.not.exist(err); |
109 |
| - (result.outBinds.cbv).should.be.a.String(); |
110 |
| - (result.outBinds.cbv).should.eql('abcdefghijklmnopqrstuvwxyz'); |
111 |
| - done(); |
112 |
| - } |
113 |
| - ); |
| 72 | + }); |
| 73 | + assert.strictEqual(typeof result.outBinds.cbv, "string"); |
| 74 | + assert.strictEqual(result.outBinds.cbv, 'abcdefghijklmnopqrstuvwxyz'); |
114 | 75 | }); // 60.1.1
|
115 | 76 |
|
116 |
| - it('60.1.2 The returned length is limited to the maximum size', function(done) { |
117 |
| - connection.execute( |
118 |
| - "BEGIN SELECT content INTO :cbv FROM nodb_myclobs WHERE num = :id; END;", |
119 |
| - { |
120 |
| - id: 1, |
121 |
| - cbv: { type: oracledb.STRING, dir: oracledb.BIND_OUT, maxSize: 5 } |
| 77 | + it('60.1.2 The returned length is limited to the maximum size', async function() { |
| 78 | + await assert.rejects( |
| 79 | + async () => { |
| 80 | + await connection.execute(`BEGIN SELECT content INTO :cbv FROM nodb_myclobs WHERE num = :id; END;`, |
| 81 | + { |
| 82 | + id: 1, |
| 83 | + cbv: { type: oracledb.STRING, dir: oracledb.BIND_OUT, maxSize: 5 }}); |
122 | 84 | },
|
123 |
| - function(err, result) { |
124 |
| - should.exist(err); |
125 |
| - (err.message).should.startWith('ORA-06502'); // PL/SQL: numeric or value error |
126 |
| - should.not.exist(result); |
127 |
| - done(); |
128 |
| - } |
| 85 | + /ORA-06502:/ // PL/SQL: numeric or value error |
129 | 86 | );
|
130 | 87 | }); // 60.1.2
|
131 | 88 | }); // 60.1
|
132 | 89 |
|
133 | 90 | describe('60.2 BIND OUT as CLOB', function() {
|
134 |
| - var dataLength = 1000000; |
135 |
| - var rawData = assist.createCharString(dataLength); |
136 |
| - |
137 |
| - it('60.2.1 maxSize option does not take effect when bind out type is clob', function(done) { |
138 |
| - async.series([ |
139 |
| - function doInsert(callback) { |
140 |
| - connection.execute( |
141 |
| - "INSERT INTO " + tableName + " VALUES (2, EMPTY_CLOB()) RETURNING content INTO :lobbv", |
142 |
| - { lobbv: {type: oracledb.CLOB, dir: oracledb.BIND_OUT} }, |
143 |
| - { autoCommit: false }, |
144 |
| - function(err, result) { |
145 |
| - should.not.exist(err); |
146 |
| - |
147 |
| - var lob = result.outBinds.lobbv[0]; |
148 |
| - lob.on('error', function(err) { |
149 |
| - should.not.exist(err); |
150 |
| - return callback(err); |
151 |
| - }); |
152 |
| - |
153 |
| - var inStream = new stream.Readable(); |
154 |
| - inStream._read = function noop() {}; |
155 |
| - inStream.push(rawData); |
156 |
| - inStream.push(null); |
157 |
| - |
158 |
| - inStream.on('error', function(err) { |
159 |
| - should.not.exist(err); |
160 |
| - return callback(err); |
161 |
| - }); |
162 |
| - |
163 |
| - inStream.on('end', function() { |
164 |
| - connection.commit(function(err) { |
165 |
| - should.not.exist(err); |
166 |
| - callback(); |
167 |
| - }); |
168 |
| - }); |
169 |
| - |
170 |
| - inStream.pipe(lob); |
171 |
| - } |
172 |
| - ); |
173 |
| - }, |
174 |
| - function doQuery(callback) { |
175 |
| - connection.execute( |
176 |
| - "BEGIN SELECT content INTO :bv FROM " + tableName + " WHERE num = 2; END;", |
177 |
| - { bv: {dir: oracledb.BIND_OUT, type: oracledb.CLOB} }, |
178 |
| - { maxRows: 500 }, |
179 |
| - function(err, result) { |
180 |
| - should.not.exist(err); |
181 |
| - |
182 |
| - var content = ''; |
183 |
| - var lob = result.outBinds.bv; |
184 |
| - lob.setEncoding('utf8'); |
185 |
| - |
186 |
| - lob.on('data', function(chunk) { |
187 |
| - content += chunk; |
188 |
| - }); |
189 |
| - |
190 |
| - lob.on('end', function() { |
191 |
| - (content.length).should.be.exactly(dataLength); |
192 |
| - (content).should.eql(rawData); |
193 |
| - callback(); |
194 |
| - }); |
195 |
| - |
196 |
| - lob.on('error', function(err) { |
197 |
| - should.not.exist(err); |
198 |
| - }); |
199 |
| - } |
200 |
| - ); |
201 |
| - } |
202 |
| - ], done); |
203 |
| - }); |
204 |
| - }); // 60.2 |
| 91 | + const dataLength = 1000000; |
| 92 | + const rawData = assist.createCharString(dataLength); |
| 93 | + |
| 94 | + it('60.2.1 maxSize option does not take effect when bind out type is clob', async function() { |
| 95 | + |
| 96 | + let result = await connection.execute( |
| 97 | + `INSERT INTO ` + tableName + ` VALUES (2, EMPTY_CLOB()) RETURNING content INTO :lobbv`, |
| 98 | + { lobbv: {type: oracledb.CLOB, dir: oracledb.BIND_OUT} }, |
| 99 | + { autoCommit: false }); |
| 100 | + |
| 101 | + const lob = result.outBinds.lobbv[0]; |
| 102 | + |
| 103 | + await new Promise((resolve, reject) => { |
| 104 | + |
| 105 | + lob.on("error", reject); |
| 106 | + |
| 107 | + let inStream = new stream.Readable(); |
| 108 | + inStream._read = function noop() {}; |
| 109 | + inStream.push(rawData); |
| 110 | + inStream.push(null); |
| 111 | + |
| 112 | + inStream.on('error', reject); |
| 113 | + |
| 114 | + inStream.on('end', resolve); |
| 115 | + |
| 116 | + inStream.pipe(lob); |
| 117 | + }); |
| 118 | + await connection.commit(); |
| 119 | + |
| 120 | + result = await connection.execute( |
| 121 | + "BEGIN SELECT content INTO :bv FROM " + tableName + " WHERE num = 2; END;", |
| 122 | + { bv: {dir: oracledb.BIND_OUT, type: oracledb.CLOB} }, |
| 123 | + { maxRows: 500 } |
| 124 | + ); |
205 | 125 |
|
| 126 | + const content = await result.outBinds.bv.getData(); |
| 127 | + assert.strictEqual(content.length, dataLength); |
| 128 | + assert.strictEqual(content, rawData); |
| 129 | + }); // 60.2 |
| 130 | + }); |
206 | 131 | });
|
0 commit comments