Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support idle_in_transaction_session_timeout and statement_timeout for native driver #2323

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 27 additions & 1 deletion packages/pg/lib/connection-parameters.js
Expand Up @@ -45,6 +45,21 @@ var add = function (params, config, paramName) {
}
}

var escapeOptionValue = function (value) {
return ('' + value).replace(/\\/g, '\\\\').replace(/ /g, '\\ ')
}

var addOption = function (options, config, optionName) {
if (!config[optionName]) {
return
}

var value = config[optionName]
if (value !== undefined && value !== null) {
options.push('-c ' + optionName + '=' + escapeOptionValue(value))
}
}

class ConnectionParameters {
constructor(config) {
// if a string is passed, it is a raw connection string so we parse it into a config
Expand Down Expand Up @@ -120,13 +135,24 @@ class ConnectionParameters {

getLibpqConnectionString(cb) {
var params = []
var pgOptions = []

add(params, this, 'user')
add(params, this, 'password')
add(params, this, 'port')
add(params, this, 'application_name')
add(params, this, 'fallback_application_name')
add(params, this, 'connect_timeout')
add(params, this, 'options')

addOption(pgOptions, this, 'statement_timeout')
addOption(pgOptions, this, 'idle_in_transaction_session_timeout')

if (this.options) {
pgOptions.push(this.options)
}
if (pgOptions.length > 0) {
params.push('options=' + quoteParamValue(pgOptions.join(' ')))
}

var ssl = typeof this.ssl === 'object' ? this.ssl : this.ssl ? { sslmode: this.ssl } : {}
add(params, ssl, 'sslmode')
Expand Down
43 changes: 43 additions & 0 deletions packages/pg/test/unit/connection-parameters/creation-tests.js
Expand Up @@ -172,6 +172,49 @@ test('libpq connection string building', function () {
)
})

test('builds conn string with options and statement_timeout', function () {
var config = {
user: 'brian',
password: 'xyz',
port: 888,
host: 'localhost',
database: 'bam',
statement_timeout: 5000,
idle_in_transaction_session_timeout: 5000,
options: '-c geqo=off -c foobar=off',
}
var subject = new ConnectionParameters(config)
subject.getLibpqConnectionString(
assert.calls(function (err, constring) {
assert(!err)
var parts = constring.split(/ (?=([^\']*\'[^\']*\')*[^\']*$)/)
checkForPart(
parts,
"options='-c statement_timeout=5000 -c idle_in_transaction_session_timeout=5000 -c geqo=off -c foobar=off'"
)
})
)
})

test('builds conn string with options and without statement_timeout', function () {
var config = {
user: 'brian',
password: 'xyz',
port: 888,
host: 'localhost',
database: 'bam',
options: '-c geqo=off -c foobar=off',
}
var subject = new ConnectionParameters(config)
subject.getLibpqConnectionString(
assert.calls(function (err, constring) {
assert(!err)
var parts = constring.split(/ (?=([^\']*\'[^\']*\')*[^\']*$)/)
checkForPart(parts, "options='-c geqo=off -c foobar=off'")
})
)
})

test('builds dns string', function () {
var config = {
user: 'brian',
Expand Down