Project

General

Profile

Custom trait (as described in the documentation) won't compile

Added by Stefan Kurek about 1 year ago

I'm trying to make my own class be serializable to be used in a dbo-field / db-column.
For this, I followed the example at https://webtoolkit.eu/wt/doc/reference/html/structWt_1_1Dbo_1_1sql__value__traits.html#details , so I have a header-filer consisting of this code:

#ifndef InclusionGuard
#define InclusionGuard
#include "A.hpp"
#include <Wt/Dbo/SqlConnection.h>
#include <Wt/Dbo/SqlTraits.h>
#include <map>
#include <string>

typedef std::map<std::string, A> AMap;

A stringToAMap(std::string str);
std::string aMapToString(AMap const & aMap);

namespace Wt {
    namespace Dbo {
        template <>
        struct sql_value_traits<AMap> {
            static std::string type(SqlConnection * sqlcon_ptr, int i_size) {
                return sql_value_traits<std::string>::type(sqlcon_ptr, i_size);
            }
            static void bind(AMap aMap, SqlStatement * sqlstatement_ptr, int i_column, int i_size) {
                sqlstatement_ptr->bind(i_column, aMapToString(aMap));
            }
            static bool read(AMap & aMap, SqlStatement * sqlstatement_ptr, int i_column, int i_size) {
                std::string str;
                bool b_result = sqlstatement_ptr->getResult(i_column, &str, i_size);
                if (!b_result)
                    return false;
                aMap = stringToAMap(str);
                return true;
            }
        };
    }
}

#endif // InclusionGuard

I get the compilation-error: "‘type’ is not a member of ‘Wt::Dbo::sql_value_traitsstd::basic_string >’" and when I looked there, indeed, there's no function named "type", exception for static const char * type (SqlConnection *connection, int size) which is also mentioned on the linked documentation-webpage, but this one in SqlTraits.h is hidden behind an #ifdef DOXYGEN_ONLY.

Please tell me: What shall I do different? How is this supposed to work?

Furthermore, if I try to split the definition from the header to add the implementation to a cpp-file (like you see below) I get other errors: "X is not an entitiy that can be explicitely specialized" (where X is each functions' name).

Hpp:

namespace Wt {
    namespace Dbo {
        template <>
        struct sql_value_traits<AMap> {
            static std::string type(SqlConnection * sqlcon_ptr, int i_size);
            static void bind(AMap aMap, SqlStatement * sqlstatement_ptr, int i_column, int i_size);
            static bool read(AMap & aMap, SqlStatement * sqlstatement_ptr, int i_column, int i_size);
        };
    }
}

Cpp:

namespace Wt {
    namespace Dbo {
        template <>
        std::string sql_value_traits<AMap>::type(SqlConnection * sqlcon_ptr, int i_size) {
            return sql_value_traits<std::string>::type(sqlcon_ptr, i_size);
        }
        template <>
        void sql_value_traits<AMap>::bind(AMap aMap, SqlStatement * sqlstatement_ptr, int i_column, int i_size) {
            sqlstatement_ptr->bind(i_column, aMapToString(aMap));
        }
        template <>
        bool sql_value_traits<AMap>::read(AMap & aMap, SqlStatement * sqlstatement_ptr, int i_column, int i_size) {
            std::string str;
            bool b_result = sqlstatement_ptr->getResult(i_column, &str, i_size);
            if (!b_result)
                return false;
            aMap = stringToAMap(str);
            return true;
        }
    }
}

After googling I tried removing the template <>-lines (even though I deem them necessary), but that yielded the same error as with having the implementation completely in the header-file.

Could you please illuminate the situation and how to solve this problem? Thank you in advance!


Replies (2)

RE: Custom trait (as described in the documentation) won't compile - Added by lm at 11 months ago

Add #include <Wt/Dbo/StdSqlTraits.h>.

The function you want is on line 35 of that file.

    (1-2/2)