Sannis / node-mysql-libmysqlclient

Asynchronous MySQL binding for Node.js
http://sannis.github.com/node-mysql-libmysqlclient
Other
229 stars 47 forks source link

Binary blob issues #185

Closed tim-king closed 9 years ago

tim-king commented 10 years ago

Table with blob type column

update fonts set content=0x1AFB where id=1

but when I retrieve it I get { content: <Buffer 1a ef bf bd> }

What's going on?

Sannis commented 10 years ago

From email:

Hi. Here's a fix for binary data types which just don't work - your binary test doesn't have any values with the top bit set. I changed the test to

  res = conn.querySync("INSERT INTO " + cfg.test_table2 +
                   " (vc, vbi, bi, b) VALUES ('34\\0\\0', '34\\0\\0', '34\\0\\0', x'00ABFA');") && res;

and the check to

 new Buffer([0, 0xab, 0xfa] )

and it fails. You need to do (I think, and I'm not a node expert, but I found http://www.samcday.com.au/blog/2011/03/03/creating-a-proper-buffer-in-a-node-c-addon/) and so did this

if (field.flags & BINARY_FLAG) {
                    node::Buffer *slowBuffer = node::Buffer::New(field_length);
memcpy(node::Buffer::Data(slowBuffer),field_value,field_length);
                    // Convert to a real buffer object
                    // First step is to get a handle to the global object.
                    v8::Local<v8::Object> globalObj = v8::Context::GetCurrent()->Global();
                    // Now we need to grab the Buffer constructor function.
                    v8::Local<v8::Function> bufferConstructor = v8::Local<v8::Function>::Cast(globalObj->Get
(v8::String::New("Buffer")));
                    // Create new Buffer
                    v8::Handle<v8::Value> constructorArgs[3] = { slowBuffer->handle_, v8::Integer::New(field
_length), v8::Integer::New(0) };
                    v8::Local<v8::Object> realBuffer = bufferConstructor->NewInstance(3, constructorArgs);
                    js_field = NanNewLocal(realBuffer);
                } else {
                    js_field = V8STR2(field_value, field_length);
                }

Can you check this out and hopefully issue a fix if it works - I have only tested it a little and it may lead to a memory leak or something.