1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
#include "common.hh"
#include "db.hh"
#include "event.hh"
namespace stuff {
namespace {
const std::string kEventTable = "events";
const std::string kEventGoingTable = "events_going";
class EventImpl : public Event {
public:
~EventImpl() override {
}
int64_t id() const override {
return id_;
}
const std::string& name() const override {
return name_;
}
void set_name(const std::string& name) override {
if (name_ == name) return;
edit();
editor_->set("name", name);
name_ = name;
}
const std::string& text() const override {
return text_;
}
void set_text(const std::string& text) override {
if (text_ == text) return;
edit();
editor_->set("text", text);
text_ = text;
}
time_t start() const override {
return start_;
}
void set_start(time_t start) override {
if (start_ == start) return;
edit();
editor_->set("start", static_cast<int64_t>(start));
start_ = start;
}
void going(std::vector<Going>* going) const override {
going->assign(going_.begin(), going_.end());
}
bool is_going(const std::string& name) const override {
for (const auto& going : going_) {
if (going.name == name) {
return going.is_going;
}
}
return false;
}
void update_going(const std::string& name, bool is_going,
const std::string& note) override {
for (auto it = going_.begin(); it != going_.end(); ++it) {
if (it->name == name) {
if (it->is_going == is_going && it->note == note) {
return;
}
going_.erase(it);
break;
}
}
std::vector<Going>::iterator it;
if (is_going) {
for (it = going_.begin(); it != going_.end(); ++it) {
if (!it->is_going) {
break;
}
}
} else {
it = going_.end();
}
going_.emplace(it, name, is_going, note, time(NULL));
going_uptodate_ = false;
}
bool store() override {
if (editor_) {
DB::Transaction transaction(db_);
if (!editor_->commit()) return false;
if (new_) {
id_ = editor_->last_insert_rowid();
}
if (store_going() && transaction.commit()) {
new_ = false;
editor_.reset();
return true;
} else {
if (new_) id_ = 0;
return false;
}
} else {
if (new_) return false;
return store_going();
}
}
bool remove() override {
if (new_) return true;
DB::Transaction transaction(db_);
if (db_->remove(kEventTable,
DB::Condition("id", DB::Condition::EQUAL, id_)) < 0 ||
db_->remove(kEventGoingTable,
DB::Condition("event", DB::Condition::EQUAL, id_)) < 0)
return false;
if (transaction.commit()) {
new_ = true;
id_ = 0;
return true;
}
return false;
}
bool load(DB::Snapshot* snapshot) {
editor_.reset();
new_ = false;
if (snapshot->bad()) return false;
if (!snapshot->get(0, &id_)) return false;
if (!snapshot->get(1, &name_)) return false;
int64_t tmp;
if (!snapshot->get(2, &tmp)) return false;
start_ = tmp;
if (!snapshot->get(3, &text_)) {
text_ = "";
}
return load_going();
}
EventImpl(std::shared_ptr<DB> db)
: db_(db), id_(0), new_(true), going_uptodate_(true) {
}
private:
void edit() {
if (editor_) return;
if (new_) {
editor_ = db_->insert(kEventTable);
} else {
editor_ = db_->update(kEventTable,
DB::Condition("id",
DB::Condition::EQUAL,
DB::Value(id_)));
}
}
bool store_going() {
if (going_uptodate_) return true;
DB::Transaction transaction(db_);
if (db_->remove(kEventGoingTable,
DB::Condition("event", DB::Condition::EQUAL, id_)) < 0)
return false;
for (const auto& going : going_) {
auto editor = db_->insert(kEventGoingTable);
editor->set("event", id_);
editor->set("name", going.name);
editor->set("is_going", going.is_going);
editor->set("note", going.note);
editor->set("added", static_cast<int64_t>(going.added));
if (!editor->commit()) {
return false;
}
}
if (transaction.commit()) {
going_uptodate_ = true;
return true;
}
return false;
}
bool load_going() {
going_.clear();
going_uptodate_ = true;
std::vector<DB::OrderBy> order_by;
order_by.push_back(DB::OrderBy("is_going", false));
order_by.push_back(DB::OrderBy("added"));
order_by.push_back(DB::OrderBy("name"));
auto snapshot = db_->select(kEventGoingTable,
DB::Condition("event",
DB::Condition::EQUAL,
id_),
order_by);
if (!snapshot) return true;
do {
std::string name;
bool is_going;
std::string note;
int64_t added;
if (!snapshot->get(0, &name) || !snapshot->get(1, &is_going) ||
!snapshot->get(3, &added))
return false;
if (!snapshot->get(2, ¬e))
note = "";
going_.emplace_back(name, is_going, note,
static_cast<time_t>(added));
} while (snapshot->next());
return !snapshot->bad();
}
std::shared_ptr<DB> db_;
std::shared_ptr<DB::Editor> editor_;
int64_t id_;
std::string name_;
std::string text_;
time_t start_;
bool new_;
bool going_uptodate_;
std::vector<Going> going_;
};
std::shared_ptr<DB::Snapshot> open(std::shared_ptr<DB> db) {
std::vector<DB::OrderBy> order_by;
order_by.push_back(DB::OrderBy("start"));
order_by.push_back(DB::OrderBy("name"));
return db->select(kEventTable, DB::Condition(), order_by);
}
} // namespace
// static
bool Event::setup(DB* db) {
DB::Declaration decl;
decl.push_back(std::make_pair("id", DB::PrimaryKey(DB::Type::INT64)));
decl.push_back(std::make_pair("name", DB::NotNull(DB::Type::STRING)));
decl.push_back(std::make_pair("start", DB::NotNull(DB::Type::INT64)));
decl.push_back(std::make_pair("text", DB::Type::STRING));
if (!db->insert_table(kEventTable, decl)) return false;
decl.clear();
decl.push_back(std::make_pair("event", DB::NotNull(DB::Type::INT64)));
decl.push_back(std::make_pair("name", DB::NotNull(DB::Type::STRING)));
decl.push_back(std::make_pair("is_going", DB::NotNull(DB::Type::BOOL)));
decl.push_back(std::make_pair("note", DB::Type::STRING));
decl.push_back(std::make_pair("added", DB::NotNull(DB::Type::INT64)));
return db->insert_table(kEventGoingTable, decl);
}
// static
std::unique_ptr<Event> Event::next(std::shared_ptr<DB> db) {
auto snapshot = open(db);
if (snapshot) {
do {
auto ev = new EventImpl(db);
if (ev->load(snapshot.get())) {
return std::unique_ptr<Event>(ev);
}
delete ev;
} while (snapshot->next());
}
return nullptr;
}
// static
std::vector<std::unique_ptr<Event>> Event::all(std::shared_ptr<DB> db) {
auto snapshot = open(db);
std::vector<std::unique_ptr<Event>> ret;
if (snapshot) {
do {
auto ev = new EventImpl(db);
if (ev->load(snapshot.get())) {
ret.emplace_back(ev);
} else {
delete ev;
}
} while (snapshot->next());
}
return ret;
}
// static
std::unique_ptr<Event> Event::create(std::shared_ptr<DB> db,
const std::string& name, time_t start) {
std::unique_ptr<Event> ev(new EventImpl(db));
ev->set_name(name);
ev->set_start(start);
return ev;
}
} // namespace stuff
|