forked from condo4/carbudget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
carevent.cpp
93 lines (80 loc) · 1.96 KB
/
carevent.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
#include "carevent.h"
#include <car.h>
CarEvent::CarEvent(Car *parent):
QObject(parent),
_car(parent),
_date(QDate::currentDate()),
_distance(0),
_eventid(0)
{
}
CarEvent::CarEvent(QDate date, unsigned int distance, unsigned int eventid, Car *parent):
QObject(parent),
_car(parent),
_date(date),
_distance(distance),
_eventid(eventid)
{
}
unsigned int CarEvent::saveevent()
{
QSqlQuery query(_car->db);
if(_eventid == 0)
{
QString sql = QString("INSERT INTO Event (id,date,distance) VALUES(NULL,'%1',%2)").arg(_date.toString("yyyy-MM-dd 00:00:00.00")).arg(_distance);
if(query.exec(sql))
{
_eventid = query.lastInsertId().toInt();
qDebug() << "Create Event in database with id " << _eventid;
}
else _eventid = 0;
}
else
{
QString sql = QString("UPDATE Event SET date='%1', distance=%2 WHERE id=%3;").arg(_date.toString("yyyy-MM-dd 00:00:00.00")).arg(_distance).arg(_eventid);
if(query.exec(sql))
{
qDebug() << "Update Event in database with id " << _eventid;
}
else
{
qDebug() << "Error during Update Event in database";
qDebug() << query.lastError();
return 0;
}
}
return _eventid;
}
bool CarEvent::delevent()
{
QSqlQuery query(_car->db);
QString sql = QString("DELETE FROM Event WHERE id=%1;").arg(_eventid);
if(query.exec(sql))
{
qDebug() << "DELETE Event in database with id " << _eventid;
return true;
}
return false;
}
int CarEvent::id() const
{
return _eventid;
}
QDateTime CarEvent::date() const
{
return QDateTime(_date);
}
void CarEvent::setDate(QDateTime date)
{
_date = date.date();
emit dateChanged();
}
unsigned int CarEvent::distance() const
{
return _distance;
}
void CarEvent::setDistance(unsigned int distance)
{
this->_distance = distance;
emit distanceChanged();
}