@@ -35,21 +35,36 @@ exports.OCCITIMESTAMP = 7;
3535exports . OCCINUMBER = 8 ;
3636exports . OCCIBLOB = 9 ;
3737
38- // Reader.prototype.nextRow is implemented in JS rather than C++.
39- // This is easier and also more efficient because we don't cross the JS/C++ boundary every time
40- // we read a record.
41- bindings . Reader . prototype . nextRow = function ( cb ) {
38+ // Reader is implemented in JS around a C++ handle
39+ // This is easier and also more efficient because we don't cross the JS/C++ boundary
40+ // every time we read a record.
41+ function Reader ( handle ) {
42+ this . _handle = handle ;
43+ this . _error = null ;
44+ this . _rows = [ ] ;
45+ }
46+
47+ Reader . prototype . nextRows = function ( cb , count ) {
48+ this . _handle . nextRows ( cb , count ) ;
49+ }
50+
51+ Reader . prototype . nextRow = function ( cb ) {
4252 var self = this ;
43- if ( self . _error || ( self . _rows && self . _rows . length > 0 ) ) {
53+ if ( ! self . _handle || self . _error || ( self . _rows && self . _rows . length > 0 ) ) {
4454 process . nextTick ( function ( ) {
4555 cb ( self . _error , self . _rows && self . _rows . shift ( ) ) ;
4656 } ) ;
4757 } else {
4858 // nextRows willl use the prefetch row count as window size
49- self . nextRows ( function ( err , result ) {
59+ self . _handle . nextRows ( function ( err , result ) {
5060 self . _error = err || self . _error ;
5161 self . _rows = result ;
62+ if ( err || result . length === 0 ) self . _handle = null ;
5263 cb ( self . _error , self . _rows && self . _rows . shift ( ) ) ;
5364 } ) ;
5465 }
5566} ;
67+
68+ bindings . Connection . prototype . reader = function ( sql , args ) {
69+ return new Reader ( this . readerHandle ( sql , args ) ) ;
70+ }
0 commit comments