@@ -72,6 +72,35 @@ struct Coordinate {
7272 }
7373};
7474
75+ enum class Pet {
76+ Cat ,
77+ Dog ,
78+ Other
79+ };
80+
81+ std ::string petToString (Pet p ) {
82+ switch (p ) {
83+ case Pet ::Cat :
84+ return "cat ";
85+ case Pet ::Dog :
86+ return "dog ";
87+ case Pet ::Other :
88+ return "other ";
89+ }
90+ throw std ::invalid_argument ("Unknown pet type : " + std ::to_string (static_cast < int > (p )));
91+ }
92+
93+ Pet petFromString (const std ::string & s ) {
94+ if (s == "cat" )
95+ return Pet ::Cat ;
96+ else if (s == "dog" )
97+ return Pet ::Dog ;
98+ else if (s == "other" )
99+ return Pet ::Other ;
100+ else
101+ throw std ::invalid_argument ("Unknown pet type: " + s );
102+ }
103+
75104std ::ostream & operator << (std ::ostream & o , const Coordinate & c )
76105{
77106 return o << "(" << c .x << ", " << c .y << ")" ;
@@ -87,6 +116,30 @@ namespace Wt {
87116 field (action , coordinate .x , name + "_x" , 1000 );
88117 field (action , coordinate .y , name + "_y" , 1000 );
89118 }
119+
120+ template < >
121+ struct sql_value_traits < Pet >
122+ {
123+ static std ::string type (SqlConnection * conn , int size )
124+ {
125+ return sql_value_traits < std ::string > ::type (conn , size );
126+ }
127+
128+ static void bind (Pet p , SqlStatement * statement , int column , int size )
129+ {
130+ statement -> bind (column , petToString (p ));
131+ }
132+
133+ static bool read (Pet & p , SqlStatement * statement , int column , int size )
134+ {
135+ std ::string s ;
136+ bool result = statement -> getResult (column , & s , size );
137+ if (!result )
138+ return false;
139+ p = petFromString (s );
140+ return true;
141+ }
142+ };
90143 }
91144}
92145
@@ -172,11 +225,22 @@ public:
172225 std ::chrono ::duration < int , std ::milli > timeduration ;
173226 bool checked ;
174227 int i ;
228+ Pet pet ;
175229 ::int64_t i64 ;
176230 long long ll ;
177231 float f ;
178232 double d ;
179233
234+ A ()
235+ : checked (false),
236+ i (0 ),
237+ pet (Pet ::Other ),
238+ i64 (0 ),
239+ ll (0 ),
240+ f (0 ),
241+ d (0 )
242+ { }
243+
180244 bool operator == (const A & other ) const {
181245 if (binary .size () != other .binary .size ()){
182246 DEBUG (std ::cerr << "ERROR: binary.size = " << binary .size ()
@@ -261,6 +325,9 @@ public:
261325 if (i != other .i ) {
262326 DEBUG (std ::cerr << "ERROR: i = " << i << " | " << other .i << std ::endl );
263327 }
328+ if (pet != other .pet ) {
329+ DEBUG (std ::cerr << "ERROR: pet = " << petToString (pet ) << " | " << petToString (other .pet ) << std ::endl );
330+ }
264331 if (i64 != other .i64 ) {
265332 DEBUG (std ::cerr << "ERROR: i64 = " <<i64 << " | "
266333 << other .i64 << std ::endl );
@@ -299,6 +366,7 @@ public:
299366 std ::cout << "date = " << date .toString () << " ; " << other .date .toString () << std ::endl ;
300367 std ::cout << "datetime = " << datetime .toString () << " ; " << other .datetime .toString () << std ::endl ;
301368 std ::cout << "i = " << i << " ; " << other .i << std ::endl ;
369+ std ::cout << "pet = " << petToString (pet ) << " ; " << petToString (other .pet ) << std ::endl ;
302370 std ::cout << "i64 = " << i64 << " ; " << other .i64 << std ::endl ;
303371 std ::cout << "ll = " << ll << " ; " << other .ll << std ::endl ;
304372 std ::cout << "f = " << f << " ; " << other .f << std ::endl ;
@@ -323,6 +391,7 @@ public:
323391 & & timepoint == other .timepoint
324392 && timeduration == other .timeduration
325393 && i == other .i
394+ && pet == other .pet
326395 && i64 == other .i64
327396 && ll == other .ll
328397 && checked == other .checked
@@ -355,6 +424,7 @@ public:
355424 dbo ::field (a , timepoint , "timepoint ");
356425 dbo ::field (a , timeduration , "timeduration ");
357426 dbo ::field (a , i , "i ");
427+ dbo ::field (a , pet , "pet ");
358428 dbo ::field (a , i64 , "i64 ");
359429 dbo ::field (a , ll , "ll ");
360430 dbo ::field (a , checked , "checked ");
@@ -558,6 +628,7 @@ BOOST_AUTO_TEST_CASE( dbo_test1 )
558628 a1 .timeduration = std ::chrono ::hours (1 ) + std ::chrono ::seconds (10 );
559629 a1 .checked = true;
560630 a1 .i = 42 ;
631+ a1 .pet = Pet ::Cat ;
561632 a1 .i64 = 9223372036854775805LL ;
562633 a1 .ll = 6066005651767221LL ;
563634 a1 .f = (float )42.42 ;
@@ -651,6 +722,7 @@ BOOST_AUTO_TEST_CASE( dbo_test2 )
651722 a1 .string = "There ";
652723 a1 .checked = false;
653724 a1 .i = 42 ;
725+ a1 .pet = Pet ::Dog ;
654726 a1 .i64 = 9223372036854775804LL ;
655727 a1 .ll = 6066005651767221LL ;
656728 a1 .f = (float )42.42 ;
@@ -826,6 +898,7 @@ BOOST_AUTO_TEST_CASE( dbo_test4 )
826898 a1 .modify ()-> wstring = "Hello ";
827899 a1 .modify ()-> string = "There ";
828900 a1 .modify ()-> i = 42 ;
901+ a1 .modify ()-> pet = Pet ::Cat ;
829902 a1 .modify ()-> i64 = 9223372036854775803LL ;
830903 a1 .modify ()-> ll = 6066005651767220LL ;
831904 a1 .modify ()-> f = (float )42.42 ;
@@ -1658,6 +1731,7 @@ BOOST_AUTO_TEST_CASE( dbo_test14 )
16581731 a1 .string = "There ";
16591732 a1 .checked = false;
16601733 a1 .i = 42 ;
1734+ a1 .pet = Pet ::Other ;
16611735 a1 .i64 = 9223372036854775804LL ;
16621736 a1 .ll = 6066005651767221LL ;
16631737 a1 .f = (float )42.42 ;
0 commit comments