Added connection testing for MySQL.
This commit is contained in:
parent
c1451f7537
commit
a90dae1ff4
@ -34,10 +34,45 @@ DatabaseFactory *DatabaseFactory::instance() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int DatabaseFactory::mysqlTestConnection(const QString &hostname, int port,
|
int DatabaseFactory::mysqlTestConnection(const QString &hostname, int port,
|
||||||
const QString &usernam, const QString &password) {
|
const QString &username, const QString &password) {
|
||||||
// TODO: Otestovat, připojení k databázi pod danými
|
QSqlDatabase database = QSqlDatabase::addDatabase(APP_DB_DRIVER_MYSQL,
|
||||||
// údaji. Vrátit kód chyby. Použije se v dialogu nastavení.
|
APP_DB_TEST_MYSQL);
|
||||||
return 0;
|
|
||||||
|
database.setHostName(hostname);
|
||||||
|
database.setPort(port);
|
||||||
|
database.setUserName(username);
|
||||||
|
database.setPassword(password);
|
||||||
|
|
||||||
|
if (database.open()) {
|
||||||
|
// Connection succeeded, clean up the mess and return 0.
|
||||||
|
database.close();
|
||||||
|
removeConnection(APP_DB_TEST_MYSQL);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Connection failed, do cleanup and return specific
|
||||||
|
// error code.
|
||||||
|
int error_code = database.lastError().number();
|
||||||
|
|
||||||
|
removeConnection(APP_DB_TEST_MYSQL);
|
||||||
|
return error_code;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QString DatabaseFactory::mysqlInterpretErrorCode(int error_code) {
|
||||||
|
switch (error_code) {
|
||||||
|
case 0:
|
||||||
|
return QObject::tr("Operation successful.");
|
||||||
|
|
||||||
|
case 2005:
|
||||||
|
return QObject::tr("No MySQL server is running in the target destination.");
|
||||||
|
|
||||||
|
case 1045:
|
||||||
|
return QObject::tr("Access denied. Invalid username or password used.");
|
||||||
|
|
||||||
|
default:
|
||||||
|
return QObject::tr("Unknown error.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void DatabaseFactory::sqliteAssemblyDatabaseFilePath() {
|
void DatabaseFactory::sqliteAssemblyDatabaseFilePath() {
|
||||||
|
@ -64,9 +64,14 @@ class DatabaseFactory : public QObject {
|
|||||||
//
|
//
|
||||||
// MySQL stuff.
|
// MySQL stuff.
|
||||||
//
|
//
|
||||||
int mysqlTestConnection(const QString &hostname, int port,
|
|
||||||
const QString &usernam, const QString &password);
|
|
||||||
|
|
||||||
|
// Tests if database connection with given data
|
||||||
|
// can be established and returns 0 if it can.
|
||||||
|
// Otherwise returns MySQL-specific error code.
|
||||||
|
int mysqlTestConnection(const QString &hostname, int port,
|
||||||
|
const QString &username, const QString &password);
|
||||||
|
|
||||||
|
QString mysqlInterpretErrorCode(int error_code);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
//
|
//
|
||||||
|
@ -42,6 +42,7 @@
|
|||||||
#define AUTO_UPDATE_INTERVAL 60000
|
#define AUTO_UPDATE_INTERVAL 60000
|
||||||
#define STARTUP_UPDATE_DELAY 1500
|
#define STARTUP_UPDATE_DELAY 1500
|
||||||
|
|
||||||
|
#define APP_DB_TEST_MYSQL "MySQLTest"
|
||||||
#define APP_DB_MYSQL_PORT 3306
|
#define APP_DB_MYSQL_PORT 3306
|
||||||
#define APP_DB_DRIVER_SQLITE "QSQLITE"
|
#define APP_DB_DRIVER_SQLITE "QSQLITE"
|
||||||
#define APP_DB_DRIVER_MYSQL "QMYSQL"
|
#define APP_DB_DRIVER_MYSQL "QMYSQL"
|
||||||
|
@ -16,7 +16,6 @@ MessagesModel::MessagesModel(QObject *parent)
|
|||||||
DatabaseFactory::instance()->connection("MessagesModel",
|
DatabaseFactory::instance()->connection("MessagesModel",
|
||||||
DatabaseFactory::FromSettings)) {
|
DatabaseFactory::FromSettings)) {
|
||||||
setObjectName("MessagesModel");
|
setObjectName("MessagesModel");
|
||||||
|
|
||||||
setupFonts();
|
setupFonts();
|
||||||
setupIcons();
|
setupIcons();
|
||||||
setupHeaderData();
|
setupHeaderData();
|
||||||
|
@ -66,12 +66,17 @@ void FeedMessageViewer::saveSize() {
|
|||||||
|
|
||||||
// States of splitters are stored, let's store
|
// States of splitters are stored, let's store
|
||||||
// widths of columns.
|
// widths of columns.
|
||||||
settings->setValue(APP_CFG_GUI,
|
int width_column_author = m_messagesView->columnWidth(MSG_DB_AUTHOR_INDEX);
|
||||||
KEY_MESSAGES_VIEW + QString::number(MSG_DB_AUTHOR_INDEX),
|
int width_column_date = m_messagesView->columnWidth(MSG_DB_DCREATED_INDEX);
|
||||||
m_messagesView->columnWidth(MSG_DB_AUTHOR_INDEX));
|
|
||||||
settings->setValue(APP_CFG_GUI,
|
if (width_column_author != 0 && width_column_date != 0) {
|
||||||
KEY_MESSAGES_VIEW + QString::number(MSG_DB_DCREATED_INDEX),
|
settings->setValue(APP_CFG_GUI,
|
||||||
m_messagesView->columnWidth(MSG_DB_DCREATED_INDEX));
|
KEY_MESSAGES_VIEW + QString::number(MSG_DB_AUTHOR_INDEX),
|
||||||
|
width_column_author);
|
||||||
|
settings->setValue(APP_CFG_GUI,
|
||||||
|
KEY_MESSAGES_VIEW + QString::number(MSG_DB_DCREATED_INDEX),
|
||||||
|
width_column_date);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void FeedMessageViewer::loadSize() {
|
void FeedMessageViewer::loadSize() {
|
||||||
|
@ -493,12 +493,13 @@ void FormSettings::saveDataStorage() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void FormSettings::mysqlTestConnection() {
|
void FormSettings::mysqlTestConnection() {
|
||||||
int result = DatabaseFactory::instance()->mysqlTestConnection(m_ui->m_txtMysqlHostname->lineEdit()->text(),
|
int error_code = DatabaseFactory::instance()->mysqlTestConnection(m_ui->m_txtMysqlHostname->lineEdit()->text(),
|
||||||
m_ui->m_spinMysqlPort->value(),
|
m_ui->m_spinMysqlPort->value(),
|
||||||
m_ui->m_txtMysqlUsername->lineEdit()->text(),
|
m_ui->m_txtMysqlUsername->lineEdit()->text(),
|
||||||
m_ui->m_txtMysqlPassword->lineEdit()->text());
|
m_ui->m_txtMysqlPassword->lineEdit()->text());
|
||||||
|
|
||||||
// TODO: zobrazit výsledek
|
// Let's interpret the result.
|
||||||
|
m_ui->m_lblMysqlTestResult->setText(DatabaseFactory::instance()->mysqlInterpretErrorCode(error_code));
|
||||||
}
|
}
|
||||||
|
|
||||||
void FormSettings::onMysqlHostnameChanged(const QString &new_hostname) {
|
void FormSettings::onMysqlHostnameChanged(const QString &new_hostname) {
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
<item row="0" column="1">
|
<item row="0" column="1">
|
||||||
<widget class="QStackedWidget" name="m_stackedSettings">
|
<widget class="QStackedWidget" name="m_stackedSettings">
|
||||||
<property name="currentIndex">
|
<property name="currentIndex">
|
||||||
<number>0</number>
|
<number>1</number>
|
||||||
</property>
|
</property>
|
||||||
<widget class="QWidget" name="m_pageGeneral">
|
<widget class="QWidget" name="m_pageGeneral">
|
||||||
<layout class="QFormLayout" name="formLayout_5">
|
<layout class="QFormLayout" name="formLayout_5">
|
||||||
@ -82,7 +82,7 @@
|
|||||||
<item row="2" column="0" colspan="2">
|
<item row="2" column="0" colspan="2">
|
||||||
<widget class="QStackedWidget" name="m_stackedDatabaseDriver">
|
<widget class="QStackedWidget" name="m_stackedDatabaseDriver">
|
||||||
<property name="currentIndex">
|
<property name="currentIndex">
|
||||||
<number>0</number>
|
<number>1</number>
|
||||||
</property>
|
</property>
|
||||||
<widget class="QWidget" name="m_pageSqlite">
|
<widget class="QWidget" name="m_pageSqlite">
|
||||||
<layout class="QFormLayout" name="formLayout_15">
|
<layout class="QFormLayout" name="formLayout_15">
|
||||||
@ -223,17 +223,20 @@ Authors of this application are NOT responsible for lost data.</string>
|
|||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<spacer name="horizontalSpacer">
|
<widget class="QLabel" name="m_lblMysqlTestResult">
|
||||||
<property name="orientation">
|
<property name="sizePolicy">
|
||||||
<enum>Qt::Horizontal</enum>
|
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
|
||||||
|
<horstretch>0</horstretch>
|
||||||
|
<verstretch>0</verstretch>
|
||||||
|
</sizepolicy>
|
||||||
</property>
|
</property>
|
||||||
<property name="sizeHint" stdset="0">
|
<property name="text">
|
||||||
<size>
|
<string>No test run so far.</string>
|
||||||
<width>40</width>
|
|
||||||
<height>20</height>
|
|
||||||
</size>
|
|
||||||
</property>
|
</property>
|
||||||
</spacer>
|
<property name="wordWrap">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</item>
|
</item>
|
||||||
@ -248,6 +251,9 @@ Authors of this application are NOT responsible for lost data.</string>
|
|||||||
<property name="wordWrap">
|
<property name="wordWrap">
|
||||||
<bool>true</bool>
|
<bool>true</bool>
|
||||||
</property>
|
</property>
|
||||||
|
<property name="margin">
|
||||||
|
<number>10</number>
|
||||||
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
@ -283,8 +289,8 @@ Authors of this application are NOT responsible for lost data.</string>
|
|||||||
<rect>
|
<rect>
|
||||||
<x>0</x>
|
<x>0</x>
|
||||||
<y>0</y>
|
<y>0</y>
|
||||||
<width>564</width>
|
<width>100</width>
|
||||||
<height>363</height>
|
<height>30</height>
|
||||||
</rect>
|
</rect>
|
||||||
</property>
|
</property>
|
||||||
<layout class="QHBoxLayout" name="horizontalLayout_4">
|
<layout class="QHBoxLayout" name="horizontalLayout_4">
|
||||||
@ -361,8 +367,8 @@ Authors of this application are NOT responsible for lost data.</string>
|
|||||||
<rect>
|
<rect>
|
||||||
<x>0</x>
|
<x>0</x>
|
||||||
<y>0</y>
|
<y>0</y>
|
||||||
<width>558</width>
|
<width>167</width>
|
||||||
<height>337</height>
|
<height>219</height>
|
||||||
</rect>
|
</rect>
|
||||||
</property>
|
</property>
|
||||||
<layout class="QFormLayout" name="formLayout">
|
<layout class="QFormLayout" name="formLayout">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user