[odb-users] Null values for unset properties

Boris Kolpackov boris at codesynthesis.com
Wed Jul 27 10:37:51 EDT 2011


Hi Rafael,

Rafael Pena <pena.rd at gmail.com> writes:

> I figured it out. My image_type was wrong. This is working.
> 
> typedef double_ptr value_type;
> typedef double query_type;
> typedef double_ptr image_type;

That doesn't look right. image_type should be double, not double_ptr.

I created a test for this and it works fine using your original version.
Here is the traits specialization for MySQL, saved into traits.hxx:

#ifndef TRAITS_HXX
#define TRAITS_HXX

#include <boost/shared-ptr.hxx>

#include <odb/mysql/traits.hxx>

namespace odb
{
  namespace mysql
  {
    typedef boost::shared_ptr<double> double_ptr;

    template <>
    class value_traits<double_ptr, id_double> {
    public:
      typedef double_ptr value_type;
      typedef double query_type;
      typedef double image_type;

      static void
      set_value(double_ptr& v, const double& b, bool is_null)
      {
        v.reset (is_null ? 0 : new double (b));
      }

      static void
      set_image(double& b, bool& is_null, const double_ptr& v)
      {
        is_null = (v.get() == 0);

        if (!is_null)
          b = *v;
      }
    };
  }
}

#endif // TRAITS_HXX

Then I included it into the generated header file with this option:

--hxx-prologue '#include "traits.hxx"'

The header file that uses double_ptr looks like this (using the new null
pragma from 1.5.0):

typedef boost::shared_ptr<double> double_ptr;
#pragma db value(double_ptr) type("DOUBLE") null

#pragma db object
struct object
{
  #pragma db id auto
  unsigned long id_;

  double_ptr d_;
};

Boris



More information about the odb-users mailing list