setDefaultDatabasePath() has been added to DataSourceManager

This commit is contained in:
Arin Alex 2016-11-08 01:09:35 +03:00
parent be67e6309b
commit 0055790bcd
4 changed files with 55 additions and 22 deletions

View File

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

View File

@ -34,6 +34,7 @@
#include <QRegExp>
#include <QSqlError>
#include <QSqlQueryModel>
#include <QFileInfo>
#include <stdexcept>
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<QueryHolder*>(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;
}

View File

@ -189,6 +189,9 @@ public:
QSharedPointer<QAbstractItemModel> 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;
};
}

View File

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