-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdatabasemanager.cpp
234 lines (201 loc) · 8.38 KB
/
cdatabasemanager.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
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
/****************************************************************************************
*
* This file is part of the MEGA-Series Project.
* Copyright (C) 2014 Joerg Pauly
* Created 01.04.2014 by Joerg Pauly
* All Rights reserved
*
* Alle Programme der MEGA-Serie sind Freie Software: Sie können sie unter den Bedingungen
* der GNU Lesser General Public License, wie von der Free Software Foundation,
* Version 3 der Lizenz oder (nach Ihrer Option) jeder späteren
* veröffentlichten Version, weiterverbreiten und/oder modifizieren.
*
* Dieses Programm wird in der Hoffnung, dass es nützlich sein wird, aber
* OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
* Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
* Siehe die GNU Lesser General Public License für weitere Details.
*
* Sie sollten eine Kopie der GNU Lesser General Public License zusammen mit diesem
* Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
*
*****************************************************************************************/
#include "cdatabasemanager.h"
#include "cmainwindow.h"
#include <QApplication>
CDatabaseManager::CDatabaseManager(QObject *parent) :
QObject(parent)
{
// Save pointer of CMainWindow (as QObject)
m_parent = parent;
/* Define & open db
* if the file doesn't exist, it will be created and
* populated with the table definitions
*/
m_db = QSqlDatabase::addDatabase("QSQLITE");
QString dbname = ((CMainWindow*)parent)->path()->absolutePath();
dbname.append("/mr.sqlite");
m_db.setDatabaseName(dbname);
// Let's check if the file exists
QFile dbfile(dbname);
bool dbmiss = false;
if (!dbfile.open(QFile::ReadOnly))
{
// file isn't there; set a flag to create it
dbmiss = true;
}
if (!m_db.open())
{
QMessageBox* msg = new QMessageBox();
msg->setText("Fehler beim Öffnen der Datenbank!");
msg->setInformativeText("Datenbank lässt sich nicht öffnen!");
msg->setIcon(QMessageBox::Critical);
QSqlError lError;
lError = m_db.lastError();
msg->setDetailedText(lError.text());
msg->exec();
}
if (dbmiss)
{
// opening the db created a plain file; now let's create the tables
QString sqlname = QApplication::applicationDirPath();
sqlname.append("/mr.sql");
QFile sqlfile(sqlname);
sqlfile.open(QFile::ReadOnly);
while (!sqlfile.atEnd())
{
QString sql = sqlfile.readLine();
QSqlQuery qry(sql);
qry.exec();
}
sqlfile.close();
}
}
QSqlDatabase CDatabaseManager::db() const
{
return m_db;
}
void CDatabaseManager::setDb(const QSqlDatabase &db)
{
m_db = db;
}
QList<CPersonal> *CDatabaseManager::personalList(QDate fromDate, QDate toDate)
{
QSqlQuery* lqry = new QSqlQuery();
lqry->prepare("SELECT * FROM tblPersonal WHERE Eintritt < :TO AND Austritt > :FROM ORDER BY Name;");
lqry->bindValue(":TO", toDate.toString("yyyy-MM-dd"));
lqry->bindValue(":FROM",fromDate.toString("yyyy-MM-dd"));
lqry->exec();
QString err = lqry->lastError().text();
lqry->first();
QList<CPersonal>* llist = new QList<CPersonal>();
while (lqry->isValid())
{
CPersonal item;
item.setId(lqry->value(lqry->record().indexOf("ID")).toInt());
item.setName(lqry->value(lqry->record().indexOf("Name")).toString());
item.setVName(lqry->value(lqry->record().indexOf("VName")).toString());
item.setPersNo(lqry->value(lqry->record().indexOf("PNr")).toString());
item.setSollTag(QTime::fromString(lqry->value(lqry->record().indexOf("SollTag")).toString(),"hh:mm:ss.zzz"));
llist->append(item);
lqry->next();
}
delete lqry;
return llist;
}
QList<CDutyType> *CDatabaseManager::dutyTypeList()
{
QList<CDutyType> *llist = new QList<CDutyType>();
QSqlQuery* lqry = new QSqlQuery();
lqry->exec("SELECT * FROM tblDutyTypes ORDER BY Mark;");
lqry->first();
while(lqry->isValid())
{
CDutyType item;
item.setId(lqry->value(lqry->record().indexOf("ID")).toInt());
item.setMark(lqry->value(lqry->record().indexOf("Mark")).toString());
item.setDesc(lqry->value(lqry->record().indexOf("Decr")).toString());
item.setTimeFrom(QTime::fromString(lqry->value(lqry->record().indexOf("TimeFrom")).toString(),"hh:mm:ss.zzz"));
item.setTimeFrom2(QTime::fromString(lqry->value(lqry->record().indexOf("TimeFrom2")).toString(),"hh:mm:ss.zzz"));
item.setTimeTo(QTime::fromString(lqry->value(lqry->record().indexOf("TimeTo")).toString(),"hh:mm:ss.zzz"));
item.setTimeTo2(QTime::fromString(lqry->value(lqry->record().indexOf("TimeTo2")).toString(),"hh:mm:ss.zzz"));
item.setTimeElapsed(QTime::fromString(lqry->value(lqry->record().indexOf("TimeElapsed")).toString(),"hh:mm:ss.zzz"));
item.setTimeElapsed2(QTime::fromString(lqry->value(lqry->record().indexOf("TimeElapsed2")).toString(),"hh:mm:ss.zzz"));
item.setMinOffBefore(QTime::fromString(lqry->value(lqry->record().indexOf("MinOffBefore")).toString(),"hh:mm:ss.zzz"));
item.setMinOffAfter(QTime::fromString(lqry->value(lqry->record().indexOf("MinOffAfter")).toString(),"hh:mm:ss.zzz"));
item.setRosterColorR(lqry->value(lqry->record().indexOf("ColorR")).toInt());
item.setRosterColorG(lqry->value(lqry->record().indexOf("ColorG")).toInt());
item.setRosterColorB(lqry->value(lqry->record().indexOf("ColorB")).toInt());
llist->append(item);
lqry->next();
}
return llist;
}
QList<CDuty> *CDatabaseManager::dutyList(QDate fromDate, QDate toDate, int PerID)
{
QList<CDuty> *llist = new QList<CDuty>();
QSqlQuery* lqry = new QSqlQuery();
if(PerID > 0)
{
lqry->prepare("SELECT * FROM tblDuty WHERE DDate >= :FROM AND DDate <= :TO AND PersID = :PID ORDER BY DDate;");
lqry->bindValue(":PID", PerID);
}
else
{
lqry->prepare("SELECT * FROM tblDuty WHERE DDate >= :FROM AND DDate <= :TO ORDER BY DDate;");
}
lqry->bindValue(":FROM", fromDate.toString("yyyy-MM-dd"));
lqry->bindValue(":TO",toDate.toString("yyyy-MM-dd"));
lqry->exec();
lqry->first();
while(lqry->isValid())
{
CDuty item;
item.setId(lqry->value(lqry->record().indexOf("ID")).toInt());
item.setDate(QDate::fromString(lqry->value(lqry->record().indexOf("DDate")).toString(),"yyyy-MM-dd"));
item.setKollege(CPersonal(lqry->value(lqry->record().indexOf("PersID")).toInt()));
item.setTimeFrom(QTime::fromString(lqry->value(lqry->record().indexOf("TimeFrom")).toString(),"hh:mm:ss.zzz"));
item.setTimeTo(QTime::fromString(lqry->value(lqry->record().indexOf("TimeTo")).toString(),"hh:mm:ss.zzz"));
item.setTimeFrom2(QTime::fromString(lqry->value(lqry->record().indexOf("TimeFrom2")).toString(),"hh:mm:ss.zzz"));
item.setTimeTo2(QTime::fromString(lqry->value(lqry->record().indexOf("TimeTo2")).toString(),"hh:mm:ss.zzz"));
item.setTyp(CDutyType(lqry->value(lqry->record().indexOf("TypID")).toInt()));
llist->append(item);
lqry->next();
}
delete lqry;
return llist;
}
QList<CDtyBaseType> *CDatabaseManager::dbaseList()
{
QList<CDtyBaseType> *llist = new QList<CDtyBaseType>();
QSqlQuery *lqry = new QSqlQuery();
lqry->prepare("SELECT * from tblDtyBase ORDER BY Codeletter;");
lqry->exec();
lqry->first();
while(lqry->isValid())
{
CDtyBaseType* ltype = new CDtyBaseType(lqry->value(lqry->record().indexOf("ID")).toInt());
llist->append(*ltype);
lqry->next();
}
delete lqry;
return llist;
}
CLogManager::CLogManager(QObject *parent)
{
}
CLogManager::CLogManager(CLogManager::sctLogEntry pEntry, QObject *parent)
{
}
void CLogManager::writeEntry(CLogManager::sctLogEntry pEntry)
{
QSqlQuery lqry;
lqry.prepare("INSERT INTO tblLogbook (PID, DtID, OldDTID, NewDTID, DDateTime) VALUES (:PID, :DTID, :ODT, :NDT, :DAT);");
lqry.bindValue(":PID", pEntry.user.id());
lqry.bindValue(":DTID", pEntry.affDuty.id());
lqry.bindValue(":ODT", pEntry.oldDuty.id());
lqry.bindValue(":NDT", pEntry.newDuty.id());
lqry.bindValue(":DAT", QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));
lqry.exec();
QString err = lqry.lastError().text();
lqry.first();
}