-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedbconnection.cpp
More file actions
189 lines (165 loc) · 5.84 KB
/
Copy pathedbconnection.cpp
File metadata and controls
189 lines (165 loc) · 5.84 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
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
#include "edbconnection.h"
#include <QSettings>
#include <QSqlQuery>
#include <QSqlDriver>
#include <QSqlRecord>
#include <QSqlError>
#include <QRegExp>
#include <QMessageBox>
EDBconnection* EDBconnection::pinstance = 0; // initialize pointer
EDBconnection* EDBconnection::getInstance()
{
if (!pinstance) {
try {
pinstance = new EDBconnection;
}
catch (QString error) {
QMessageBox::warning(0, "ExLibris", error, QMessageBox::Ok);
delete pinstance;
throw (int) 3;
}
catch (...) {
qWarning("DB connection error");
delete pinstance;
throw (int) 2;
}
}
Q_ASSERT(pinstance != NULL);
return pinstance;
}
EDBconnection::EDBconnection()
{
// Read DB settings and user's login and password from config.ini
// !!! config file must be in the same directory as the executable program
QSettings settings("config.ini", QSettings::IniFormat);
QString strKey("DB.config/");
dbHost = settings.value(strKey + "host", "localhost").toString();
dbName = settings.value(strKey + "database", "exlibris").toString();
dbUser = settings.value(strKey + "login").toString();
dbPass = settings.value(strKey + "password").toString();
db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName(dbHost);
db.setDatabaseName(dbName);
db.setUserName(dbUser);
db.setPassword(dbPass);
if (!db.open()) {
qDebug("Error occured in connection to database");
throw db.lastError().text();
}
qDebug("connected to database \n");
// QSqlDriver *driver = QSqlDatabase::database().driver();
// if (driver->hasFeature(QSqlDriver::Transactions)) qDebug("transaction is ok."); else qDebug("transaction is bad.");
query("SET NAMES 'utf8'");
query("SET character_set_results = 'utf8'");
// query("SET AUTOCOMMIT = 0");
}
EDBconnection::~EDBconnection()
{
db.close();
qDebug("\nclosed connection to database");
}
void EDBconnection::destroyInstance()
{
if (pinstance) delete pinstance;
}
QString EDBconnection::escape(const QString q)
{
QString res = q.trimmed();
return res.replace("\\", "\\\\")
.replace("\"", "\\\"").replace("'", "\\'")
.replace("\n", "\\n").replace("\t", "\\t")
.replace("\r", "\\r").replace("\b", "\\b")
/*.replace("\x00", "\\0").replace("\x32", "\\Z")*/
/*.replace("_", "\\_").replace("%", "\\%")*/;
}
bool EDBconnection::query(const QString query) const
{
qDebug()<<"[query]\t:"<<query;
QSqlQuery sqlQuery(query, db);
// setLastError(sqlQuery.lastError().text());
if (!sqlQuery.isActive()) {
QString error = sqlQuery.lastError().text();
qDebug()<<"ERROR\t:"<< error;
emit EDBconnection::getInstance()->returnLastError(error);
return false;
}
return true;
}
QList<QStringList> EDBconnection::get(const QString query) const
{
qDebug()<<"[get]\t:"<<query;
QList<QStringList> List;
QSqlQuery sqlQuery(query,db);
QSqlRecord rec = sqlQuery.record();
QStringList row;
while( sqlQuery.next() ){
row.clear();
for( int i=0; i < rec.count(); ++i ) {
row << sqlQuery.value(i).toString();
}
List << row;
}
// lastError = sqlQuery.lastError().text();
if (!sqlQuery.isActive()) {
QString error = sqlQuery.lastError().text();
qDebug()<<"ERROR\t:"<< error;
emit EDBconnection::getInstance()->returnLastError(error);
} else {
qDebug()<<"return\t:"<<List;
// emit returnSelQuery(List);
}
return List;
}
int EDBconnection::insert(const QString query) const
{
qDebug()<<"[insert]\t:"<<query;
QSqlQuery sqlQuery(query, db);
// lastError = sqlQuery.lastError().text();
if (!sqlQuery.isActive()) {
QString error = sqlQuery.lastError().text();
qDebug()<<"ERROR\t:"<< error;
emit EDBconnection::getInstance()->returnLastError(error);
return -1;
}
return QVariant(sqlQuery.lastInsertId()).toInt();
}
void EDBconnection::checkUser(QString login, QString pwd)
{
login.remove(QRegExp("['\"]"));
QString query;
// find user
if (pwd != "ANY") {
pwd.remove(QRegExp("['\"]"));
qDebug("> login: %s\n> password: %s",qPrintable(login), qPrintable(pwd));
// need smth like this: "SELECT id FROM users WHERE MD5(CONCAT(login, password)) = '"+ md5(login.pwd) +"'"
query = "SELECT id FROM users WHERE login='"+login+"' AND password=MD5('"+pwd+"')";
} else {
qDebug("> login: %s",qPrintable(login));
query = "SELECT id FROM users WHERE login='"+login+"'";
}
QSqlQuery sqlQuery(query, db);
// if error
if (!sqlQuery.isActive())
QMessageBox::warning(0, "Database Error", sqlQuery.lastError().text());
if (sqlQuery.next()) {
// user is found. return user_id.
long user_id = sqlQuery.value(0).toInt();
emit setUserId(user_id);
} else {
// can not found user with this pair of login and password.
emit setUserId(-1);
}
}
// newUser(login, pwd, lastname, name, address, phone, email);
int EDBconnection::newUser(QString login, QString password, QString lastname, QString name,
QString address , QString phone, QString email)
{
QDateTime regdate = QDateTime::currentDateTime();
qDebug()<<regdate;
QString sqlQuery = QString("INSERT INTO users " \
"(login, password, lastname, name, address, phone, email, reg_date) " \
"VALUES ('%1', MD5('%2'), '%3', '%4', '%5', '%6', '%7', '%8')")
.arg(login).arg(password).arg(lastname).arg(name).arg(address)
.arg(phone).arg(email).arg(regdate.toString("yyyy-MM-dd HH-mm-ss"));
return insert(sqlQuery);
}