[odb-users] NULL Fields, how do they work ?

Nicolas ALBEZA n.albeza at gmail.com
Fri Jul 22 09:46:47 EDT 2011


Thanks a lot for your help, Boris.

Do you know when the version including this feature will be released ?

Regards,

2011/7/22 Boris Kolpackov <boris at codesynthesis.com>

> Hi Nicolas,
>
> Nicolas ALBEZA <n.albeza at gmail.com> writes:
>
> > I'm trying to make a field nullable, but i can't find how in the manual.
>
> For this you will need two things:
>
> 1. A C++ type that is capable of representing the special NULL value.
>   This can be, for example, a pointer as in:
>
>   #pragma db object
>   class object
>   {
>     ...
>
>     // NULL pointer is equivalent to the NULL value in the database.
>     //
>     shared_ptr<std::string> str;
>   };
>
>   Or you can create a wrapper along these lines:
>
>   struct nullable_string
>   {
>     bool null;
>     std::string str;
>   };
>
>   #pragma db object
>   class object
>   {
>     nullable_string str;
>   };
>
> 2. Once you have a type that has the notion of NULL, you will need
>   to provide a value_traits implementation for it. For an example,
>   take a look at the tests/mysql/types test in the odb-tests
>   package. There is a type called string_ptr which is used to
>   test NULL-ness. The value_traits implementation is in the
>   traits.hxx file.
>
> Also, some of the types supported by profiles provide the notion
> of NULL values and these type automatically store such values as
> NULL in the database. Once such type QString in the Qt profile.
>
> Finally, in one of the upcoming versions we are planning to add
> support for the boost::optional container which has a natural
> mapping to a column with NULL enabled. This will make working
> with NULL values a much more straightforward matter if you are
> using Boost. Perhaps we will also provide our own version of
> something like this in libodb, in case you cannot use Boost
> for some reason.
>
> Boris
>



-- 
ALBEZA "Pause" Nicolas


More information about the odb-users mailing list