Skip to content
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
8 changes: 3 additions & 5 deletions lib/mysql-native/charset.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ exports.Charset.prototype.convertFromBytes = function(bytes) {

// "あい" => "\xE3\x81\x82\xE3\x81\x84" // UTF-8
var convertUTF8ToBytes = function(str) {
if(typeof(str)=='undefined') return undefined;
if(typeof(str)=='undefined') return undefined;

if(typeof(str)!='string')return str;
var surrogate_1st = 0;
var unicode_codes = [];
for (var i = 0; i < str.length; ++i) {
Expand Down Expand Up @@ -85,7 +83,7 @@ var convertUTF8ToBytes = function(str) {
}

var convertUTF8FromBytes = function(str) {
if(typeof(str)=='undefined') return undefined;
if(typeof(str)!='string')return str;

var unicode_str = "";
var unicode_code = 0;
Expand Down Expand Up @@ -133,7 +131,7 @@ var convertUTF8FromBytes = function(str) {
} else {
// Malformed UTF-8 sequence ignored.
}

return unicode_str.substring(1);

}
Expand Down
5 changes: 5 additions & 0 deletions lib/mysql-native/commands/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ function execPrepared(sql, parameters)
if(typeof parameters[i] == "boolean" || (parameters[i] instanceof Array && parameters[i].every(function(x){return typeof x == 'boolean';}))){
packet.lcbits(parameters[i]);
}
if(this.client.charset){
parameters[i] = this.client.charset.convertToBytes(parameters[i]);
}
packet.lcstring(parameters[i].toString())
}
}
Expand Down Expand Up @@ -173,6 +176,8 @@ function execPrepared(sql, parameters)
if (!null_bit_map[f])
{
var value = r.unpackBinary(field.type, field.flags & field_flags.UNSIGNED);
if(this.client.charset)
value = this.client.charset.convertFromBytes(value);
this.store_column(row, field, value, use_hash);
} else {
this.store_column(row, field, null, use_hash);
Expand Down
3 changes: 2 additions & 1 deletion lib/mysql-native/commands/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ module.exports = function(sql)
start: function()
{
this.stmt = sql;
var charset = this.connection.charset;
var charset = this.client.charset;
this.write( new writer().add("\u0003").add( charset ? charset.convertToBytes( this.stmt ) : this.stmt ));
return 'rs_ok';
},
Expand Down Expand Up @@ -156,6 +156,7 @@ module.exports = function(sql)
var strValue = r.lcstring();
if( charset )
strValue = charset.convertFromBytes( strValue );

var value = string2type(strValue, field.type); // todo: move to serialiser unpackString
this.store_column(row, field, value, use_hash)
field_index++;
Expand Down
18 changes: 18 additions & 0 deletions test/insert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var test=require('./insert.test.js');
var mysql=require('./common.js');
var db=mysql.createConnection();

test['test insert quoted']();
test['test insert quoted multiline']();
test['test insert multibyte characters']();
test['test insert multibyte using execute']();

setTimeout(function(){
db.query("select * from tbl").on('row',function(r){
console.log(r);
});
},3000);

setTimeout(function(){
process.exit(0);
},4500);
21 changes: 21 additions & 0 deletions test/insert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,27 @@ module.exports = {
})

})
},

'test insert multibyte using execute':function(cb) {

var db = createConnection();
var parent = 43;
var field = "中文语言支持了";
db.execute('INSERT INTO tbl SET id = NULL,parent = ?,field = (?)',[parent , field]).on('end', function(){
var insert_id = this.result.insert_id;

assert.ok(insert_id >= 0)

db.execute('SELECT id,parent,field FROM tbl WHERE id = ' + insert_id).on('row', function(row){
assert.equal( row.id, insert_id );
assert.equal( row.field, "中文语言支持了" );
}).on('end', function() {
db.close();
if(cb)
cb();
})
});

}
}