Skip to content

Commit dbeaed4

Browse files
committed
Several change:
- WTableView: Fixed calculation of first in modelRowsAboutToBeRemoved - SqlTraits: - AuxId = SurrogateId | LiteralJoinId doesn't seem to make sense - Added example for custom sql_value_traits (with test)
1 parent 2b1541c commit dbeaed4

File tree

3 files changed

+140
-4
lines changed

3 files changed

+140
-4
lines changed

src/Wt/Dbo/SqlTraits.h

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,68 @@ class SqlStatement;
5656
* - Json::Object
5757
* - Json::Array
5858
*
59+
* Example for an enum that is saved as a string rather than an int:
60+
* \code
61+
* enum class Pet {
62+
* Cat,
63+
* Dog,
64+
* Other
65+
* };
66+
*
67+
* std::string petToString(Pet p) {
68+
* switch (p) {
69+
* case Pet::Cat:
70+
* return "cat";
71+
* case Pet::Dog:
72+
* return "dog";
73+
* case Pet::Other:
74+
* return "other";
75+
* }
76+
* throw std::invalid_argument("Unknown pet type: " + std::to_string(static_cast<int>(p)));
77+
* }
78+
*
79+
* Pet petFromString(const std::string &s) {
80+
* if (s == "cat")
81+
* return Pet::Cat;
82+
* else if (s == "dog")
83+
* return Pet::Dog;
84+
* else if (s == "other")
85+
* return Pet::Other;
86+
* else
87+
* throw std::invalid_argument("Unknown pet type: " + s);
88+
* }
89+
*
90+
* namespace Wt {
91+
* namespace Dbo {
92+
*
93+
* template<>
94+
* struct sql_value_traits<Pet>
95+
* {
96+
* static std::string type(SqlConnection *conn, int size)
97+
* {
98+
* return sql_value_traits<std::string>::type(conn, size);
99+
* }
100+
101+
* static void bind(Pet p, SqlStatement *statement, int column, int size)
102+
* {
103+
* statement->bind(column, petToString(p));
104+
* }
105+
106+
* static bool read(Pet &p, SqlStatement *statement, int column, int size)
107+
* {
108+
* std::string s;
109+
* bool result = statement->getResult(column, &s, size);
110+
* if (!result)
111+
* return false;
112+
* p = petFromString(s);
113+
* return true;
114+
* }
115+
* };
116+
*
117+
* } // Dbo
118+
* } // Wt
119+
* \endcode
120+
*
59121
* \sa query_result_traits
60122
*
61123
* \ingroup dbo
@@ -115,8 +177,8 @@ enum FieldFlags {
115177
ForeignKey = 0x20, //!< Field is (part of) a foreign key
116178
FirstDboField = 0x40,
117179
LiteralJoinId = 0x80,
118-
AuxId = 0x81,
119-
AliasedName = 0x100 // there is an AS in the field, so the name is aliased
180+
AuxId = 0x100,
181+
AliasedName = 0x200 // there is an AS in the field, so the name is aliased
120182
};
121183

122184
/*! \class FieldInfo Wt/Dbo/SqlTraits.h Wt/Dbo/SqlTraits.h
@@ -170,7 +232,7 @@ class WTDBO_API FieldInfo
170232
*/
171233
bool isNaturalIdField() const { return (flags_ & NaturalId) != 0; }
172234

173-
/*! \brief Returns whether the field is a Surroaget Id field.
235+
/*! \brief Returns whether the field is a Surrogate Id field.
174236
*/
175237
bool isSurrogateIdField() const { return flags_ & SurrogateId; }
176238

src/Wt/WTableView.C

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1528,7 +1528,7 @@ void WTableView::modelRowsAboutToBeRemoved(const WModelIndex& parent,
15281528
start, end + 1);
15291529

15301530
if (overlapMiddle > 0) {
1531-
int first = std::min(start, firstRow());
1531+
int first = std::max(0, start - firstRow());
15321532

15331533
for (int i = 0; i < renderedColumnsCount(); ++i) {
15341534
ColumnWidget *column = columnContainer(i);

test/dbo/DboTest.C

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
75104
std::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

Comments
 (0)