-
Notifications
You must be signed in to change notification settings - Fork 39
/
RegExDatabase.cpp
127 lines (98 loc) · 4.07 KB
/
RegExDatabase.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
#include "MainWindow.h"
#include "RegExDatabase.h"
#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QMessageBox>
#include <QSettings>
#include <QSqlError>
#include <QSqlQuery>
#include <QStandardPaths>
constexpr auto defaultDatabaseType = "QSQLITE";
Nedrysoft::RegExDatabase::RegExDatabase()
{
QVariantMap settingsMap;
auto defaultDatabaseFile = QDir::cleanPath(QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation).at(0)+QDir::separator()+"regex101.sqlite");
settingsMap = getSettings();
auto databaseDriver = settingsMap["databaseDriver"].toString();
auto databaseName = settingsMap["databaseName"].toString();
auto databaseHost = settingsMap["databaseHost"].toString();;
auto databaseUser = settingsMap["databaseUser"].toString();
auto databasePassword = settingsMap["databasePassword"].toString();
m_database = QSqlDatabase::addDatabase(databaseDriver);
m_database.setDatabaseName(databaseName);
m_database.setHostName(databaseHost);
m_database.setUserName(databaseUser);
m_database.setPassword(databasePassword);
if (!m_database.open()) {
auto errorTitle = QCoreApplication::translate("database", "Unable to open error");
auto errorMessage = QCoreApplication::translate("database", "Unable to open database, the following error was reported.\r\n\r\n%1").arg(m_database.lastError().text());;
QMessageBox::warning(Nedrysoft::MainWindow::getInstance(), errorTitle, errorMessage);
} else {
initialiseDatabase();
}
}
Nedrysoft::RegExDatabase *Nedrysoft::RegExDatabase::getInstance()
{
static Nedrysoft::RegExDatabase instance;
return(&instance);
}
QVariantMap Nedrysoft::RegExDatabase::getSettings()
{
QSettings settings;
auto defaultDatabaseFile = QDir::cleanPath(QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation).at(0)+QDir::separator()+"regex101.sqlite");
QVariantMap settingsMap;
settingsMap["databaseDriver"] = settings.value(QStringLiteral("databaseDriver"), defaultDatabaseType);
settingsMap["databaseHost"] = settings.value(QStringLiteral("databaseHost"), QStringLiteral(""));
settingsMap["databaseName"] = settings.value(QStringLiteral("databaseName"), defaultDatabaseFile);
settingsMap["databaseUser"] = settings.value(QStringLiteral("databaseUser"), QStringLiteral(""));
settingsMap["databasePassword"]= settings.value(QStringLiteral("databasePassword"), QStringLiteral(""));
return settingsMap;
}
void Nedrysoft::RegExDatabase::storeSettings(QVariantMap settingsMap)
{
QSettings settings;
settings.setValue("databaseDriver", settingsMap["databaseDriver"]);
settings.setValue("databaseHost", settingsMap["databaseHost"]);
settings.setValue("databaseName", settingsMap["databaseName"]);
settings.setValue("databaseUser", settingsMap["databaseUser"]);
settings.setValue("databasePassword", settingsMap["databasePassword"]);
}
QSqlQuery Nedrysoft::RegExDatabase::prepareQuery(QString queryName)
{
QFile sqlFile(QString(":/regex101/sql/%1.sql").arg(queryName));
QSqlQuery query;
if (sqlFile.open(QFile::ReadOnly)) {
query.prepare(QString::fromLatin1(sqlFile.readAll()));
}
return query;
}
bool Nedrysoft::RegExDatabase::initialiseDatabase()
{
QFile sqlFile(QString(":/regex101/sql/createDatabase.sql"));
QString sqlStatement;
QStringList commandList;
QSqlQuery query;
bool returnValue = true;
char characterIn;
if (sqlFile.open(QFile::ReadOnly)) {
while(sqlFile.getChar(&characterIn)) {
if ((characterIn=='\r') || (characterIn=='\n'))
continue;
sqlStatement = sqlStatement.append(characterIn);
if (characterIn==';') {
commandList.append(sqlStatement);
sqlStatement.clear();
}
}
}
for(auto sqlStatement : commandList) {
query.prepare(sqlStatement);
if (!query.exec(sqlStatement)) {
returnValue = false;
}
}
return commandList.count()?returnValue:false;
}