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,
|
||||
const QString &usernam, const QString &password) {
|
||||
// TODO: Otestovat, připojení k databázi pod danými
|
||||
// údaji. Vrátit kód chyby. Použije se v dialogu nastavení.
|
||||
return 0;
|
||||
const QString &username, const QString &password) {
|
||||
QSqlDatabase database = QSqlDatabase::addDatabase(APP_DB_DRIVER_MYSQL,
|
||||
APP_DB_TEST_MYSQL);
|
||||
|
||||
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() {
|
||||
|
@ -64,9 +64,14 @@ class DatabaseFactory : public QObject {
|
||||
//
|
||||
// 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:
|
||||
//
|
||||
|
@ -42,6 +42,7 @@
|
||||
#define AUTO_UPDATE_INTERVAL 60000
|
||||
#define STARTUP_UPDATE_DELAY 1500
|
||||
|
||||
#define APP_DB_TEST_MYSQL "MySQLTest"
|
||||
#define APP_DB_MYSQL_PORT 3306
|
||||
#define APP_DB_DRIVER_SQLITE "QSQLITE"
|
||||
#define APP_DB_DRIVER_MYSQL "QMYSQL"
|
||||
|
@ -16,7 +16,6 @@ MessagesModel::MessagesModel(QObject *parent)
|
||||
DatabaseFactory::instance()->connection("MessagesModel",
|
||||
DatabaseFactory::FromSettings)) {
|
||||
setObjectName("MessagesModel");
|
||||
|
||||
setupFonts();
|
||||
setupIcons();
|
||||
setupHeaderData();
|
||||
|
@ -66,12 +66,17 @@ void FeedMessageViewer::saveSize() {
|
||||
|
||||
// States of splitters are stored, let's store
|
||||
// widths of columns.
|
||||
settings->setValue(APP_CFG_GUI,
|
||||
KEY_MESSAGES_VIEW + QString::number(MSG_DB_AUTHOR_INDEX),
|
||||
m_messagesView->columnWidth(MSG_DB_AUTHOR_INDEX));
|
||||
settings->setValue(APP_CFG_GUI,
|
||||
KEY_MESSAGES_VIEW + QString::number(MSG_DB_DCREATED_INDEX),
|
||||
m_messagesView->columnWidth(MSG_DB_DCREATED_INDEX));
|
||||
int width_column_author = m_messagesView->columnWidth(MSG_DB_AUTHOR_INDEX);
|
||||
int width_column_date = m_messagesView->columnWidth(MSG_DB_DCREATED_INDEX);
|
||||
|
||||
if (width_column_author != 0 && width_column_date != 0) {
|
||||
settings->setValue(APP_CFG_GUI,
|
||||
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() {
|
||||
|
@ -493,12 +493,13 @@ void FormSettings::saveDataStorage() {
|
||||
}
|
||||
|
||||
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_txtMysqlUsername->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) {
|
||||
|
@ -17,7 +17,7 @@
|
||||
<item row="0" column="1">
|
||||
<widget class="QStackedWidget" name="m_stackedSettings">
|
||||
<property name="currentIndex">
|
||||
<number>0</number>
|
||||
<number>1</number>
|
||||
</property>
|
||||
<widget class="QWidget" name="m_pageGeneral">
|
||||
<layout class="QFormLayout" name="formLayout_5">
|
||||
@ -82,7 +82,7 @@
|
||||
<item row="2" column="0" colspan="2">
|
||||
<widget class="QStackedWidget" name="m_stackedDatabaseDriver">
|
||||
<property name="currentIndex">
|
||||
<number>0</number>
|
||||
<number>1</number>
|
||||
</property>
|
||||
<widget class="QWidget" name="m_pageSqlite">
|
||||
<layout class="QFormLayout" name="formLayout_15">
|
||||
@ -223,17 +223,20 @@ Authors of this application are NOT responsible for lost data.</string>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<spacer name="horizontalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
<widget class="QLabel" name="m_lblMysqlTestResult">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>40</width>
|
||||
<height>20</height>
|
||||
</size>
|
||||
<property name="text">
|
||||
<string>No test run so far.</string>
|
||||
</property>
|
||||
</spacer>
|
||||
<property name="wordWrap">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
@ -248,6 +251,9 @@ Authors of this application are NOT responsible for lost data.</string>
|
||||
<property name="wordWrap">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="margin">
|
||||
<number>10</number>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
@ -283,8 +289,8 @@ Authors of this application are NOT responsible for lost data.</string>
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>564</width>
|
||||
<height>363</height>
|
||||
<width>100</width>
|
||||
<height>30</height>
|
||||
</rect>
|
||||
</property>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_4">
|
||||
@ -361,8 +367,8 @@ Authors of this application are NOT responsible for lost data.</string>
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>558</width>
|
||||
<height>337</height>
|
||||
<width>167</width>
|
||||
<height>219</height>
|
||||
</rect>
|
||||
</property>
|
||||
<layout class="QFormLayout" name="formLayout">
|
||||
|
Loading…
x
Reference in New Issue
Block a user