0
0
mirror of https://github.com/fralx/LimeReport.git synced 2024-12-23 16:22:58 +03:00

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){
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<LimeReport::ConnectionDesc> 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()

View File

@ -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;

View File

@ -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)

View File

@ -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;
};
}

View File

@ -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<QString,QString> &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();
}

View File

@ -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<FieldsCorrelation> fields);
@ -179,6 +180,7 @@ public:
QString extractField(QString source);
QString replaceVariables(QString query, QMap<QString, QString> &aliasesToParam);
QString replaceVariables(QString value);
QString replaceFields(QString query, QMap<QString, QString> &aliasesToParam, 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 setLastError(const QString& value);
void invalidateLinkedDatasources(QString datasourceName);
private slots:
void slotConnectionRenamed(const QString& oldName,const QString& newName);
private: