Is it safe to convert a mysqlpp::sql_blob to a std::string?

Posted by Runcible on Stack Overflow See other posts from Stack Overflow or by Runcible
Published on 2009-10-09T00:44:37Z Indexed on 2010/04/20 18:13 UTC
Read the original article Hit count: 330

I'm grabbing some binary data out of my MySQL database. It comes out as a mysqlpp::sql_blob type.

It just so happens that this BLOB is a serialized Google Protobuf. I need to de-serialize it so that I can access it normally.

This gives a compile error, since ParseFromString() is not intended for mysqlpp:sql_blob types:

protobuf.ParseFromString( record.data );

However, if I force the cast, it compiles OK:

protobuf.ParseFromString( (std::string) record.data );

Is this safe? I'm particularly worried because of this snippet from the mysqlpp documentation:

"Because C++ strings handle binary data just fine, you might think you can use std::string instead of sql_blob, but the current design of String converts to std::string via a C string. As a result, the BLOB data is truncated at the first embedded null character during population of the SSQLS. There’s no way to fix that without completely redesigning either String or the SSQLS mechanism."

Thanks for your assistance!

© Stack Overflow or respective owner

Related posts about google

Related posts about protocol-buffers