Skip to content

Commit

Permalink
Got the row parsing working, yeah!
Browse files Browse the repository at this point in the history
  • Loading branch information
felixge committed Aug 12, 2010
1 parent bff0615 commit c5c83c7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
14 changes: 11 additions & 3 deletions lib/mysql/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,20 +482,28 @@ Parser.prototype.write = function(buffer) {
packet.columnLength = lengthCoded(packet.columnLength);
break;
case Parser.COLUMN_VALUE_STRING:
var remaining = packet.columnLength - packet.index;
console.log(remaining);
var remaining = packet.columnLength - packet.index, read;
if (i + remaining >= buffer.length) {
var read = buffer.length - i;
read = buffer.length - i;
packet.emit('data', buffer.slice(i, buffer.length), remaining - read);
packet.index += read;
// the -1 offsets are because these values are also manipulated by the loop itself
packet.received += read - 1;
i = buffer.length;
} else {
packet.emit('data', buffer.slice(i, i + remaining), 0);
i += remaining - 1;
packet.received += remaining - 1;
advance(Parser.COLUMN_VALUE_LENGTH);
// advance() sets this to -1, but packet.index++ is skipped, so we need to manually fix
packet.index = 0;
}

if (packet.received == packet.length) {
self.packet = packet = null;
self.state = state = Parser.PACKET_LENGTH;
}

continue;
}

Expand Down
6 changes: 4 additions & 2 deletions test/simple/test-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ test(function write() {
})();

(function testRowPacket() {
parser.write(new Buffer([20, 0, 0, 1]));
parser.write(new Buffer([23, 0, 0, 1]));
var packet = parser.packet;

gently.expect(parser, 'emit', function(event, val) {
Expand Down Expand Up @@ -332,7 +332,9 @@ test(function write() {
});

parser.write(new Buffer(' are you?\u0005Fine!'));
console.log(parser);

assert.equal(parser.packet, null);
assert.equal(parser.state, Parser.PACKET_LENGTH);
})();

(function testEofPacketAfterRowPacket() {
Expand Down
11 changes: 11 additions & 0 deletions test/system/test-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ client.query(
})
);

client.query(
'INSERT INTO '+TEST_TABLE+' '+
'SET title = ?, text = ?',
['another entry', 'because 2 entries make a better test'],
gently.expect(function insertCb(err) {
if (err) {
throw err;
}
})
);

var query = client.query(
'SELECT * FROM '+TEST_TABLE,
gently.expect(function selectCb(err, results) {
Expand Down

0 comments on commit c5c83c7

Please sign in to comment.