@@ -160,124 +160,149 @@ describe('1. connection.js', function(){
160
160
} ) ;
161
161
} ) ;
162
162
163
- describe ( '1.2 limits the number of rows fetched' , function ( ) {
164
- var connection = false ;
165
- var createTable =
166
- "BEGIN \
167
- DECLARE \
168
- e_table_missing EXCEPTION; \
169
- PRAGMA EXCEPTION_INIT(e_table_missing, -00942); \
170
- BEGIN \
171
- EXECUTE IMMEDIATE ('DROP TABLE nodb_conn_emp2 PURGE'); \
172
- EXCEPTION \
173
- WHEN e_table_missing \
174
- THEN NULL; \
175
- END; \
176
- EXECUTE IMMEDIATE (' \
177
- CREATE TABLE nodb_conn_emp2 ( \
178
- employee_id NUMBER, \
179
- employee_name VARCHAR2(20) \
180
- ) \
181
- '); \
182
- END; " ;
183
-
184
- var insertRows =
185
- "DECLARE \
186
- x NUMBER := 0; \
187
- n VARCHAR2(20); \
188
- BEGIN \
189
- FOR i IN 1..107 LOOP \
190
- x := x + 1; \
191
- n := 'staff ' || x; \
192
- INSERT INTO nodb_conn_emp2 VALUES (x, n); \
193
- END LOOP; \
194
- END; " ;
195
- var rowsAmount = 107 ;
163
+ describe ( "1.2 'maxRows' property limits the number of fetched rows" , function ( ) {
164
+ var connection = null ;
165
+ var totalAmount = 107 ;
196
166
197
167
before ( function ( done ) {
198
-
199
- oracledb . getConnection (
200
- credentials ,
201
- function ( err , conn ) {
202
- should . not . exist ( err ) ;
203
- connection = conn ;
204
- connection . execute ( createTable , function ( err ) {
168
+ async . series ( [
169
+ function getConn ( cb ) {
170
+ oracledb . getConnection ( dbConfig , function ( err , conn ) {
205
171
should . not . exist ( err ) ;
206
- connection . execute ( insertRows , function ( err ) {
207
- should . not . exist ( err ) ;
208
- done ( ) ;
209
- } ) ;
172
+ connection = conn ;
173
+ cb ( ) ;
210
174
} ) ;
211
- }
212
- ) ;
175
+ } ,
176
+ function createTab ( cb ) {
177
+ var proc = "BEGIN \n" +
178
+ " DECLARE \n" +
179
+ " e_table_missing EXCEPTION; \n" +
180
+ " PRAGMA EXCEPTION_INIT(e_table_missing, -00942); \n" +
181
+ " BEGIN \n" +
182
+ " EXECUTE IMMEDIATE('DROP TABLE nodb_tab_conn_emp2 PURGE'); \n" +
183
+ " EXCEPTION \n" +
184
+ " WHEN e_table_missing \n" +
185
+ " THEN NULL; \n" +
186
+ " END; \n" +
187
+ " EXECUTE IMMEDIATE (' \n" +
188
+ " CREATE TABLE nodb_tab_conn_emp2 ( \n" +
189
+ " id NUMBER NOT NULL, \n" +
190
+ " name VARCHAR2(20) \n" +
191
+ " ) \n" +
192
+ " '); \n" +
193
+ "END; " ;
213
194
214
- } ) ; // before
195
+ connection . execute (
196
+ proc ,
197
+ function ( err ) {
198
+ should . not . exist ( err ) ;
199
+ cb ( ) ;
200
+ }
201
+ ) ;
202
+ } ,
203
+ function insertData ( cb ) {
204
+ var proc = "DECLARE \n" +
205
+ " x NUMBER := 0; \n" +
206
+ " n VARCHAR2(20); \n" +
207
+ "BEGIN \n" +
208
+ " FOR i IN 1..107 LOOP \n" +
209
+ " x := x + 1; \n" +
210
+ " n := 'staff ' || x; \n" +
211
+ " INSERT INTO nodb_tab_conn_emp2 VALUES (x, n); \n" +
212
+ " END LOOP; \n" +
213
+ "END; " ;
215
214
216
- after ( function ( done ) {
217
- connection . execute (
218
- 'DROP TABLE nodb_conn_emp2 PURGE' ,
219
- function ( err ) {
220
- if ( err ) { console . error ( err . message ) ; return ; }
221
- connection . release ( function ( err ) {
222
- if ( err ) { console . error ( err . message ) ; return ; }
223
- done ( ) ;
215
+ connection . execute (
216
+ proc ,
217
+ function ( err ) {
218
+ should . not . exist ( err ) ;
219
+ cb ( ) ;
220
+ }
221
+ ) ;
222
+ }
223
+ ] , done ) ;
224
+ } ) ; // before()
225
+
226
+ after ( function ( done ) {
227
+ async . series ( [
228
+ function ( cb ) {
229
+ connection . execute (
230
+ "DROP TABLE nodb_tab_conn_emp2 PURGE" ,
231
+ function ( err ) {
232
+ should . not . exist ( err ) ;
233
+ cb ( ) ;
234
+ }
235
+ ) ;
236
+ } ,
237
+ function ( cb ) {
238
+ connection . close ( function ( err ) {
239
+ should . not . exist ( err ) ;
240
+ cb ( ) ;
224
241
} ) ;
225
242
}
226
- ) ;
227
- } ) ;
243
+ ] , done ) ;
244
+ } ) ; // after()
228
245
229
- it . skip ( '1.2.1 by default, the number is 100' , function ( done ) {
230
- var defaultLimit = oracledb . maxRows ;
231
- defaultLimit . should . be . exactly ( 100 ) ;
246
+ var verifyRows = function ( rows , amount ) {
247
+ for ( var i = 0 ; i < amount ; i ++ ) {
248
+ should . strictEqual ( rows [ i ] [ 0 ] , ( i + 1 ) ) ;
249
+ should . strictEqual ( rows [ i ] [ 1 ] , ( "staff " + String ( i + 1 ) ) ) ;
250
+ }
251
+ } ;
252
+
253
+ var sqlQuery = "SELECT * FROM nodb_tab_conn_emp2 ORDER BY id" ;
254
+
255
+ it ( '1.2.1 Default maxRows == 0, which means unlimited' , function ( done ) {
256
+ should . strictEqual ( oracledb . maxRows , 0 ) ;
232
257
233
- connection . should . be . ok ( ) ;
234
258
connection . execute (
235
- "SELECT * FROM nodb_conn_emp2 ORDER BY employee_id" ,
259
+ sqlQuery ,
236
260
function ( err , result ) {
237
261
should . not . exist ( err ) ;
238
262
should . exist ( result ) ;
239
- // Return 100 records although the table has 107 rows.
240
- ( result . rows ) . should . have . length ( 100 ) ;
263
+ should . strictEqual ( result . rows . length , totalAmount ) ;
264
+ verifyRows ( result . rows , totalAmount ) ;
241
265
done ( ) ;
242
266
}
243
267
) ;
244
268
} ) ;
245
269
246
- it ( '1.2.2 can also specify for each execution' , function ( done ) {
247
- connection . should . be . ok ( ) ;
270
+ it ( '1.2.2 can be specified for at execution' , function ( done ) {
271
+ var fetchAmount = 25 ;
248
272
connection . execute (
249
- "SELECT * FROM nodb_conn_emp2 ORDER BY employee_id" ,
250
- { } , { maxRows : 25 } ,
273
+ sqlQuery ,
274
+ { } ,
275
+ { maxRows : fetchAmount } ,
251
276
function ( err , result ) {
252
277
should . not . exist ( err ) ;
253
278
should . exist ( result ) ;
254
- // Return 25 records according to execution setting
255
- ( result . rows ) . should . have . length ( 25 ) ;
279
+ should . strictEqual ( result . rows . length , fetchAmount ) ;
280
+ verifyRows ( result . rows , fetchAmount ) ;
256
281
done ( ) ;
257
282
}
258
283
) ;
259
284
} ) ;
260
285
261
- it . skip ( '1.2.3 can not set maxRows to be 0' , function ( done ) {
262
- connection . should . be . ok ( ) ;
286
+ it ( '1.2.3 maxRows == amount of rows) means unlimited' , function ( done ) {
263
287
connection . execute (
264
- "SELECT * FROM nodb_conn_emp2 ORDER BY employee_id" ,
265
- { } , { maxRows : 0 } ,
288
+ sqlQuery ,
289
+ { } ,
290
+ { maxRows : totalAmount } ,
266
291
function ( err , result ) {
267
- should . exist ( err ) ;
268
- ( err . message ) . should . startWith ( 'NJS-026:' ) ;
269
- // NJS-026: maxRows must be greater than zero
270
- should . not . exist ( result ) ;
292
+ should . not . exist ( err ) ;
293
+ should . exist ( result ) ;
294
+ should . strictEqual ( result . rows . length , totalAmount ) ;
295
+ verifyRows ( result . rows , totalAmount ) ;
271
296
done ( ) ;
272
297
}
273
298
) ;
274
299
} ) ;
275
300
276
301
it ( '1.2.4 cannot set maxRows to be a negative number' , function ( done ) {
277
- connection . should . be . ok ( ) ;
278
302
connection . execute (
279
- "SELECT * FROM nodb_conn_emp2 ORDER BY employee_id" ,
280
- { } , { maxRows : - 5 } ,
303
+ sqlQuery ,
304
+ { } ,
305
+ { maxRows : - 5 } ,
281
306
function ( err , result ) {
282
307
should . exist ( err ) ;
283
308
( err . message ) . should . startWith ( 'NJS-007:' ) ;
@@ -287,14 +312,15 @@ describe('1. connection.js', function(){
287
312
) ;
288
313
} ) ;
289
314
290
- it ( '1.2.5 sets maxRows to be very large value' , function ( done ) {
315
+ it ( '1.2.5 sets maxRows to be large value' , function ( done ) {
291
316
connection . execute (
292
- "SELECT * FROM nodb_conn_emp2 ORDER BY employee_id" ,
317
+ sqlQuery ,
293
318
{ } ,
294
- { maxRows : 500000 } ,
319
+ { maxRows : 500000 } ,
295
320
function ( err , result ) {
296
321
should . not . exist ( err ) ;
297
- ( result . rows . length ) . should . eql ( rowsAmount ) ;
322
+ should . exist ( result ) ;
323
+ verifyRows ( result . rows , totalAmount ) ;
298
324
done ( ) ;
299
325
}
300
326
) ;
@@ -305,7 +331,7 @@ describe('1. connection.js', function(){
305
331
306
332
var myoffset = 2 ; // number of rows to skip
307
333
var mymaxnumrows = 6 ; // number of rows to fetch
308
- var sql = "SELECT employee_id, employee_name FROM nodb_conn_emp2 ORDER BY employee_id " ;
334
+ var sql = "SELECT * FROM nodb_tab_conn_emp2 ORDER BY id " ;
309
335
310
336
if ( connection . oracleServerVersion >= 1201000000 ) {
311
337
// 12c row-limiting syntax
0 commit comments