32
32
'use strict' ;
33
33
34
34
const oracledb = require ( 'oracledb' ) ;
35
- const should = require ( 'should' ) ;
36
- const async = require ( 'async' ) ;
35
+ const assert = require ( 'assert' ) ;
37
36
const dbConfig = require ( './dbconfig.js' ) ;
38
- const file = require ( './file.js' ) ;
39
- const sql = require ( './sql.js' ) ;
40
37
const fs = require ( 'fs' ) ;
38
+ const fsPromises = require ( 'fs/promises' ) ;
39
+ const random = require ( './random.js' ) ;
41
40
42
41
describe ( '128.clobStream.js' , function ( ) {
43
- let connection = null ;
42
+ let connection ;
44
43
const fileRoot = "." ;
45
44
let insertID = 1 ;
46
- let inFileName ;
47
45
48
46
const proc_clob_prepare_tab = "BEGIN \n" +
49
47
" DECLARE \n" +
@@ -63,148 +61,81 @@ describe('128.clobStream.js', function() {
63
61
" '); \n" +
64
62
"END; " ;
65
63
66
- before ( function ( done ) {
67
- async . series ( [
68
- function ( cb ) {
69
- oracledb . getConnection ( dbConfig , function ( err , conn ) {
70
- should . not . exist ( err ) ;
71
- connection = conn ;
72
- cb ( ) ;
73
- } ) ;
74
- } ,
75
- function ( cb ) {
76
- setupAllTable ( cb ) ;
77
- }
78
- ] , done ) ;
79
- } ) ; // before
64
+ before ( async function ( ) {
65
+ connection = await oracledb . getConnection ( dbConfig ) ;
66
+ await connection . execute ( proc_clob_prepare_tab ) ;
67
+ } ) ;
80
68
81
- after ( function ( done ) {
82
- async . series ( [
83
- function ( cb ) {
84
- dropAllTable ( cb ) ;
85
- } ,
86
- function ( cb ) {
87
- connection . release ( function ( err ) {
88
- should . not . exist ( err ) ;
89
- cb ( ) ;
90
- } ) ;
91
- }
92
- ] , done ) ;
93
- } ) ; // after
69
+ after ( async function ( ) {
70
+ await connection . execute ( "DROP TABLE nodb_tab_lobs_pre PURGE" ) ;
71
+ await connection . close ( ) ;
72
+ } ) ;
94
73
95
- beforeEach ( function ( done ) {
74
+ beforeEach ( function ( ) {
96
75
insertID ++ ;
97
- done ( ) ;
98
76
} ) ;
99
77
100
78
describe ( '128.1 stream txt file into CLOB column' , function ( ) {
101
- it ( '128.1.1 works with 64KB txt file' , function ( done ) {
102
- inFileName = fileRoot + '/smallString.txt' ;
103
- let selectID = insertID + 200 ;
104
- let specialStr = '128.1.1' ;
105
- let fileSize = 65536 ;
106
-
107
- bindIn_small ( inFileName , fileSize , selectID , insertID , specialStr , done ) ;
79
+ it ( '128.1.1 works with 64KB txt file' , async function ( ) {
80
+ const inFileName = fileRoot + '/smallString.txt' ;
81
+ const selectID = insertID + 200 ;
82
+ const specialStr = '128.1.1' ;
83
+ const fileSize = 65536 ;
84
+ await bindIn_small ( inFileName , fileSize , selectID , insertID , specialStr ) ;
108
85
} ) ;
109
86
110
- it ( '128.1.2 works with 64KB+1 txt file' , function ( done ) {
111
- inFileName = fileRoot + '/smallString.txt' ;
112
- let selectID = insertID + 200 ;
113
- let specialStr = '128.1.2' ;
114
- let fileSize = 655376 ;
115
-
116
- bindIn_small ( inFileName , fileSize , selectID , insertID , specialStr , done ) ;
87
+ it ( '128.1.2 works with 64KB+1 txt file' , async function ( ) {
88
+ const inFileName = fileRoot + '/smallString.txt' ;
89
+ const selectID = insertID + 200 ;
90
+ const specialStr = '128.1.2' ;
91
+ const fileSize = 655376 ;
92
+ await bindIn_small ( inFileName , fileSize , selectID , insertID , specialStr ) ;
117
93
} ) ;
118
94
119
- it ( '128.1.3 works with 1MB+1 txt file' , function ( done ) {
120
- inFileName = fileRoot + '/smallString.txt' ;
121
- let selectID = insertID + 200 ;
122
- let specialStr = '128.1.3' ;
123
- let fileSize = 1 * 1024 * 1024 ;
124
-
125
- bindIn_small ( inFileName , fileSize , selectID , insertID , specialStr , done ) ;
95
+ it ( '128.1.3 works with 1MB+1 txt file' , async function ( ) {
96
+ const inFileName = fileRoot + '/smallString.txt' ;
97
+ const selectID = insertID + 200 ;
98
+ const specialStr = '128.1.3' ;
99
+ const fileSize = 1 * 1024 * 1024 ;
100
+ await bindIn_small ( inFileName , fileSize , selectID , insertID , specialStr ) ;
126
101
} ) ;
127
102
128
103
} ) ; // 4.1
129
104
130
- var bindIn_small = function ( inFileName , fileSize , selectID , insertID , specialStr , callback ) {
131
- async . series ( [
132
- function ( cb ) {
133
- file . createFileInKB ( inFileName , fileSize , specialStr ) ;
134
- cb ( ) ;
135
- } ,
136
- function ( cb ) {
137
- insetTableWithClob ( selectID , inFileName , cb ) ;
138
- } ,
139
- function ( cb ) {
140
- verifyClob ( selectID , insertID , fileSize , cb ) ;
141
- } ,
142
- function ( cb ) {
143
- file . delete ( inFileName ) ;
144
- cb ( ) ;
145
- }
146
- ] , callback ) ;
147
- } ;
148
-
149
- var setupAllTable = function ( callback ) {
150
- connection . execute (
151
- proc_clob_prepare_tab ,
152
- function ( err ) {
153
- should . not . exist ( err ) ;
154
- callback ( ) ;
155
- } ) ;
156
- } ;
157
-
158
- var dropAllTable = function ( callback ) {
159
- connection . execute (
160
- "DROP TABLE nodb_tab_lobs_pre PURGE" ,
161
- function ( err ) {
162
- should . not . exist ( err ) ;
163
- callback ( ) ;
164
- } ) ;
105
+ const bindIn_small = async function ( fileName , fileSize , selectID , insertID , specialStr ) {
106
+ const bigStr = random . getRandomString ( fileSize , specialStr ) ;
107
+ await fsPromises . writeFile ( fileName , bigStr ) ;
108
+ await insertTableWithClob ( selectID , fileName ) ;
109
+ await verifyClob ( selectID , insertID , fileSize ) ;
110
+ await fsPromises . unlink ( fileName ) ;
165
111
} ;
166
112
167
- var insetTableWithClob = function ( id , inFileName , callback ) {
168
- var sql = "INSERT INTO nodb_tab_lobs_pre (id, clob) VALUES (:i, EMPTY_CLOB()) RETURNING clob INTO :lobbv" ;
169
- var bindVar = { i : id , lobbv : { type : oracledb . CLOB , dir : oracledb . BIND_OUT } } ;
170
-
171
- connection . execute (
172
- sql ,
173
- bindVar ,
174
- { autoCommit : false } , // a transaction needs to span the INSERT and pipe()
175
- function ( err , result ) {
176
- should . not . exist ( err ) ;
177
- ( result . rowsAffected ) . should . be . exactly ( 1 ) ;
178
- ( result . outBinds . lobbv . length ) . should . be . exactly ( 1 ) ;
179
-
180
- var inStream = fs . createReadStream ( inFileName ) ;
181
- var lob = result . outBinds . lobbv [ 0 ] ;
182
-
183
- lob . on ( 'error' , function ( err ) {
184
- should . not . exist ( err ) ;
185
- } ) ;
186
-
187
- inStream . on ( 'error' , function ( err ) {
188
- should . not . exist ( err ) ;
189
- } ) ;
190
-
191
- lob . on ( 'finish' , function ( ) {
192
- connection . commit ( function ( err ) {
193
- should . not . exist ( err ) ;
194
- callback ( ) ;
195
- } ) ;
196
- } ) ;
197
-
198
- inStream . pipe ( lob ) ; // copies the text to the CLOB
199
- }
200
- ) ;
113
+ const insertTableWithClob = async function ( id , fileName ) {
114
+ const sql = "INSERT INTO nodb_tab_lobs_pre (id, clob) VALUES (:i, EMPTY_CLOB()) RETURNING clob INTO :lobbv" ;
115
+ const binds = {
116
+ i : id ,
117
+ lobbv : { type : oracledb . CLOB , dir : oracledb . BIND_OUT }
118
+ } ;
119
+ // a transaction needs to span the INSERT and pipe()
120
+ const options = { autoCommit : false } ;
121
+ const result = await connection . execute ( sql , binds , options ) ;
122
+ assert . strictEqual ( result . rowsAffected , 1 ) ;
123
+ assert . strictEqual ( result . outBinds . lobbv . length , 1 ) ;
124
+ const inStream = fs . createReadStream ( fileName ) ;
125
+ const lob = result . outBinds . lobbv [ 0 ] ;
126
+ await new Promise ( ( resolve , reject ) => {
127
+ lob . on ( 'error' , reject ) ;
128
+ inStream . on ( 'error' , reject ) ;
129
+ lob . on ( 'finish' , resolve ) ;
130
+ inStream . pipe ( lob ) ;
131
+ } ) ;
132
+ await connection . commit ( ) ;
201
133
} ;
202
134
203
- var verifyClob = function ( selectID , insertID , lenExpected , callback ) {
204
- var lob = { } ;
205
- var selectSql = "select clob from nodb_tab_lobs_pre where id = " + selectID ;
206
- var insetSql = "INSERT INTO nodb_tab_lobs_pre (id, clob) VALUES (:i, :c)" ;
207
- var proc_compare_clob = "CREATE OR REPLACE PROCEDURE nodb_clob_compare(result OUT NUMBER, len OUT NUMBER) \n" +
135
+ const verifyClob = async function ( selectID , insertID , lenExpected ) {
136
+ const selectSql = "select clob from nodb_tab_lobs_pre where id = " + selectID ;
137
+ const insertSql = "INSERT INTO nodb_tab_lobs_pre (id, clob) VALUES (:i, :c)" ;
138
+ const proc_compare_clob = "CREATE OR REPLACE PROCEDURE nodb_clob_compare(result OUT NUMBER, len OUT NUMBER) \n" +
208
139
"IS \n" +
209
140
" clob1 CLOB; \n" +
210
141
" clob2 CLOB; \n" +
@@ -214,56 +145,27 @@ describe('128.clobStream.js', function() {
214
145
" result := DBMS_LOB.COMPARE(clob1, clob2); \n" + // Zero if the comparison succeeds, nonzero if not.
215
146
" len := length(clob1); \n" +
216
147
"END nodb_clob_compare;" ;
217
- var sqlRunComparePorc = "begin nodb_clob_compare(:r, :l); end;" ;
218
- var sqlDropComparePorc = "DROP PROCEDURE nodb_clob_compare" ;
219
-
220
- async . series ( [
221
- function ( cb ) {
222
- connection . execute (
223
- selectSql ,
224
- function ( err , result ) {
225
- should . not . exist ( err ) ;
226
- lob = result . rows [ 0 ] [ 0 ] ;
227
- should . exist ( lob ) ;
228
- cb ( ) ;
229
- }
230
- ) ;
231
- } ,
232
- function ( cb ) {
233
- var bindVar = { i : insertID , c : { val : lob , type : oracledb . CLOB , dir : oracledb . BIND_IN } } ;
234
- connection . execute (
235
- insetSql ,
236
- bindVar ,
237
- { autoCommit : true } ,
238
- function ( err ) {
239
- should . not . exist ( err ) ;
240
- lob . close ( cb ) ;
241
- }
242
- ) ;
243
- } ,
244
- function ( cb ) {
245
- sql . executeSql ( connection , proc_compare_clob , { } , { } , cb ) ;
246
- } ,
247
- function ( cb ) {
248
- var bindVar = {
249
- r : { type : oracledb . NUMBER , dir : oracledb . BIND_OUT } ,
250
- l : { type : oracledb . NUMBER , dir : oracledb . BIND_OUT }
251
- } ;
252
- connection . execute (
253
- sqlRunComparePorc ,
254
- bindVar ,
255
- function ( err , result ) {
256
- should . not . exist ( err ) ;
257
- result . outBinds . r . should . eql ( 0 ) ;
258
- should . strictEqual ( result . outBinds . l , lenExpected ) ;
259
- cb ( ) ;
260
- } ) ;
261
- } ,
262
- function ( cb ) {
263
- sql . executeSql ( connection , sqlDropComparePorc , { } , { } , cb ) ;
264
- }
265
- ] , callback ) ;
266
-
148
+ const sqlRunCompareProc = "begin nodb_clob_compare(:r, :l); end;" ;
149
+ const sqlDropCompareProc = "DROP PROCEDURE nodb_clob_compare" ;
150
+
151
+ let result = await connection . execute ( selectSql ) ;
152
+ const lob = result . rows [ 0 ] [ 0 ] ;
153
+ let binds = {
154
+ i : insertID ,
155
+ c : { val : lob , type : oracledb . CLOB , dir : oracledb . BIND_IN }
156
+ } ;
157
+ const options = { autoCommit : true } ;
158
+ await connection . execute ( insertSql , binds , options ) ;
159
+ await lob . close ( ) ;
160
+ await connection . execute ( proc_compare_clob ) ;
161
+ binds = {
162
+ r : { type : oracledb . NUMBER , dir : oracledb . BIND_OUT } ,
163
+ l : { type : oracledb . NUMBER , dir : oracledb . BIND_OUT }
164
+ } ;
165
+ result = await connection . execute ( sqlRunCompareProc , binds ) ;
166
+ assert . strictEqual ( result . outBinds . r , 0 ) ;
167
+ assert . strictEqual ( result . outBinds . l , lenExpected ) ;
168
+ await connection . execute ( sqlDropCompareProc ) ;
267
169
} ;
268
170
269
171
} ) ;
0 commit comments