How to use Id of binary type in DBO?

Added by Plug Gulp 3 months ago

Dear Wt Team,

In DBO, how to support a table that has a binary field as primary key?

The documentation at*value*_traits.html

indicates that there is a support for binary data i.e. std::vector<unsigned char>.

So if I have a DBO column of type std::vector<unsigned char>, then will I be able to set that as a primary key using Wt::Dbo::id()?

When I tried that I get an error related to stream(std::ostream) insertion operator(<<) for the binary type. As there is no default stream insertion support for std::vector<unsigned char> I implemented one:

std::ostream& operator<<(std::ostream &o, std::vector<unsigned char> const &v)
        for(auto i : v) {
                unsigned short d = i;
                o << std::hex << d;

        return o;

But this does not work. I still get the following error:

wt/include/Wt/Dbo/ptr.h:372:7: error: no match for 'operator<<' (operand types are 'std::stringstream {aka std::__cxx11::basic_stringstream}' and 'Wt::Dbo::MetaDbo::IdType {aka std::vector}')

s << id();


This is how I implemented a table in DBO:

struct my_table
    std::vector<unsigned char> id;
    int dummy;

    template<typename Action>
    void persist(Action &a)
        Wt::Dbo::id(a, id);
        Wt::Dbo::field(a, dummy, "dummy");

namespace Wt {
        namespace Dbo {
                struct dbo_traits<struct my_table> : public dbo_default_traits
                                typedef std::vector<unsigned char> IdType;

                                static IdType invalidId()
                                        return std::vector<unsigned char>();

                                static const char *surrogateIdField() { return 0; }

How to support binary primary key in DBO?

Thanks and kind regards,


Replies (1)

RE: How to use Id of binary type in DBO? - Added by Plug Gulp 3 months ago

Okay, I found a solution to this. The problem is that extraction and insertion function overloads for std containers do not become part of std and hence any code from Wt does not pickup those overloaded functions. The solution is to either encapsulate or inherit from std::vector and use that class in place of std classes.