Hello Nico,
Right now SOCI (or at least the MySQL backend) is very strict about the
types when using the row interface. This is often inconvenient and we
should probably change it. But for now the rules are:
- All integer types smaller than INT are promoted to int, regardless
whether the original type was signed or unsigned. So, e.g., if you select
a value of type TINYINT UNSIGNED, you should retrieve it from the rowset
using v.get<int>("id").
- INT is retrieved as int: v.get<int>("id")
- INT UNSIGNED is retrieved as (signed) long long: v.get<long long>("id"),
although this will work too: v.get<unsigned>("id").
- BIGINT is retrieved as long long.
- BIGINT UNSIGNED is retrieved as unsigned long long.
If you don't know the type that was selected form the database, you can use
v.get_properties("id").get_data_type() to find out. It will be
dt_unsigned_long_long for BIGINT UNSIGNED, dt_long_long for BIGINT and INT
UNSIGNED, and dt_integer for all the smaller integral types.
Unless I'm mistaken this problem only pertains to the row interface
(dynamic binding), when you use static binding, i.e., into(n) this should
be more forgiving with respect to types.
Thanks,
Aleksander
On Tue, Mar 5, 2013 at 6:21 AM, Nicolas Deroche <[email protected]> wrote:
>
> Hello,
>
> I just started using SOCI, and I have one quick question about casting
>
> Consider this structure:
> struct A
> { unsigned long one; unsigned int two; unsigned short three; }
>
> In MySQL, the fields are defined like:
> one : INT unsigned
> two: SMALLINT unsigned
> three: TINYINT unsigned
>
> How am I supposed to cast using the static templated function from_base
> the SQL values into my structure?
> static void from_base(values const & v, indicator /* ind */, A& s)
> {
> s.one = v.get<unsigned long>("id"); /// Throws std::bad_cast()
> }
>
> Thanks,
>
> nico
>
>
>
>
>
> ------------------------------------------------------------------------------
> Everyone hates slow websites. So do we.
> Make your web apps faster with AppDynamics
> Download AppDynamics Lite for free today:
> http://p.sf.net/sfu/appdyn_d2d_feb
> _______________________________________________
> soci-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/soci-users
>
------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_feb
_______________________________________________
soci-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/soci-users