Added ability to use variables in the connection settings

This commit is contained in:
Arin Alex 2016-02-25 00:27:11 +03:00
parent f774ab5a49
commit 011b17b537
6 changed files with 81 additions and 32 deletions

View File

@ -69,7 +69,7 @@ void ConnectionDialog::slotAccept()
if (!m_connection){ if (!m_connection){
m_controller->addConnectionDesc(uiToConnection()); m_controller->addConnectionDesc(uiToConnection());
} else { } else {
m_controller->changeConnectionDesc(uiToConnection()); m_controller->changeConnectionDesc(uiToConnection(m_connection));
} }
close(); close();
} }
@ -98,35 +98,35 @@ void ConnectionDialog::checkFieldsFill()
bool ConnectionDialog::checkConnection() bool ConnectionDialog::checkConnection()
{ {
bool connectionEstablished = false; QScopedPointer<LimeReport::ConnectionDesc> con(uiToConnection());
QString lastError; // LimeReport::ConnectionDesc con;
{ // con.setName(ui->leConnectionName->text()+"_check");
QSqlDatabase db = QSqlDatabase::addDatabase(ui->cbbDrivers->currentText(),ui->leConnectionName->text()+"_check"); // con.setHost(ui->leServerName->text());
db.setHostName(ui->leServerName->text()); // con.setUserName(ui->leUserName->text());
db.setUserName(ui->leUserName->text()); // con.setPassword(ui->lePassword->text());
db.setDatabaseName(ui->leDataBase->text()); // con.setDatabaseName(ui->leDataBase->text());
db.setPassword(ui->lePassword->text()); // con.setDriver(ui->cbbDrivers->currentText());
connectionEstablished = db.open(); if (!m_controller->checkConnectionDesc(con.data())){
if (!db.open()) { throw LimeReport::ReportError(m_controller->lastError());
lastError=db.lastError().text();
}
} }
QSqlDatabase::removeDatabase(ui->leConnectionName->text()+"_check");
if (!connectionEstablished) throw LimeReport::ReportError(lastError);
return true; return true;
} }
LimeReport::ConnectionDesc *ConnectionDialog::uiToConnection() LimeReport::ConnectionDesc *ConnectionDialog::uiToConnection(LimeReport::ConnectionDesc* conDesc)
{ {
if (!m_connection) m_connection = new LimeReport::ConnectionDesc(); LimeReport::ConnectionDesc* result;
m_connection->setName(ui->leConnectionName->text()); if (conDesc)
m_connection->setHost(ui->leServerName->text()); result = conDesc;
m_connection->setDriver(ui->cbbDrivers->currentText()); else
m_connection->setUserName(ui->leUserName->text()); result = new LimeReport::ConnectionDesc();
m_connection->setPassword(ui->lePassword->text()); result ->setName(ui->leConnectionName->text());
m_connection->setDatabaseName(ui->leDataBase->text()); result ->setHost(ui->leServerName->text());
m_connection->setAutoconnect(ui->cbAutoConnect->isChecked()); result ->setDriver(ui->cbbDrivers->currentText());
return m_connection; 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() void ConnectionDialog::connectionToUI()

View File

@ -48,7 +48,7 @@ protected:
void init(); void init();
void checkFieldsFill(); void checkFieldsFill();
bool checkConnection(); bool checkConnection();
LimeReport::ConnectionDesc* uiToConnection(); LimeReport::ConnectionDesc* uiToConnection(LimeReport::ConnectionDesc *conDesc = 0);
void connectionToUI(); void connectionToUI();
signals: signals:
void conectionRegistred(LimeReport::ConnectionDesc* connectionDesc); void conectionRegistred(LimeReport::ConnectionDesc* connectionDesc);
@ -56,7 +56,6 @@ private slots:
void slotAccept(); void slotAccept();
void slotCheckConnection(); void slotCheckConnection();
void on_toolButton_clicked(); void on_toolButton_clicked();
private: private:
Ui::ConnectionDialog *ui; Ui::ConnectionDialog *ui;
LimeReport::ConnectionDesc* m_connection; LimeReport::ConnectionDesc* m_connection;

View File

@ -595,11 +595,27 @@ void DataBrowser::addConnectionDesc(ConnectionDesc *connection)
void DataBrowser::changeConnectionDesc(ConnectionDesc *connection) void DataBrowser::changeConnectionDesc(ConnectionDesc *connection)
{ {
Q_UNUSED(connection)
if (connection->autoconnect()) m_report->dataManager()->connectConnection(connection->name()); if (connection->autoconnect()) m_report->dataManager()->connectConnection(connection->name());
updateDataTree(); 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) void DataBrowser::on_dataTree_currentItemChanged(QTreeWidgetItem *current, QTreeWidgetItem *previous)
{ {
Q_UNUSED(previous) Q_UNUSED(previous)

View File

@ -59,6 +59,9 @@ public:
void closeAllDataWindows(); void closeAllDataWindows();
void setSettings(QSettings* value, bool owned = false); void setSettings(QSettings* value, bool owned = false);
QSettings* settings(); QSettings* settings();
QString lastError() const;
void setLastError(const QString &lastError);
private slots: private slots:
void slotDatasourcesChanged(); void slotDatasourcesChanged();
void slotAddConnection(); void slotAddConnection();
@ -100,6 +103,7 @@ private:
void addConnectionDesc(ConnectionDesc *connection); void addConnectionDesc(ConnectionDesc *connection);
void changeConnectionDesc(ConnectionDesc *connection); void changeConnectionDesc(ConnectionDesc *connection);
bool checkConnectionDesc(ConnectionDesc *connection);
private: private:
Ui::DataBrowser* ui; Ui::DataBrowser* ui;
@ -109,6 +113,7 @@ private:
bool m_closingWindows; bool m_closingWindows;
QSettings* m_settings; QSettings* m_settings;
bool m_ownedSettings; bool m_ownedSettings;
QString m_lastError;
}; };
} }

View File

@ -341,6 +341,23 @@ QString DataSourceManager::extractField(QString source)
return 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<QString,QString> &aliasesToParam) QString DataSourceManager::replaceVariables(QString query, QMap<QString,QString> &aliasesToParam)
{ {
QRegExp rx(Const::VARIABLE_RX); 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) void DataSourceManager::addQueryDesc(QueryDesc *query)
{ {
m_queries.append(query); m_queries.append(query);
@ -633,10 +659,10 @@ bool DataSourceManager::connectConnection(ConnectionDesc *connectionDesc)
if (!QSqlDatabase::contains(connectionDesc->name())){ if (!QSqlDatabase::contains(connectionDesc->name())){
{ {
QSqlDatabase db = QSqlDatabase::addDatabase(connectionDesc->driver(),connectionDesc->name()); QSqlDatabase db = QSqlDatabase::addDatabase(connectionDesc->driver(),connectionDesc->name());
db.setHostName(connectionDesc->host()); db.setHostName(replaceVariables(connectionDesc->host()));
db.setUserName(connectionDesc->userName()); db.setUserName(replaceVariables(connectionDesc->userName()));
db.setPassword(connectionDesc->password()); db.setPassword(replaceVariables(connectionDesc->password()));
db.setDatabaseName(connectionDesc->databaseName()); db.setDatabaseName(replaceVariables(connectionDesc->databaseName()));
connected=db.open(); connected=db.open();
if (!connected) lastError=db.lastError().text(); if (!connected) lastError=db.lastError().text();
} }

View File

@ -108,6 +108,7 @@ public:
void connectAllDatabases(); void connectAllDatabases();
void addConnection(const QString& connectionName); void addConnection(const QString& connectionName);
void addConnectionDesc(ConnectionDesc *); void addConnectionDesc(ConnectionDesc *);
bool checkConnectionDesc(ConnectionDesc *connection);
void addQuery(const QString& name, const QString& sqlText, const QString& connectionName=""); 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 addSubQuery(const QString& name, const QString& sqlText, const QString& connectionName, const QString& masterDatasource);
void addProxy(const QString& name, QString master, QString detail, QList<FieldsCorrelation> fields); void addProxy(const QString& name, QString master, QString detail, QList<FieldsCorrelation> fields);
@ -179,6 +180,7 @@ public:
QString extractField(QString source); QString extractField(QString source);
QString replaceVariables(QString query, QMap<QString, QString> &aliasesToParam); QString replaceVariables(QString query, QMap<QString, QString> &aliasesToParam);
QString replaceVariables(QString value);
QString replaceFields(QString query, QMap<QString, QString> &aliasesToParam, QString masterDatasource = ""); QString replaceFields(QString query, QMap<QString, QString> &aliasesToParam, QString masterDatasource = "");
QSharedPointer<QAbstractItemModel> previewSQL(const QString& connectionName, const QString& sqlText, QString masterDatasource=""); QSharedPointer<QAbstractItemModel> 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 setSystemVariable(const QString& name, const QVariant& value, RenderPass pass);
void setLastError(const QString& value); void setLastError(const QString& value);
void invalidateLinkedDatasources(QString datasourceName); void invalidateLinkedDatasources(QString datasourceName);
private slots: private slots:
void slotConnectionRenamed(const QString& oldName,const QString& newName); void slotConnectionRenamed(const QString& oldName,const QString& newName);
private: private: