From 0055790bcd0edffff30c2d096e8616703d2fb8b3 Mon Sep 17 00:00:00 2001 From: Arin Alex Date: Tue, 8 Nov 2016 01:09:35 +0300 Subject: [PATCH] setDefaultDatabasePath() has been added to DataSourceManager --- include/lrdatasourcemanagerintf.h | 1 + limereport/lrdatasourcemanager.cpp | 70 +++++++++++++++++++--------- limereport/lrdatasourcemanager.h | 5 +- limereport/lrdatasourcemanagerintf.h | 1 + 4 files changed, 55 insertions(+), 22 deletions(-) diff --git a/include/lrdatasourcemanagerintf.h b/include/lrdatasourcemanagerintf.h index 7ff5aee..85cf388 100644 --- a/include/lrdatasourcemanagerintf.h +++ b/include/lrdatasourcemanagerintf.h @@ -40,6 +40,7 @@ namespace LimeReport{ class IDataSourceManager{ public: virtual void setReportVariable(const QString& name, const QVariant& value)=0; + virtual void setDefaultDatabasePath(const QString &defaultDatabasePath)=0; virtual void deleteVariable(const QString& name)=0; virtual bool containsVariable(const QString& variableName)=0; virtual QVariant variable(const QString& variableName)=0; diff --git a/limereport/lrdatasourcemanager.cpp b/limereport/lrdatasourcemanager.cpp index 1b228ea..b6c25d3 100644 --- a/limereport/lrdatasourcemanager.cpp +++ b/limereport/lrdatasourcemanager.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include namespace LimeReport{ @@ -227,6 +228,16 @@ DataSourceManager::DataSourceManager(QObject *parent) : setSystemVariable(QLatin1String("#PAGE_COUNT"),0,SecondPass); m_datasourcesModel.setDataSourceManager(this); } + +QString DataSourceManager::defaultDatabasePath() const +{ + return m_defaultDatabasePath; +} + +void DataSourceManager::setDefaultDatabasePath(const QString &defaultDatabasePath) +{ + m_defaultDatabasePath = defaultDatabasePath; +} bool DataSourceManager::designTime() const { return m_designTime; @@ -364,7 +375,8 @@ QString DataSourceManager::replaceVariables(QString value){ QString var=rx.cap(0); var.remove("$V{"); var.remove("}"); - if (variableNames().contains(var)){ + + if (variable(var).isValid()){ value.replace(pos,rx.cap(0).length(),variable(var).toString()); } else { value.replace(pos,rx.cap(0).length(),QString(tr("Variable \"%1\" not found!").arg(var))); @@ -670,28 +682,11 @@ bool DataSourceManager::connectConnection(ConnectionDesc *connectionDesc) dataSourceHolder(datasourceName)->clearErrors(); } - if (!QSqlDatabase::contains(connectionDesc->name())){ - { - QSqlDatabase db = QSqlDatabase::addDatabase(connectionDesc->driver(),connectionDesc->name()); - 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(); - } - } else { - connected = QSqlDatabase::database(connectionDesc->name()).isOpen(); - } - if (!connected) { - QSqlDatabase::removeDatabase(connectionDesc->name()); - setLastError(lastError); - return false; - } else { + if (QSqlDatabase::contains(connectionDesc->name())){ foreach(QString datasourceName, dataSourceNames()){ if (isQuery(datasourceName)){ QueryHolder* qh = dynamic_cast(dataSourceHolder(datasourceName)); - if (qh){ + if (qh && qh->connectionName().compare(connectionDesc->name())==0){ qh->invalidate(designTime()?IDataSource::DESIGN_MODE:IDataSource::RENDER_MODE); invalidateChildren(datasourceName); } @@ -705,8 +700,41 @@ bool DataSourceManager::connectConnection(ConnectionDesc *connectionDesc) } } } - if (designTime()) emit datasourcesChanged(); + + QSqlDatabase::removeDatabase(connectionDesc->name()); } + + QSqlDatabase db = QSqlDatabase::addDatabase(connectionDesc->driver(),connectionDesc->name()); + db.setHostName(replaceVariables(connectionDesc->host())); + db.setUserName(replaceVariables(connectionDesc->userName())); + db.setPassword(replaceVariables(connectionDesc->password())); + QString dbName = replaceVariables(connectionDesc->databaseName()); + if (connectionDesc->driver().compare("QSQLITE")==0){ + if (!defaultDatabasePath().isEmpty()){ + dbName = !QFileInfo(dbName).exists() ? + defaultDatabasePath()+QFileInfo(dbName).fileName() : + dbName; + } + if (QFileInfo(dbName).exists()){ + db.setDatabaseName(dbName); + connected=db.open(); + } else { + lastError = tr("Database \"%1\" not found").arg(dbName); + } + } else { + db.setDatabaseName(dbName); + connected=db.open(); + if (!connected) lastError=db.lastError().text(); + } + + if (designTime()) emit datasourcesChanged(); + + if (!connected) { + QSqlDatabase::removeDatabase(connectionDesc->name()); + setLastError(lastError); + return false; + } + return true; } diff --git a/limereport/lrdatasourcemanager.h b/limereport/lrdatasourcemanager.h index 3e44687..1567ae5 100644 --- a/limereport/lrdatasourcemanager.h +++ b/limereport/lrdatasourcemanager.h @@ -189,6 +189,9 @@ public: QSharedPointer previewSQL(const QString& connectionName, const QString& sqlText, QString masterDatasource=""); void updateDatasourceModel(); bool isNeedUpdateDatasourceModel(){ return m_needUpdate;} + QString defaultDatabasePath() const; + void setDefaultDatabasePath(const QString &defaultDatabasePath); + signals: void loadCollectionFinished(const QString& collectionName); void cleared(); @@ -208,7 +211,6 @@ protected: virtual QObject *elementAt(const QString& collectionName,int index); virtual void collectionLoadFinished(const QString& collectionName); - void setSystemVariable(const QString& name, const QVariant& value, RenderPass pass); void setLastError(const QString& value); void invalidateLinkedDatasources(QString datasourceName); @@ -236,6 +238,7 @@ private: QStringList m_errorsList; bool m_designTime; bool m_needUpdate; + QString m_defaultDatabasePath; }; } diff --git a/limereport/lrdatasourcemanagerintf.h b/limereport/lrdatasourcemanagerintf.h index 7ff5aee..85cf388 100644 --- a/limereport/lrdatasourcemanagerintf.h +++ b/limereport/lrdatasourcemanagerintf.h @@ -40,6 +40,7 @@ namespace LimeReport{ class IDataSourceManager{ public: virtual void setReportVariable(const QString& name, const QVariant& value)=0; + virtual void setDefaultDatabasePath(const QString &defaultDatabasePath)=0; virtual void deleteVariable(const QString& name)=0; virtual bool containsVariable(const QString& variableName)=0; virtual QVariant variable(const QString& variableName)=0;