-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlingredientmodel.cpp
More file actions
49 lines (41 loc) · 1.48 KB
/
sqlingredientmodel.cpp
File metadata and controls
49 lines (41 loc) · 1.48 KB
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
#include "sqlingredientmodel.hpp"
#include <QSqlRecord>
#include <QSqlQuery>
#include <QDebug>
#include <QSqlError>
SqlIngredientModel::SqlIngredientModel(QObject *parent) :QSqlQueryModel (parent)
{
roleNameHash.insert(Qt::UserRole, QByteArray("id"));
roleNameHash.insert(Qt::UserRole + 1, QByteArray("unit"));
roleNameHash.insert(Qt::UserRole + 2, QByteArray("amount"));
roleNameHash.insert(Qt::UserRole + 3, QByteArray("ingredient"));
roleNameHash.insert(Qt::UserRole + 4, QByteArray("group"));;
}
QVariant SqlIngredientModel::data(const QModelIndex &index, int role) const
{
if (role < Qt::UserRole)
return QSqlQueryModel::data(index, role);
const QSqlRecord sqlRecord = record(index.row());
return sqlRecord.value(role - Qt::UserRole);
}
void SqlIngredientModel::setRecipeId(quint16 id) {
if (id == m_recipeId) {
return;
}
m_recipeId = id;
QSqlQuery query;
query.prepare(SqlIngredientModel::queryIngredientsForRecipe);
query.bindValue(":id", m_recipeId);
//qDebug() << "Query:" << query.lastQuery();
if (!query.exec()) {
qDebug() << "Error:" << query.lastError().text();
qFatal("Query failed");
}
setQuery(query);
emit recipeIdChanged();
}
const QString SqlIngredientModel::queryIngredientsForRecipe = QStringLiteral(
"SELECT id, unit, amount, item, inggroup"
" FROM ingredients WHERE recipe_id == :id"
" AND deleted == 0"
" ORDER BY position");