-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmodel.cpp
136 lines (112 loc) · 2.2 KB
/
model.cpp
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
#include "model.h"
#include <QDebug>
#include "db.h"
Model::Model() :
exists(false),
data(),
original()
{
}
Model::Model(const QMap<QString, QVariant> &map) :
exists(false),
data(map),
original()
{
}
Model::~Model()
{
}
QString Model::get(const QString &key) const
{
return data.value(key).toString();
}
int Model::getInt(const QString &key) const
{
bool ok = true;
int intValue = data.value(key).toInt(&ok);
if (!ok)
qDebug("Not a int value: trying to convert %s = `%s' to integer.",
key.toLatin1().data(), data.value(key).toString().toLatin1().data());
return intValue;
}
void Model::set(const QString &key, const QVariant &value)
{
data[key] = value;
}
int Model::id() const
{
return getInt("id");
}
QStringList Model::keys() const
{
return data.keys();
}
QStringList Model::dirtyKeys() const
{
QStringList dirty;
for (QString key : data.keys()) {
if (original.count(key) == 0 || data.value(key) != original.value(key))
dirty.append(key);
}
return dirty;
}
QString Model::created_at() const
{
return "created_at";
}
QString Model::updated_at() const
{
return "updated_at";
}
bool Model::useTimestamps() const
{
return true;
}
void Model::touch()
{
if (useTimestamps())
set(updated_at(), DB::sqlTime());
}
Model::operator bool() const
{
return data.size() || original.size();
}
Model::AttributeRef Model::operator [](const char *key)
{
return AttributeRef(*this, key);
}
Model::AttributeRef Model::operator [](const QString &key)
{
return AttributeRef(*this, key);
}
void Model::saved()
{
exists = true;
original = data;
}
Model::AttributeRef::AttributeRef(Model &model, const QString &key) :
model(model),
key(key)
{
}
QVariant &Model::AttributeRef::operator =(const QVariant &value) const
{
model.set(key, value);
return model.data[key];
}
QString Model::AttributeRef::toString() const
{
return model.get(key);
}
Model::AttributeRef::operator QString() const
{
return model.get(key);
}
Model::AttributeRef::operator int() const
{
return model.getInt(key);
}
Model::AttributeRef::operator QVariant() const
{
return model.data[key];
}