Skip to content

Commit ff09b3f

Browse files
authored
Merge branch 'master' into fix-closing-finished-connections
2 parents 24e485e + 6d47026 commit ff09b3f

File tree

13 files changed

+1525
-193
lines changed

13 files changed

+1525
-193
lines changed

.eslintrc

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
{
2-
"extends": "standard",
2+
"extends": ["eslint:recommended"],
3+
"plugins": ["prettier"],
4+
"rules": {
5+
"prettier/prettier": "error",
6+
"prefer-const": "error",
7+
"no-var": "error"
8+
},
39
"env": {
10+
"es6": true,
11+
"node": true,
412
"mocha": true
5-
},
6-
"rules": {
7-
"no-new-func": "off"
813
}
914
}

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ language: node_js
22
dist: trusty
33
sudo: false
44
node_js:
5-
- "4.2"
6-
- "6"
75
- "8"
6+
- "10"
7+
- "12"
88
env:
99
- PGUSER=postgres
1010
services:

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
.PHONY: publish-patch test
2-
1+
.PHONY: test
32
test:
43
npm test
54

5+
.PHONY: patch
66
patch: test
77
npm version patch -m "Bump version"
88
git push origin master --tags
99
npm publish
1010

11+
.PHONY: minor
1112
minor: test
1213
npm version minor -m "Bump version"
1314
git push origin master --tags

index.js

Lines changed: 52 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,56 @@
11
'use strict'
2-
const Result = require('./pg').Result
3-
const prepare = require('./pg').prepareValue
2+
const Result = require('pg/lib/result.js')
3+
const prepare = require('pg/lib/utils.js').prepareValue
44
const EventEmitter = require('events').EventEmitter
55
const util = require('util')
66

