1
- var Result = require ( './pg' ) . Result
2
- var prepare = require ( './pg' ) . prepareValue
3
- var EventEmitter = require ( 'events' ) . EventEmitter ;
4
- var util = require ( 'util' ) ;
1
+ 'use strict'
2
+ const Result = require ( './pg' ) . Result
3
+ const prepare = require ( './pg' ) . prepareValue
4
+ const EventEmitter = require ( 'events' ) . EventEmitter
5
+ const util = require ( 'util' )
5
6
6
7
function Cursor ( text , values ) {
7
- EventEmitter . call ( this ) ;
8
+ EventEmitter . call ( this )
8
9
9
10
this . text = text
10
11
this . values = values ? values . map ( prepare ) : null
@@ -18,11 +19,10 @@ function Cursor (text, values) {
18
19
19
20
util . inherits ( Cursor , EventEmitter )
20
21
21
- Cursor . prototype . submit = function ( connection ) {
22
+ Cursor . prototype . submit = function ( connection ) {
22
23
this . connection = connection
23
24
24
- var con = connection
25
- var self = this
25
+ const con = connection
26
26
27
27
con . parse ( {
28
28
text : this . text
@@ -34,145 +34,143 @@ Cursor.prototype.submit = function(connection) {
34
34
35
35
con . describe ( {
36
36
type : 'P' ,
37
- name : '' //use unamed portal
37
+ name : '' // use unamed portal
38
38
} , true )
39
39
40
40
con . flush ( )
41
41
42
+ const ifNoData = ( ) => {
43
+ this . state = 'idle'
44
+ this . _shiftQueue ( )
45
+ }
46
+
42
47
con . once ( 'noData' , ifNoData )
43
48
con . once ( 'rowDescription' , function ( ) {
44
- con . removeListener ( 'noData' , ifNoData ) ;
45
- } ) ;
46
-
47
- function ifNoData ( ) {
48
- self . state = 'idle'
49
- self . _shiftQueue ( ) ;
50
- }
49
+ con . removeListener ( 'noData' , ifNoData )
50
+ } )
51
51
}
52
52
53
53
Cursor . prototype . _shiftQueue = function ( ) {
54
- if ( this . _queue . length ) {
54
+ if ( this . _queue . length ) {
55
55
this . _getRows . apply ( this , this . _queue . shift ( ) )
56
56
}
57
57
}
58
58
59
- Cursor . prototype . handleRowDescription = function ( msg ) {
59
+ Cursor . prototype . handleRowDescription = function ( msg ) {
60
60
this . _result . addFields ( msg . fields )
61
61
this . state = 'idle'
62
- this . _shiftQueue ( ) ;
62
+ this . _shiftQueue ( )
63
63
}
64
64
65
- Cursor . prototype . handleDataRow = function ( msg ) {
66
- var row = this . _result . parseRow ( msg . fields )
65
+ Cursor . prototype . handleDataRow = function ( msg ) {
66
+ const row = this . _result . parseRow ( msg . fields )
67
67
this . emit ( 'row' , row , this . _result )
68
68
this . _rows . push ( row )
69
69
}
70
70
71
- Cursor . prototype . _sendRows = function ( ) {
71
+ Cursor . prototype . _sendRows = function ( ) {
72
72
this . state = 'idle'
73
- setImmediate ( function ( ) {
74
- var cb = this . _cb
75
- //remove callback before calling it
76
- //because likely a new one will be added
77
- //within the call to this callback
73
+ setImmediate ( ( ) => {
74
+ const cb = this . _cb
75
+ // remove callback before calling it
76
+ // because likely a new one will be added
77
+ // within the call to this callback
78
78
this . _cb = null
79
- if ( cb ) {
79
+ if ( cb ) {
80
80
this . _result . rows = this . _rows
81
81
cb ( null , this . _rows , this . _result )
82
82
}
83
83
this . _rows = [ ]
84
- } . bind ( this ) )
84
+ } )
85
85
}
86
86
87
- Cursor . prototype . handleCommandComplete = function ( ) {
87
+ Cursor . prototype . handleCommandComplete = function ( ) {
88
88
this . connection . sync ( )
89
89
}
90
90
91
- Cursor . prototype . handlePortalSuspended = function ( ) {
91
+ Cursor . prototype . handlePortalSuspended = function ( ) {
92
92
this . _sendRows ( )
93
93
}
94
94
95
- Cursor . prototype . handleReadyForQuery = function ( ) {
95
+ Cursor . prototype . handleReadyForQuery = function ( ) {
96
96
this . _sendRows ( )
97
97
this . emit ( 'end' , this . _result )
98
98
this . state = 'done'
99
99
}
100
100
101
- Cursor . prototype . handleEmptyQuery = function ( ) {
101
+ Cursor . prototype . handleEmptyQuery = function ( ) {
102
102
this . connection . sync ( )
103
- } ;
103
+ }
104
104
105
- Cursor . prototype . handleError = function ( msg ) {
105
+ Cursor . prototype . handleError = function ( msg ) {
106
106
this . state = 'error'
107
107
this . _error = msg
108
- //satisfy any waiting callback
109
- if ( this . _cb ) {
108
+ // satisfy any waiting callback
109
+ if ( this . _cb ) {
110
110
this . _cb ( msg )
111
111
}
112
- //dispatch error to all waiting callbacks
113
- for ( var i = 0 ; i < this . _queue . length ; i ++ ) {
112
+ // dispatch error to all waiting callbacks
113
+ for ( var i = 0 ; i < this . _queue . length ; i ++ ) {
114
114
this . _queue . pop ( ) [ 1 ] ( msg )
115
115
}
116
116
117
117
if ( this . listenerCount ( 'error' ) > 0 ) {
118
- //only dispatch error events if we have a listener
118
+ // only dispatch error events if we have a listener
119
119
this . emit ( 'error' , msg )
120
120
}
121
- //call sync to keep this connection from hanging
121
+ // call sync to keep this connection from hanging
122
122
this . connection . sync ( )
123
123
}
124
124
125
- Cursor . prototype . _getRows = function ( rows , cb ) {
125
+ Cursor . prototype . _getRows = function ( rows , cb ) {
126
126
this . state = 'busy'
127
127
this . _cb = cb
128
128
this . _rows = [ ]
129
- var msg = {
129
+ const msg = {
130
130
portal : '' ,
131
131
rows : rows
132
132
}
133
133
this . connection . execute ( msg , true )
134
134
this . connection . flush ( )
135
135
}
136
136
137
- Cursor . prototype . end = function ( cb ) {
138
- if ( this . state != 'initialized' ) {
137
+ Cursor . prototype . end = function ( cb ) {
138
+ if ( this . state != = 'initialized' ) {
139
139
this . connection . sync ( )
140
140
}
141
141
this . connection . stream . once ( 'end' , cb )
142
142
console . log ( 'calling end on connection' )
143
143
this . connection . end ( )
144
144
}
145
145
146
- Cursor . prototype . close = function ( cb ) {
147
- if ( this . state == 'done' ) {
146
+ Cursor . prototype . close = function ( cb ) {
147
+ if ( this . state === 'done' ) {
148
148
return setImmediate ( cb )
149
149
}
150
150
this . connection . close ( { type : 'P' } )
151
151
this . connection . sync ( )
152
152
this . state = 'done'
153
- if ( cb ) {
154
- this . connection . once ( 'closeComplete' , function ( ) {
153
+ if ( cb ) {
154
+ this . connection . once ( 'closeComplete' , function ( ) {
155
155
cb ( )
156
156
} )
157
157
}
158
158
}
159
159
160
- Cursor . prototype . read = function ( rows , cb ) {
161
- var self = this
162
- if ( this . state == 'idle' ) {
160
+ Cursor . prototype . read = function ( rows , cb ) {
161
+ if ( this . state === 'idle' ) {
163
162
return this . _getRows ( rows , cb )
164
163
}
165
- if ( this . state == 'busy' || this . state == 'initialized' ) {
164
+ if ( this . state === 'busy' || this . state = == 'initialized' ) {
166
165
return this . _queue . push ( [ rows , cb ] )
167
166
}
168
- if ( this . state == 'error' ) {
167
+ if ( this . state = == 'error' ) {
169
168
return setImmediate ( ( ) => cb ( this . _error ) )
170
169
}
171
- if ( this . state == 'done' ) {
170
+ if ( this . state = == 'done' ) {
172
171
return setImmediate ( ( ) => cb ( null , [ ] ) )
173
- }
174
- else {
175
- throw new Error ( "Unknown state: " + this . state )
172
+ } else {
173
+ throw new Error ( 'Unknown state: ' + this . state )
176
174
}
177
175
}
178
176
0 commit comments