Project

General

Profile

MWSupport for custom data types in dbo library

Added by Michael Wagner over 16 years ago

How can I implement a custom data type in addition to the types already supported by the dbo library? In particular I would need a boolean type that I can map to a PostgreSQL "boolean" type. I created a class "BooleanType" that inherits from the sql_value_traits class but now I am stuck:

class BooleanType : public dbo::sql_value_traits< bool, void >
...
...

Is there any example around?

Many thanks,

Michael


Replies (2)

KD RE: Support for custom data types in dbo library - Added by Koen Deforche over 16 years ago

Hey Michael,

The traits class should describe properties about your particular type through specialization for your type:

Header file:

template<>
struct sql_value_traits<bool, void>
{
  static const bool specialized = true;

  static const char *type(SqlConnection *conn, int size);
  static void bind(bool v, SqlStatement *statement, int column, int size);
  static bool read(bool& v, SqlStatement *statement, int column, int size);
};

Implementation:

const char *sql_value_traits<bool>::type(SqlConnection *conn, int size)
{
  return "boolean not null";
}

void sql_value_traits<bool>::bind(bool v,
                  SqlStatement *statement, int column,
                  int size)
{
  statement->bind(column, static_cast<int>(v ? 1 : 0));
}

bool sql_value_traits<bool>::read(bool& v,
                  SqlStatement *statement, int column,
                  int size)
{
  int value;
  bool notNull = statement->getResult(column, &value);
  if (notNull)
    v = value;
  return notNull;
}

I think the bool type could actually be supported by the library itself...

MW RE: Support for custom data types in dbo library - Added by Michael Wagner over 16 years ago

You guys are really helpful, thanks a lot!!!
Another way that I tried works as well since the PostgreSQL "boolean" can be mapped to a string:

Header:
...

private:
std::string transparent_;

public:

template
void persist(Action& a)
{
dbo::field(a, transparent_, "transparent"); // "transparent" column is of type "boolean"
...
}

CPP:
...

void Layer::setTransparent(bool transparent)
{
transparent_ = transparent ? "true" : "false";
}

bool Layer::getTransparent()
{
return transparent_ = "true" ? true : false;
}

    (1-2/2)