Skip to content
Open
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
5 changes: 5 additions & 0 deletions packages/pg/lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ Client.prototype._attachListeners = function (con) {
self.activeQuery.handleDataRow(msg)
})

// delegate paramDescription to active query
con.on('paramDescription', function (msg) {
self.activeQuery.handleParamDescription(msg, con)
})

// delegate portalSuspended to active query
// eslint-disable-next-line no-unused-vars
con.on('portalSuspended', function (msg) {
Expand Down
14 changes: 14 additions & 0 deletions packages/pg/lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,9 @@ Connection.prototype.parseMessage = function (buffer) {
case 0x73: // s
return new Message('portalSuspended', length)

case 0x74: // t
return this.parset(buffer, length)

case 0x47: // G
return this.parseG(buffer, length)

Expand Down Expand Up @@ -540,6 +543,17 @@ Connection.prototype.parseField = function (buffer) {
return field
}

Connection.prototype.parset = function (buffer, length) {
var msg = new Message('paramDescription', length)
msg.paramCount = this.parseInt16(buffer)
var params = []
for (var i = 0; i < msg.paramCount; i++) {
params.push(this.parseInt32(buffer))
}
msg.params = params
return msg
}

var DATA_ROW = 'dataRow'
var DataRowMessage = function (length, fieldCount) {
this.name = DATA_ROW
Expand Down
20 changes: 20 additions & 0 deletions packages/pg/lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Query extends EventEmitter {
this.types = config.types
this.name = config.name
this.binary = config.binary
this.describe = config.describe
// use unique portal name each time
this.portal = config.portal || ''
this.callback = config.callback
Expand All @@ -45,6 +46,10 @@ class Query extends EventEmitter {
if (this.name) {
return true
}
// always prepare if describing a query
if (this.describe) {
return true
}
// always prepare if there are max number of rows expected per
// portal execution
if (this.rows) {
Expand Down Expand Up @@ -103,6 +108,11 @@ class Query extends EventEmitter {
}
}

handleParamDescription(msg, con) {
this._result.addParams(msg.params)
con.sync()
}

handleCommandComplete(msg, con) {
this._checkForMultirow()
this._result.addCommandComplete(msg)
Expand Down Expand Up @@ -202,6 +212,16 @@ class Query extends EventEmitter {
)
}

if (this.describe) {
// if describe is set, the query is not executed
connection.describe({
type: 'S',
name: this.name,
})
connection.flush()
return
}

if (this.values) {
try {
this.values = this.values.map(utils.prepareValue)
Expand Down
11 changes: 11 additions & 0 deletions packages/pg/lib/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var Result = function (rowMode, types) {
this.oid = null
this.rows = []
this.fields = []
this.params = []
this._parsers = undefined
this._types = types
this.RowCtor = null
Expand Down Expand Up @@ -102,4 +103,14 @@ Result.prototype.addFields = function (fieldDescriptions) {
}
}

Result.prototype.addParams = function (paramDescriptions) {
if (this.params.length) {
this.params = []
}
for (var i = 0; i < paramDescriptions.length; i++) {
var desc = paramDescriptions[i]
this.params.push(desc)
}
}

module.exports = Result
29 changes: 29 additions & 0 deletions packages/pg/test/integration/client/prepared-statement-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,32 @@ var suite = new helper.Suite()

suite.test('cleanup', () => client.end())
})()
;(function () {
var client = helper.client()
client.on('drain', client.end.bind(client))

suite.test('describe', function (done) {
client.query(
new Query(
{
text: 'SELECT id, name, age FROM person WHERE age > $1',
describe: true,
},
(res) => {
assert.deepEqual(res.params, [23])
assert.deepEqual(
res.fields.map((field) => ({ name: field.name, type: field.dataTypeID })),
[
{ name: 'id', type: 23 },
{ name: 'name', type: 1043 },
{ name: 'age', type: 23 },
]
)
done()
}
)
)
})

suite.test('cleanup', () => client.end())
})()
53 changes: 53 additions & 0 deletions packages/pg/test/unit/client/prepared-statement-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,56 @@ test('prepared statement with explicit portal', function () {
})
})
})

var describeClient = helper.client()
var describeCon = describeClient.connection
describeCon.parse = function (arg) {
process.nextTick(function () {
describeCon.emit('parseComplete')
})
}
var describeDescribeArg = null
describeCon.describe = function (arg) {
describeDescribeArg = arg
}

var describeFlushCalled = false
describeCon.flush = function () {
describeFlushCalled = true
process.nextTick(function () {
describeCon.emit('paramDescription', { params: [] })
describeCon.emit('rowDescription', { fields: [] })
})
}

var describeSyncCalled = false
describeCon.sync = function () {
process.nextTick(function () {
describeSyncCalled = true
describeCon.emit('readyForQuery')
})
}

test('describe prepared statement', function () {
assert.ok(describeClient.connection.emit('readyForQuery'))

var query = describeClient.query(
new Query({
text: 'select * from X where name = $1',
describe: true,
})
)

assert.emits(query, 'end', function () {
test('describe argument', function () {
assert.equal(describeDescribeArg.type, 'S')
assert.equal(describeDescribeArg.name, undefined)
})
test('flush called', function () {
assert.ok(describeFlushCalled)
})
test('sync called', function () {
assert.ok(describeSyncCalled)
})
})
})