-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbmanager.cpp
155 lines (127 loc) · 3.86 KB
/
dbmanager.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 "dbmanager.h"
#include <QMessageBox>
#include <QtSql/QSqlDatabase>
#include <QDebug>
#include "dbconnectiondescriptor.h"
#include <QSqlError>
bool DBManager::instanceFlag = false;
DBManager* DBManager::instance = NULL;
DBManager::DBManager(QObject *parent) :
QObject(parent)
{
DBConnectionDescriptor descriptor;
QList<DBConnectionDescriptor> storedConnections;
}
DBManager::~DBManager() {
disconnect();
instanceFlag = false;
}
DBManager* DBManager::getInstance() {
if(! instanceFlag) {
instance = new DBManager();
instanceFlag = true;
return instance;
} else {
return instance;
}
}
void DBManager::setConnectionDescriptor(DBConnectionDescriptor *descriptor) {
this->descriptor.name = descriptor->name;
this->descriptor.host = descriptor->host;
this->descriptor.user = descriptor->user;
this->descriptor.password = descriptor->password;
this->descriptor.schema = descriptor->schema;
this->descriptor.sshHost = descriptor->sshHost;
this->descriptor.sshUser = descriptor->sshUser;
this->descriptor.sshPassword = descriptor->sshPassword;
this->descriptor.sshKeyFile = descriptor->sshKeyFile;
}
DBConnectionDescriptor* DBManager::getConnectionDescriptor() {
return &descriptor;
}
void DBManager::setConnection(QString host, QString user, QString password) {
descriptor.host = host;
descriptor.user = user;
descriptor.password = password;
}
void DBManager::setConnection(QString host, QString user) {
descriptor.host = host;
descriptor.user = user;
}
void DBManager::setConnection(QString host) {
descriptor.host = host;
}
void DBManager::setSSHTunnelCredentials(QString host, QString user, QString password) {
descriptor.sshHost = host;
descriptor.sshUser = user;
descriptor.sshPassword = password;
}
void DBManager::setSSHTunnelCredentials(QString host, QString user) {
descriptor.sshHost = host;
descriptor.sshUser = user;
}
void DBManager::setSSHTunnelCredentials(QString host) {
descriptor.sshHost = host;
}
void DBManager::setSSHTunnelKeyFile(QString host, QString user, QString keyFile) {
descriptor.sshHost = host;
descriptor.sshUser = user;
descriptor.sshKeyFile = keyFile;
}
void DBManager::setSSHTunnelKeyFile(QString host, QString user) {
descriptor.sshHost = host;
descriptor.sshUser = user;
}
void DBManager::setSSHTunnelKeyFile(QString host) {
descriptor.sshHost = host;
}
void DBManager::setSchema(QString schema) {
if (db.isOpen()) {
db.close();
}
qDebug()<<"DBManager::setSChema() : schema="+schema;
db.setDatabaseName(schema);
if (!db.open()) {
qDebug()<<"...schema could not be changed";
}
descriptor.schema = schema;
}
bool DBManager::connect() {
if (QSqlDatabase::isDriverAvailable("QMYSQL")) {
db = QSqlDatabase::addDatabase("QMYSQL");
qDebug()<<"descriptor.host="<<descriptor.host;
db.setHostName(descriptor.host);
qDebug()<<"descriptor.schema.size()="<<descriptor.schema.size();
if (descriptor.schema.size()>0) {
db.setDatabaseName(descriptor.schema);
}
db.setUserName(descriptor.user);
db.setPassword(descriptor.password);
if (!db.open()) {
QMessageBox m;
QSqlError error = db.lastError();
m.setText("Unable to connect to database:\n\n"+error.databaseText());
m.exec();
} else {
qDebug()<<"Connected to database";
}
return true;
} else {
QMessageBox m;
m.setText("Database driver not available.");
m.exec();
}
return false;
}
bool DBManager::isConnected() {
return db.isOpen();
}
void DBManager::disconnect() {
qDebug()<<"DBManager::disconnect()";
if (db.isOpen()) {
db.close();
}
}
QStringList DBManager::getTables() {
return db.tables();
}