This repository has been archived by the owner on Dec 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathindex.js
218 lines (188 loc) · 5.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
'use strict'
const Result = require('pg/lib/result.js')
const prepare = require('pg/lib/utils.js').prepareValue
const EventEmitter = require('events').EventEmitter
const util = require('util')
let nextUniqueID = 1 // concept borrowed from org.postgresql.core.v3.QueryExecutorImpl
function Cursor(text, values, config) {
EventEmitter.call(this)
this._conf = config || {}
this.text = text
this.values = values ? values.map(prepare) : null
this.connection = null
this._queue = []
this.state = 'initialized'
this._result = new Result(this._conf.rowMode, this._conf.types)
this._cb = null
this._rows = null
this._portal = null
this._ifNoData = this._ifNoData.bind(this)
this._rowDescription = this._rowDescription.bind(this)
}
util.inherits(Cursor, EventEmitter)
Cursor.prototype._ifNoData = function() {
this.state = 'idle'
this._shiftQueue()
}
Cursor.prototype._rowDescription = function() {
if (this.connection) {
this.connection.removeListener('noData', this._ifNoData)
}
}
Cursor.prototype.submit = function(connection) {
this.connection = connection
this._portal = 'C_' + nextUniqueID++
const con = connection
con.parse(
{
text: this.text,
},
true
)
con.bind(
{
portal: this._portal,
values: this.values,
},
true
)
con.describe(
{
type: 'P',
name: this._portal, // AWS Redshift requires a portal name
},
true
)
con.flush()
if (this._conf.types) {
this._result._getTypeParser = this._conf.types.getTypeParser
}
con.once('noData', this._ifNoData)
con.once('rowDescription', this._rowDescription)
}
Cursor.prototype._shiftQueue = function() {
if (this._queue.length) {
this._getRows.apply(this, this._queue.shift())
}
}
Cursor.prototype._closePortal = function() {
// because we opened a named portal to stream results
// we need to close the same named portal. Leaving a named portal
// open can lock tables for modification if inside a transaction.
// see https://github.com/brianc/node-pg-cursor/issues/56
this.connection.close({ type: 'P', name: this._portal })
this.connection.sync()
}
Cursor.prototype.handleRowDescription = function(msg) {
this._result.addFields(msg.fields)
this.state = 'idle'
this._shiftQueue()
}
Cursor.prototype.handleDataRow = function(msg) {
const row = this._result.parseRow(msg.fields)
this.emit('row', row, this._result)
this._rows.push(row)
}
Cursor.prototype._sendRows = function() {
this.state = 'idle'
setImmediate(() => {
const cb = this._cb
// remove callback before calling it
// because likely a new one will be added
// within the call to this callback
this._cb = null
if (cb) {
this._result.rows = this._rows
cb(null, this._rows, this._result)
}
this._rows = []
})
}
Cursor.prototype.handleCommandComplete = function(msg) {
this._result.addCommandComplete(msg)
this._closePortal()
}
Cursor.prototype.handlePortalSuspended = function() {
this._sendRows()
}
Cursor.prototype.handleReadyForQuery = function() {
this._sendRows()
this.state = 'done'
this.emit('end', this._result)
}
Cursor.prototype.handleEmptyQuery = function() {
this.connection.sync()
}
Cursor.prototype.handleError = function(msg) {
this.connection.removeListener('noData', this._ifNoData)
this.connection.removeListener('rowDescription', this._rowDescription)
this.state = 'error'
this._error = msg
// satisfy any waiting callback
if (this._cb) {
this._cb(msg)
}
// dispatch error to all waiting callbacks
for (let i = 0; i < this._queue.length; i++) {
this._queue.pop()[1](msg)
}
if (this.listenerCount('error') > 0) {
// only dispatch error events if we have a listener
this.emit('error', msg)
}
// call sync to keep this connection from hanging
this.connection.sync()
}
Cursor.prototype._getRows = function(rows, cb) {
this.state = 'busy'
this._cb = cb
this._rows = []
const msg = {
portal: this._portal,
rows: rows,
}
this.connection.execute(msg, true)
this.connection.flush()
}
// users really shouldn't be calling 'end' here and terminating a connection to postgres
// via the low level connection.end api
Cursor.prototype.end = util.deprecate(function(cb) {
if (this.state !== 'initialized') {
this.connection.sync()
}
this.connection.once('end', cb)
this.connection.end()
}, 'Cursor.end is deprecated. Call end on the client itself to end a connection to the database.')
Cursor.prototype.close = function(cb) {
if (this.state === 'done') {
if (cb) {
return setImmediate(cb)
} else {
return
}
}
this._closePortal()
this.state = 'done'
if (cb) {
this.connection.once('closeComplete', function() {
cb()
})
}
}
Cursor.prototype.read = function(rows, cb) {
if (this.state === 'idle') {
return this._getRows(rows, cb)
}
if (this.state === 'busy' || this.state === 'initialized') {
return this._queue.push([rows, cb])
}
if (this.state === 'error') {
return setImmediate(() => cb(this._error))
}
if (this.state === 'done') {
return setImmediate(() => cb(null, []))
} else {
throw new Error('Unknown state: ' + this.state)
}
}
module.exports = Cursor