@@ -4,9 +4,9 @@ const prepare = require('pg/lib/utils.js').prepareValue
4
4
const EventEmitter = require ( 'events' ) . EventEmitter
5
5
const util = require ( 'util' )
6
6
7
- var nextUniqueID = 1 // concept borrowed from org.postgresql.core.v3.QueryExecutorImpl
7
+ let nextUniqueID = 1 // concept borrowed from org.postgresql.core.v3.QueryExecutorImpl
8
8
9
- function Cursor ( text , values , config ) {
9
+ function Cursor ( text , values , config ) {
10
10
EventEmitter . call ( this )
11
11
12
12
this . _conf = config || { }
@@ -15,33 +15,42 @@ function Cursor (text, values, config) {
15
15
this . connection = null
16
16
this . _queue = [ ]
17
17
this . state = 'initialized'
18
- this . _result = new Result ( this . _conf . rowMode )
18
+ this . _result = new Result ( this . _conf . rowMode , this . _conf . types )
19
19
this . _cb = null
20
20
this . _rows = null
21
21
this . _portal = null
22
22
}
23
23
24
24
util . inherits ( Cursor , EventEmitter )
25
25
26
- Cursor . prototype . submit = function ( connection ) {
26
+ Cursor . prototype . submit = function ( connection ) {
27
27
this . connection = connection
28
- this . _portal = 'C_' + ( nextUniqueID ++ )
28
+ this . _portal = 'C_' + nextUniqueID ++
29
29
30
30
const con = connection
31
31
32
- con . parse ( {
33
- text : this . text
34
- } , true )
35
-
36
- con . bind ( {
37
- portal : this . _portal ,
38
- values : this . values
39
- } , true )
40
-
41
- con . describe ( {
42
- type : 'P' ,
43
- name : this . _portal // AWS Redshift requires a portal name
44
- } , true )
32
+ con . parse (
33
+ {
34
+ text : this . text ,
35
+ } ,
36
+ true
37
+ )
38
+
39
+ con . bind (
40
+ {
41
+ portal : this . _portal ,
42
+ values : this . values ,
43
+ } ,
44
+ true
45
+ )
46
+
47
+ con . describe (
48
+ {
49
+ type : 'P' ,
50
+ name : this . _portal , // AWS Redshift requires a portal name
51
+ } ,
52
+ true
53
+ )
45
54
46
55
con . flush ( )
47
56
@@ -60,25 +69,25 @@ Cursor.prototype.submit = function (connection) {
60
69
} )
61
70
}
62
71
63
- Cursor . prototype . _shiftQueue = function ( ) {
72
+ Cursor . prototype . _shiftQueue = function ( ) {
64
73
if ( this . _queue . length ) {
65
74
this . _getRows . apply ( this , this . _queue . shift ( ) )
66
75
}
67
76
}
68
77
69
- Cursor . prototype . handleRowDescription = function ( msg ) {
78
+ Cursor . prototype . handleRowDescription = function ( msg ) {
70
79
this . _result . addFields ( msg . fields )
71
80
this . state = 'idle'
72
81
this . _shiftQueue ( )
73
82
}
74
83
75
- Cursor . prototype . handleDataRow = function ( msg ) {
84
+ Cursor . prototype . handleDataRow = function ( msg ) {
76
85
const row = this . _result . parseRow ( msg . fields )
77
86
this . emit ( 'row' , row , this . _result )
78
87
this . _rows . push ( row )
79
88
}
80
89
81
- Cursor . prototype . _sendRows = function ( ) {
90
+ Cursor . prototype . _sendRows = function ( ) {
82
91
this . state = 'idle'
83
92
setImmediate ( ( ) => {
84
93
const cb = this . _cb
@@ -94,34 +103,34 @@ Cursor.prototype._sendRows = function () {
94
103
} )
95
104
}
96
105
97
- Cursor . prototype . handleCommandComplete = function ( msg ) {
106
+ Cursor . prototype . handleCommandComplete = function ( msg ) {
98
107
this . _result . addCommandComplete ( msg )
99
108
this . connection . sync ( )
100
109
}
101
110
102
- Cursor . prototype . handlePortalSuspended = function ( ) {
111
+ Cursor . prototype . handlePortalSuspended = function ( ) {
103
112
this . _sendRows ( )
104
113
}
105
114
106
- Cursor . prototype . handleReadyForQuery = function ( ) {
115
+ Cursor . prototype . handleReadyForQuery = function ( ) {
107
116
this . _sendRows ( )
108
117
this . emit ( 'end' , this . _result )
109
118
this . state = 'done'
110
119
}
111
120
112
- Cursor . prototype . handleEmptyQuery = function ( ) {
121
+ Cursor . prototype . handleEmptyQuery = function ( ) {
113
122
this . connection . sync ( )
114
123
}
115
124
116
- Cursor . prototype . handleError = function ( msg ) {
125
+ Cursor . prototype . handleError = function ( msg ) {
117
126
this . state = 'error'
118
127
this . _error = msg
119
128
// satisfy any waiting callback
120
129
if ( this . _cb ) {
121
130
this . _cb ( msg )
122
131
}
123
132
// dispatch error to all waiting callbacks
124
- for ( var i = 0 ; i < this . _queue . length ; i ++ ) {
133
+ for ( let i = 0 ; i < this . _queue . length ; i ++ ) {
125
134
this . _queue . pop ( ) [ 1 ] ( msg )
126
135
}
127
136
@@ -133,41 +142,41 @@ Cursor.prototype.handleError = function (msg) {
133
142
this . connection . sync ( )
134
143
}
135
144
136
- Cursor . prototype . _getRows = function ( rows , cb ) {
145
+ Cursor . prototype . _getRows = function ( rows , cb ) {
137
146
this . state = 'busy'
138
147
this . _cb = cb
139
148
this . _rows = [ ]
140
149
const msg = {
141
150
portal : this . _portal ,
142
- rows : rows
151
+ rows : rows ,
143
152
}
144
153
this . connection . execute ( msg , true )
145
154
this . connection . flush ( )
146
155
}
147
156
148
- Cursor . prototype . end = function ( cb ) {
157
+ Cursor . prototype . end = function ( cb ) {
149
158
if ( this . state !== 'initialized' ) {
150
159
this . connection . sync ( )
151
160
}
152
161
this . connection . once ( 'end' , cb )
153
162
this . connection . end ( )
154
163
}
155
164
156
- Cursor . prototype . close = function ( cb ) {
165
+ Cursor . prototype . close = function ( cb ) {
157
166
if ( this . state === 'done' ) {
158
167
return setImmediate ( cb )
159
168
}
160
169
this . connection . close ( { type : 'P' } )
161
170
this . connection . sync ( )
162
171
this . state = 'done'
163
172
if ( cb ) {
164
- this . connection . once ( 'closeComplete' , function ( ) {
173
+ this . connection . once ( 'closeComplete' , function ( ) {
165
174
cb ( )
166
175
} )
167
176
}
168
177
}
169
178
170
- Cursor . prototype . read = function ( rows , cb ) {
179
+ Cursor . prototype . read = function ( rows , cb ) {
171
180
if ( this . state === 'idle' ) {
172
181
return this . _getRows ( rows , cb )
173
182
}
0 commit comments