7-
function Cursor (text, values, config) {
7+
let nextUniqueID = 1 // concept borrowed from org.postgresql.core.v3.QueryExecutorImpl
8+
9+
function Cursor(text, values, config) {
810
EventEmitter.call(this)
911

10-
this._conf = config || { }
12+
this._conf = config || {}
1113
this.text = text
1214
this.values = values ? values.map(prepare) : null
1315
this.connection = null
1416
this._queue = []
1517
this.state = 'initialized'
16-
this._result = new Result(this._conf.rowMode)
18+
this._result = new Result(this._conf.rowMode, this._conf.types)
1719
this._cb = null
1820
this._rows = null
21+
this._portal = null
1922
}
2023

2124
util.inherits(Cursor, EventEmitter)
2225

23-
Cursor.prototype.submit = function (connection) {
26+
Cursor.prototype.submit = function(connection) {
2427
this.connection = connection
28+
this._portal = 'C_' + nextUniqueID++
2529

2630
const con = connection
2731

28-
con.parse({
29-
text: this.text
30-
}, true)
31-
32-
con.bind({
33-
values: this.values
34-
}, true)
35-
36-
con.describe({
37-
type: 'P',
38-
name: '' // use unamed portal
39-
}, 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+
)
4054

4155
con.flush()
4256

@@ -55,25 +69,25 @@ Cursor.prototype.submit = function (connection) {
5569
})
5670
}
5771

58-
Cursor.prototype._shiftQueue = function () {
72+
Cursor.prototype._shiftQueue = function() {
5973
if (this._queue.length) {
6074
this._getRows.apply(this, this._queue.shift())
6175
}
6276
}
6377

64-
Cursor.prototype.handleRowDescription = function (msg) {
78+
Cursor.prototype.handleRowDescription = function(msg) {
6579
this._result.addFields(msg.fields)
6680
this.state = 'idle'
6781
this._shiftQueue()
6882
}
6983

70-
Cursor.prototype.handleDataRow = function (msg) {
84+
Cursor.prototype.handleDataRow = function(msg) {
7185
const row = this._result.parseRow(msg.fields)
7286
this.emit('row', row, this._result)
7387
this._rows.push(row)
7488
}
7589

76-
Cursor.prototype._sendRows = function () {
90+
Cursor.prototype._sendRows = function() {
7791
this.state = 'idle'
7892
setImmediate(() => {
7993
const cb = this._cb
@@ -89,33 +103,34 @@ Cursor.prototype._sendRows = function () {
89103
})
90104
}
91105

92-
Cursor.prototype.handleCommandComplete = function () {
106+
Cursor.prototype.handleCommandComplete = function(msg) {
107+
this._result.addCommandComplete(msg)
93108
this.connection.sync()
94109
}
95110

96-
Cursor.prototype.handlePortalSuspended = function () {
111+
Cursor.prototype.handlePortalSuspended = function() {
97112
this._sendRows()
98113
}
99114

100-
Cursor.prototype.handleReadyForQuery = function () {
115+
Cursor.prototype.handleReadyForQuery = function() {
101116
this._sendRows()
102117
this.emit('end', this._result)
103118
this.state = 'done'
104119
}
105120

106-
Cursor.prototype.handleEmptyQuery = function () {
121+
Cursor.prototype.handleEmptyQuery = function() {
107122
this.connection.sync()
108123
}
109124

110-
Cursor.prototype.handleError = function (msg) {
125+
Cursor.prototype.handleError = function(msg) {
111126
this.state = 'error'
112127
this._error = msg
113128
// satisfy any waiting callback
114129
if (this._cb) {
115130
this._cb(msg)
116131
}
117132
// dispatch error to all waiting callbacks
118-
for (var i = 0; i < this._queue.length; i++) {
133+
for (let i = 0; i < this._queue.length; i++) {
119134
this._queue.pop()[1](msg)
120135
}
121136

@@ -127,45 +142,45 @@ Cursor.prototype.handleError = function (msg) {
127142
this.connection.sync()
128143
}
129144

130-
Cursor.prototype._getRows = function (rows, cb) {
145+
Cursor.prototype._getRows = function(rows, cb) {
131146
this.state = 'busy'
132147
this._cb = cb
133148
this._rows = []
134149
const msg = {
135-
portal: '',
136-
rows: rows
150+
portal: this._portal,
151+
rows: rows,
137152
}
138153
this.connection.execute(msg, true)
139154
this.connection.flush()
140155
}
141156

142-
Cursor.prototype.end = function (cb) {
157+
Cursor.prototype.end = function(cb) {
143158
if (this.state !== 'initialized') {
144159
this.connection.sync()
145160
}
146-
this.connection.stream.once('end', cb)
161+
this.connection.once('end', cb)
147162
this.connection.end()
148163
}
149164

150-
Cursor.prototype.close = function (cb) {
165+
Cursor.prototype.close = function(cb) {
151166
if (this.state === 'done') {
152167
if (cb) {
153168
return setImmediate(cb)
154169
} else {
155170
return
156171
}
157172
}
158-
this.connection.close({type: 'P'})
173+
this.connection.close({ type: 'P' })
159174
this.connection.sync()
160175
this.state = 'done'
161176
if (cb) {
162-
this.connection.once('closeComplete', function () {
177+
this.connection.once('closeComplete', function() {
163178
cb()
164179
})
165180
}
166181
}
167182

168-
Cursor.prototype.read = function (rows, cb) {
183+
Cursor.prototype.read = function(rows, cb) {
169184
if (this.state === 'idle') {
170185
return this._getRows(rows, cb)
171186
}

package.json

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
11
{
22
"name": "pg-cursor",
3-
"version": "1.3.0",
4-
"description": "",
3+
"version": "2.0.0",
4+
"description": "Query cursor extension for node-postgres",
55
"main": "index.js",
66
"directories": {
77
"test": "test"
88
},
99
"scripts": {
1010
"test": " mocha && eslint ."
1111
},
12+
"repository": {
13+
"type": "git",
14+
"url": "git://github.com/brianc/node-pg-cursor.git"
15+
},
1216
"author": "Brian M. Carlson",
1317
"license": "MIT",
1418
"devDependencies": {
15-
"eslint": "^4.4.0",
16-
"eslint-config-standard": "^10.2.1",
17-
"eslint-plugin-import": "^2.7.0",
18-
"eslint-plugin-node": "^5.1.1",
19-
"eslint-plugin-promise": "^3.5.0",
20-
"eslint-plugin-standard": "^3.0.1",
21-
"mocha": "^3.5.0",
22-
"pg": "6.x"
19+
"eslint": "^6.5.1",
20+
"eslint-config-prettier": "^6.4.0",
21+
"eslint-plugin-prettier": "^3.1.1",
22+
"mocha": "^6.2.2",
23+
"pg": "7.x",
24+
"prettier": "^1.18.2"
2325
},
24-
"dependencies": {}
26+
"prettier": {
27+
"semi": false,
28+
"printWidth": 120,
29+
"trailingComma": "es5",
30+
"singleQuote": true
31+
}
2532
}

pg.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

test/close.js

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
var assert = require('assert')
2-
var Cursor = require('../')
3-
var pg = require('pg')
1+
const assert = require('assert')
2+
const Cursor = require('../')
3+
const pg = require('pg')
44

5-
var text = 'SELECT generate_series as num FROM generate_series(0, 50)'
6-
describe('close', function () {
7-
beforeEach(function (done) {
8-
var client = this.client = new pg.Client()
5+
const text = 'SELECT generate_series as num FROM generate_series(0, 50)'
6+
describe('close', function() {
7+
beforeEach(function(done) {
8+
const client = (this.client = new pg.Client())
99
client.connect(done)
1010
client.on('drain', client.end.bind(client))
1111
})
1212

13-
it('can close a finished cursor without a callback', function (done) {
14-
var cursor = new Cursor(text)
13+
it('can close a finished cursor without a callback', function(done) {
14+
const cursor = new Cursor(text)
1515
this.client.query(cursor)
1616
this.client.query('SELECT NOW()', done)
1717
cursor.read(100, function (err, res) {
@@ -20,23 +20,24 @@ describe('close', function () {
2020
})
2121
})
2222

23-
it('closes cursor early', function (done) {
24-
var cursor = new Cursor(text)
23+
24+
it('closes cursor early', function(done) {
25+
const cursor = new Cursor(text)
2526
this.client.query(cursor)
2627
this.client.query('SELECT NOW()', done)
27-
cursor.read(25, function (err, res) {
28+
cursor.read(25, function(err) {
2829
assert.ifError(err)
2930
cursor.close()
3031
})
3132
})
3233

33-
it('works with callback style', function (done) {
34-
var cursor = new Cursor(text)
35-
var client = this.client
34+
it('works with callback style', function(done) {
35+
const cursor = new Cursor(text)
36+
const client = this.client
3637
client.query(cursor)
37-
cursor.read(25, function (err, res) {
38+
cursor.read(25, function(err) {
3839
assert.ifError(err)
39-
cursor.close(function (err) {
40+
cursor.close(function(err) {
4041
assert.ifError(err)
4142
client.query('SELECT NOW()', done)
4243
})

0 commit comments

Comments
 (0)