-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.cc
300 lines (258 loc) · 6.94 KB
/
database.cc
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
292
293
294
295
296
297
298
299
300
#include "database.h"
#include <QSqlDatabase>
#include <QVariant>
#include <QSqlQuery>
#include <QSqlError>
namespace
{
int schema_version = 0;
bool initialized = false;
bool db_init_units()
{
QString statement;
QSqlQuery query;
statement =
"insert into units (name) values"
"('pinch'),"
"('teaspoon'),"
"('tablespoon'),"
"('cup'),"
"('quart'),"
"('pint'),"
"('gallon'),"
"('ounce'),"
"('pound');";
if (!query.exec(statement))
return false;
return true;
}
QMap<QString, int> db_name_id_map(QString table)
{
QMap<QString, int> result;
QSqlQuery query(QString("select name, id from %1;").arg(table));
while (query.next())
result.insert(query.value(0).toString(), query.value(1).toInt());
return result;
}
int db_schema_version()
{
QSqlQuery query("select value from schema_versions order by value asc limit 1;");
if (query.next())
return query.value(0).toInt();
return -1;
}
bool db_set_field_by_id(int id, QString table, QString field, QVariant value)
{
QSqlQuery query;
if (!query.prepare(QString("update %1 set %2 = :value where id = :id;").arg(table).arg(field)))
return false;
query.bindValue(":value", value);
query.bindValue(":id", id);
return query.exec();
}
int db_id_by_field(QString table, QString field, QVariant value)
{
QSqlQuery query;
if (!query.prepare(QString("select id from %1 where %2 = :value;").arg(table).arg(field)))
return -1;
query.bindValue(":value", value);
if (!query.exec() || !query.next())
return -1;
return query.value(0).toInt();
}
bool db_set_schema_version()
{
QSqlQuery query;
if (!query.prepare("insert into schema_versions (value) values (?)"))
return false;
query.addBindValue(QVariant(schema_version));
return query.exec();
}
QVariant db_field_by_id(QString table, QString field, int id)
{
QSqlQuery query;
if (!query.prepare(QString("select %1 from %2 where id = :id").arg(field).arg(table)))
return QVariant();
query.bindValue(":id", id);
if (!query.exec() || !query.next())
return QVariant();
return query.value(0);
}
QStringList db_field_list(QString table, QString field)
{
QStringList result;
QSqlQuery query(QString("select %1 from %2;").arg(field).arg(table));
while (query.next())
result.append(query.value(0).toString());
return result;
}
}
bool db_init(QString src)
{
if (initialized)
return false;
initialized = true;
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
if (src.length())
db.setDatabaseName(src);
else
db.setDatabaseName(":memory:");
if (!db.open())
return false;
QSqlQuery query("pragma foreign_keys = on;");
QString statement;
statement =
"create table if not exists schema_versions ("
"id integer primary key asc,"
"value integer"
");";
if (!query.exec(statement))
return false;
int current_version = db_schema_version();
bool fresh = current_version < 0;
if (current_version < schema_version && !db_set_schema_version())
return false;
statement =
"create table if not exists units ("
"id integer primary key asc,"
"name text not null,"
"constraint unit_name_unique unique (name)"
");";
if (!query.exec(statement))
return false;
statement =
"create table if not exists recipes ("
"id integer primary key asc,"
"name text not null,"
"planned integer not null default 0,"
"meals real not null default 1,"
"steps text not null default ''"
");";
if (!query.exec(statement))
return false;
statement =
"create table if not exists foods ("
"id integer primary key asc,"
"name text not null,"
"staple integer not null default 0,"
"price real not null default 0,"
"constraint food_name_unique unique (name)"
");";
if (!query.exec(statement))
return false;
statement =
"create table if not exists ingredients ("
"id integer primary key asc,"
"recipe integer not null references recipes(id) on delete cascade,"
"food integer not null references foods(id) on delete cascade,"
"unit integer references units(id),"
"quantity real not null default 0"
");";
if (!query.exec(statement))
return false;
statement =
"create table if not exists groceries ("
"id integer primary key asc,"
"food integer not null references foods(id),"
"quantity real not null,"
"generated integer not null default 0"
");";
if (!query.exec(statement))
return false;
if (fresh && !db_init_units())
return false;
return true;
}
QMap<QString, int> db_unit_id_map()
{
return db_name_id_map("units");
}
QMap<QString, int> db_food_id_map()
{
return db_name_id_map("foods");
}
QMap<QString, int> db_recipe_id_map()
{
return db_name_id_map("recipes");
}
int db_add_recipe(QString name)
{
QSqlQuery query;
if (!query.prepare("insert into recipes (name) values (:name);"))
return -1;
query.bindValue(":name", name);
return query.exec() ? query.lastInsertId().toInt() : -1;
}
bool db_remove_id(QString table, int id)
{
QSqlQuery query;
if (!query.prepare(QString("delete from %1 where id = :id;").arg(table)))
return false;
query.bindValue(":id", id);
return query.exec();
}
QString db_recipe_name(int id)
{
return db_field_by_id("recipes", "name", id).toString();
}
QString db_recipe_steps(int id)
{
return db_field_by_id("recipes", "steps", id).toString();
}
bool db_set_recipe_name(int id, QString name)
{
return db_set_field_by_id(id, "recipes", "name", name);
}
bool db_set_recipe_steps(int id, QString steps)
{
return db_set_field_by_id(id, "recipes", "steps", steps);
}
QStringList db_food_names()
{
return db_field_list("foods", "name");
}
QStringList db_recipe_names()
{
return db_field_list("recipes", "name");
}
int db_food_id(QString name)
{
return db_id_by_field("foods", "name", name);
}
int db_recipe_id(QString name)
{
return db_id_by_field("recipes", "name", name);
}
int db_add_food(QString name)
{
QSqlQuery query;
if (!query.prepare("insert into foods (name) values (:name);"))
return -1;
query.bindValue(":name", name);
return query.exec() ? query.lastInsertId().toInt() : -1;
}
void db_clear_planned_groceries()
{
QSqlQuery query("delete from groceries where generated = 1;");
}
void db_generate_planned_groceries()
{
QSqlQuery query(
"insert into groceries (generated, food, quantity) "
"select 1, f.id, (case f.staple when 0 then sum(i.quantity) else 1 end) "
"from recipes r join ingredients i on r.id = i.recipe join foods f on f.id = i.food where r.planned = 1 "
"group by f.id;"
);
}
bool db_add_planned(int recipe)
{
QSqlQuery query;
if (!query.prepare("update recipes set planned = 1 where id = :id"))
return false;
query.bindValue(":id", recipe);
return query.exec();
}
void db_clear_planned()
{
QSqlQuery query("update recipes set planned = 0;");
}