-
Notifications
You must be signed in to change notification settings - Fork 0
/
localdata.cpp
executable file
·154 lines (129 loc) · 3.84 KB
/
localdata.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
#include "localdata.h"
#ifdef QT_DEBUG
#include <QDebug>
#endif
LocalData* LocalData::m_Instance = 0;
LocalData::LocalData()
{
}
LocalData::~LocalData()
{
}
LocalData* LocalData::Instance()
{
static QMutex mutex;
if (!m_Instance)
{
mutex.lock();
if (!m_Instance)
{
m_Instance = new LocalData;
m_Instance->initialized = m_Instance->initialize();
}
mutex.unlock();
}
return m_Instance;
}
bool LocalData::initialize()
{
db = QSqlDatabase::addDatabase("QSQLITE");
#ifdef Q_OS_WIN32
db.setDatabaseName(QCoreApplication::applicationDirPath() + "/quickmon.sqlite");
#else
db.setDatabaseName("~/.quickmon.sqlite");
#endif
#ifdef QT_DEBUG
if (db.open())
qDebug() << "LocalData : initialize :" << db.databaseName() << ": Success";
else {
qDebug() << "LocalData : initialize :" << db.databaseName() << ": Error : " << getLastError();
return false;
}
#else
if (!db.open())
return false;
#endif
//check existence of database through its hosts table
QSqlQuery query = createQuery();
executeQuery("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='hosts';", query);
if (query.next())
{
if ( query.value(0).toInt() <= 0 )
{
//create host table
executeQuery("CREATE TABLE hosts ("
"id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"
"name TEXT NOT NULL,"
"address TEXT,"
"snmpversion INTEGER,"
"snmpcommunityuser TEXT,"
"snmpv3seclevel TEXT,"
"snmpv3authprotocol TEXT,"
"snmpv3authpassphrase TEXT,"
"snmpv3privprotocol TEXT,"
"snmpv3privpassphrase TEXT"
")", query);
//add first host
executeQuery("INSERT INTO hosts ("
"name,"
"address,"
"snmpversion,"
"snmpcommunityuser"
") VALUES ("
"'localhost',"
"'127.0.0.1',"
"2,"
"'public'"
")", query);
}
return true;
}
else
return false;
}
bool LocalData::isInitialized()
{
return initialized;
}
QSqlQuery LocalData::createQuery()
{
return QSqlQuery(db);
}
QSqlQuery LocalData::createQuery(const QString & query)
{
QSqlQuery returnQuery(db);
returnQuery.prepare(query);
return returnQuery;
}
bool LocalData::executeQuery(const QString & query)
{
return executeQuery(query, createQuery());
}
bool LocalData::executeQuery(const QString & query, QSqlQuery & queryObj)
{
queryObj.prepare(query);
return executeQuery(queryObj);
}
bool LocalData::executeQuery(QSqlQuery & queryObj)
{
#ifdef QT_DEBUG
if (queryObj.exec())
{
if (queryObj.isSelect())
qDebug() << "LocalData : executeQuery :" << queryObj.executedQuery() << ": Success :" << queryObj.size() << "rows returned";
else
qDebug() << "LocalData : executeQuery :" << queryObj.executedQuery() << ": Success :" << queryObj.numRowsAffected() << "rows affected";
return true;
} else
{
qDebug() << "LocalData : executeQuery :" << queryObj.executedQuery() << ": Error : " << getLastError();
return false;
}
#else
return queryObj.exec();
#endif
}
QString LocalData::getLastError()
{
return db.lastError().text();
}