-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.h
70 lines (55 loc) · 2.26 KB
/
database.h
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
#ifndef DATABASE_H
#define DATABASE_H
#include "constants.h"
#include "nntpproxy.h" // inline log functions
QT_FORWARD_DECLARE_CLASS(User)
#include <QSqlDatabase>
#include <QSqlError>
#include <QMutex>
/*!
* \brief Interface to the Database. Thread safe.
*/
class Database
{
public:
explicit Database(); //!< default constructor
Database(const Database &) = delete;
Database(const Database &&) = delete;
Database & operator=(const Database &) = delete;
Database & operator=(const Database &&) = delete;
~Database(); //!< destructor will close the connection to the DB
/*!
* \brief Add the database to the system
* \param aDbParam
* \return if the connection is valid (not open)
*/
bool addDatabase(DatabaseParameters * const aDbParam);
bool connect(); //!< Try to connect to the Database
/*!
* \brief Check user authentication in the auth table
* \param aUser: to get the login and fill the rest of the structure
* \param aPass: encrypted pass
* \return result from the Database
*/
bool checkAuthentication(User *const aUser, const QString aPass);
uint addUserSize(User *aUser); //!< call stored proc add_user_size_QT
private:
inline void _log(const QString & aMessage) const; //!< log function for QString
inline void _log(const char * aMessage) const; //!< log function for char *
inline void _log_error(const char * aMessage, const QSqlError & aErr) const;
bool prepareSqlRequest(QSqlQuery &aQuery, const char * aSqlReq);
private:
QSqlDatabase iDb; //!< actual Database connection
QMutex mMutex; //!< Mutex to be thread safe
const QString iLogPrefix; //!< log prefix
};
void Database::_log(const char* aMessage) const {NntpProxy::log(iLogPrefix, aMessage);}
void Database::_log(const QString & aMessage) const {NntpProxy::log(iLogPrefix, aMessage);}
void Database::_log_error(const char * aMessage, const QSqlError & aErr) const{
QTextStream &ostream = NntpProxy::acquireLog(iLogPrefix);
ostream << "Error #" << aErr.number()
<< ", " << aMessage
<< ", error: " << aErr.text();
NntpProxy::releaseLog();
}
#endif // DATABASE_H