forked from condo4/carbudget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fueltype.cpp
97 lines (87 loc) · 2.32 KB
/
fueltype.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
/**
* CarBudget, Sailfish application to manage car cost
*
* Copyright (C) 2014 Fabien Proriol
*
* This file is part of CarBudget.
*
* CarBudget is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* CarBudget is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the GNU General Public License for more details. You should have received a copy of the GNU
* General Public License along with CarBudget. If not, see <http://www.gnu.org/licenses/>.
*
* Authors: Fabien Proriol
*/
#include "fueltype.h"
#include "car.h"
Fueltype::Fueltype(QObject *parent) :
QObject(parent),
_id(0),
_name("Not Set")
{
}
Fueltype::Fueltype(unsigned int id, QString name, Car *parent):
QObject(parent),
_car(parent),
_id(id),
_name(name)
{
}
unsigned int Fueltype::id() const
{
return _id;
}
void Fueltype::setId(unsigned int id)
{
_id = id;
emit idChanged();
}
QString Fueltype::name() const
{
return _name;
}
void Fueltype::setName(QString name)
{
_name = name;
emit nameChanged();
}
void Fueltype::save()
{
if(_id < 0)
{
QSqlQuery query(_car->db);
QString sql = QString("INSERT INTO FueltypeList (id,name) VALUES(NULL,'%1')").arg(_name);
if(query.exec(sql))
{
_id = query.lastInsertId().toInt();
qDebug() << "Create Fueltype in database with id " << _id << "and value " << _name;
_car->db.commit();
}
else
{
qDebug() << "Error during Create Fueltype in database";
qDebug() << query.lastError();
}
}
else
{
QSqlQuery query(_car->db);
QString sql = QString("UPDATE FueltypeList SET name='%1' WHERE id=%2;").arg(_name).arg(_id);
qDebug() << sql;
if(query.exec(sql))
{
qDebug() << "Update Fueltype in database with id " << _id;
_car->db.commit();
}
else
{
qDebug() << "Error during Update Fueltype in database";
qDebug() << query.lastError();
}
}
}