-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Get formatted query from Pool.query() method #991
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
Open
surfbuds
wants to merge
7
commits into
mysqljs:master
Choose a base branch
from
surfbuds:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b44221d
Enable returning formatted sql in Pool.query()
surfbuds f341946
Enable returning formatted query in Pool.query()
surfbuds 249b0a3
Logical statement
surfbuds e350fad
Merge remote-tracking branch 'upstream/master'
surfbuds 6bf1987
Unit test for fix to issue #984
surfbuds 119d6ec
Merge remote-tracking branch 'upstream/master'
surfbuds 3cd3833
Moved isFormatted property
surfbuds File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
var assert = require('assert'); | ||
var common = require('../../common'); | ||
var pool1 = common.createPool({port: common.fakeServerPort}); | ||
var pool2 = common.createPool({port: common.fakeServerPort, queryFormat: queryFormat}); | ||
|
||
|
||
function queryFormat(query, values, tz) { | ||
if (!values) { | ||
return query; | ||
} | ||
|
||
var escape = this.escape.bind(this); | ||
|
||
return query.replace(/\:(\w+)/g, function (txt, key) { | ||
if (values.hasOwnProperty(key)) { | ||
return escape(values[key]); | ||
} | ||
return txt; | ||
}); | ||
} | ||
|
||
var server = common.createFakeServer(); | ||
|
||
server.listen(common.fakeServerPort, function (err) { | ||
assert.ifError(err); | ||
|
||
|
||
|
||
// When the fix is applied, this query demonstrates that connection.query(), | ||
// which gets invoked internally by pool.query(), will not re-format the query | ||
// a second time. Re-formatting the query a second time in this case would cause | ||
// an error when the question marks inside the string 'John ? J ?' are interpreted | ||
// as placeholders for values. | ||
|
||
assert.equal(pool1.query('SELECT ?? FROM ?? WHERE id=? OR name=?', [['name', 'rdate'], 'dummytable', 1, "John ? J ?"]).sql, 'SELECT `name`, `rdate` FROM `dummytable` WHERE id=1 OR name=\'John ? J ?\''); | ||
|
||
|
||
|
||
// This one will will fail before the fix is applied | ||
|
||
assert.equal(pool2.query('SELECT :a1, :a2', { a1: 1, a2: 'two' }).sql, 'SELECT 1, \'two\''); | ||
|
||
|
||
|
||
// These last 2 will work before and after the fix is applied as there is no transformation performed | ||
|
||
assert.equal(pool2.query('SELECT :a1', []).sql, 'SELECT :a1'); | ||
assert.equal(pool2.query('SELECT :a1').sql, 'SELECT :a1'); | ||
|
||
|
||
|
||
pool1.end(function (err) { | ||
assert.ifError(err); | ||
pool2.end(function (err) { | ||
assert.ifError(err); | ||
server.destroy(); | ||
}); | ||
}); | ||
|
||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have to think about if it's acceptable that the context of the
queryFormat
call is not going to be aConnection
object in these cases and if this is going to be an issue.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did find a solution to keep
createQuery
as a static method ofConnection
and provide the right context for instance config, which keeps things backward compatible. I'll commit the changes later from another machine.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it take care of this context issue, though?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise there is no need to add a new commit, really. I've already adjusted your commit to make it an instance method (still under your name). The problem right now is backwards compatibility with this function context.