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

Add option to force use of Extended Queries #3214

Merged
merged 6 commits into from
Jun 4, 2024
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
3 changes: 3 additions & 0 deletions docs/pages/apis/client.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ type QueryConfig {

// custom type parsers just for this query result
types?: Types;

// TODO: document
queryMode?: string;
}
```

Expand Down
2 changes: 1 addition & 1 deletion packages/pg-native/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Client.prototype.query = function (text, values, cb) {
cb = values
}

if (Array.isArray(values) && values.length > 0) {
if (Array.isArray(values)) {
queryFn = function () {
return self.pq.sendQueryParams(text, values)
}
Expand Down
3 changes: 3 additions & 0 deletions packages/pg/lib/native/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var NativeQuery = (module.exports = function (config, values, callback) {
this.text = config.text
this.values = config.values
this.name = config.name
this.queryMode = config.queryMode
this.callback = config.callback
this.state = 'new'
this._arrayMode = config.rowMode === 'array'
Expand Down Expand Up @@ -159,6 +160,8 @@ NativeQuery.prototype.submit = function (client) {
}
var vals = this.values.map(utils.prepareValue)
client.native.query(this.text, vals, after)
} else if (this.queryMode === 'extended') {
client.native.query(this.text, [], after)
} else {
client.native.query(this.text, after)
}
Expand Down
5 changes: 5 additions & 0 deletions packages/pg/lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Query extends EventEmitter {
this.rows = config.rows
this.types = config.types
this.name = config.name
this.queryMode = config.queryMode
this.binary = config.binary
// use unique portal name each time
this.portal = config.portal || ''
Expand All @@ -32,6 +33,10 @@ class Query extends EventEmitter {
}

requiresPreparation() {
if (this.queryMode === 'extended') {
return true
}

// named queries must always be prepared
if (this.name) {
return true
Expand Down
25 changes: 25 additions & 0 deletions packages/pg/test/integration/client/multiple-results-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,31 @@ suite.test(
})
)

suite.test(
'throws if queryMode set to "extended"',
co.wrap(function* () {
const client = new helper.Client()
yield client.connect()

// TODO should be text or sql?
try {
const results = yield client.query({
text: `SELECT 'foo'::text as name; SELECT 'bar'::text as baz`,
queryMode: 'extended',
})
assert.fail('Should have thrown')
} catch (err) {
if (err instanceof assert.AssertionError) throw err

assert.equal(err.severity, 'ERROR')
assert.equal(err.code, '42601')
assert.equal(err.message, 'cannot insert multiple commands into a prepared statement')
}

return client.end()
})
)

suite.test(
'multiple selects work',
co.wrap(function* () {
Expand Down