Skip to content

Commit

Permalink
Handle null-values properly
Browse files Browse the repository at this point in the history
  • Loading branch information
felixge committed Aug 22, 2010
1 parent 4a93056 commit 09df6f9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/mysql/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,16 @@ Parser.prototype.write = function(buffer) {
}

packet.columnLength = lengthCoded(packet.columnLength);
if (!packet.columnLength) {
packet.emit('data', new Buffer(0), 0);
if (packet.received < packet.length) {
advance(Parser.COLUMN_VALUE_LENGTH);
} else {
self.packet = packet = null;
self.state = state = Parser.PACKET_LENGTH;
continue;
}
}
break;
case Parser.COLUMN_VALUE_STRING:
var remaining = packet.columnLength - packet.index, read;
Expand Down
6 changes: 5 additions & 1 deletion lib/mysql/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ Query.prototype._handlePacket = function(packet) {
row[field] = '';

packet.on('data', function(buffer, remaining) {
row[field] += buffer;
if (buffer.length) {
row[field] += buffer;
} else {
row[field] = null;
}

if (remaining == 0) {
self._rowIndex++;
Expand Down
17 changes: 17 additions & 0 deletions test/system/test-client-query-null.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require('../common');
var Client = require('mysql').Client,
client = Client(TEST_CONFIG),
gently = new Gently();

// our test db might not exist yet, so don't try to connect to it
client.database = '';
client.connect();

client.query('SELECT NULL as field_a, NULL as field_b', function(err, results) {
if (err) throw err;

assert.strictEqual(results[0].field_a, null);
assert.strictEqual(results[0].field_b, null);
client.end();
});

2 comments on commit 09df6f9

@piscisaureus
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't handle null values; now there is no distinction between null and the empty string. Try:

client.query('SELECT NULL as field_a, NULL as field_b, "" AS field_c', function(err, results) {
if (err) throw err;

assert.strictEqual(results[0].field_a, null);
assert.strictEqual(results[0].field_b, null);
assert.strictEqual(results[0].field_c, '');
client.end();
});

@felixge
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in: 10e81c2

Please sign in to comment.