mirror of
https://github.com/python-LimeReport/LimeReport.git
synced 2024-12-25 13:04:38 +03:00
setDefaultDatabasePath() has been added to DataSourceManager
This commit is contained in:
parent
be67e6309b
commit
0055790bcd
@ -40,6 +40,7 @@ namespace LimeReport{
|
|||||||
class IDataSourceManager{
|
class IDataSourceManager{
|
||||||
public:
|
public:
|
||||||
virtual void setReportVariable(const QString& name, const QVariant& value)=0;
|
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 void deleteVariable(const QString& name)=0;
|
||||||
virtual bool containsVariable(const QString& variableName)=0;
|
virtual bool containsVariable(const QString& variableName)=0;
|
||||||
virtual QVariant variable(const QString& variableName)=0;
|
virtual QVariant variable(const QString& variableName)=0;
|
||||||
|
@ -34,6 +34,7 @@
|
|||||||
#include <QRegExp>
|
#include <QRegExp>
|
||||||
#include <QSqlError>
|
#include <QSqlError>
|
||||||
#include <QSqlQueryModel>
|
#include <QSqlQueryModel>
|
||||||
|
#include <QFileInfo>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
namespace LimeReport{
|
namespace LimeReport{
|
||||||
@ -227,6 +228,16 @@ DataSourceManager::DataSourceManager(QObject *parent) :
|
|||||||
setSystemVariable(QLatin1String("#PAGE_COUNT"),0,SecondPass);
|
setSystemVariable(QLatin1String("#PAGE_COUNT"),0,SecondPass);
|
||||||
m_datasourcesModel.setDataSourceManager(this);
|
m_datasourcesModel.setDataSourceManager(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString DataSourceManager::defaultDatabasePath() const
|
||||||
|
{
|
||||||
|
return m_defaultDatabasePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
void DataSourceManager::setDefaultDatabasePath(const QString &defaultDatabasePath)
|
||||||
|
{
|
||||||
|
m_defaultDatabasePath = defaultDatabasePath;
|
||||||
|
}
|
||||||
bool DataSourceManager::designTime() const
|
bool DataSourceManager::designTime() const
|
||||||
{
|
{
|
||||||
return m_designTime;
|
return m_designTime;
|
||||||
@ -364,7 +375,8 @@ QString DataSourceManager::replaceVariables(QString value){
|
|||||||
QString var=rx.cap(0);
|
QString var=rx.cap(0);
|
||||||
var.remove("$V{");
|
var.remove("$V{");
|
||||||
var.remove("}");
|
var.remove("}");
|
||||||
if (variableNames().contains(var)){
|
|
||||||
|
if (variable(var).isValid()){
|
||||||
value.replace(pos,rx.cap(0).length(),variable(var).toString());
|
value.replace(pos,rx.cap(0).length(),variable(var).toString());
|
||||||
} else {
|
} else {
|
||||||
value.replace(pos,rx.cap(0).length(),QString(tr("Variable \"%1\" not found!").arg(var)));
|
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();
|
dataSourceHolder(datasourceName)->clearErrors();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!QSqlDatabase::contains(connectionDesc->name())){
|
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 {
|
|
||||||
foreach(QString datasourceName, dataSourceNames()){
|
foreach(QString datasourceName, dataSourceNames()){
|
||||||
if (isQuery(datasourceName)){
|
if (isQuery(datasourceName)){
|
||||||
QueryHolder* qh = dynamic_cast<QueryHolder*>(dataSourceHolder(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);
|
qh->invalidate(designTime()?IDataSource::DESIGN_MODE:IDataSource::RENDER_MODE);
|
||||||
invalidateChildren(datasourceName);
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -189,6 +189,9 @@ public:
|
|||||||
QSharedPointer<QAbstractItemModel> previewSQL(const QString& connectionName, const QString& sqlText, QString masterDatasource="");
|
QSharedPointer<QAbstractItemModel> previewSQL(const QString& connectionName, const QString& sqlText, QString masterDatasource="");
|
||||||
void updateDatasourceModel();
|
void updateDatasourceModel();
|
||||||
bool isNeedUpdateDatasourceModel(){ return m_needUpdate;}
|
bool isNeedUpdateDatasourceModel(){ return m_needUpdate;}
|
||||||
|
QString defaultDatabasePath() const;
|
||||||
|
void setDefaultDatabasePath(const QString &defaultDatabasePath);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void loadCollectionFinished(const QString& collectionName);
|
void loadCollectionFinished(const QString& collectionName);
|
||||||
void cleared();
|
void cleared();
|
||||||
@ -208,7 +211,6 @@ protected:
|
|||||||
virtual QObject *elementAt(const QString& collectionName,int index);
|
virtual QObject *elementAt(const QString& collectionName,int index);
|
||||||
virtual void collectionLoadFinished(const QString& collectionName);
|
virtual void collectionLoadFinished(const QString& collectionName);
|
||||||
|
|
||||||
|
|
||||||
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);
|
||||||
@ -236,6 +238,7 @@ private:
|
|||||||
QStringList m_errorsList;
|
QStringList m_errorsList;
|
||||||
bool m_designTime;
|
bool m_designTime;
|
||||||
bool m_needUpdate;
|
bool m_needUpdate;
|
||||||
|
QString m_defaultDatabasePath;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -40,6 +40,7 @@ namespace LimeReport{
|
|||||||
class IDataSourceManager{
|
class IDataSourceManager{
|
||||||
public:
|
public:
|
||||||
virtual void setReportVariable(const QString& name, const QVariant& value)=0;
|
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 void deleteVariable(const QString& name)=0;
|
||||||
virtual bool containsVariable(const QString& variableName)=0;
|
virtual bool containsVariable(const QString& variableName)=0;
|
||||||
virtual QVariant variable(const QString& variableName)=0;
|
virtual QVariant variable(const QString& variableName)=0;
|
||||||
|
Loading…
Reference in New Issue
Block a user