Question

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!

Was it helpful?

Solution

It doesn't look like it would be a problem judging by that quote (it's basically saying if a null character is found in the blob it will stop the string there, however ASCII strings won't have random nulls in the middle of them). However, this might present a problem for internalization (multibyte charsets may have nulls in the middle).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top