-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflightsdatabasecontroller.cpp
More file actions
147 lines (129 loc) · 3.51 KB
/
flightsdatabasecontroller.cpp
File metadata and controls
147 lines (129 loc) · 3.51 KB
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
#include "flightsdatabasecontroller.h"
FlightsDatabaseController::FlightsDatabaseController(const QString &path)
{
m_flightsDatabase = QSqlDatabase::addDatabase("QSQLITE");
m_flightsDatabase.setDatabaseName(path);
if(!m_flightsDatabase.open())
{
qDebug() << "Error: connection with database failed";
}
else
{
qDebug() << "Database: connection ok ";
}
}
FlightsDatabaseController::~FlightsDatabaseController()
{
if(m_flightsDatabase.isOpen())
{
m_flightsDatabase.close();
}
}
bool FlightsDatabaseController::IsOpen() const
{
return m_flightsDatabase.isOpen();
}
bool FlightsDatabaseController::CreateTable()
{
bool success = false;
QSqlQuery query;
query.prepare("CREATE TABLE flights_db(id INTEGER PRIMARY KEY, plane_type TEXT, code TEXT, departure TEXT, arrival TEXT);");
if (!query.exec())
{
qDebug() << "Couldn't create the table 'plane_type': one might already exist.";
success = false;
}
return success;
}
bool FlightsDatabaseController::AddFlightToTable(const QString planeType, const QString code, const QString departure, const QString arrival)
{
bool success = false;
if(!planeType.isEmpty())
{
QSqlQuery queryAdd;
queryAdd.prepare("INSERT INTO flights_db (plane_type, code, departure, arrival) VALUES (:plane_type, :code, :departure, :arrival )");
queryAdd.bindValue(":plane_type", planeType);
queryAdd.bindValue(":code", code);
queryAdd.bindValue(":departure", departure);
queryAdd.bindValue(":arrival", arrival);
if(queryAdd.exec())
{
success = true;
qDebug() << "flight added to database";
}
else
{
qDebug() << "add flights failed" << queryAdd.lastError();
}
}
else
{
qDebug() << "add flights failed: name cannot be empty";
}
return success;
}
bool FlightsDatabaseController::FlightExists(const QString code) const
{
bool exists = false;
QSqlQuery queryCheck;
queryCheck.prepare("SELECT code FROM flights_db WHERE code = (:code)");
queryCheck.bindValue(":code", code);
if(queryCheck.exec())
{
if(queryCheck.next())
{
exists = true;
qDebug() << "flight " << code << " already exist in flights_db";
}
}
else
{
qDebug() << "flight exist failed: " << queryCheck.lastError();
}
return exists;
}
QString FlightsDatabaseController::GetPlaneTypeByCode(const QString code)
{
QSqlQuery queryGet;
queryGet.prepare("SELECT plane_type FROM flights_db WHERE code = (:code)");
queryGet.bindValue(":code",code);
if(queryGet.exec())
{
while (queryGet.next())
{
return queryGet.value(0).toString();
}
}
else
{
qDebug() << queryGet.lastError();
}
return 0;
}
QString FlightsDatabaseController::GetStringRecordByID(int id, const QString date)
{
QSqlQuery queryGet;
queryGet.prepare("SELECT " + date + " FROM flights_db WHERE id = (:id)");
queryGet.bindValue(":id",id);
if(queryGet.exec())
{
while (queryGet.next())
{
return queryGet.value(0).toString();
}
}
else
{
qDebug() << queryGet.lastError();
}
return 0;
}
int FlightsDatabaseController::CountItemsInDatabase()
{
QSqlQuery query("SELECT COUNT(*) as count FROM flights_db");
if (query.next())
{
return query.value(0).toInt();
}
return 0;
}