2016-02-17 10:11:00 +03:00
|
|
|
/***************************************************************************
|
|
|
|
* This file is part of the Lime Report project *
|
2021-08-18 20:21:36 +03:00
|
|
|
* Copyright (C) 2021 by Alexander Arin *
|
2016-02-17 10:11:00 +03:00
|
|
|
* arin_a@bk.ru *
|
|
|
|
* *
|
|
|
|
** GNU General Public License Usage **
|
|
|
|
* *
|
|
|
|
* This library is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
|
|
|
* *
|
|
|
|
** GNU Lesser General Public License **
|
|
|
|
* *
|
|
|
|
* This library is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU Lesser General Public License as *
|
|
|
|
* published by the Free Software Foundation, either version 3 of the *
|
|
|
|
* License, or (at your option) any later version. *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public *
|
|
|
|
* License along with this library. *
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>. *
|
|
|
|
* *
|
|
|
|
* This library is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
****************************************************************************/
|
|
|
|
#ifndef LRDATASOURCEMANAGER_H
|
|
|
|
#define LRDATASOURCEMANAGER_H
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QIcon>
|
|
|
|
#include "lrdatadesignintf.h"
|
|
|
|
#include "lrcollection.h"
|
|
|
|
#include "lrglobal.h"
|
|
|
|
#include "lrvariablesholder.h"
|
|
|
|
#include "lrgroupfunctions.h"
|
|
|
|
#include "lrdatasourcemanagerintf.h"
|
2019-01-24 22:10:15 +03:00
|
|
|
#include "lrdatasourceintf.h"
|
2016-02-17 10:11:00 +03:00
|
|
|
|
|
|
|
namespace LimeReport{
|
|
|
|
|
|
|
|
|
|
|
|
class DataSourceManager;
|
|
|
|
|
|
|
|
class DataNode {
|
|
|
|
public:
|
|
|
|
enum NodeType{Root,Connection,DataSources,Query,SubQuery,Model,Field,Variables,Variable};
|
|
|
|
DataNode(const QString& name="", NodeType type=Root, DataNode* parent=0, const QIcon& icon=QIcon())
|
|
|
|
:m_name(name), m_icon(icon), m_type(type), m_parent(parent){}
|
|
|
|
virtual ~DataNode();
|
|
|
|
int childCount(){return m_childs.count();}
|
|
|
|
DataNode* child(int index){return m_childs[index];}
|
|
|
|
DataNode* parent(){return m_parent;}
|
|
|
|
DataNode* addChild(const QString& name="", NodeType type=Root, const QIcon& icon=QIcon());
|
|
|
|
int row();
|
|
|
|
QString name(){return m_name;}
|
|
|
|
QIcon icon(){return m_icon;}
|
|
|
|
void clear();
|
|
|
|
NodeType type(){return m_type;}
|
|
|
|
private:
|
|
|
|
QString m_name;
|
|
|
|
QIcon m_icon;
|
|
|
|
NodeType m_type;
|
|
|
|
DataNode* m_parent;
|
|
|
|
QVector<DataNode*> m_childs;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DataSourceModel : public QAbstractItemModel{
|
|
|
|
Q_OBJECT
|
2016-04-13 23:04:56 +03:00
|
|
|
friend class DataSourceManager;
|
2016-02-17 10:11:00 +03:00
|
|
|
public:
|
2017-04-09 04:38:23 +03:00
|
|
|
DataSourceModel():m_dataManager(NULL),m_rootNode(new DataNode()){}
|
2016-02-17 10:11:00 +03:00
|
|
|
DataSourceModel(DataSourceManager* dataManager);
|
|
|
|
~DataSourceModel();
|
2016-06-10 18:05:18 +03:00
|
|
|
QModelIndex index(int row, int column, const QModelIndex &parent) const;
|
2016-02-17 10:11:00 +03:00
|
|
|
QModelIndex parent(const QModelIndex &child) const;
|
|
|
|
int rowCount(const QModelIndex &parent) const;
|
|
|
|
int columnCount(const QModelIndex &parent) const;
|
|
|
|
QVariant data(const QModelIndex &index, int role) const;
|
|
|
|
void setDataSourceManager(DataSourceManager* dataManager);
|
|
|
|
private slots:
|
|
|
|
void slotDatasourcesChanged();
|
|
|
|
private:
|
|
|
|
DataNode* nodeFromIndex(const QModelIndex &index) const;
|
|
|
|
void fillFields(DataNode* parent);
|
|
|
|
void updateModel();
|
|
|
|
private:
|
|
|
|
DataSourceManager* m_dataManager;
|
|
|
|
DataNode* m_rootNode;
|
|
|
|
};
|
|
|
|
|
|
|
|
class DataSourceManager : public QObject, public ICollectionContainer, public IVariablesContainer, public IDataSourceManager
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
Q_PROPERTY(ACollectionProperty connections READ fakeCollectionReader)
|
|
|
|
Q_PROPERTY(ACollectionProperty queries READ fakeCollectionReader)
|
|
|
|
Q_PROPERTY(ACollectionProperty subqueries READ fakeCollectionReader)
|
|
|
|
Q_PROPERTY(ACollectionProperty subproxies READ fakeCollectionReader)
|
|
|
|
Q_PROPERTY(ACollectionProperty variables READ fakeCollectionReader)
|
2019-04-04 04:37:18 +03:00
|
|
|
Q_PROPERTY(ACollectionProperty csvs READ fakeCollectionReader)
|
2016-02-17 10:11:00 +03:00
|
|
|
friend class ReportEnginePrivate;
|
|
|
|
friend class ReportRender;
|
|
|
|
public:
|
|
|
|
typedef QHash<QString,IDataSourceHolder*> DataSourcesMap;
|
|
|
|
enum ClearMethod {All,Owned};
|
|
|
|
~DataSourceManager();
|
|
|
|
void connectAllDatabases();
|
|
|
|
void addConnection(const QString& connectionName);
|
|
|
|
void addConnectionDesc(ConnectionDesc *);
|
2016-02-25 00:27:11 +03:00
|
|
|
bool checkConnectionDesc(ConnectionDesc *connection);
|
2016-02-17 10:11:00 +03:00
|
|
|
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);
|
2019-04-04 04:37:18 +03:00
|
|
|
void addProxy(const QString& name, const QString& master, const QString& detail, QList<FieldsCorrelation> fields);
|
|
|
|
void addCSV(const QString& name, const QString& csvText, const QString& separator, bool firstRowIsHeader);
|
2016-02-17 10:39:17 +03:00
|
|
|
bool addModel(const QString& name, QAbstractItemModel *model, bool owned);
|
|
|
|
void removeModel(const QString& name);
|
2017-04-08 19:53:05 +03:00
|
|
|
ICallbackDatasource* createCallbackDatasource(const QString &name);
|
2017-02-16 04:11:39 +03:00
|
|
|
void registerDbCredentialsProvider(IDbCredentialsProvider *provider);
|
2016-02-17 10:11:00 +03:00
|
|
|
void addCallbackDatasource(ICallbackDatasource *datasource, const QString &name);
|
|
|
|
void setReportVariable(const QString& name, const QVariant& value);
|
|
|
|
void deleteVariable(const QString& name);
|
|
|
|
bool containsVariable(const QString& variableName);
|
|
|
|
void clearUserVariables();
|
2016-06-07 00:44:21 +03:00
|
|
|
void addVariable(const QString& name, const QVariant& value, VarDesc::VarType type=VarDesc::User, RenderPass pass=FirstPass);
|
|
|
|
void changeVariable(const QString& name,const QVariant& value);
|
2017-09-26 00:19:59 +03:00
|
|
|
QVariant variable(const QString& variableName);
|
|
|
|
RenderPass variablePass(const QString& name);
|
2016-02-17 10:11:00 +03:00
|
|
|
QStringList variableNames();
|
2017-08-31 02:53:34 +03:00
|
|
|
QStringList variableNamesByRenderPass(RenderPass pass);
|
2017-09-26 00:19:59 +03:00
|
|
|
QStringList userVariableNames();
|
|
|
|
VarDesc::VarType variableType(const QString& name);
|
|
|
|
VariableDataType variableDataType(const QString& name);
|
|
|
|
void setVariableDataType(const QString &name, VariableDataType value);
|
2016-02-17 10:11:00 +03:00
|
|
|
bool variableIsSystem(const QString& name);
|
2017-09-26 00:19:59 +03:00
|
|
|
bool variableIsMandatory(const QString& name);
|
|
|
|
void setVarableMandatory(const QString &name, bool value);
|
2016-02-17 10:11:00 +03:00
|
|
|
QString queryText(const QString& dataSourceName);
|
|
|
|
QString connectionName(const QString& dataSourceName);
|
|
|
|
void removeDatasource(const QString& name);
|
2017-02-15 00:10:02 +03:00
|
|
|
void removeConnection(const QString& connectionName);
|
2016-02-17 10:11:00 +03:00
|
|
|
bool isQuery(const QString& dataSourceName);
|
|
|
|
bool containsDatasource(const QString& dataSourceName);
|
|
|
|
bool isSubQuery(const QString& dataSourceName);
|
|
|
|
bool isProxy(const QString& dataSourceName);
|
2019-04-04 04:37:18 +03:00
|
|
|
bool isCSV(const QString& datasourceName);
|
2016-02-17 10:11:00 +03:00
|
|
|
bool isConnection(const QString& connectionName);
|
|
|
|
bool isConnectionConnected(const QString& connectionName);
|
|
|
|
bool connectConnection(const QString &connectionName);
|
2016-02-25 02:04:06 +03:00
|
|
|
void connectAutoConnections();
|
2016-02-17 10:11:00 +03:00
|
|
|
void disconnectConnection(const QString &connectionName);
|
2019-04-04 04:37:18 +03:00
|
|
|
QueryDesc* queryByName(const QString& datasourceName);
|
|
|
|
SubQueryDesc* subQueryByName(const QString& datasourceName);
|
|
|
|
ProxyDesc* proxyByName(const QString& datasourceName);
|
|
|
|
CSVDesc* csvByName(const QString& datasourceName);
|
2016-02-17 10:11:00 +03:00
|
|
|
ConnectionDesc *connectionByName(const QString& connectionName);
|
|
|
|
int queryIndexByName(const QString& dataSourceName);
|
|
|
|
int subQueryIndexByName(const QString& dataSourceName);
|
|
|
|
int proxyIndexByName(const QString& dataSourceName);
|
2019-04-04 04:37:18 +03:00
|
|
|
int csvIndexByName(const QString& dataSourceName);
|
2016-02-17 10:11:00 +03:00
|
|
|
int connectionIndexByName(const QString& connectionName);
|
|
|
|
|
2017-02-16 04:11:39 +03:00
|
|
|
QList<ConnectionDesc *> &conections();
|
2016-02-17 10:11:00 +03:00
|
|
|
bool dataSourceIsValid(const QString& name);
|
|
|
|
IDataSource* dataSource(const QString& name);
|
|
|
|
IDataSourceHolder* dataSourceHolder(const QString& name);
|
|
|
|
QStringList dataSourceNames();
|
|
|
|
QStringList dataSourceNames(const QString& connectionName);
|
|
|
|
QStringList connectionNames();
|
|
|
|
QStringList fieldNames(const QString& datasourceName);
|
2017-09-19 21:02:55 +03:00
|
|
|
bool containsField(const QString& fieldName);
|
|
|
|
QVariant fieldData(const QString& fieldName);
|
2019-02-19 02:43:16 +03:00
|
|
|
QVariant fieldDataByRowIndex(const QString& fieldName, int rowIndex);
|
Add: added a function to get an arbitrary role of a model item
Example:
$D{appdata.Column_1}
$S{
var vRow = line('DataBand1') - 1;
// 8 - Qt::BackgroundRole
var vColor = getFieldByRowIndexEx('appdata.Column_1', vRow, 8);
THIS.backgroundColor = LimeReport.color('lightgray');
if(vColor > '')
{
THIS.backgroundColor = vColor;
}
''
}
Added several functions to get extended information from the model
- getFieldByRowIndexEx2(fieldName, rowIndex, roleName), default:
Qt::DisplayRole
- getHeaderData(fieldName, roleName), default: Qt::DisplayRole
- getHeaderColumnNameByIndex(datasourceName, columnIndex), default:
Qt::UserRole or Qt::DisplayRole
- getColumnCount(datasourceName), default: -1
2024-03-26 11:43:58 +03:00
|
|
|
QVariant fieldDataByRowIndex(const QString &fieldName, int rowIndex, int role);
|
|
|
|
QVariant fieldDataByRowIndex(const QString &fieldName, int rowIndex, const QString &roleName);
|
2017-09-19 21:02:55 +03:00
|
|
|
QVariant fieldDataByKey(
|
2017-08-31 02:53:34 +03:00
|
|
|
const QString& datasourceName,
|
|
|
|
const QString& valueFieldName,
|
|
|
|
const QString& keyFieldName,
|
|
|
|
QVariant keyValue
|
|
|
|
);
|
Add: added a function to get an arbitrary role of a model item
Example:
$D{appdata.Column_1}
$S{
var vRow = line('DataBand1') - 1;
// 8 - Qt::BackgroundRole
var vColor = getFieldByRowIndexEx('appdata.Column_1', vRow, 8);
THIS.backgroundColor = LimeReport.color('lightgray');
if(vColor > '')
{
THIS.backgroundColor = vColor;
}
''
}
Added several functions to get extended information from the model
- getFieldByRowIndexEx2(fieldName, rowIndex, roleName), default:
Qt::DisplayRole
- getHeaderData(fieldName, roleName), default: Qt::DisplayRole
- getHeaderColumnNameByIndex(datasourceName, columnIndex), default:
Qt::UserRole or Qt::DisplayRole
- getColumnCount(datasourceName), default: -1
2024-03-26 11:43:58 +03:00
|
|
|
QVariant headerData(const QString &fieldName, const QString &roleName);
|
|
|
|
QString columnName(const QString &datasourceName, int index);
|
|
|
|
int columnCount(const QString &datasourceName);
|
2017-09-19 21:02:55 +03:00
|
|
|
void reopenDatasource(const QString& datasourceName);
|
2016-02-17 10:11:00 +03:00
|
|
|
|
|
|
|
QString extractDataSource(const QString& fieldName);
|
|
|
|
QString extractFieldName(const QString& fieldName);
|
|
|
|
void setAllDatasourcesToFirst();
|
|
|
|
void clear(ClearMethod method);
|
|
|
|
void clearGroupFunction();
|
|
|
|
void clearGroupFunctionValues(const QString &bandObjectName);
|
|
|
|
|
|
|
|
QList<QString> groupFunctionNames(){return m_groupFunctionFactory.functionNames();}
|
|
|
|
GroupFunction* addGroupFunction(const QString& name, const QString& expression, const QString& band, const QString &dataBand);
|
|
|
|
GroupFunction* groupFunction(const QString& name, const QString &expression, const QString& band);
|
|
|
|
QList<GroupFunction*> groupFunctionsByBand(const QString& band);
|
|
|
|
void updateChildrenData(const QString& datasourceName);
|
|
|
|
|
|
|
|
DataSourceModel* datasourcesModel(){return &m_datasourcesModel;}
|
|
|
|
QString lastError() const { return m_lastError;}
|
|
|
|
|
|
|
|
void putError(QString error){ if (!m_errorsList.contains(error)) m_errorsList.append(error);}
|
2016-06-24 23:12:33 +03:00
|
|
|
void clearErrors(){ m_errorsList.clear(); m_lastError = "";}
|
2016-02-17 10:11:00 +03:00
|
|
|
QStringList errorsList(){ return m_errorsList;}
|
|
|
|
bool designTime() const;
|
|
|
|
void setDesignTime(bool designTime);
|
2016-02-17 10:19:50 +03:00
|
|
|
|
|
|
|
QString extractField(QString source);
|
|
|
|
QString replaceVariables(QString query, QMap<QString, QString> &aliasesToParam);
|
2016-02-25 00:27:11 +03:00
|
|
|
QString replaceVariables(QString value);
|
2016-02-17 10:19:50 +03:00
|
|
|
QString replaceFields(QString query, QMap<QString, QString> &aliasesToParam, QString masterDatasource = "");
|
|
|
|
QSharedPointer<QAbstractItemModel> previewSQL(const QString& connectionName, const QString& sqlText, QString masterDatasource="");
|
2016-04-13 23:04:56 +03:00
|
|
|
void updateDatasourceModel();
|
|
|
|
bool isNeedUpdateDatasourceModel(){ return m_needUpdate;}
|
2016-11-08 01:09:35 +03:00
|
|
|
QString defaultDatabasePath() const;
|
|
|
|
void setDefaultDatabasePath(const QString &defaultDatabasePath);
|
|
|
|
|
2017-01-28 02:20:15 +03:00
|
|
|
QString putGroupFunctionsExpressions(QString expression);
|
|
|
|
void clearGroupFuntionsExpressions();
|
|
|
|
QString getExpression(QString index);
|
|
|
|
|
|
|
|
ReportSettings *reportSettings() const;
|
|
|
|
void setReportSettings(ReportSettings *reportSettings);
|
|
|
|
|
2019-02-05 21:51:46 +03:00
|
|
|
bool hasChanges(){ return m_hasChanges; }
|
2019-01-31 21:01:55 +03:00
|
|
|
void dropChanges(){ m_hasChanges = false; }
|
2016-02-17 10:11:00 +03:00
|
|
|
signals:
|
|
|
|
void loadCollectionFinished(const QString& collectionName);
|
|
|
|
void cleared();
|
|
|
|
void datasourcesChanged();
|
|
|
|
protected:
|
2016-07-07 23:49:54 +03:00
|
|
|
void putHolder(const QString& name, LimeReport::IDataSourceHolder* dataSource);
|
2016-02-17 10:11:00 +03:00
|
|
|
void putQueryDesc(QueryDesc *queryDesc);
|
|
|
|
void putSubQueryDesc(SubQueryDesc *subQueryDesc);
|
|
|
|
void putProxyDesc(ProxyDesc *proxyDesc);
|
2019-04-04 04:37:18 +03:00
|
|
|
void putCSVDesc(CSVDesc* csvDesc);
|
2016-02-17 10:11:00 +03:00
|
|
|
bool connectConnection(ConnectionDesc* connectionDesc);
|
2016-06-07 00:44:21 +03:00
|
|
|
void clearReportVariables();
|
2016-02-17 10:39:17 +03:00
|
|
|
QList<QString> childDatasources(const QString& datasourceName);
|
|
|
|
void invalidateChildren(const QString& parentDatasourceName);
|
2016-02-17 10:11:00 +03:00
|
|
|
//ICollectionContainer
|
|
|
|
virtual QObject* createElement(const QString& collectionName,const QString&);
|
|
|
|
virtual int elementsCount(const QString& collectionName);
|
|
|
|
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);
|
2017-02-11 00:24:59 +03:00
|
|
|
bool checkConnection(QSqlDatabase db);
|
2017-09-19 21:02:55 +03:00
|
|
|
void invalidateQueriesContainsVariable(const QString& variableName);
|
2016-02-17 10:11:00 +03:00
|
|
|
private slots:
|
|
|
|
void slotConnectionRenamed(const QString& oldName,const QString& newName);
|
2016-07-07 23:49:54 +03:00
|
|
|
void slotQueryTextChanged(const QString& queryName, const QString& queryText);
|
2017-09-19 21:02:55 +03:00
|
|
|
void slotVariableHasBeenAdded(const QString& variableName);
|
|
|
|
void slotVariableHasBeenChanged(const QString& variableName);
|
2019-04-04 04:37:18 +03:00
|
|
|
void slotCSVTextChanged(const QString& csvName, const QString& csvText);
|
2016-02-17 10:11:00 +03:00
|
|
|
private:
|
|
|
|
explicit DataSourceManager(QObject *parent = 0);
|
2016-11-17 00:37:37 +03:00
|
|
|
bool initAndOpenDB(QSqlDatabase &db, ConnectionDesc &connectionDesc);
|
2016-02-17 10:11:00 +03:00
|
|
|
Q_DISABLE_COPY(DataSourceManager)
|
|
|
|
private:
|
|
|
|
QList<ConnectionDesc*> m_connections;
|
|
|
|
QList<QueryDesc*> m_queries;
|
|
|
|
QList<SubQueryDesc*> m_subqueries;
|
|
|
|
QList<ProxyDesc*> m_proxies;
|
|
|
|
QList<VarDesc*> m_tempVars;
|
2019-04-04 04:37:18 +03:00
|
|
|
QList<CSVDesc*> m_csvs;
|
2016-02-17 10:11:00 +03:00
|
|
|
|
|
|
|
QMultiMap<QString,GroupFunction*> m_groupFunctions;
|
|
|
|
GroupFunctionFactory m_groupFunctionFactory;
|
2016-06-07 00:44:21 +03:00
|
|
|
VariablesHolder m_reportVariables;
|
|
|
|
VariablesHolder m_userVariables;
|
2016-02-17 10:11:00 +03:00
|
|
|
DataSourcesMap m_datasources;
|
|
|
|
DataSourceModel m_datasourcesModel;
|
|
|
|
QString m_lastError;
|
|
|
|
QStringList m_errorsList;
|
|
|
|
bool m_designTime;
|
2016-04-13 23:04:56 +03:00
|
|
|
bool m_needUpdate;
|
2016-11-08 01:09:35 +03:00
|
|
|
QString m_defaultDatabasePath;
|
2017-01-28 02:20:15 +03:00
|
|
|
ReportSettings* m_reportSettings;
|
|
|
|
QHash<QString,int> m_groupFunctionsExpressionsMap;
|
|
|
|
QVector<QString> m_groupFunctionsExpressions;
|
2017-02-16 04:11:39 +03:00
|
|
|
IDbCredentialsProvider* m_dbCredentialsProvider;
|
2019-10-16 02:05:25 +03:00
|
|
|
|
2019-10-16 13:43:23 +03:00
|
|
|
QMap< QString, QVector<QString> > m_varToDataSource;
|
2019-10-16 02:05:25 +03:00
|
|
|
|
2019-01-31 21:01:55 +03:00
|
|
|
bool m_hasChanges;
|
2016-02-17 10:11:00 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif // LRDATASOURCEMANAGER_H
|
2017-01-28 02:20:15 +03:00
|
|
|
|