Skip to content

Commit f7ae159

Browse files
committed
added possibility of using encoding while writing strings
1 parent 4578f3a commit f7ae159

File tree

5 files changed

+15
-7
lines changed

5 files changed

+15
-7
lines changed

lib/commands/prepare.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ function Prepare (options, callback)
2121
util.inherits(Prepare, Command);
2222

2323
Prepare.prototype.start = function (packet, connection) {
24-
connection.writePacket(new Packets.PrepareStatement(this.query).toPacket(1));
24+
var cmdPacket = new Packets.PrepareStatement(this.query, connection.config.charsetNumber);
25+
connection.writePacket(cmdPacket.toPacket(1));
2526
return Prepare.prototype.prepareHeader;
2627
};
2728

lib/commands/query.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Query.prototype.start = function (packet, connection) {
3535
console.log(' Sending query command: %s', this.sql);
3636
}
3737
this._connection = connection;
38-
var cmdPacket = new Packets.Query(this.sql);
38+
var cmdPacket = new Packets.Query(this.sql, connection.config.charsetNumber);
3939
connection.writePacket(cmdPacket.toPacket(1));
4040
return Query.prototype.resultsetHeader;
4141
};

lib/packets/packet.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ Packet.prototype.writeNullTerminatedString = function (s) {
729729
this.writeInt8(0);
730730
};
731731

732-
Packet.prototype.writeString = function (s) {
732+
Packet.prototype.writeString = function (s, encoding) {
733733
if (s === null) {
734734
this.writeInt8(0xfb);
735735

@@ -744,7 +744,7 @@ Packet.prototype.writeString = function (s) {
744744
// this.buffer.write(s, this.offset, bytes, 'utf8');
745745
// this.offset += bytes;
746746

747-
var buf = StringParser.encode(s, 'cesu8');
747+
var buf = StringParser.encode(s, encoding || 'cesu8');
748748
this.offset += buf.copy(this.buffer, this.offset);
749749
};
750750

lib/packets/prepare_statement.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,24 @@ var Packet = require('../packets/packet');
33
var CommandCodes = require('../constants/commands');
44
var StringParser = require('../parsers/string.js');
55

6-
function PrepareStatement (sql)
6+
function PrepareStatement (sql, charsetNumber)
77
{
88
this.query = sql;
9+
this.charsetNumber = charsetNumber;
910
}
1011

1112
PrepareStatement.prototype.toPacket = function ()
1213
{
14+
// TODO: use this.charsetNumber and get proper encoding type
1315
var buf = StringParser.encode(this.query, 'cesu8');
1416
var length = 5 + buf.length;
17+
1518
var buffer = Buffer.allocUnsafe(length);
1619
var packet = new Packet(0, buffer, 0, length);
1720
packet.offset = 4;
1821
packet.writeInt8(CommandCodes.STMT_PREPARE);
19-
packet.writeString(this.query);
22+
// TODO: pass down encoding to this method too
23+
packet.writeString(this.query, 'cesu8');
2024
return packet;
2125
};
2226

lib/packets/query.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ function Query (sql, charsetNumber)
1111

1212
Query.prototype.toPacket = function ()
1313
{
14+
// TODO: use this.charsetNumber and get proper encoding type
1415
var buf = StringParser.encode(this.query, 'cesu8');
1516
var length = 5 + buf.length;
17+
1618
var buffer = Buffer.allocUnsafe(length);
1719
var packet = new Packet(0, buffer, 0, length);
1820
packet.offset = 4;
1921
packet.writeInt8(CommandCode.QUERY);
20-
packet.writeString(this.query);
22+
// TODO: pass down encoding to this method too
23+
packet.writeString(this.query, 'cesu8');
2124
return packet;
2225
};
2326

0 commit comments

Comments
 (0)