diff --git a/src/databrowser/lrconnectiondialog.cpp b/src/databrowser/lrconnectiondialog.cpp index 1676c82..99dc3b1 100644 --- a/src/databrowser/lrconnectiondialog.cpp +++ b/src/databrowser/lrconnectiondialog.cpp @@ -69,7 +69,7 @@ void ConnectionDialog::slotAccept() if (!m_connection){ m_controller->addConnectionDesc(uiToConnection()); } else { - m_controller->changeConnectionDesc(uiToConnection()); + m_controller->changeConnectionDesc(uiToConnection(m_connection)); } close(); } @@ -98,35 +98,35 @@ void ConnectionDialog::checkFieldsFill() bool ConnectionDialog::checkConnection() { - bool connectionEstablished = false; - QString lastError; - { - QSqlDatabase db = QSqlDatabase::addDatabase(ui->cbbDrivers->currentText(),ui->leConnectionName->text()+"_check"); - db.setHostName(ui->leServerName->text()); - db.setUserName(ui->leUserName->text()); - db.setDatabaseName(ui->leDataBase->text()); - db.setPassword(ui->lePassword->text()); - connectionEstablished = db.open(); - if (!db.open()) { - lastError=db.lastError().text(); - } + QScopedPointer con(uiToConnection()); +// LimeReport::ConnectionDesc con; +// con.setName(ui->leConnectionName->text()+"_check"); +// con.setHost(ui->leServerName->text()); +// con.setUserName(ui->leUserName->text()); +// con.setPassword(ui->lePassword->text()); +// con.setDatabaseName(ui->leDataBase->text()); +// con.setDriver(ui->cbbDrivers->currentText()); + if (!m_controller->checkConnectionDesc(con.data())){ + throw LimeReport::ReportError(m_controller->lastError()); } - QSqlDatabase::removeDatabase(ui->leConnectionName->text()+"_check"); - if (!connectionEstablished) throw LimeReport::ReportError(lastError); return true; } -LimeReport::ConnectionDesc *ConnectionDialog::uiToConnection() +LimeReport::ConnectionDesc *ConnectionDialog::uiToConnection(LimeReport::ConnectionDesc* conDesc) { - if (!m_connection) m_connection = new LimeReport::ConnectionDesc(); - m_connection->setName(ui->leConnectionName->text()); - m_connection->setHost(ui->leServerName->text()); - m_connection->setDriver(ui->cbbDrivers->currentText()); - m_connection->setUserName(ui->leUserName->text()); - m_connection->setPassword(ui->lePassword->text()); - m_connection->setDatabaseName(ui->leDataBase->text()); - m_connection->setAutoconnect(ui->cbAutoConnect->isChecked()); - return m_connection; + LimeReport::ConnectionDesc* result; + if (conDesc) + result = conDesc; + else + result = new LimeReport::ConnectionDesc(); + result ->setName(ui->leConnectionName->text()); + result ->setHost(ui->leServerName->text()); + result ->setDriver(ui->cbbDrivers->currentText()); + result ->setUserName(ui->leUserName->text()); + result ->setPassword(ui->lePassword->text()); + result ->setDatabaseName(ui->leDataBase->text()); + result ->setAutoconnect(ui->cbAutoConnect->isChecked()); + return result ; } void ConnectionDialog::connectionToUI() diff --git a/src/databrowser/lrconnectiondialog.h b/src/databrowser/lrconnectiondialog.h index 7d22dd9..8dbf30b 100644 --- a/src/databrowser/lrconnectiondialog.h +++ b/src/databrowser/lrconnectiondialog.h @@ -48,7 +48,7 @@ protected: void init(); void checkFieldsFill(); bool checkConnection(); - LimeReport::ConnectionDesc* uiToConnection(); + LimeReport::ConnectionDesc* uiToConnection(LimeReport::ConnectionDesc *conDesc = 0); void connectionToUI(); signals: void conectionRegistred(LimeReport::ConnectionDesc* connectionDesc); @@ -56,7 +56,6 @@ private slots: void slotAccept(); void slotCheckConnection(); void on_toolButton_clicked(); - private: Ui::ConnectionDialog *ui; LimeReport::ConnectionDesc* m_connection; diff --git a/src/databrowser/lrdatabrowser.cpp b/src/databrowser/lrdatabrowser.cpp index aa80064..6b26163 100644 --- a/src/databrowser/lrdatabrowser.cpp +++ b/src/databrowser/lrdatabrowser.cpp @@ -595,11 +595,27 @@ void DataBrowser::addConnectionDesc(ConnectionDesc *connection) void DataBrowser::changeConnectionDesc(ConnectionDesc *connection) { - Q_UNUSED(connection) if (connection->autoconnect()) m_report->dataManager()->connectConnection(connection->name()); updateDataTree(); } +bool DataBrowser::checkConnectionDesc(ConnectionDesc *connection) +{ + bool result = m_report->dataManager()->checkConnectionDesc(connection); + if (!result) setLastError(m_report->dataManager()->lastError()); + return result; +} + +QString DataBrowser::lastError() const +{ + return m_lastError; +} + +void DataBrowser::setLastError(const QString &lastError) +{ + m_lastError = lastError; +} + void DataBrowser::on_dataTree_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous) { Q_UNUSED(previous) diff --git a/src/databrowser/lrdatabrowser.h b/src/databrowser/lrdatabrowser.h index 06b10d3..11a489a 100644 --- a/src/databrowser/lrdatabrowser.h +++ b/src/databrowser/lrdatabrowser.h @@ -59,6 +59,9 @@ public: void closeAllDataWindows(); void setSettings(QSettings* value, bool owned = false); QSettings* settings(); + QString lastError() const; + void setLastError(const QString &lastError); + private slots: void slotDatasourcesChanged(); void slotAddConnection(); @@ -100,6 +103,7 @@ private: void addConnectionDesc(ConnectionDesc *connection); void changeConnectionDesc(ConnectionDesc *connection); + bool checkConnectionDesc(ConnectionDesc *connection); private: Ui::DataBrowser* ui; @@ -109,6 +113,7 @@ private: bool m_closingWindows; QSettings* m_settings; bool m_ownedSettings; + QString m_lastError; }; } diff --git a/src/lrdatasourcemanager.cpp b/src/lrdatasourcemanager.cpp index ddf7789..3335497 100644 --- a/src/lrdatasourcemanager.cpp +++ b/src/lrdatasourcemanager.cpp @@ -341,6 +341,23 @@ QString DataSourceManager::extractField(QString source) return source; } +QString DataSourceManager::replaceVariables(QString value){ + QRegExp rx(Const::VARIABLE_RX); + + if (value.contains(rx)){ + int pos = -1; + while ((pos=rx.indexIn(value))!=-1){ + QString var=rx.cap(0); + var.remove("$V{"); + var.remove("}"); + if (variableNames().contains(var)){ + value.replace(pos,rx.cap(0).length(),variable(var).toString()); + } + } + } + return value; +} + QString DataSourceManager::replaceVariables(QString query, QMap &aliasesToParam) { QRegExp rx(Const::VARIABLE_RX); @@ -583,6 +600,15 @@ void DataSourceManager::addConnectionDesc(ConnectionDesc * connection) } } +bool DataSourceManager::checkConnectionDesc(ConnectionDesc *connection) +{ + if (connectConnection(connection)){ + QSqlDatabase::removeDatabase(connection->name()); + return true; + } + return false; +} + void DataSourceManager::addQueryDesc(QueryDesc *query) { m_queries.append(query); @@ -633,10 +659,10 @@ bool DataSourceManager::connectConnection(ConnectionDesc *connectionDesc) if (!QSqlDatabase::contains(connectionDesc->name())){ { QSqlDatabase db = QSqlDatabase::addDatabase(connectionDesc->driver(),connectionDesc->name()); - db.setHostName(connectionDesc->host()); - db.setUserName(connectionDesc->userName()); - db.setPassword(connectionDesc->password()); - db.setDatabaseName(connectionDesc->databaseName()); + db.setHostName(replaceVariables(connectionDesc->host())); + db.setUserName(replaceVariables(connectionDesc->userName())); + db.setPassword(replaceVariables(connectionDesc->password())); + db.setDatabaseName(replaceVariables(connectionDesc->databaseName())); connected=db.open(); if (!connected) lastError=db.lastError().text(); } diff --git a/src/lrdatasourcemanager.h b/src/lrdatasourcemanager.h index 674bd3f..7e49c77 100644 --- a/src/lrdatasourcemanager.h +++ b/src/lrdatasourcemanager.h @@ -108,6 +108,7 @@ public: void connectAllDatabases(); void addConnection(const QString& connectionName); void addConnectionDesc(ConnectionDesc *); + bool checkConnectionDesc(ConnectionDesc *connection); void addQuery(const QString& name, const QString& sqlText, const QString& connectionName=""); void addSubQuery(const QString& name, const QString& sqlText, const QString& connectionName, const QString& masterDatasource); void addProxy(const QString& name, QString master, QString detail, QList fields); @@ -179,6 +180,7 @@ public: QString extractField(QString source); QString replaceVariables(QString query, QMap &aliasesToParam); + QString replaceVariables(QString value); QString replaceFields(QString query, QMap &aliasesToParam, QString masterDatasource = ""); QSharedPointer previewSQL(const QString& connectionName, const QString& sqlText, QString masterDatasource=""); @@ -206,6 +208,7 @@ protected: void setSystemVariable(const QString& name, const QVariant& value, RenderPass pass); void setLastError(const QString& value); void invalidateLinkedDatasources(QString datasourceName); + private slots: void slotConnectionRenamed(const QString& oldName,const QString& newName); private: