@@ -6,6 +6,7 @@ const util = require('util')
6
6
7
7
let nextUniqueID = 1 // concept borrowed from org.postgresql.core.v3.QueryExecutorImpl
8
8
9
+ << < << << HEAD
9
10
function Cursor ( text , values , config ) {
10
11
EventEmitter . call ( this )
11
12
@@ -30,6 +31,25 @@ Cursor.prototype._ifNoData = function () {
30
31
this . _shiftQueue ( )
31
32
if ( this . connection ) {
32
33
this . connection . removeListener ( 'rowDescription' , this . _rowDescription )
34
+ === = ===
35
+ class Cursor extends EventEmitter {
36
+ constructor ( text , values , config ) {
37
+ super ( )
38
+
39
+ this . _conf = config || { }
40
+ this . text = text
41
+ this . values = values ? values . map ( prepare ) : null
42
+ this . connection = null
43
+ this . _queue = [ ]
44
+ this . state = 'initialized'
45
+ this . _result = new Result ( this . _conf . rowMode , this . _conf . types )
46
+ this . _Promise = this . _conf . Promise || global . Promise
47
+ this . _cb = null
48
+ this . _rows = null
49
+ this . _portal = null
50
+ this . _ifNoData = this . _ifNoData . bind ( this )
51
+ this . _rowDescription = this . _rowDescription . bind ( this )
52
+ >>> > >>> e800f45 ( Add support for using promises in Cursor . read ( ) )
33
53
}
34
54
}
35
55
@@ -216,20 +236,31 @@ Cursor.prototype.close = function (cb) {
216
236
}
217
237
}
218
238
219
- Cursor . prototype . read = function ( rows , cb ) {
220
- if ( this . state === 'idle' || this . state === 'submitted' ) {
221
- return this . _getRows ( rows , cb )
222
- }
223
- if ( this . state === 'busy' || this . state === 'initialized' ) {
224
- return this . _queue . push ( [ rows , cb ] )
225
- }
226
- if ( this . state === 'error' ) {
227
- return setImmediate ( ( ) => cb ( this . _error ) )
228
- }
229
- if ( this . state === 'done' ) {
230
- return setImmediate ( ( ) => cb ( null , [ ] ) )
231
- } else {
232
- throw new Error ( 'Unknown state: ' + this . state )
239
+ read ( rows , cb ) {
240
+ var result
241
+
242
+ if ( ! cb ) {
243
+ result = new this . _Promise ( ( resolve , reject ) => {
244
+ cb = ( err , rows ) => ( err ? reject ( err ) : resolve ( rows ) )
245
+ } )
246
+ }
247
+ if ( this . state === 'idle' || this . state === 'submitted' ) {
248
+ this . _getRows ( rows , cb )
249
+ }
250
+ else if ( this . state === 'busy' || this . state === 'initialized' ) {
251
+ this . _queue . push ( [ rows , cb ] )
252
+ }
253
+ else if ( this . state === 'error' ) {
254
+ setImmediate ( ( ) => cb ( this . _error ) )
255
+ }
256
+ else if ( this . state === 'done' ) {
257
+ setImmediate ( ( ) => cb ( null , [ ] ) )
258
+ } else {
259
+ throw new Error ( 'Unknown state: ' + this . state )
260
+ }
261
+
262
+ // Return the promise (or undefined)
263
+ return result
233
264
}
234
265
}
235
266
0 commit comments