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

Fixed quotation of numbers inside prepared statements #153

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,8 @@ Client.prototype._format_value = function(v) {
for (var i = 0, len = v.length; i < len; ++i)
r.push(this._format_value(v[i]));
return r.join(',');
} else if ((v ^ 0) === v) {
return v;
} else if (v !== null && v !== undefined)
return "'" + Client.escape(v + '') + "'";

Expand Down
58 changes: 51 additions & 7 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,40 @@ var tests = [
var client = new Client();
var fn;
fn = client.prepare("SELECT * FROM foo WHERE id = '123'");
assert.strictEqual(fn({ id: 456 }),
assert.strictEqual(fn({ id: '456' }),
"SELECT * FROM foo WHERE id = '123'");
fn = client.prepare("SELECT * FROM foo WHERE id = :id");
assert.strictEqual(fn({ id: 123 }),
assert.strictEqual(fn({ id: '123' }),
"SELECT * FROM foo WHERE id = '123'");
assert.strictEqual(fn({ id: 456 }),
assert.strictEqual(fn({ id: '456' }),
"SELECT * FROM foo WHERE id = '456'");
fn = client.prepare("SELECT * FROM foo WHERE id = ?");
assert.strictEqual(fn([123]),
assert.strictEqual(fn(['123']),
"SELECT * FROM foo WHERE id = '123'");
assert.strictEqual(fn([456]),
assert.strictEqual(fn(['456']),
"SELECT * FROM foo WHERE id = '456'");
fn = client.prepare("SELECT * FROM foo WHERE id = :0 AND name = :1");
assert.strictEqual(fn(['123', 'baz']),
"SELECT * FROM foo WHERE id = '123' AND name = 'baz'");

// Integer parsing cases
fn = client.prepare("SELECT * FROM foo WHERE id = 123");
assert.strictEqual(fn({ id: 456 }),
"SELECT * FROM foo WHERE id = 123");
fn = client.prepare("SELECT * FROM foo WHERE id = :id");
assert.strictEqual(fn({ id: 123 }),
"SELECT * FROM foo WHERE id = 123");
assert.strictEqual(fn({ id: 456 }),
"SELECT * FROM foo WHERE id = 456");
fn = client.prepare("SELECT * FROM foo WHERE id = ?");
assert.strictEqual(fn([123]),
"SELECT * FROM foo WHERE id = 123");
assert.strictEqual(fn([456]),
"SELECT * FROM foo WHERE id = 456");
fn = client.prepare("SELECT * FROM foo WHERE id = :0 AND name = :1");
assert.strictEqual(fn([123, 'baz']),
"SELECT * FROM foo WHERE id = 123 AND name = 'baz'");

// Edge cases
fn = client.prepare("SELECT * FROM foo WHERE id = :id"
+ " AND first = ? AND last = ? AND middle = '?'");
Expand All @@ -76,6 +94,32 @@ var tests = [
"SELECT * FROM foo WHERE id = '123'"
+ " AND first = 'foo' AND last = '?'"
+ " AND middle = '?'");

// Edge cases with integers
fn = client.prepare("SELECT * FROM foo WHERE id = :id"
+ " AND first = ? AND last = ? AND middle = '?'");
assert.strictEqual(fn(appendProps(['foo', 'bar', 'baz'], { id: 123 })),
"SELECT * FROM foo WHERE id = 123"
+ " AND first = 'foo' AND last = 'bar'"
+ " AND middle = '?'");
fn = client.prepare("SELECT * FROM foo WHERE id = :id"
+ " AND first = ? AND last = '?' AND middle = ?");
assert.strictEqual(fn(appendProps(['foo', 'bar', 'baz'], { id: 123 })),
"SELECT * FROM foo WHERE id = 123"
+ " AND first = 'foo' AND last = '?'"
+ " AND middle = 'bar'");
fn = client.prepare("SELECT * FROM foo WHERE id = :id"
+ " AND first = '?' AND last = '?' AND middle = ?");
assert.strictEqual(fn(appendProps(['foo', 'bar', 'baz'], { id: 123 })),
"SELECT * FROM foo WHERE id = 123"
+ " AND first = '?' AND last = '?'"
+ " AND middle = 'foo'");
fn = client.prepare("SELECT * FROM foo WHERE id = :id"
+ " AND first = ? AND last = '?' AND middle = '?'");
assert.strictEqual(fn(appendProps(['foo', 'bar', 'baz'], { id: 123 })),
"SELECT * FROM foo WHERE id = 123"
+ " AND first = 'foo' AND last = '?'"
+ " AND middle = '?'");
next();
}
},
Expand Down Expand Up @@ -1035,7 +1079,7 @@ function makeFooTable(client, colDefs, tableOpts) {
opts.push(format('%s=%s', key, tableOpts[key]));
});
tableOpts = opts.join(' ');
} else
} else
tableOpts = '';

client.query('CREATE DATABASE IF NOT EXISTS `foo`', NOOP);
Expand All @@ -1045,7 +1089,7 @@ function makeFooTable(client, colDefs, tableOpts) {
cols.join(', '),
tableOpts);
client.query(query, NOOP);

}

function next() {
Expand Down