-
Notifications
You must be signed in to change notification settings - Fork 0
/
sitesdatabaseinterface.cpp
executable file
·177 lines (146 loc) · 4.26 KB
/
sitesdatabaseinterface.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
#include "sitesdatabaseinterface.h"
#include <QDebug>
#include <QtSql/QtSql>
SitesDatabaseInterface::SitesDatabaseInterface()
{
}
void SitesDatabaseInterface::setStoragePaths(QString appStoragePath, QString dbPath)
{
this->appStoragePath = appStoragePath;
this->dbPath = dbPath;
}
void SitesDatabaseInterface::saveSite(Site* site)
{
qDebug() << "db: " << this->dbPath;
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(this->dbPath);
if(false == db.open())
{
qDebug() << "can not open database";
}
//convert QStringList to CSV
QString yearsCsv = site->getYears().join(";");
if(0 != site->getId()) //if an id is set we update the site in the database
{
QSqlQuery query;
query.prepare("UPDATE sites SET nom = :nom, years = :years WHERE ID = :id;");
query.bindValue(":id", site->getId());
query.bindValue(":nom", site->getNom());
query.bindValue(":years", yearsCsv);
if(false == query.exec())
{
qDebug() << "SQL ERROR 12: " << query.lastError();
}
}
else
{
QSqlQuery query;
query.prepare("INSERT INTO sites(ID,nom,years) VALUES (NULL, :nom, :years);");
query.bindValue(":nom", site->getNom());
query.bindValue(":years", yearsCsv);
if(false == query.exec())
{
qDebug() << "SQL ERROR : " << query.lastError();
}
}
db.commit();
db.close();
}
QList<Site*> SitesDatabaseInterface::getAllSites()
{
qDebug() << "db: " << this->dbPath;
QList<Site*> siteList;
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(this->dbPath);
if(false == db.open())
{
qDebug() << "can not open database";
}
QSqlQuery query= db.exec("SELECT * FROM sites");
while (query.next())
{
int id = query.value(0).toInt();
QString nom = query.value(1).toString();
QString years = query.value(2).toString();
QStringList yearsList = years.split(";");
Site* site = new Site();
site->setId(id);
site->setNom(nom);
site->setYears(yearsList);
siteList.append(site);
}
db.close();
return siteList;
}
Site* SitesDatabaseInterface::getSite(int id)
{
Site* site = new Site();
qDebug() << "db: " << this->dbPath;
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(this->dbPath);
if(false == db.open())
{
qDebug() << "can not open database";
}
QSqlQuery query;
query.prepare("SELECT * FROM sites WHERE ID = :siteId ");
query.bindValue(":siteId", id);
if(query.exec())
{
query.next();
int id = query.value(0).toInt();
QString nom = query.value(1).toString();
QString years = query.value(2).toString();
QStringList yearsList = years.split(";");
site->setId( id );
site->setNom( nom );
site->setYears( yearsList );
}
else
{
qDebug() << "SQL ERROR 0: " << query.lastError();
}
db.close();
return site;
}
void SitesDatabaseInterface::deleteSite(int id)
{
qDebug() << "db: " << this->dbPath;
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(this->dbPath);
if(false == db.open())
{
qDebug() << "can not open database";
}
QSqlQuery query;
query.prepare("DELETE FROM sites WHERE ID = :id;");
query.bindValue(":id", id);
if(false == query.exec())
{
qDebug() << "SQL ERROR : " << query.lastError();
}
db.commit();
//We have also to delete the associated weather data to this site
query.prepare("DELETE FROM meteo WHERE id_site = :id;");
query.bindValue(":id", id);
if(false == query.exec())
{
qDebug() << "SQL ERROR : " << query.lastError();
}
db.commit();
db.close();
}
int SitesDatabaseInterface::lastInsertedRowId()
{
qDebug() << "db: " << this->dbPath;
QList<Site*> siteList;
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(this->dbPath);
if(false == db.open())
{
qDebug() << "can not open database";
}
QSqlQuery query= db.exec("SELECT MAX(ID) FROM sites");
query.next();
return query.value(0).toInt();
}