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