This commit is contained in:
Brian M. Carlson 2019-10-25 18:03:07 -05:00
parent 414fac6a05
commit 4164686c4b
8 changed files with 385 additions and 635 deletions

View File

@ -1,9 +1,10 @@
{
"extends": "eslint:recommended",
"extends": ["eslint:recommended"],
"plugins": ["prettier"],
"rules": {
"prettier/prettier": "error",
"no-new-func": "off"
"prefer-const": "error",
"no-var": "error"
},
"env": {
"es6": true,

View File

@ -4,7 +4,7 @@ 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) {
EventEmitter.call(this)
@ -130,7 +130,7 @@ Cursor.prototype.handleError = function(msg) {
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)
}
@ -155,6 +155,7 @@ Cursor.prototype._getRows = function(rows, cb) {
}
Cursor.prototype.end = function(cb) {
console.log(this.state)
if (this.state !== 'initialized') {
this.connection.sync()
}
@ -177,6 +178,7 @@ Cursor.prototype.close = function(cb) {
}
Cursor.prototype.read = function(rows, cb) {
console.log('state', this.state)
if (this.state === 'idle') {
return this._getRows(rows, cb)
}

View File

@ -18,7 +18,6 @@
"devDependencies": {
"eslint": "^6.5.1",
"eslint-config-prettier": "^6.4.0",
"eslint-plugin-import": "^2.7.0",
"eslint-plugin-prettier": "^3.1.1",
"mocha": "^6.2.2",
"pg": "^7.12.1",

View File

@ -1,17 +1,17 @@
var assert = require('assert')
var Cursor = require('../')
var pg = require('pg')
const assert = require('assert')
const Cursor = require('../')
const pg = require('pg')
var text = 'SELECT generate_series as num FROM generate_series(0, 50)'
const text = 'SELECT generate_series as num FROM generate_series(0, 50)'
describe('close', function() {
beforeEach(function(done) {
var client = (this.client = new pg.Client())
const client = (this.client = new pg.Client())
client.connect(done)
client.on('drain', client.end.bind(client))
})
it('closes cursor early', function(done) {
var cursor = new Cursor(text)
const cursor = new Cursor(text)
this.client.query(cursor)
this.client.query('SELECT NOW()', done)
cursor.read(25, function(err) {
@ -21,8 +21,8 @@ describe('close', function() {
})
it('works with callback style', function(done) {
var cursor = new Cursor(text)
var client = this.client
const cursor = new Cursor(text)
const client = this.client
client.query(cursor)
cursor.read(25, function(err) {
assert.ifError(err)

View File

@ -1,15 +1,15 @@
'use strict'
var assert = require('assert')
var Cursor = require('../')
var pg = require('pg')
const assert = require('assert')
const Cursor = require('../')
const pg = require('pg')
var text = 'SELECT generate_series as num FROM generate_series(0, 4)'
const text = 'SELECT generate_series as num FROM generate_series(0, 4)'
describe('error handling', function() {
it('can continue after error', function(done) {
var client = new pg.Client()
const client = new pg.Client()
client.connect()
var cursor = client.query(new Cursor('asdfdffsdf'))
const cursor = client.query(new Cursor('asdfdffsdf'))
cursor.read(1, function(err) {
assert(err)
client.query('SELECT NOW()', function(err) {
@ -23,9 +23,9 @@ describe('error handling', function() {
describe('read callback does not fire sync', () => {
it('does not fire error callback sync', done => {
var client = new pg.Client()
const client = new pg.Client()
client.connect()
var cursor = client.query(new Cursor('asdfdffsdf'))
const cursor = client.query(new Cursor('asdfdffsdf'))
let after = false
cursor.read(1, function(err) {
assert(err, 'error should be returned')
@ -43,9 +43,9 @@ describe('read callback does not fire sync', () => {
})
it('does not fire result sync after finished', done => {
var client = new pg.Client()
const client = new pg.Client()
client.connect()
var cursor = client.query(new Cursor('SELECT NOW()'))
const cursor = client.query(new Cursor('SELECT NOW()'))
let after = false
cursor.read(1, function(err) {
assert(!err)
@ -68,13 +68,13 @@ describe('read callback does not fire sync', () => {
describe('proper cleanup', function() {
it('can issue multiple cursors on one client', function(done) {
var client = new pg.Client()
const client = new pg.Client()
client.connect()
var cursor1 = client.query(new Cursor(text))
const cursor1 = client.query(new Cursor(text))
cursor1.read(8, function(err, rows) {
assert.ifError(err)
assert.equal(rows.length, 5)
var cursor2 = client.query(new Cursor(text))
const cursor2 = client.query(new Cursor(text))
cursor2.read(8, function(err, rows) {
assert.ifError(err)
assert.equal(rows.length, 5)

View File

@ -1,16 +1,15 @@
var assert = require('assert')
var Cursor = require('../')
var pg = require('pg')
const assert = require('assert')
const Cursor = require('../')
const pg = require('pg')
var text = 'SELECT generate_series as num FROM generate_series(0, 5)'
const text = 'SELECT generate_series as num FROM generate_series(0, 5)'
describe('cursor', function() {
beforeEach(function(done) {
var client = (this.client = new pg.Client())
const client = (this.client = new pg.Client())
client.connect(done)
this.pgCursor = function(text, values) {
client.on('drain', client.end.bind(client))
return client.query(new Cursor(text, values || []))
}
})
@ -20,7 +19,7 @@ describe('cursor', function() {
})
it('fetch 6 when asking for 10', function(done) {
var cursor = this.pgCursor(text)
const cursor = this.pgCursor(text)
cursor.read(10, function(err, res) {
assert.ifError(err)
assert.equal(res.length, 6)
@ -28,8 +27,8 @@ describe('cursor', function() {
})
})
it('end before reading to end', function(done) {
var cursor = this.pgCursor(text)
it.only('end before reading to end', function(done) {
const cursor = this.pgCursor(text)
cursor.read(3, function(err, res) {
assert.ifError(err)
assert.equal(res.length, 3)
@ -38,7 +37,7 @@ describe('cursor', function() {
})
it('callback with error', function(done) {
var cursor = this.pgCursor('select asdfasdf')
const cursor = this.pgCursor('select asdfasdf')
cursor.read(1, function(err) {
assert(err)
done()
@ -46,7 +45,7 @@ describe('cursor', function() {
})
it('read a partial chunk of data', function(done) {
var cursor = this.pgCursor(text)
const cursor = this.pgCursor(text)
cursor.read(2, function(err, res) {
assert.ifError(err)
assert.equal(res.length, 2)
@ -68,7 +67,7 @@ describe('cursor', function() {
})
it('read return length 0 past the end', function(done) {
var cursor = this.pgCursor(text)
const cursor = this.pgCursor(text)
cursor.read(2, function(err) {
assert(!err)
cursor.read(100, function(err, res) {
@ -85,11 +84,11 @@ describe('cursor', function() {
it('read huge result', function(done) {
this.timeout(10000)
var text = 'SELECT generate_series as num FROM generate_series(0, 100000)'
var values = []
var cursor = this.pgCursor(text, values)
var count = 0
var read = function() {
const text = 'SELECT generate_series as num FROM generate_series(0, 100000)'
const values = []
const cursor = this.pgCursor(text, values)
let count = 0
const read = function() {
cursor.read(100, function(err, rows) {
if (err) return done(err)
if (!rows.length) {
@ -107,9 +106,9 @@ describe('cursor', function() {
})
it('normalizes parameter values', function(done) {
var text = 'SELECT $1::json me'
var values = [{ name: 'brian' }]
var cursor = this.pgCursor(text, values)
const text = 'SELECT $1::json me'
const values = [{ name: 'brian' }]
const cursor = this.pgCursor(text, values)
cursor.read(1, function(err, rows) {
if (err) return done(err)
assert.equal(rows[0].me.name, 'brian')
@ -122,7 +121,7 @@ describe('cursor', function() {
})
it('returns result along with rows', function(done) {
var cursor = this.pgCursor(text)
const cursor = this.pgCursor(text)
cursor.read(1, function(err, rows, result) {
assert.ifError(err)
assert.equal(rows.length, 1)
@ -133,7 +132,7 @@ describe('cursor', function() {
})
it('emits row events', function(done) {
var cursor = this.pgCursor(text)
const cursor = this.pgCursor(text)
cursor.read(10)
cursor.on('row', (row, result) => result.addRow(row))
cursor.on('end', result => {
@ -143,7 +142,7 @@ describe('cursor', function() {
})
it('emits row events when cursor is closed manually', function(done) {
var cursor = this.pgCursor(text)
const cursor = this.pgCursor(text)
cursor.on('row', (row, result) => result.addRow(row))
cursor.on('end', result => {
assert.equal(result.rows.length, 3)
@ -154,7 +153,7 @@ describe('cursor', function() {
})
it('emits error events', function(done) {
var cursor = this.pgCursor('select asdfasdf')
const cursor = this.pgCursor('select asdfasdf')
cursor.on('error', function(err) {
assert(err)
done()
@ -162,11 +161,11 @@ describe('cursor', function() {
})
it('returns rowCount on insert', function(done) {
var pgCursor = this.pgCursor
const pgCursor = this.pgCursor
this.client
.query('CREATE TEMPORARY TABLE pg_cursor_test (foo VARCHAR(1), bar VARCHAR(1))')
.then(function() {
var cursor = pgCursor('insert into pg_cursor_test values($1, $2)', ['a', 'b'])
const cursor = pgCursor('insert into pg_cursor_test values($1, $2)', ['a', 'b'])
cursor.read(1, function(err, rows, result) {
assert.ifError(err)
assert.equal(rows.length, 0)

View File

@ -1,10 +1,10 @@
var assert = require('assert')
var pg = require('pg')
var Cursor = require('../')
const assert = require('assert')
const pg = require('pg')
const Cursor = require('../')
describe('queries with no data', function() {
beforeEach(function(done) {
var client = (this.client = new pg.Client())
const client = (this.client = new pg.Client())
client.connect(done)
})
@ -13,7 +13,7 @@ describe('queries with no data', function() {
})
it('handles queries that return no data', function(done) {
var cursor = new Cursor('CREATE TEMPORARY TABLE whatwhat (thing int)')
const cursor = new Cursor('CREATE TEMPORARY TABLE whatwhat (thing int)')
this.client.query(cursor)
cursor.read(100, function(err, rows) {
assert.ifError(err)
@ -23,7 +23,7 @@ describe('queries with no data', function() {
})
it('handles empty query', function(done) {
var cursor = new Cursor('-- this is a comment')
let cursor = new Cursor('-- this is a comment')
cursor = this.client.query(cursor)
cursor.read(100, function(err, rows) {
assert.ifError(err)

903
yarn.lock

File diff suppressed because it is too large Load Diff