-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbconnection.cpp
107 lines (91 loc) · 2.61 KB
/
dbconnection.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
#include "dbconnection.h"
#include "ui_dbconnection.h"
#include <QDebug>
DBconnection::DBconnection(QDialog *parent) :
QDialog(parent),
ui(new Ui::DBconnection),
db(new QSqlDatabase)
{
ui->setupUi(this);
QStringList drivers = QSqlDatabase::drivers();
ui->comboBox->addItems(drivers);
ui->comboBox->setCurrentText("QPSQL");
ui->checkBox->setChecked(true);
}
DBconnection::~DBconnection()
{
delete ui;
}
int DBconnection::defaultPort(QString driver)
{
if (!(QString::compare(driver,"QPSQL",Qt::CaseInsensitive)))
return 5432;
else
return 0;
}
QSqlError DBconnection::addConnection(QSqlDatabase *db)
{
QSqlError err;
if (!db->open())
err = db->lastError();
return err;
}
void DBconnection::on_submitButton_clicked()
{
QString Driver = ui->comboBox->currentText();
QString DBname = ui->editDB->text();
QString UserName = ui->editUser->text();
QString Password = ui->editPassword->text();
QString HostName = ui->editHost->text();
int Port = ui->editPort->value();
if ((DBname.isEmpty()) || (HostName.isEmpty()) || (UserName.isEmpty()))
{
QMessageBox::information(this, tr("Information about the database is incomplete"),
tr("Please check the entered information and try again"));
return;
}
*db = QSqlDatabase::addDatabase(Driver);
db->setDatabaseName(DBname);
db->setUserName(UserName);
db->setPassword(Password);
db->setHostName(HostName);
db->setPort(Port);
QSqlError err = addConnection(db);
if (err.type() != QSqlError::NoError)
{
QMessageBox::warning(this, tr("Unable to open database"), tr("An error occurred while "
"opening the connection: ") + err.text());
}
else
{
emit dbCorrectlyOpen(db);
qDebug() << "сигнал испущен";
}
return;
}
void DBconnection::on_clearButton_clicked()
{
ui->editDB->clear();
ui->editHost->clear();
ui->editPassword->clear();
ui->editUser->clear();
ui->editPort->setValue(defaultPort(ui->comboBox->currentText()));
ui->checkBox->setChecked(false);
return;
}
void DBconnection::on_checkBox_stateChanged(int arg1)
{
if (arg1)
{
ui->comboBox->setCurrentText("QPSQL");
ui->editDB->setText("demo");
ui->editUser->setText("postgres");
ui->editPassword->setText("110713wtf");
ui->editHost->setText("localhost");
ui->editPort->setValue(defaultPort("QPSQL"));
ui->submitButton->setFocus();
}
else
on_clearButton_clicked();
return;
}