-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.cpp
247 lines (226 loc) · 7.21 KB
/
Database.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
235
236
237
238
239
240
241
242
243
244
245
246
247
#include "Database.hpp"
#include <iostream>
#include <sstream>
#include <tuple>
#include <set>
std::string AddSlashes( const std::string& str)
{
std::map<char, std::string> reps;
reps['\0'] = "\0";
reps['\''] = "\'";
reps['\"'] = "\'";
reps['\\'] = "\\";
decltype(reps.begin()) pos;
std::stringstream ss;
for(auto& a : str)
{
pos = reps.find(a);
if (pos!=reps.end())
ss << pos->second;
else
ss << a;
}
return ss.str();
}
std::string StripSlashes( const std::string& str)
{
return str;
/*
std::set<char> reps;
reps.insert('\0');
reps.insert('\\');
std::stringstream ss;
for(auto ait=str.begin(); ait!=str.end(); ++ait)
{
if(*ait=='\\' && ait+1 !=str.end() && reps.find(*(ait+1)) != reps.end())
continue;
ss << *ait;
}
return ss.str();
*/
}
Database::Database(QWidget *parent):
QWidget(parent)
{
m_db = QSqlDatabase::addDatabase("QSQLITE");
}
std::deque<std::pair<int, std::string>> Database::GetNames() const
{
std::deque<std::pair<int, std::string>> ret;
QSqlQuery query("SELECT * from Person");
if (query.lastError().isValid())
{
QMessageBox::critical(0,"Error",query.lastError().text() );
}
while (query.next())
{
ret.push_back(std::make_pair(query.value(0).toInt(), StripSlashes(query.value(1).toString().toStdString())));
}
return ret;
}
void Database::OpenFile(const std::string& filename)
{
m_db.setDatabaseName(filename.c_str());
if(!m_db.open())
{
std::cerr << "error: Could not find database" << std::endl;
QMessageBox::information(0,"Ok","Database not found");
}
QSqlQuery query("SELECT id from FileInfo");
query.next();
if(query.value(0).toInt() != 1)
{
QMessageBox::information(0,"Ok","Incorrect database type");
}
}
void Database::NewDatabase(const std::string& filename)
{
m_db.setDatabaseName(filename.c_str());
if(!m_db.open())
{
std::cerr << "error: Unknown error" << std::endl;
QMessageBox::information(0,"Ok","Unknown error");
}
QSqlQuery query1("CREATE TABLE FileInfo ( id int )");
QSqlQuery query2("insert into FileInfo (id) values (1)");
QSqlQuery query3("CREATE TABLE Log(ID INT PRIMARY KEY NOT NULL, \
Log TEXT, \
Date DATE, \
PersonID INT, Title TEXT);");
QSqlQuery query4("CREATE TABLE Person( \
ID INT PRIMARY KEY NOT NULL, \
Name TEXT \
);");
}
int Database::AddName(const std::string& name)
{
QSqlQuery query("select MAX(id) from Person");
if (query.lastError().isValid())
{
QMessageBox::critical(0,"Error",query.lastError().text() );
}
query.next();
int id = query.value(0).toInt()+1;
std::stringstream ss;
ss << "insert into Person (id, name) values ("<<id << ",\"" << AddSlashes(name) << "\")";
QSqlQuery query2(tr(ss.str().c_str()));
if (query2.lastError().isValid())
{
QMessageBox::critical(0,"Error",query2.lastError().text() );
}
return id;
}
std::deque< std::tuple<int, std::string, std::string> > Database::GetLogsFor(const int id)
{
std::stringstream ss;
ss << "select id, Title, Date from Log where PersonId="<< id << " ORDER BY Date DESC";
QSqlQuery query(tr(ss.str().c_str()));
if (query.lastError().isValid())
{
QMessageBox::critical(0,"Error",query.lastError().text() );
}
std::deque< std::tuple<int, std::string, std::string> > ret;
while (query.next())
{
ret.push_back(std::make_tuple(query.value(0).toInt(),
StripSlashes(query.value(1).toString().toStdString()),
query.value(2).toString().toStdString()));
}
return ret;
}
std::tuple<int, std::string, std::string, std::string> Database::GetLog(const int id)
{
std::stringstream ss;
ss << "select id, Title, Date, Log from Log where id="<< id;
QSqlQuery query(tr(ss.str().c_str()));
if (query.lastError().isValid())
{
QMessageBox::critical(0,"Error",query.lastError().text() );
}
query.next();
return std::make_tuple(query.value(0).toInt(),
StripSlashes(query.value(1).toString().toStdString()),
query.value(2).toString().toStdString(),
StripSlashes(query.value(3).toString().toStdString()));
}
void Database::AddLog(const std::string& logtitle,
const std::string& date,
const std::string& logcontent,
const int logid)
{
QSqlQuery query("select MAX(id) from Log");
query.next();
if (query.lastError().isValid())
{
QMessageBox::critical(0,"Error",query.lastError().text() );
}
int id = query.value(0).toInt()+1;
std::stringstream ss;
ss << "insert into Log (ID, Log, Date, PersonID, Title) values ("
<< id << "," << "\"" << AddSlashes(logcontent) << "\",\"" << date << "\"," <<
logid << ",\"" << AddSlashes(logtitle) << "\")";
QSqlQuery query2(ss.str().c_str());
if (query2.lastError().isValid())
{
QMessageBox::critical(0,"Error",query2.lastError().text() );
}
}
void Database::EditLog( const std::string& logtitle,
const std::string& date,
const std::string& logcontent,
const int personid,
const int logid)
{
std::stringstream ss;
ss << "update Log set Log = \"" << AddSlashes(logcontent) << "\","
<< "Date = \"" << date << "\","
<< "PersonID = " << personid << ","
<< "Title = \"" << AddSlashes(logtitle) << "\" where ID=" << logid;
QSqlQuery query(ss.str().c_str());
if (query.lastError().isValid())
{
QMessageBox::critical(0,"Error",query.lastError().text() );
}
}
std::string Database::GetName(const uint32_t id)
{
std::stringstream ss;
ss << "SELECT Name from Person where id=" << id;
QSqlQuery query(ss.str().c_str());
if (query.lastError().isValid())
{
QMessageBox::critical(0,"Error",query.lastError().text() );
}
query.next();
return StripSlashes(query.value(0).toString().toStdString());
}
void Database::EditName(const int id, const std::string& newname)
{
std::stringstream ss;
ss << "update Person set Name=\"" << AddSlashes(newname) << "\" where id="<<id;
QSqlQuery query(ss.str().c_str());
if (query.lastError().isValid())
{
QMessageBox::critical(0,"Error",query.lastError().text() );
}
}
void Database::DeleteName(const int id)
{
std::stringstream ss;
ss << "delete from Person where id="<<id;
QSqlQuery query(ss.str().c_str());
if (query.lastError().isValid())
{
QMessageBox::critical(0,"Error",query.lastError().text() );
}
}
void Database::DeleteLog(const int id)
{
std::stringstream ss;
ss << "delete from Log where id="<<id;
QSqlQuery query(ss.str().c_str());
if (query.lastError().isValid())
{
QMessageBox::critical(0,"Error",query.lastError().text() );
}
}