I am trying to create a custom type for PostgreSQL MACADDRESS column
type.

I have implemented it like his:

class MAC

{

public:

    MAC () { ; }

    MAC (std::string i_mac) : m_mac (i_mac) { ; }

 

    void set (const MAC & i_mac) { m_mac = i_mac.m_mac; }

    void set (std::string i_mac) { m_mac = i_mac; }

    std::string get() const { return m_mac; }

 

    friend ostream & operator << (ostream & stream, const MAC & i_mac) {
stream << i_mac.m_mac; }

    MAC& operator = (const std::string & i_mac) 

    {  

            m_mac = i_mac;

            return *this;

    }

    MAC& operator = (const MAC& i_mac) 

    {  

            m_mac = i_mac.m_mac;

            return *this;

    }

 

private:

    std::string m_mac;

};

 

namespace soci

{

      template<> struct type_conversion<MAC>

      {

            typedef std::string base_type;

            

            static void from_base(std::string i_mac, indicator ind, MAC
& mac)

            {

                  if (ind == i_null)

                  {

                        throw soci_error("Null value not allowed for
this type");

                  }

                  mac = i_mac;

            }

 

            static void to_base (const MAC & mac, std::string & i_mac,
indicator & ind)

            {

                  i_mac = mac.get();

                  ind = i_ok;

            }

      };

}

 

This implementation works and this is the code to show it:

 

MAC m;

sql << "select mac from my_types where type = 1", into(m);

cout << m << endl;

 

//
------------------------------------------------------------------------
----------------------------

 

My next step was to include this as part of a structure

I have implemented a structure like this:

 

struct MyTypes

{

      int type;

      std::string name;

      MAC mac;

      std::tm ctrl_timestamp;

};

 

namespace soci

{

      template<> struct type_conversion<MyTypes>

      {

            typedef values base_type;

            static void from_base(values const & v, indicator, MyTypes &
ut)

            {

                  ut.type = v.get<int>("type");

                  ut.name = v.get<std::string>("name");

                  ut.mac = v.get<MAC>("mac");

                  ut.ctrl_timestamp = v.get<std::tm>("ctrl_timestamp");

            }

 

            static void to_base(const MyTypes & ut, values & v,
indicator & ind)

            {

                  v.set<int>("type", ut.type);

                  v.set<std::string>("name", ut.name );

                  v.set<MAC>("mac", ut.mac);

                  v.set<std::tm>("ctrl_timestamp", ut.ctrl_timestamp);

                  ind = i_ok;

            }

      };

}

Trying to run this as the following code shows gives me: "PostgreSQL
error: Unknown data type"

 

rowset<MyTypes> rs = (sql.prepare << "select type, name, mac,
ctrl_timestamp, from my_types");

 

 


Regards

Gabriel Schwartz 

We can't solve problems by using the same kind of thinking we used when
we created them.



 

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Soci-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/soci-users

Reply via email to