Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Fix for pg@7.x #57

Merged
merged 3 commits into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
{
"extends": "standard",
"extends": ["eslint:recommended"],
"plugins": ["prettier"],
"rules": {
"prettier/prettier": "error",
"prefer-const": "error",
"no-var": "error"
},
"env": {
"es6": true,
"node": true,
"mocha": true
},
"rules": {
"no-new-func": "off"
}
}
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ language: node_js
dist: trusty
sudo: false
node_js:
- "4.2"
- "6"
- "8"
- "10"
- "12"
env:
- PGUSER=postgres
services:
Expand Down
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
.PHONY: publish-patch test

.PHONY: test
test:
npm test

.PHONY: patch
patch: test
npm version patch -m "Bump version"
git push origin master --tags
npm publish

.PHONY: minor
minor: test
npm version minor -m "Bump version"
git push origin master --tags
Expand Down
77 changes: 43 additions & 34 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ const prepare = require('pg/lib/utils.js').prepareValue
const EventEmitter = require('events').EventEmitter
const util = require('util')

var nextUniqueID = 1 // concept borrowed from org.postgresql.core.v3.QueryExecutorImpl
let nextUniqueID = 1 // concept borrowed from org.postgresql.core.v3.QueryExecutorImpl

function Cursor (text, values, config) {
function Cursor(text, values, config) {
EventEmitter.call(this)

this._conf = config || {}
Expand All @@ -15,33 +15,42 @@ function Cursor (text, values, config) {
this.connection = null
this._queue = []
this.state = 'initialized'
this._result = new Result(this._conf.rowMode)
this._result = new Result(this._conf.rowMode, this._conf.types)
this._cb = null
this._rows = null
this._portal = null
}

util.inherits(Cursor, EventEmitter)

Cursor.prototype.submit = function (connection) {
Cursor.prototype.submit = function(connection) {
this.connection = connection
this._portal = 'C_' + (nextUniqueID++)
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.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()

Expand All @@ -60,25 +69,25 @@ Cursor.prototype.submit = function (connection) {
})
}

Cursor.prototype._shiftQueue = function () {
Cursor.prototype._shiftQueue = function() {
if (this._queue.length) {
this._getRows.apply(this, this._queue.shift())
}
}

Cursor.prototype.handleRowDescription = function (msg) {
Cursor.prototype.handleRowDescription = function(msg) {
this._result.addFields(msg.fields)
this.state = 'idle'
this._shiftQueue()
}

Cursor.prototype.handleDataRow = function (msg) {
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 () {
Cursor.prototype._sendRows = function() {
this.state = 'idle'
setImmediate(() => {
const cb = this._cb
Expand All @@ -94,34 +103,34 @@ Cursor.prototype._sendRows = function () {
})
}

Cursor.prototype.handleCommandComplete = function (msg) {
Cursor.prototype.handleCommandComplete = function(msg) {
this._result.addCommandComplete(msg)
this.connection.sync()
}

Cursor.prototype.handlePortalSuspended = function () {
Cursor.prototype.handlePortalSuspended = function() {
this._sendRows()
}

Cursor.prototype.handleReadyForQuery = function () {
Cursor.prototype.handleReadyForQuery = function() {
this._sendRows()
this.emit('end', this._result)
this.state = 'done'
}

Cursor.prototype.handleEmptyQuery = function () {
Cursor.prototype.handleEmptyQuery = function() {
this.connection.sync()
}

Cursor.prototype.handleError = function (msg) {
Cursor.prototype.handleError = function(msg) {
this.state = 'error'
this._error = msg
// satisfy any waiting callback
if (this._cb) {
this._cb(msg)
}
// dispatch error to all waiting callbacks
for (var i = 0; i < this._queue.length; i++) {
for (let i = 0; i < this._queue.length; i++) {
this._queue.pop()[1](msg)
}

Expand All @@ -133,41 +142,41 @@ Cursor.prototype.handleError = function (msg) {
this.connection.sync()
}

Cursor.prototype._getRows = function (rows, cb) {
Cursor.prototype._getRows = function(rows, cb) {
this.state = 'busy'
this._cb = cb
this._rows = []
const msg = {
portal: this._portal,
rows: rows
rows: rows,
}
this.connection.execute(msg, true)
this.connection.flush()
}

Cursor.prototype.end = function (cb) {
Cursor.prototype.end = function(cb) {
if (this.state !== 'initialized') {
this.connection.sync()
}
this.connection.once('end', cb)
this.connection.end()
}

Cursor.prototype.close = function (cb) {
Cursor.prototype.close = function(cb) {
if (this.state === 'done') {
return setImmediate(cb)
}
this.connection.close({ type: 'P' })
this.connection.sync()
this.state = 'done'
if (cb) {
this.connection.once('closeComplete', function () {
this.connection.once('closeComplete', function() {
cb()
})
}
}

Cursor.prototype.read = function (rows, cb) {
Cursor.prototype.read = function(rows, cb) {
if (this.state === 'idle') {
return this._getRows(rows, cb)
}
Expand Down
Loading