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. *
|
|
|
|
****************************************************************************/
|
|
|
|
#include "lrdatasourcemanager.h"
|
2024-09-04 17:31:16 +03:00
|
|
|
|
2016-02-17 10:11:00 +03:00
|
|
|
#include "lrdatadesignintf.h"
|
2024-09-04 17:31:16 +03:00
|
|
|
|
2016-02-17 10:19:50 +03:00
|
|
|
#include <QSqlQuery>
|
2024-09-04 17:31:16 +03:00
|
|
|
#include <QStringList>
|
2021-12-16 00:13:39 +03:00
|
|
|
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 1))
|
2016-02-17 10:11:00 +03:00
|
|
|
#include <QRegExp>
|
2021-08-24 10:22:30 +03:00
|
|
|
#endif
|
2024-09-04 17:31:16 +03:00
|
|
|
#include <QFileInfo>
|
2016-02-17 10:11:00 +03:00
|
|
|
#include <QSqlError>
|
|
|
|
#include <QSqlQueryModel>
|
2024-09-04 17:31:16 +03:00
|
|
|
|
2016-02-17 10:11:00 +03:00
|
|
|
#include <stdexcept>
|
|
|
|
|
2018-05-07 22:52:28 +03:00
|
|
|
#ifdef BUILD_WITH_EASY_PROFILER
|
2018-02-28 23:19:04 +03:00
|
|
|
#include "easy/profiler.h"
|
2018-03-22 23:00:51 +03:00
|
|
|
#else
|
2024-09-04 17:31:16 +03:00
|
|
|
#define EASY_BLOCK(...)
|
|
|
|
#define EASY_END_BLOCK
|
2018-03-22 23:00:51 +03:00
|
|
|
#endif
|
2018-02-28 23:19:04 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
namespace LimeReport {
|
2016-02-17 10:11:00 +03:00
|
|
|
|
|
|
|
DataNode::~DataNode()
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
for (int i = 0; i < m_childs.count(); ++i) {
|
2016-02-17 10:11:00 +03:00
|
|
|
delete m_childs[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
DataNode* DataNode::addChild(const QString& name, DataNode::NodeType type, const QIcon& icon)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
DataNode* res = new DataNode(name, type, this, icon);
|
2016-02-17 10:11:00 +03:00
|
|
|
m_childs.push_back(res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DataNode::row()
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (m_parent) {
|
2016-02-17 10:11:00 +03:00
|
|
|
return m_parent->m_childs.indexOf(const_cast<DataNode*>(this));
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataNode::clear()
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
for (int i = 0; i < m_childs.count(); ++i) {
|
2016-02-17 10:11:00 +03:00
|
|
|
delete m_childs[i];
|
|
|
|
}
|
|
|
|
m_childs.clear();
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
DataSourceModel::DataSourceModel(DataSourceManager* dataManager):
|
|
|
|
m_dataManager(0),
|
|
|
|
m_rootNode(new DataNode())
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
|
|
|
setDataSourceManager(dataManager);
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
DataSourceModel::~DataSourceModel() { delete m_rootNode; }
|
2016-02-17 10:11:00 +03:00
|
|
|
|
|
|
|
QModelIndex DataSourceModel::index(int row, int column, const QModelIndex& parent) const
|
|
|
|
{
|
|
|
|
if (!m_rootNode)
|
|
|
|
return QModelIndex();
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!hasIndex(row, column, parent))
|
2016-02-17 10:11:00 +03:00
|
|
|
return QModelIndex();
|
|
|
|
|
|
|
|
DataNode* parentNode;
|
2024-09-04 17:31:16 +03:00
|
|
|
if (parent.isValid()) {
|
2016-02-17 10:11:00 +03:00
|
|
|
parentNode = nodeFromIndex(parent);
|
|
|
|
} else {
|
|
|
|
parentNode = m_rootNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataNode* childNode = parentNode->child(row);
|
2024-09-04 17:31:16 +03:00
|
|
|
if (childNode) {
|
|
|
|
return createIndex(row, column, childNode);
|
|
|
|
} else
|
|
|
|
return QModelIndex();
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex DataSourceModel::parent(const QModelIndex& child) const
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!child.isValid())
|
|
|
|
return QModelIndex();
|
2016-02-17 10:11:00 +03:00
|
|
|
|
|
|
|
DataNode* childNode = nodeFromIndex(child);
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!childNode)
|
|
|
|
return QModelIndex();
|
2016-02-17 10:11:00 +03:00
|
|
|
|
|
|
|
DataNode* parentNode = childNode->parent();
|
2024-09-04 17:31:16 +03:00
|
|
|
if ((parentNode == m_rootNode) || (!parentNode))
|
|
|
|
return QModelIndex();
|
|
|
|
return createIndex(parentNode->row(), 0, parentNode);
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int DataSourceModel::rowCount(const QModelIndex& parent) const
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!m_rootNode)
|
|
|
|
return 0;
|
2016-02-17 10:11:00 +03:00
|
|
|
DataNode* parentNode;
|
|
|
|
if (parent.isValid())
|
|
|
|
parentNode = nodeFromIndex(parent);
|
|
|
|
else
|
|
|
|
parentNode = m_rootNode;
|
|
|
|
return parentNode->childCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
int DataSourceModel::columnCount(const QModelIndex& parent) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(parent)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant DataSourceModel::data(const QModelIndex& index, int role) const
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
DataNode* node = nodeFromIndex(index);
|
2016-02-17 10:11:00 +03:00
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!node)
|
|
|
|
return QVariant();
|
2016-02-17 10:11:00 +03:00
|
|
|
return node->name();
|
|
|
|
break;
|
2024-09-04 17:31:16 +03:00
|
|
|
case Qt::DecorationRole:
|
|
|
|
if (!node)
|
|
|
|
return QIcon();
|
2016-02-17 10:11:00 +03:00
|
|
|
return node->icon();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataSourceModel::setDataSourceManager(DataSourceManager* dataManager)
|
|
|
|
{
|
|
|
|
m_dataManager = dataManager;
|
2024-09-04 17:31:16 +03:00
|
|
|
connect(m_dataManager, SIGNAL(datasourcesChanged()), this, SLOT(slotDatasourcesChanged()));
|
2016-02-17 10:11:00 +03:00
|
|
|
updateModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataSourceModel::slotDatasourcesChanged()
|
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
updateModel();
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
DataNode* DataSourceModel::nodeFromIndex(const QModelIndex& index) const
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (index.isValid()) {
|
2016-02-17 10:11:00 +03:00
|
|
|
return static_cast<DataNode*>(index.internalPointer());
|
2024-09-04 17:31:16 +03:00
|
|
|
} else
|
|
|
|
return m_rootNode;
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataSourceModel::fillFields(DataNode* parent)
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (QString name, m_dataManager->fieldNames(parent->name())) {
|
|
|
|
parent->addChild(name, DataNode::Field, QIcon(":/report/images/field"));
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataSourceModel::updateModel()
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
QMap<QString, DataNode*> connections;
|
2016-02-17 10:11:00 +03:00
|
|
|
|
|
|
|
m_rootNode->clear();
|
2024-09-04 17:31:16 +03:00
|
|
|
DataNode* ds = m_rootNode->addChild(tr("Datasources"), DataNode::DataSources,
|
|
|
|
QIcon(":/report/images/databases"));
|
2016-02-17 10:11:00 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (QString name, m_dataManager->connectionNames()) {
|
|
|
|
DataNode* connection
|
|
|
|
= ds->addChild(name, DataNode::Connection, QIcon(":/report/images/database"));
|
|
|
|
connections.insert(name, connection);
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach (QString name, m_dataManager->dataSourceNames()) {
|
|
|
|
DataNode* datasource;
|
2024-09-04 17:31:16 +03:00
|
|
|
if (m_dataManager->isQuery(name)) {
|
|
|
|
DataNode* connection
|
|
|
|
= connections.value(m_dataManager->queryByName(name)->connectionName());
|
|
|
|
if (connection)
|
|
|
|
datasource
|
|
|
|
= connection->addChild(name, DataNode::Query, QIcon(":/report/images/table"));
|
|
|
|
else
|
|
|
|
datasource = ds->addChild(name, DataNode::Query);
|
|
|
|
} else if (m_dataManager->isSubQuery(name)) {
|
|
|
|
DataNode* connection
|
|
|
|
= connections.value(m_dataManager->subQueryByName(name)->connectionName());
|
|
|
|
if (connection)
|
|
|
|
datasource = connection->addChild(name, DataNode::SubQuery,
|
|
|
|
QIcon(":/report/images/table"));
|
|
|
|
else
|
|
|
|
datasource = ds->addChild(name, DataNode::SubQuery);
|
2016-02-17 10:11:00 +03:00
|
|
|
} else {
|
2024-09-04 17:31:16 +03:00
|
|
|
datasource = ds->addChild(name, DataNode::Model, QIcon(":/report/images/table"));
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
fillFields(datasource);
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
DataNode* vars = m_rootNode->addChild(tr("Variables"), DataNode::Variables,
|
|
|
|
QIcon(":/report/images/folder"));
|
|
|
|
foreach (QString name, m_dataManager->variableNames()) {
|
|
|
|
vars->addChild(name, DataNode::Variable, QIcon(":/report/images/value"));
|
|
|
|
}
|
|
|
|
|
|
|
|
vars = m_rootNode->addChild(tr("External variables"), DataNode::Variables,
|
|
|
|
QIcon(":/report/images/folder"));
|
|
|
|
foreach (QString name, m_dataManager->userVariableNames()) {
|
|
|
|
vars->addChild(name, DataNode::Variable, QIcon(":/report/images/value"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DataSourceManager::DataSourceManager(QObject* parent):
|
|
|
|
QObject(parent),
|
|
|
|
m_lastError(""),
|
|
|
|
m_designTime(false),
|
|
|
|
m_needUpdate(false),
|
|
|
|
m_dbCredentialsProvider(0),
|
|
|
|
m_hasChanges(false)
|
|
|
|
{
|
|
|
|
m_groupFunctionFactory.registerFunctionCreator(
|
|
|
|
QLatin1String("COUNT"), new ConstructorGroupFunctionCreator<CountGroupFunction>);
|
|
|
|
m_groupFunctionFactory.registerFunctionCreator(
|
|
|
|
QLatin1String("SUM"), new ConstructorGroupFunctionCreator<SumGroupFunction>);
|
|
|
|
m_groupFunctionFactory.registerFunctionCreator(
|
|
|
|
QLatin1String("AVG"), new ConstructorGroupFunctionCreator<AvgGroupFunction>);
|
|
|
|
m_groupFunctionFactory.registerFunctionCreator(
|
|
|
|
QLatin1String("MIN"), new ConstructorGroupFunctionCreator<MinGroupFunction>);
|
|
|
|
m_groupFunctionFactory.registerFunctionCreator(
|
|
|
|
QLatin1String("MAX"), new ConstructorGroupFunctionCreator<MaxGroupFunction>);
|
|
|
|
setSystemVariable(QLatin1String("#PAGE"), 1, SecondPass);
|
|
|
|
setSystemVariable(QLatin1String("#PAGE_COUNT"), 0, SecondPass);
|
|
|
|
setSystemVariable(QLatin1String("#IS_LAST_PAGEFOOTER"), false, FirstPass);
|
|
|
|
setSystemVariable(QLatin1String("#IS_FIRST_PAGEFOOTER"), false, FirstPass);
|
2016-02-17 10:11:00 +03:00
|
|
|
m_datasourcesModel.setDataSourceManager(this);
|
2017-09-19 21:02:55 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
connect(&m_reportVariables, SIGNAL(variableHasBeenAdded(QString)), this,
|
|
|
|
SLOT(slotVariableHasBeenAdded(QString)));
|
|
|
|
connect(&m_reportVariables, SIGNAL(variableHasBeenChanged(QString)), this,
|
|
|
|
SLOT(slotVariableHasBeenChanged(QString)));
|
|
|
|
connect(&m_userVariables, SIGNAL(variableHasBeenAdded(QString)), this,
|
|
|
|
SLOT(slotVariableHasBeenAdded(QString)));
|
|
|
|
connect(&m_userVariables, SIGNAL(variableHasBeenChanged(QString)), this,
|
|
|
|
SLOT(slotVariableHasBeenChanged(QString)));
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
2016-11-08 01:09:35 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QString DataSourceManager::defaultDatabasePath() const { return m_defaultDatabasePath; }
|
2016-11-08 01:09:35 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::setDefaultDatabasePath(const QString& defaultDatabasePath)
|
2016-11-08 01:09:35 +03:00
|
|
|
{
|
|
|
|
m_defaultDatabasePath = defaultDatabasePath;
|
|
|
|
}
|
2017-01-28 02:20:15 +03:00
|
|
|
|
|
|
|
QString DataSourceManager::putGroupFunctionsExpressions(QString expression)
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (m_groupFunctionsExpressionsMap.contains(expression)) {
|
2017-01-28 02:20:15 +03:00
|
|
|
return QString::number(m_groupFunctionsExpressionsMap.value(expression));
|
|
|
|
} else {
|
|
|
|
m_groupFunctionsExpressions.append(expression);
|
2024-09-04 17:31:16 +03:00
|
|
|
m_groupFunctionsExpressionsMap.insert(expression, m_groupFunctionsExpressions.size() - 1);
|
|
|
|
return QString::number(m_groupFunctionsExpressions.size() - 1);
|
2017-01-28 02:20:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataSourceManager::clearGroupFuntionsExpressions()
|
|
|
|
{
|
|
|
|
m_groupFunctionsExpressionsMap.clear();
|
|
|
|
m_groupFunctionsExpressions.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString DataSourceManager::getExpression(QString index)
|
|
|
|
{
|
2017-06-07 23:47:43 +03:00
|
|
|
bool ok = false;
|
|
|
|
int i = index.toInt(&ok);
|
2024-09-04 17:31:16 +03:00
|
|
|
if (ok && m_groupFunctionsExpressions.size() > i)
|
2017-06-07 23:47:43 +03:00
|
|
|
return m_groupFunctionsExpressions.at(index.toInt());
|
2024-09-04 17:31:16 +03:00
|
|
|
else
|
|
|
|
return "";
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
bool DataSourceManager::designTime() const { return m_designTime; }
|
2016-02-17 10:11:00 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::setDesignTime(bool designTime) { m_designTime = designTime; }
|
2016-02-17 10:11:00 +03:00
|
|
|
|
|
|
|
DataSourceManager::~DataSourceManager()
|
|
|
|
{
|
|
|
|
clear(All);
|
|
|
|
clearGroupFunction();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataSourceManager::connectAllDatabases()
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (ConnectionDesc* conn, m_connections) {
|
|
|
|
try {
|
2016-02-17 10:11:00 +03:00
|
|
|
connectConnection(conn);
|
2024-09-04 17:31:16 +03:00
|
|
|
} catch (ReportError& e) {
|
2016-02-17 10:11:00 +03:00
|
|
|
putError(e.what());
|
|
|
|
setLastError(e.what());
|
2024-09-04 17:31:16 +03:00
|
|
|
qDebug() << e.what();
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
bool DataSourceManager::addModel(const QString& name, QAbstractItemModel* model, bool owned)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2016-02-17 10:39:17 +03:00
|
|
|
if (m_datasources.contains(name.toLower()))
|
|
|
|
removeDatasource(name.toLower());
|
2024-09-04 17:31:16 +03:00
|
|
|
ModelHolder* mh = new ModelHolder(model, owned);
|
|
|
|
try {
|
2016-02-17 10:39:17 +03:00
|
|
|
putHolder(name, mh);
|
|
|
|
connect(mh, SIGNAL(modelStateChanged()), this, SIGNAL(datasourcesChanged()));
|
2024-09-04 17:31:16 +03:00
|
|
|
} catch (ReportError& e) {
|
2016-02-17 10:39:17 +03:00
|
|
|
putError(e.what());
|
|
|
|
setLastError(e.what());
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-17 10:11:00 +03:00
|
|
|
emit datasourcesChanged();
|
2016-02-17 10:39:17 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::removeModel(const QString& name)
|
2016-02-17 10:39:17 +03:00
|
|
|
{
|
|
|
|
if (m_datasources.contains(name.toLower()))
|
|
|
|
removeDatasource(name.toLower());
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
ICallbackDatasource* DataSourceManager::createCallbackDatasource(const QString& name)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
|
|
|
ICallbackDatasource* ds = new CallbackDatasource();
|
2024-09-04 17:31:16 +03:00
|
|
|
IDataSourceHolder* holder = new CallbackDatasourceHolder(dynamic_cast<IDataSource*>(ds), true);
|
|
|
|
putHolder(name, holder);
|
2016-02-17 10:11:00 +03:00
|
|
|
emit datasourcesChanged();
|
2016-04-13 23:04:56 +03:00
|
|
|
m_needUpdate = true;
|
2016-02-17 10:11:00 +03:00
|
|
|
return ds;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::registerDbCredentialsProvider(IDbCredentialsProvider* provider)
|
2017-02-16 04:11:39 +03:00
|
|
|
{
|
|
|
|
m_dbCredentialsProvider = provider;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::addCallbackDatasource(ICallbackDatasource* datasource, const QString& name)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
|
|
|
IDataSource* datasourceIntf = dynamic_cast<IDataSource*>(datasource);
|
2024-09-04 17:31:16 +03:00
|
|
|
if (datasourceIntf) {
|
|
|
|
IDataSourceHolder* holder = new CallbackDatasourceHolder(datasourceIntf, true);
|
|
|
|
putHolder(name, holder);
|
2016-02-17 10:11:00 +03:00
|
|
|
emit datasourcesChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QSharedPointer<QAbstractItemModel> DataSourceManager::previewSQL(const QString& connectionName,
|
|
|
|
const QString& sqlText,
|
|
|
|
QString masterDatasource)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(connectionName);
|
2016-02-17 10:19:50 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
if (db.isValid() && db.isOpen()) {
|
2016-02-17 10:19:50 +03:00
|
|
|
|
2016-02-17 10:11:00 +03:00
|
|
|
QSqlQueryModel* model = new QSqlQueryModel();
|
2024-09-04 17:31:16 +03:00
|
|
|
QMap<QString, QString> aliasesToParam;
|
|
|
|
QString queryText = replaceVariables(sqlText, aliasesToParam);
|
|
|
|
queryText = replaceFields(queryText, aliasesToParam, masterDatasource);
|
2016-02-17 10:19:50 +03:00
|
|
|
QSqlQuery query(db);
|
|
|
|
query.prepare(queryText);
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (QString param, aliasesToParam.keys()) {
|
2016-02-17 10:19:50 +03:00
|
|
|
QVariant value;
|
2024-09-04 17:31:16 +03:00
|
|
|
if (param.contains(".")) {
|
2016-02-17 10:19:50 +03:00
|
|
|
value = fieldData(aliasesToParam.value(param));
|
2024-09-04 17:31:16 +03:00
|
|
|
param = param.right(param.length() - param.indexOf('.') - 1);
|
2016-02-17 10:19:50 +03:00
|
|
|
} else {
|
|
|
|
value = variable(aliasesToParam.value(param));
|
|
|
|
}
|
|
|
|
if (value.isValid() || m_designTime)
|
2024-09-04 17:31:16 +03:00
|
|
|
query.bindValue(':' + param, value);
|
2016-02-17 10:19:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
query.exec();
|
2022-07-19 00:43:54 +03:00
|
|
|
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0)
|
|
|
|
model->setQuery(std::move(query));
|
|
|
|
#else
|
2016-02-17 10:19:50 +03:00
|
|
|
model->setQuery(query);
|
2022-07-19 00:43:54 +03:00
|
|
|
#endif
|
|
|
|
|
2016-02-17 10:11:00 +03:00
|
|
|
m_lastError = model->lastError().text();
|
2016-06-24 23:12:33 +03:00
|
|
|
putError(m_lastError);
|
2016-02-17 10:11:00 +03:00
|
|
|
if (model->query().isActive())
|
|
|
|
return QSharedPointer<QAbstractItemModel>(model);
|
|
|
|
else
|
|
|
|
return QSharedPointer<QAbstractItemModel>(0);
|
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!db.isOpen()) {
|
2016-02-17 10:11:00 +03:00
|
|
|
m_lastError = tr("Connection \"%1\" is not open").arg(connectionName);
|
2016-06-24 23:12:33 +03:00
|
|
|
putError(m_lastError);
|
|
|
|
}
|
2016-02-17 10:11:00 +03:00
|
|
|
return QSharedPointer<QAbstractItemModel>(0);
|
|
|
|
}
|
|
|
|
|
2016-04-13 23:04:56 +03:00
|
|
|
void DataSourceManager::updateDatasourceModel()
|
|
|
|
{
|
|
|
|
m_datasourcesModel.updateModel();
|
2017-08-18 22:55:29 +03:00
|
|
|
emit datasourcesChanged();
|
2016-04-13 23:04:56 +03:00
|
|
|
m_needUpdate = false;
|
|
|
|
}
|
|
|
|
|
2016-02-17 10:19:50 +03:00
|
|
|
QString DataSourceManager::extractField(QString source)
|
|
|
|
{
|
|
|
|
if (source.contains('.')) {
|
2024-09-04 17:31:16 +03:00
|
|
|
return source.right(source.length() - (source.indexOf('.') + 1));
|
2016-02-17 10:19:50 +03:00
|
|
|
}
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QString DataSourceManager::replaceVariables(QString value)
|
|
|
|
{
|
2022-07-16 02:33:39 +03:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 1)
|
|
|
|
QRegularExpression rx = getVariableRegEx();
|
2021-11-02 22:30:29 +03:00
|
|
|
QRegularExpressionMatchIterator iter = rx.globalMatch(value);
|
|
|
|
qsizetype pos = 0;
|
|
|
|
QString result;
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
QRegularExpressionMatch match = iter.next();
|
|
|
|
QString var = match.captured(0);
|
|
|
|
var.remove("$V{");
|
|
|
|
var.remove("}");
|
|
|
|
result += value.mid(pos, match.capturedStart(0));
|
2024-09-04 17:31:16 +03:00
|
|
|
if (variable(var).isValid()) {
|
2021-11-02 22:30:29 +03:00
|
|
|
result += variable(var).toString();
|
|
|
|
} else {
|
|
|
|
result += QString(tr("Variable \"%1\" not found!").arg(var));
|
|
|
|
}
|
|
|
|
pos = match.capturedEnd(0);
|
|
|
|
}
|
|
|
|
result += value.mid(pos);
|
|
|
|
return result;
|
|
|
|
#else
|
2016-02-25 00:27:11 +03:00
|
|
|
QRegExp rx(Const::VARIABLE_RX);
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
if (value.contains(rx)) {
|
2016-02-25 00:27:11 +03:00
|
|
|
int pos = -1;
|
2024-09-04 17:31:16 +03:00
|
|
|
while ((pos = rx.indexIn(value)) != -1) {
|
|
|
|
QString var = rx.cap(0);
|
2016-02-25 00:27:11 +03:00
|
|
|
var.remove("$V{");
|
|
|
|
var.remove("}");
|
2016-11-08 01:09:35 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
if (variable(var).isValid()) {
|
|
|
|
value.replace(pos, rx.cap(0).length(), variable(var).toString());
|
2016-02-25 02:04:06 +03:00
|
|
|
} else {
|
2024-09-04 17:31:16 +03:00
|
|
|
value.replace(pos, rx.cap(0).length(),
|
|
|
|
QString(tr("Variable \"%1\" not found!").arg(var)));
|
2016-02-25 00:27:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
return value;
|
2021-08-24 10:22:30 +03:00
|
|
|
#endif
|
|
|
|
return QString();
|
2016-02-25 00:27:11 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QString DataSourceManager::replaceVariables(QString query, QMap<QString, QString>& aliasesToParam)
|
2016-02-17 10:19:50 +03:00
|
|
|
{
|
2022-07-16 02:33:39 +03:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 1)
|
|
|
|
QRegularExpression rx = getVariableRegEx();
|
2021-11-02 22:30:29 +03:00
|
|
|
int curentAliasIndex = 0;
|
2024-09-04 17:31:16 +03:00
|
|
|
if (query.contains(rx)) {
|
2021-11-02 22:30:29 +03:00
|
|
|
int pos = -1;
|
|
|
|
QRegularExpressionMatch match = rx.match(query);
|
2024-09-04 17:31:16 +03:00
|
|
|
while ((pos = match.capturedStart()) != -1) {
|
2021-11-02 22:30:29 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QString var = match.captured(0);
|
2021-11-02 22:30:29 +03:00
|
|
|
var.remove("$V{");
|
|
|
|
var.remove("}");
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!match.captured(1).isEmpty()) {
|
|
|
|
if (aliasesToParam.contains(var)) {
|
2021-11-02 22:30:29 +03:00
|
|
|
curentAliasIndex++;
|
2024-09-04 17:31:16 +03:00
|
|
|
aliasesToParam.insert(var + "_v_alias" + QString::number(curentAliasIndex),
|
|
|
|
var);
|
|
|
|
var += "_v_alias" + QString::number(curentAliasIndex);
|
2021-11-02 22:30:29 +03:00
|
|
|
} else {
|
2024-09-04 17:31:16 +03:00
|
|
|
aliasesToParam.insert(var, var);
|
2021-11-02 22:30:29 +03:00
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
query.replace(pos, match.captured(0).length(), ":" + var);
|
2021-11-02 22:30:29 +03:00
|
|
|
} else {
|
|
|
|
QString varName = match.captured(2).trimmed();
|
|
|
|
QString varParam = match.captured(3).trimmed();
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!varName.isEmpty()) {
|
|
|
|
if (!varParam.isEmpty() && varParam.compare("nobind") == 0) {
|
|
|
|
query.replace(pos, match.captured(0).length(),
|
|
|
|
variable(varName).toString());
|
2021-11-02 22:30:29 +03:00
|
|
|
} else {
|
2024-09-04 17:31:16 +03:00
|
|
|
query.replace(
|
|
|
|
pos, match.captured(0).length(),
|
|
|
|
QString(tr("Unknown parameter \"%1\" for variable \"%2\" found!")
|
|
|
|
.arg(varName)
|
|
|
|
.arg(varParam)));
|
2021-11-02 22:30:29 +03:00
|
|
|
}
|
|
|
|
} else {
|
2024-09-04 17:31:16 +03:00
|
|
|
query.replace(pos, match.captured(0).length(),
|
|
|
|
QString(tr("Variable \"%1\" not found!").arg(var)));
|
2021-11-02 22:30:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
match = rx.match(query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2016-02-17 10:19:50 +03:00
|
|
|
QRegExp rx(Const::VARIABLE_RX);
|
|
|
|
int curentAliasIndex = 0;
|
2024-09-04 17:31:16 +03:00
|
|
|
if (query.contains(rx)) {
|
2016-02-17 10:19:50 +03:00
|
|
|
int pos = -1;
|
2024-09-04 17:31:16 +03:00
|
|
|
while ((pos = rx.indexIn(query)) != -1) {
|
2016-02-17 10:19:50 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QString var = rx.cap(0);
|
2016-02-17 10:19:50 +03:00
|
|
|
var.remove("$V{");
|
|
|
|
var.remove("}");
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!rx.cap(1).isEmpty()) {
|
|
|
|
if (aliasesToParam.contains(var)) {
|
2019-01-30 22:49:20 +03:00
|
|
|
curentAliasIndex++;
|
2024-09-04 17:31:16 +03:00
|
|
|
aliasesToParam.insert(var + "_v_alias" + QString::number(curentAliasIndex),
|
|
|
|
var);
|
|
|
|
var += "_v_alias" + QString::number(curentAliasIndex);
|
2019-01-30 22:49:20 +03:00
|
|
|
} else {
|
2024-09-04 17:31:16 +03:00
|
|
|
aliasesToParam.insert(var, var);
|
2019-01-30 22:49:20 +03:00
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
query.replace(pos, rx.cap(0).length(), ":" + var);
|
2016-02-17 10:19:50 +03:00
|
|
|
} else {
|
2019-01-30 22:49:20 +03:00
|
|
|
QString varName = rx.cap(2).trimmed();
|
|
|
|
QString varParam = rx.cap(3).trimmed();
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!varName.isEmpty()) {
|
|
|
|
if (!varParam.isEmpty() && varParam.compare("nobind") == 0) {
|
|
|
|
query.replace(pos, rx.cap(0).length(), variable(varName).toString());
|
2019-01-30 22:49:20 +03:00
|
|
|
} else {
|
2024-09-04 17:31:16 +03:00
|
|
|
query.replace(
|
|
|
|
pos, rx.cap(0).length(),
|
|
|
|
QString(tr("Unknown parameter \"%1\" for variable \"%2\" found!")
|
|
|
|
.arg(varName)
|
|
|
|
.arg(varParam)));
|
2019-01-30 22:49:20 +03:00
|
|
|
}
|
|
|
|
} else {
|
2024-09-04 17:31:16 +03:00
|
|
|
query.replace(pos, rx.cap(0).length(),
|
|
|
|
QString(tr("Variable \"%1\" not found!").arg(var)));
|
2019-01-30 22:49:20 +03:00
|
|
|
}
|
2016-02-17 10:19:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-24 10:22:30 +03:00
|
|
|
#endif
|
2021-11-02 22:30:29 +03:00
|
|
|
return query;
|
2016-02-17 10:19:50 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QString DataSourceManager::replaceFields(QString query, QMap<QString, QString>& aliasesToParam,
|
|
|
|
QString masterDatasource)
|
2016-02-17 10:19:50 +03:00
|
|
|
{
|
2022-07-16 02:33:39 +03:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 1)
|
|
|
|
QRegularExpression rx = getFieldRegEx();
|
2021-11-02 22:30:29 +03:00
|
|
|
int curentAliasIndex = 0;
|
2024-09-04 17:31:16 +03:00
|
|
|
if (query.contains(rx)) {
|
2021-11-02 22:30:29 +03:00
|
|
|
int pos = -1;
|
|
|
|
QRegularExpressionMatch match = rx.match(query);
|
2024-09-04 17:31:16 +03:00
|
|
|
while ((pos = match.capturedStart()) != -1) {
|
2021-11-02 22:30:29 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QString field = match.captured(0);
|
2021-11-02 22:30:29 +03:00
|
|
|
field.remove("$D{");
|
|
|
|
field.remove("}");
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!aliasesToParam.contains(field)) {
|
2021-11-02 22:30:29 +03:00
|
|
|
if (field.contains("."))
|
|
|
|
aliasesToParam.insert(field, field);
|
|
|
|
else
|
2024-09-04 17:31:16 +03:00
|
|
|
aliasesToParam.insert(field, masterDatasource + "." + field);
|
2021-11-02 22:30:29 +03:00
|
|
|
} else {
|
|
|
|
curentAliasIndex++;
|
|
|
|
if (field.contains("."))
|
2024-09-04 17:31:16 +03:00
|
|
|
aliasesToParam.insert(field + "_f_alias" + QString::number(curentAliasIndex),
|
|
|
|
field);
|
2021-11-02 22:30:29 +03:00
|
|
|
else
|
2024-09-04 17:31:16 +03:00
|
|
|
aliasesToParam.insert(field + "_f_alias" + QString::number(curentAliasIndex),
|
|
|
|
masterDatasource + "." + field);
|
|
|
|
field += "_f_alias" + QString::number(curentAliasIndex);
|
2021-11-02 22:30:29 +03:00
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
query.replace(pos, match.capturedLength(), ":" + extractField(field));
|
2021-11-02 22:30:29 +03:00
|
|
|
match = rx.match(query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2016-02-17 10:19:50 +03:00
|
|
|
QRegExp rx(Const::FIELD_RX);
|
2024-09-04 17:31:16 +03:00
|
|
|
if (query.contains(rx)) {
|
|
|
|
int curentAliasIndex = 0;
|
2016-02-17 10:19:50 +03:00
|
|
|
int pos;
|
2024-09-04 17:31:16 +03:00
|
|
|
while ((pos = rx.indexIn(query)) != -1) {
|
|
|
|
QString field = rx.cap(0);
|
2016-02-17 10:19:50 +03:00
|
|
|
field.remove("$D{");
|
|
|
|
field.remove("}");
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!aliasesToParam.contains(field)) {
|
2016-02-17 10:19:50 +03:00
|
|
|
if (field.contains("."))
|
2024-09-04 17:31:16 +03:00
|
|
|
aliasesToParam.insert(field, field);
|
2016-02-17 10:19:50 +03:00
|
|
|
else
|
2024-09-04 17:31:16 +03:00
|
|
|
aliasesToParam.insert(field, masterDatasource + "." + field);
|
2016-02-17 10:19:50 +03:00
|
|
|
} else {
|
|
|
|
curentAliasIndex++;
|
|
|
|
if (field.contains("."))
|
2024-09-04 17:31:16 +03:00
|
|
|
aliasesToParam.insert(field + "_f_alias" + QString::number(curentAliasIndex),
|
|
|
|
field);
|
2016-02-17 10:19:50 +03:00
|
|
|
else
|
2024-09-04 17:31:16 +03:00
|
|
|
aliasesToParam.insert(field + "_f_alias" + QString::number(curentAliasIndex),
|
|
|
|
masterDatasource + "." + field);
|
|
|
|
field += "_f_alias" + QString::number(curentAliasIndex);
|
2016-02-17 10:19:50 +03:00
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
query.replace(pos, rx.cap(0).length(), ":" + extractField(field));
|
2016-02-17 10:19:50 +03:00
|
|
|
}
|
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
return query;
|
2021-08-24 10:22:30 +03:00
|
|
|
#endif
|
2021-11-02 22:30:29 +03:00
|
|
|
return query;
|
2016-02-17 10:19:50 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::setReportVariable(const QString& name, const QVariant& value)
|
|
|
|
{
|
|
|
|
if (!containsVariable(name)) {
|
|
|
|
addVariable(name, value);
|
|
|
|
} else
|
|
|
|
changeVariable(name, value);
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::addQuery(const QString& name, const QString& sqlText,
|
|
|
|
const QString& connectionName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
QueryDesc* queryDecs = new QueryDesc(name, sqlText, connectionName);
|
2016-02-17 10:11:00 +03:00
|
|
|
putQueryDesc(queryDecs);
|
2024-09-04 17:31:16 +03:00
|
|
|
putHolder(name, new QueryHolder(sqlText, connectionName, this));
|
2019-01-31 21:01:55 +03:00
|
|
|
m_hasChanges = true;
|
2019-10-16 02:05:25 +03:00
|
|
|
m_varToDataSource.clear();
|
2016-02-17 10:11:00 +03:00
|
|
|
emit datasourcesChanged();
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::addSubQuery(const QString& name, const QString& sqlText,
|
|
|
|
const QString& connectionName, const QString& masterDatasource)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
SubQueryDesc* subQueryDesc
|
|
|
|
= new SubQueryDesc(name.toLower(), sqlText, connectionName, masterDatasource);
|
2016-02-17 10:11:00 +03:00
|
|
|
putSubQueryDesc(subQueryDesc);
|
2024-09-04 17:31:16 +03:00
|
|
|
putHolder(name, new SubQueryHolder(sqlText, connectionName, masterDatasource, this));
|
2019-01-31 21:01:55 +03:00
|
|
|
m_hasChanges = true;
|
2019-10-16 02:05:25 +03:00
|
|
|
m_varToDataSource.clear();
|
2016-02-17 10:11:00 +03:00
|
|
|
emit datasourcesChanged();
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::addProxy(const QString& name, const QString& master, const QString& detail,
|
|
|
|
QList<FieldsCorrelation> fields)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
ProxyDesc* proxyDesc = new ProxyDesc();
|
2016-02-17 10:11:00 +03:00
|
|
|
proxyDesc->setName(name);
|
|
|
|
proxyDesc->setMaster(master);
|
|
|
|
proxyDesc->setDetail(detail);
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (FieldsCorrelation correlation, fields) {
|
2016-02-17 10:11:00 +03:00
|
|
|
proxyDesc->addFieldsCorrelation(correlation);
|
|
|
|
}
|
|
|
|
putProxyDesc(proxyDesc);
|
2024-09-04 17:31:16 +03:00
|
|
|
putHolder(name, new ProxyHolder(proxyDesc, this));
|
2019-01-31 21:01:55 +03:00
|
|
|
m_hasChanges = true;
|
2016-02-17 10:11:00 +03:00
|
|
|
emit datasourcesChanged();
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::addCSV(const QString& name, const QString& csvText,
|
|
|
|
const QString& separator, bool firstRowIsHeader)
|
2019-04-04 04:37:18 +03:00
|
|
|
{
|
|
|
|
CSVDesc* csvDesc = new CSVDesc(name, csvText, separator, firstRowIsHeader);
|
|
|
|
putCSVDesc(csvDesc);
|
|
|
|
putHolder(name, new CSVHolder(*csvDesc, this));
|
|
|
|
m_hasChanges = true;
|
|
|
|
emit datasourcesChanged();
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QString DataSourceManager::queryText(const QString& dataSourceName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (isQuery(dataSourceName))
|
|
|
|
return queryByName(dataSourceName)->queryText();
|
|
|
|
if (isSubQuery(dataSourceName))
|
|
|
|
return subQueryByName(dataSourceName)->queryText();
|
|
|
|
else
|
|
|
|
return QString();
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QueryDesc* DataSourceManager::queryByName(const QString& datasourceName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2019-04-04 04:37:18 +03:00
|
|
|
int queryIndex = queryIndexByName(datasourceName);
|
2024-09-04 17:31:16 +03:00
|
|
|
if (queryIndex != -1)
|
|
|
|
return m_queries.at(queryIndex);
|
2016-02-17 10:11:00 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
SubQueryDesc* DataSourceManager::subQueryByName(const QString& datasourceName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2019-04-04 04:37:18 +03:00
|
|
|
int queryIndex = subQueryIndexByName(datasourceName);
|
2024-09-04 17:31:16 +03:00
|
|
|
if (queryIndex != -1)
|
|
|
|
return m_subqueries.at(queryIndex);
|
2016-02-17 10:11:00 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
ConnectionDesc* DataSourceManager::connectionByName(const QString& connectionName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
|
|
|
int queryIndex = connectionIndexByName(connectionName);
|
2024-09-04 17:31:16 +03:00
|
|
|
if (queryIndex != -1)
|
|
|
|
return m_connections.at(queryIndex);
|
2016-02-17 10:11:00 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
int DataSourceManager::queryIndexByName(const QString& dataSourceName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
for (int i = 0; i < m_queries.count(); i++) {
|
|
|
|
QueryDesc* desc = m_queries.at(i);
|
|
|
|
if (desc->queryName().compare(dataSourceName, Qt::CaseInsensitive) == 0)
|
|
|
|
return i;
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
int DataSourceManager::subQueryIndexByName(const QString& dataSourceName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
for (int i = 0; i < m_subqueries.count(); ++i) {
|
|
|
|
QueryDesc* desc = m_subqueries.at(i);
|
|
|
|
if (desc->queryName().compare(dataSourceName, Qt::CaseInsensitive) == 0)
|
|
|
|
return i;
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
int DataSourceManager::proxyIndexByName(const QString& dataSourceName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
for (int i = 0; i < m_proxies.count(); ++i) {
|
|
|
|
ProxyDesc* desc = m_proxies.at(i);
|
|
|
|
if (desc->name().compare(dataSourceName, Qt::CaseInsensitive) == 0)
|
|
|
|
return i;
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
int DataSourceManager::csvIndexByName(const QString& dataSourceName)
|
2019-04-04 04:37:18 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
for (int i = 0; i < m_csvs.count(); ++i) {
|
|
|
|
CSVDesc* desc = m_csvs.at(i);
|
|
|
|
if (desc->name().compare(dataSourceName, Qt::CaseInsensitive) == 0)
|
|
|
|
return i;
|
2019-04-04 04:37:18 +03:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
int DataSourceManager::connectionIndexByName(const QString& connectionName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
for (int i = 0; i < m_connections.count(); ++i) {
|
|
|
|
ConnectionDesc* desc = m_connections.at(i);
|
|
|
|
if (desc->name().compare(connectionName, Qt::CaseInsensitive) == 0)
|
|
|
|
return i;
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QList<ConnectionDesc*>& DataSourceManager::conections() { return m_connections; }
|
2017-02-16 04:11:39 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
bool DataSourceManager::dataSourceIsValid(const QString& name)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (m_datasources.value(name.toLower()))
|
|
|
|
return !m_datasources.value(name.toLower())->isInvalid();
|
|
|
|
else
|
|
|
|
throw ReportError(tr("Datasource \"%1\" not found!").arg(name));
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
bool DataSourceManager::isQuery(const QString& dataSourceName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
|
|
|
return (queryByName(dataSourceName));
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QString DataSourceManager::connectionName(const QString& dataSourceName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (isQuery(dataSourceName))
|
|
|
|
return queryByName(dataSourceName)->connectionName();
|
|
|
|
if (isSubQuery(dataSourceName))
|
|
|
|
return subQueryByName(dataSourceName)->connectionName();
|
2016-02-17 10:11:00 +03:00
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
ProxyDesc* DataSourceManager::proxyByName(const QString& datasourceName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
|
|
|
int proxyIndex = proxyIndexByName(datasourceName);
|
2024-09-04 17:31:16 +03:00
|
|
|
if (proxyIndex > -1)
|
|
|
|
return m_proxies.at(proxyIndex);
|
|
|
|
else
|
|
|
|
return 0;
|
2019-04-04 04:37:18 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
CSVDesc* DataSourceManager::csvByName(const QString& datasourceName)
|
2019-04-04 04:37:18 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
int csvIndex = csvIndexByName(datasourceName);
|
|
|
|
if (csvIndex > -1)
|
|
|
|
return m_csvs.at(csvIndex);
|
|
|
|
else
|
|
|
|
return 0;
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::removeDatasource(const QString& name)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (m_datasources.contains(name)) {
|
|
|
|
IDataSourceHolder* holder;
|
|
|
|
holder = m_datasources.value(name);
|
2016-02-17 10:11:00 +03:00
|
|
|
m_datasources.remove(name);
|
|
|
|
delete holder;
|
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
if (isQuery(name)) {
|
|
|
|
int queryIndex = queryIndexByName(name);
|
2016-02-17 10:11:00 +03:00
|
|
|
delete m_queries.at(queryIndex);
|
|
|
|
m_queries.removeAt(queryIndex);
|
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
if (isSubQuery(name)) {
|
|
|
|
int queryIndex = subQueryIndexByName(name);
|
2016-02-17 10:11:00 +03:00
|
|
|
delete m_subqueries.at(queryIndex);
|
|
|
|
m_subqueries.removeAt(queryIndex);
|
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
if (isProxy(name)) {
|
|
|
|
int proxyIndex = proxyIndexByName(name);
|
2016-02-17 10:11:00 +03:00
|
|
|
delete m_proxies.at(proxyIndex);
|
|
|
|
m_proxies.removeAt(proxyIndex);
|
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
if (isCSV(name)) {
|
|
|
|
int csvIndex = csvIndexByName(name);
|
2019-04-04 04:37:18 +03:00
|
|
|
delete m_csvs.at(csvIndex);
|
|
|
|
m_csvs.removeAt(csvIndex);
|
|
|
|
}
|
2020-08-16 17:54:44 +03:00
|
|
|
invalidateLinkedDatasources(name);
|
2019-01-31 21:01:55 +03:00
|
|
|
m_hasChanges = true;
|
2016-02-17 10:11:00 +03:00
|
|
|
emit datasourcesChanged();
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::removeConnection(const QString& connectionName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2017-02-15 02:52:11 +03:00
|
|
|
QList<ConnectionDesc*>::iterator cit = m_connections.begin();
|
2024-09-04 17:31:16 +03:00
|
|
|
while (cit != m_connections.end()) {
|
|
|
|
if (((*cit)->name().compare(connectionName) == 0)) {
|
|
|
|
if ((*cit)->isInternal()) {
|
2017-02-15 00:10:02 +03:00
|
|
|
QSqlDatabase db = QSqlDatabase::database(connectionName);
|
|
|
|
db.close();
|
|
|
|
}
|
2017-02-15 02:52:11 +03:00
|
|
|
QSqlDatabase::removeDatabase(connectionName);
|
2017-10-27 20:10:59 +03:00
|
|
|
delete (*cit);
|
|
|
|
cit = m_connections.erase(cit);
|
|
|
|
} else {
|
2019-06-27 10:45:26 +03:00
|
|
|
++cit;
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
2019-01-31 21:01:55 +03:00
|
|
|
m_hasChanges = true;
|
2016-02-17 10:11:00 +03:00
|
|
|
emit datasourcesChanged();
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::addConnectionDesc(ConnectionDesc* connection)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
|
|
|
if (!isConnection(connection->name())) {
|
2024-09-04 17:31:16 +03:00
|
|
|
connect(connection, SIGNAL(nameChanged(QString, QString)), this,
|
|
|
|
SLOT(slotConnectionRenamed(QString, QString)));
|
2016-02-17 10:11:00 +03:00
|
|
|
m_connections.append(connection);
|
2019-01-31 21:01:55 +03:00
|
|
|
m_hasChanges = true;
|
2024-09-04 17:31:16 +03:00
|
|
|
if (connection->autoconnect()) {
|
|
|
|
try {
|
|
|
|
connectConnection(connection);
|
|
|
|
} catch (ReportError& exception) {
|
|
|
|
qDebug() << exception.what();
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2024-09-04 17:31:16 +03:00
|
|
|
throw ReportError(
|
|
|
|
tr("Connection with name \"%1\" already exists!").arg(connection->name()));
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
bool DataSourceManager::checkConnectionDesc(ConnectionDesc* connection)
|
2016-02-25 00:27:11 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (connectConnection(connection)) {
|
|
|
|
if (connection->isInternal()) {
|
2017-02-15 00:10:02 +03:00
|
|
|
QSqlDatabase::removeDatabase(connection->name());
|
2024-09-04 17:31:16 +03:00
|
|
|
if (designTime())
|
|
|
|
emit datasourcesChanged();
|
2017-02-21 22:16:09 +03:00
|
|
|
}
|
2016-02-25 00:27:11 +03:00
|
|
|
return true;
|
|
|
|
}
|
2017-02-15 00:10:02 +03:00
|
|
|
if (connection->isInternal())
|
|
|
|
QSqlDatabase::removeDatabase(connection->name());
|
2016-02-25 00:27:11 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::putHolder(const QString& name, IDataSourceHolder* dataSource)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!m_datasources.contains(name.toLower())) {
|
|
|
|
m_datasources.insert(name.toLower(), dataSource);
|
|
|
|
} else
|
|
|
|
throw ReportError(tr("Datasource with name \"%1\" already exists!").arg(name));
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataSourceManager::putQueryDesc(QueryDesc* queryDesc)
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!containsDatasource(queryDesc->queryName())) {
|
2016-02-17 10:11:00 +03:00
|
|
|
m_queries.append(queryDesc);
|
2024-09-04 17:31:16 +03:00
|
|
|
connect(queryDesc, SIGNAL(queryTextChanged(QString, QString)), this,
|
|
|
|
SLOT(slotQueryTextChanged(QString, QString)));
|
|
|
|
} else
|
|
|
|
throw ReportError(
|
|
|
|
tr("Datasource with name \"%1\" already exists!").arg(queryDesc->queryName()));
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::putSubQueryDesc(SubQueryDesc* subQueryDesc)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!containsDatasource(subQueryDesc->queryName())) {
|
2016-02-17 10:11:00 +03:00
|
|
|
m_subqueries.append(subQueryDesc);
|
2024-09-04 17:31:16 +03:00
|
|
|
connect(subQueryDesc, SIGNAL(queryTextChanged(QString, QString)), this,
|
|
|
|
SLOT(slotQueryTextChanged(QString, QString)));
|
|
|
|
} else
|
|
|
|
throw ReportError(
|
|
|
|
tr("Datasource with name \"%1\" already exists!").arg(subQueryDesc->queryName()));
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::putProxyDesc(ProxyDesc* proxyDesc)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!containsDatasource(proxyDesc->name())) {
|
2016-02-17 10:11:00 +03:00
|
|
|
m_proxies.append(proxyDesc);
|
2024-09-04 17:31:16 +03:00
|
|
|
} else
|
|
|
|
throw ReportError(tr("Datasource with name \"%1\" already exists!").arg(proxyDesc->name()));
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::putCSVDesc(CSVDesc* csvDesc)
|
2019-04-04 04:37:18 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!containsDatasource(csvDesc->name())) {
|
2019-04-04 04:37:18 +03:00
|
|
|
m_csvs.append(csvDesc);
|
2024-09-04 17:31:16 +03:00
|
|
|
connect(csvDesc, SIGNAL(cvsTextChanged(QString, QString)), this,
|
|
|
|
SLOT(slotCSVTextChanged(QString, QString)));
|
|
|
|
} else
|
|
|
|
throw ReportError(tr("Datasource with name \"%1\" already exists!").arg(csvDesc->name()));
|
2019-04-04 04:37:18 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
bool DataSourceManager::initAndOpenDB(QSqlDatabase& db, ConnectionDesc& connectionDesc)
|
|
|
|
{
|
2016-11-17 00:37:37 +03:00
|
|
|
|
|
|
|
bool connected = false;
|
2017-02-16 04:11:39 +03:00
|
|
|
|
2016-11-17 00:37:37 +03:00
|
|
|
db.setHostName(replaceVariables(connectionDesc.host()));
|
|
|
|
db.setUserName(replaceVariables(connectionDesc.userName()));
|
|
|
|
db.setPassword(replaceVariables(connectionDesc.password()));
|
2020-05-08 14:19:02 +03:00
|
|
|
db.setDatabaseName(replaceVariables(connectionDesc.databaseName()));
|
2024-09-04 17:31:16 +03:00
|
|
|
if (connectionDesc.port() != "")
|
2020-05-08 14:19:02 +03:00
|
|
|
db.setPort(replaceVariables(connectionDesc.port()).toInt());
|
2016-11-17 00:37:37 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!connectionDesc.keepDBCredentials() && m_dbCredentialsProvider) {
|
2017-02-16 04:11:39 +03:00
|
|
|
if (!m_dbCredentialsProvider->getUserName(connectionDesc.name()).isEmpty())
|
|
|
|
db.setUserName(m_dbCredentialsProvider->getUserName(connectionDesc.name()));
|
|
|
|
if (!m_dbCredentialsProvider->getPassword(connectionDesc.name()).isEmpty())
|
|
|
|
db.setPassword(m_dbCredentialsProvider->getPassword(connectionDesc.name()));
|
|
|
|
}
|
|
|
|
|
2016-11-17 00:37:37 +03:00
|
|
|
QString dbName = replaceVariables(connectionDesc.databaseName());
|
2024-09-04 17:31:16 +03:00
|
|
|
if (connectionDesc.driver().compare("QSQLITE") == 0) {
|
|
|
|
if (!defaultDatabasePath().isEmpty()) {
|
|
|
|
dbName = !QFileInfo(dbName).exists()
|
|
|
|
? defaultDatabasePath() + QFileInfo(dbName).fileName()
|
|
|
|
: dbName;
|
2016-11-17 00:37:37 +03:00
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
if (QFileInfo(dbName).exists()) {
|
2016-11-17 00:37:37 +03:00
|
|
|
db.setDatabaseName(dbName);
|
|
|
|
} else {
|
|
|
|
setLastError(tr("Database \"%1\" not found").arg(dbName));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
db.setDatabaseName(dbName);
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
connected = db.open();
|
|
|
|
if (!connected)
|
|
|
|
setLastError(db.lastError().text());
|
|
|
|
return connected;
|
2016-11-17 00:37:37 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
ReportSettings* DataSourceManager::reportSettings() const { return m_reportSettings; }
|
2017-01-28 02:20:15 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::setReportSettings(ReportSettings* reportSettings)
|
2017-01-28 02:20:15 +03:00
|
|
|
{
|
|
|
|
m_reportSettings = reportSettings;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
bool DataSourceManager::checkConnection(QSqlDatabase db)
|
|
|
|
{
|
|
|
|
QSqlQuery query("Select 1", db);
|
2017-02-11 00:24:59 +03:00
|
|
|
return query.first();
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
bool DataSourceManager::connectConnection(ConnectionDesc* connectionDesc)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2016-11-17 00:37:37 +03:00
|
|
|
|
2016-02-17 10:11:00 +03:00
|
|
|
bool connected = false;
|
2016-06-24 23:12:33 +03:00
|
|
|
clearErrors();
|
2024-09-04 17:31:16 +03:00
|
|
|
QString lastError = "";
|
2016-02-17 10:19:50 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (QString datasourceName, dataSourceNames()) {
|
2016-02-17 10:39:17 +03:00
|
|
|
dataSourceHolder(datasourceName)->clearErrors();
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!QSqlDatabase::contains(connectionDesc->name())) {
|
2017-02-16 04:11:39 +03:00
|
|
|
QString dbError;
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
QSqlDatabase db
|
|
|
|
= QSqlDatabase::addDatabase(connectionDesc->driver(), connectionDesc->name());
|
2017-02-16 04:11:39 +03:00
|
|
|
connectionDesc->setInternal(true);
|
2024-09-04 17:31:16 +03:00
|
|
|
connected = initAndOpenDB(db, *connectionDesc);
|
2017-02-16 04:11:39 +03:00
|
|
|
dbError = db.lastError().text();
|
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!connected) {
|
2017-02-16 04:11:39 +03:00
|
|
|
if (!dbError.trimmed().isEmpty())
|
|
|
|
setLastError(dbError);
|
|
|
|
QSqlDatabase::removeDatabase(connectionDesc->name());
|
2016-11-17 00:37:37 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(connectionDesc->name());
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!connectionDesc->isEqual(db) && connectionDesc->isInternal()) {
|
2016-11-17 00:37:37 +03:00
|
|
|
db.close();
|
|
|
|
connected = initAndOpenDB(db, *connectionDesc);
|
|
|
|
} else {
|
2017-02-11 00:24:59 +03:00
|
|
|
connected = checkConnection(db);
|
2017-02-15 00:10:02 +03:00
|
|
|
if (!connected && connectionDesc->isInternal())
|
2017-02-11 00:24:59 +03:00
|
|
|
connected = initAndOpenDB(db, *connectionDesc);
|
2016-11-17 00:37:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!connected) {
|
2017-02-15 00:10:02 +03:00
|
|
|
if (connectionDesc->isInternal())
|
|
|
|
QSqlDatabase::removeDatabase(connectionDesc->name());
|
2016-11-17 00:37:37 +03:00
|
|
|
return false;
|
|
|
|
} else {
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (QString datasourceName, dataSourceNames()) {
|
|
|
|
if (isQuery(datasourceName)) {
|
|
|
|
QueryHolder* qh = dynamic_cast<QueryHolder*>(dataSourceHolder(datasourceName));
|
|
|
|
if (qh) {
|
|
|
|
qh->invalidate(designTime() ? IDataSource::DESIGN_MODE
|
|
|
|
: IDataSource::RENDER_MODE);
|
|
|
|
invalidateChildren(datasourceName);
|
|
|
|
}
|
2016-02-17 10:28:27 +03:00
|
|
|
}
|
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (QString datasourceName, dataSourceNames()) {
|
|
|
|
if (isProxy(datasourceName)) {
|
|
|
|
ProxyHolder* ph = dynamic_cast<ProxyHolder*>(dataSourceHolder(datasourceName));
|
|
|
|
if (ph) {
|
|
|
|
ph->invalidate(designTime() ? IDataSource::DESIGN_MODE
|
|
|
|
: IDataSource::RENDER_MODE);
|
|
|
|
}
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
if (designTime())
|
|
|
|
emit datasourcesChanged();
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::clearReportVariables() { m_reportVariables.clearUserVariables(); }
|
2016-06-07 00:44:21 +03:00
|
|
|
|
2016-02-25 02:04:06 +03:00
|
|
|
void DataSourceManager::connectAutoConnections()
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (ConnectionDesc* conn, m_connections) {
|
2016-02-25 02:04:06 +03:00
|
|
|
if (conn->autoconnect()) {
|
|
|
|
try {
|
|
|
|
connectConnection(conn);
|
2024-09-04 17:31:16 +03:00
|
|
|
} catch (ReportError& e) {
|
2016-02-25 02:04:06 +03:00
|
|
|
setLastError(e.what());
|
|
|
|
putError(e.what());
|
2024-09-04 17:31:16 +03:00
|
|
|
qDebug() << e.what();
|
2016-02-25 02:04:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QList<QString> DataSourceManager::childDatasources(const QString& parentDatasourceName)
|
2016-02-17 10:39:17 +03:00
|
|
|
{
|
|
|
|
QList<QString> result;
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (QString datasourceName, dataSourceNames()) {
|
|
|
|
if (isSubQuery(datasourceName)) {
|
2016-02-17 10:39:17 +03:00
|
|
|
SubQueryHolder* sh = dynamic_cast<SubQueryHolder*>(dataSourceHolder(datasourceName));
|
2024-09-04 17:31:16 +03:00
|
|
|
if (sh
|
|
|
|
&& sh->masterDatasource().compare(parentDatasourceName, Qt::CaseInsensitive) == 0) {
|
2016-02-17 10:39:17 +03:00
|
|
|
result.append(datasourceName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::invalidateChildren(const QString& parentDatasourceName)
|
2016-02-17 10:39:17 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (QString datasourceName, childDatasources(parentDatasourceName)) {
|
2016-02-17 10:39:17 +03:00
|
|
|
SubQueryHolder* sh = dynamic_cast<SubQueryHolder*>(dataSourceHolder(datasourceName));
|
2017-03-03 00:55:35 +03:00
|
|
|
if (sh)
|
2024-09-04 17:31:16 +03:00
|
|
|
sh->invalidate(designTime() ? IDataSource::DESIGN_MODE : IDataSource::RENDER_MODE);
|
2016-02-17 10:39:17 +03:00
|
|
|
invalidateChildren(datasourceName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
bool DataSourceManager::containsDatasource(const QString& dataSourceName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
|
|
|
return m_datasources.contains(dataSourceName.toLower());
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
bool DataSourceManager::isSubQuery(const QString& dataSourceName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2019-04-04 04:37:18 +03:00
|
|
|
return subQueryIndexByName(dataSourceName) != -1;
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
bool DataSourceManager::isProxy(const QString& dataSourceName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2019-04-04 04:37:18 +03:00
|
|
|
return proxyIndexByName(dataSourceName) != -1;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
bool DataSourceManager::isCSV(const QString& datasourceName)
|
2019-04-04 04:37:18 +03:00
|
|
|
{
|
|
|
|
return csvIndexByName(datasourceName) != -1;
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
bool DataSourceManager::isConnection(const QString& connectionName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2019-04-04 04:37:18 +03:00
|
|
|
return connectionIndexByName(connectionName) != -1;
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
bool DataSourceManager::isConnectionConnected(const QString& connectionName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (isConnection(connectionName) && QSqlDatabase::contains(connectionName)) {
|
2017-02-16 04:11:39 +03:00
|
|
|
QSqlDatabase db = QSqlDatabase::database(connectionName);
|
|
|
|
return db.isValid() && QSqlDatabase::database(connectionName).isOpen();
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DataSourceManager::connectConnection(const QString& connectionName)
|
|
|
|
{
|
|
|
|
return connectConnection(connectionByName(connectionName));
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataSourceManager::disconnectConnection(const QString& connectionName)
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (QString datasourceName, dataSourceNames()) {
|
|
|
|
if (isQuery(datasourceName) || isSubQuery(datasourceName)) {
|
2016-02-17 10:11:00 +03:00
|
|
|
QueryHolder* qh = dynamic_cast<QueryHolder*>(dataSourceHolder(datasourceName));
|
2024-09-04 17:31:16 +03:00
|
|
|
if (qh && qh->connectionName().compare(connectionName, Qt::CaseInsensitive) == 0) {
|
|
|
|
qh->invalidate(designTime() ? IDataSource::DESIGN_MODE : IDataSource::RENDER_MODE,
|
|
|
|
true);
|
2016-02-17 10:11:00 +03:00
|
|
|
qh->setLastError(tr("invalid connection"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-15 00:10:02 +03:00
|
|
|
|
|
|
|
ConnectionDesc* connectionDesc = connectionByName(connectionName);
|
2020-07-24 10:11:24 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
if (connectionDesc->isInternal()) {
|
2017-02-16 04:11:39 +03:00
|
|
|
{
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(connectionName);
|
2024-09-04 17:31:16 +03:00
|
|
|
if (db.isOpen())
|
|
|
|
db.close();
|
2017-02-16 04:11:39 +03:00
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
if (QSqlDatabase::contains(connectionName))
|
|
|
|
QSqlDatabase::removeDatabase(connectionName);
|
2016-02-17 10:28:27 +03:00
|
|
|
}
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
IDataSource* DataSourceManager::dataSource(const QString& name)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
|
|
|
IDataSourceHolder* holder = m_datasources.value(name.toLower());
|
|
|
|
if (holder) {
|
|
|
|
if (holder->isInvalid()) {
|
2024-09-04 17:31:16 +03:00
|
|
|
setLastError(name + " : " + holder->lastError());
|
2016-02-17 10:11:00 +03:00
|
|
|
return 0;
|
|
|
|
} else {
|
2024-09-04 17:31:16 +03:00
|
|
|
return holder->dataSource(designTime() ? IDataSource::DESIGN_MODE
|
|
|
|
: IDataSource::RENDER_MODE);
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
} else {
|
2017-06-28 16:58:01 +03:00
|
|
|
setLastError(tr("Datasource \"%1\" not found!").arg(name));
|
2016-02-17 10:11:00 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
IDataSourceHolder* DataSourceManager::dataSourceHolder(const QString& name)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (m_datasources.value(name.toLower()))
|
|
|
|
return m_datasources.value(name.toLower());
|
|
|
|
else
|
|
|
|
return 0;
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QStringList DataSourceManager::dataSourceNames()
|
|
|
|
{
|
|
|
|
QStringList result;
|
2024-09-04 17:31:16 +03:00
|
|
|
for (int i = 0; i < m_datasources.keys().count(); i++) {
|
|
|
|
result.append(m_datasources.keys().at(i));
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
result.sort();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList DataSourceManager::dataSourceNames(const QString& connectionName)
|
|
|
|
{
|
|
|
|
QStringList result;
|
|
|
|
foreach (QueryDesc* query, m_queries) {
|
2024-09-04 17:31:16 +03:00
|
|
|
if (query->connectionName().compare(connectionName, Qt::CaseInsensitive) == 0) {
|
2016-02-17 10:11:00 +03:00
|
|
|
result.append(query->queryName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach (QueryDesc* query, m_subqueries) {
|
2024-09-04 17:31:16 +03:00
|
|
|
if (query->connectionName().compare(connectionName, Qt::CaseInsensitive) == 0) {
|
2016-02-17 10:11:00 +03:00
|
|
|
result.append(query->queryName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList DataSourceManager::connectionNames()
|
|
|
|
{
|
|
|
|
QStringList result;
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (ConnectionDesc* conDesc, m_connections) {
|
|
|
|
result.append(conDesc->name());
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QStringList DataSourceManager::fieldNames(const QString& datasourceName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
|
|
|
QStringList result;
|
|
|
|
IDataSource* ds = dataSource(datasourceName);
|
2024-09-04 17:31:16 +03:00
|
|
|
if (ds && !ds->isInvalid()) {
|
|
|
|
for (int i = 0; i < ds->columnCount(); i++) {
|
2016-02-17 10:11:00 +03:00
|
|
|
result.append(ds->columnNameByIndex(i));
|
|
|
|
}
|
|
|
|
result.sort();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::addConnection(const QString& connectionName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
|
|
|
addConnectionDesc(new ConnectionDesc(QSqlDatabase::database(connectionName)));
|
2019-01-31 21:01:55 +03:00
|
|
|
m_hasChanges = true;
|
2016-02-17 10:11:00 +03:00
|
|
|
emit datasourcesChanged();
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QObject* DataSourceManager::createElement(const QString& collectionName, const QString&)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (collectionName == "connections") {
|
2016-02-17 10:11:00 +03:00
|
|
|
ConnectionDesc* connection = new ConnectionDesc;
|
|
|
|
m_connections.append(connection);
|
|
|
|
return connection;
|
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
if (collectionName == "queries") {
|
2016-02-17 10:11:00 +03:00
|
|
|
QueryDesc* queryDesc = new QueryDesc;
|
|
|
|
m_queries.append(queryDesc);
|
|
|
|
return queryDesc;
|
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
if (collectionName == "subqueries") {
|
2016-02-17 10:11:00 +03:00
|
|
|
SubQueryDesc* subQueryDesc = new SubQueryDesc;
|
|
|
|
m_subqueries.append(subQueryDesc);
|
|
|
|
return subQueryDesc;
|
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
if (collectionName == "subproxies") {
|
2016-02-17 10:11:00 +03:00
|
|
|
ProxyDesc* proxyDesc = new ProxyDesc;
|
|
|
|
m_proxies.append(proxyDesc);
|
|
|
|
return proxyDesc;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
if (collectionName == "variables") {
|
2016-02-17 10:11:00 +03:00
|
|
|
VarDesc* var = new VarDesc;
|
|
|
|
m_tempVars.append(var);
|
|
|
|
return var;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
if (collectionName == "csvs") {
|
2019-04-04 04:37:18 +03:00
|
|
|
CSVDesc* csvDesc = new CSVDesc;
|
|
|
|
m_csvs.append(csvDesc);
|
2024-09-04 17:31:16 +03:00
|
|
|
return csvDesc;
|
2019-04-04 04:37:18 +03:00
|
|
|
}
|
|
|
|
|
2016-02-17 10:11:00 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
int DataSourceManager::elementsCount(const QString& collectionName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (collectionName == "connections") {
|
|
|
|
return m_connections.count();
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
if (collectionName == "queries") {
|
2016-02-17 10:11:00 +03:00
|
|
|
return m_queries.count();
|
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
if (collectionName == "subqueries") {
|
2016-02-17 10:11:00 +03:00
|
|
|
return m_subqueries.count();
|
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
if (collectionName == "subproxies") {
|
2016-02-17 10:11:00 +03:00
|
|
|
return m_proxies.count();
|
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
if (collectionName == "variables") {
|
2017-09-26 00:19:59 +03:00
|
|
|
return m_reportVariables.variablesCount();
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
if (collectionName == "csvs") {
|
2019-04-04 04:37:18 +03:00
|
|
|
return m_csvs.count();
|
|
|
|
}
|
2016-02-17 10:11:00 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QObject* DataSourceManager::elementAt(const QString& collectionName, int index)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (collectionName == "connections") {
|
2016-02-17 10:11:00 +03:00
|
|
|
return m_connections.at(index);
|
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
if (collectionName == "queries") {
|
2016-02-17 10:11:00 +03:00
|
|
|
return m_queries.at(index);
|
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
if (collectionName == "subqueries") {
|
2016-02-17 10:11:00 +03:00
|
|
|
return m_subqueries.at(index);
|
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
if (collectionName == "subproxies") {
|
2016-02-17 10:11:00 +03:00
|
|
|
return m_proxies.at(index);
|
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
if (collectionName == "variables") {
|
2017-09-26 00:19:59 +03:00
|
|
|
return m_reportVariables.variableAt(index);
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
if (collectionName == "csvs") {
|
2019-04-04 04:37:18 +03:00
|
|
|
return m_csvs.at(index);
|
|
|
|
}
|
2016-02-17 10:11:00 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::collectionLoadFinished(const QString& collectionName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2018-02-28 23:19:04 +03:00
|
|
|
EASY_BLOCK("DataSourceManager::collectionLoadFinished");
|
2024-09-04 17:31:16 +03:00
|
|
|
if (collectionName.compare("connections", Qt::CaseInsensitive) == 0) { }
|
2018-02-28 23:19:04 +03:00
|
|
|
EASY_BLOCK("queryes");
|
2024-09-04 17:31:16 +03:00
|
|
|
if (collectionName.compare("queries", Qt::CaseInsensitive) == 0) {
|
2017-03-03 00:30:54 +03:00
|
|
|
|
|
|
|
QMutableListIterator<QueryDesc*> it(m_queries);
|
2024-09-04 17:31:16 +03:00
|
|
|
while (it.hasNext()) {
|
2017-03-03 00:30:54 +03:00
|
|
|
it.next();
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!m_datasources.contains(it.value()->queryName().toLower())) {
|
|
|
|
connect(it.value(), SIGNAL(queryTextChanged(QString, QString)), this,
|
|
|
|
SLOT(slotQueryTextChanged(QString, QString)));
|
|
|
|
putHolder(
|
|
|
|
it.value()->queryName(),
|
|
|
|
new QueryHolder(it.value()->queryText(), it.value()->connectionName(), this));
|
2017-03-03 00:30:54 +03:00
|
|
|
} else {
|
|
|
|
delete it.value();
|
|
|
|
it.remove();
|
|
|
|
}
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
2018-02-28 23:19:04 +03:00
|
|
|
EASY_END_BLOCK;
|
|
|
|
EASY_BLOCK("subqueries")
|
2024-09-04 17:31:16 +03:00
|
|
|
if (collectionName.compare("subqueries", Qt::CaseInsensitive) == 0) {
|
2017-03-03 00:30:54 +03:00
|
|
|
|
|
|
|
QMutableListIterator<SubQueryDesc*> it(m_subqueries);
|
2024-09-04 17:31:16 +03:00
|
|
|
while (it.hasNext()) {
|
2017-03-03 00:30:54 +03:00
|
|
|
it.next();
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!m_datasources.contains(it.value()->queryName().toLower())) {
|
|
|
|
connect(it.value(), SIGNAL(queryTextChanged(QString, QString)), this,
|
|
|
|
SLOT(slotQueryTextChanged(QString, QString)));
|
|
|
|
putHolder(it.value()->queryName(),
|
|
|
|
new SubQueryHolder(it.value()->queryText(), it.value()->connectionName(),
|
|
|
|
it.value()->master(), this));
|
2017-03-03 00:30:54 +03:00
|
|
|
} else {
|
|
|
|
delete it.value();
|
|
|
|
it.remove();
|
|
|
|
}
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
2018-02-28 23:19:04 +03:00
|
|
|
EASY_END_BLOCK;
|
|
|
|
EASY_BLOCK("subproxies");
|
2024-09-04 17:31:16 +03:00
|
|
|
if (collectionName.compare("subproxies", Qt::CaseInsensitive) == 0) {
|
2017-03-03 00:30:54 +03:00
|
|
|
QMutableListIterator<ProxyDesc*> it(m_proxies);
|
2024-09-04 17:31:16 +03:00
|
|
|
while (it.hasNext()) {
|
2017-03-03 00:30:54 +03:00
|
|
|
it.next();
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!m_datasources.contains(it.value()->name().toLower())) {
|
|
|
|
putHolder(it.value()->name(), new ProxyHolder(it.value(), this));
|
2017-03-03 00:30:54 +03:00
|
|
|
} else {
|
|
|
|
delete it.value();
|
|
|
|
it.remove();
|
|
|
|
}
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
2018-02-28 23:19:04 +03:00
|
|
|
EASY_END_BLOCK;
|
|
|
|
EASY_BLOCK("variables");
|
2024-09-04 17:31:16 +03:00
|
|
|
if (collectionName.compare("variables", Qt::CaseInsensitive) == 0) {
|
2016-02-17 10:11:00 +03:00
|
|
|
foreach (VarDesc* item, m_tempVars) {
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!m_reportVariables.containsVariable(item->name())) {
|
|
|
|
m_reportVariables.addVariable(item->name(), item->value(), VarDesc::Report,
|
|
|
|
FirstPass);
|
2017-09-26 00:19:59 +03:00
|
|
|
VarDesc* currentVar = m_reportVariables.variableByName(item->name());
|
|
|
|
currentVar->initFrom(item);
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
delete item;
|
|
|
|
}
|
|
|
|
m_tempVars.clear();
|
|
|
|
}
|
2018-02-28 23:19:04 +03:00
|
|
|
EASY_END_BLOCK;
|
2019-04-04 04:37:18 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
if (collectionName.compare("csvs", Qt::CaseInsensitive) == 0) {
|
2019-04-04 04:37:18 +03:00
|
|
|
QMutableListIterator<CSVDesc*> it(m_csvs);
|
2024-09-04 17:31:16 +03:00
|
|
|
while (it.hasNext()) {
|
2019-04-04 04:37:18 +03:00
|
|
|
it.next();
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!m_datasources.contains(it.value()->name().toLower())) {
|
|
|
|
connect(it.value(), SIGNAL(cvsTextChanged(QString, QString)), this,
|
|
|
|
SLOT(slotCSVTextChanged(QString, QString)));
|
|
|
|
putHolder(it.value()->name(), new CSVHolder(*it.value(), this));
|
2019-04-04 04:37:18 +03:00
|
|
|
} else {
|
|
|
|
delete it.value();
|
|
|
|
it.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
if (designTime()) {
|
2018-02-28 23:19:04 +03:00
|
|
|
EASY_BLOCK("emit datasourcesChanged()");
|
|
|
|
emit datasourcesChanged();
|
|
|
|
EASY_END_BLOCK;
|
|
|
|
}
|
|
|
|
EASY_BLOCK("emit loadCollectionFinished(collectionName)");
|
2016-02-17 10:11:00 +03:00
|
|
|
emit loadCollectionFinished(collectionName);
|
2018-02-28 23:19:04 +03:00
|
|
|
EASY_END_BLOCK;
|
|
|
|
EASY_END_BLOCK;
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::addVariable(const QString& name, const QVariant& value,
|
|
|
|
VarDesc::VarType type, RenderPass pass)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (type == VarDesc::User) {
|
2016-06-07 00:44:21 +03:00
|
|
|
m_userVariables.addVariable(name, value, type, pass);
|
|
|
|
} else {
|
2024-09-04 17:31:16 +03:00
|
|
|
m_reportVariables.addVariable(name, value, type, pass);
|
2016-06-07 00:44:21 +03:00
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
if (designTime()) {
|
2018-02-28 23:19:04 +03:00
|
|
|
EASY_BLOCK("DataSourceManager::addVariable emit ds changed");
|
|
|
|
emit datasourcesChanged();
|
|
|
|
EASY_END_BLOCK;
|
|
|
|
}
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataSourceManager::deleteVariable(const QString& name)
|
|
|
|
{
|
2016-06-07 00:44:21 +03:00
|
|
|
m_userVariables.deleteVariable(name);
|
2024-09-04 17:31:16 +03:00
|
|
|
if (m_reportVariables.containsVariable(name)
|
|
|
|
&& m_reportVariables.variableType(name) == VarDesc::Report) {
|
2016-06-07 00:44:21 +03:00
|
|
|
m_reportVariables.deleteVariable(name);
|
2024-09-04 17:31:16 +03:00
|
|
|
if (designTime()) {
|
2019-01-31 21:01:55 +03:00
|
|
|
m_hasChanges = true;
|
|
|
|
emit datasourcesChanged();
|
|
|
|
}
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::changeVariable(const QString& name, const QVariant& value)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (m_userVariables.containsVariable(name)) {
|
|
|
|
m_userVariables.changeVariable(name, value);
|
2016-06-07 00:44:21 +03:00
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
if (m_reportVariables.containsVariable(name)) {
|
|
|
|
m_reportVariables.changeVariable(name, value);
|
2016-06-07 00:44:21 +03:00
|
|
|
}
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::setSystemVariable(const QString& name, const QVariant& value,
|
|
|
|
RenderPass pass)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
addVariable(name, value, VarDesc::System, pass);
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::setLastError(const QString& value)
|
|
|
|
{
|
|
|
|
m_lastError = value;
|
2016-02-17 10:39:17 +03:00
|
|
|
if (!value.isEmpty() && !m_errorsList.contains(value)) {
|
2016-02-17 10:11:00 +03:00
|
|
|
m_errorsList.append(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataSourceManager::invalidateLinkedDatasources(QString datasourceName)
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (QString name, dataSourceNames()) {
|
|
|
|
if (isSubQuery(name)) {
|
|
|
|
if (subQueryByName(name)->master().compare(datasourceName) == 0)
|
|
|
|
dataSourceHolder(name)->invalidate(designTime() ? IDataSource::DESIGN_MODE
|
|
|
|
: IDataSource::RENDER_MODE);
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
if (isProxy(name)) {
|
2016-02-17 10:11:00 +03:00
|
|
|
ProxyDesc* proxy = proxyByName(name);
|
2024-09-04 17:31:16 +03:00
|
|
|
if ((proxy->master().compare(datasourceName) == 0)
|
|
|
|
|| (proxy->child().compare(datasourceName) == 0))
|
|
|
|
dataSourceHolder(name)->invalidate(designTime() ? IDataSource::DESIGN_MODE
|
|
|
|
: IDataSource::RENDER_MODE);
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::slotConnectionRenamed(const QString& oldName, const QString& newName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (QueryDesc* query, m_queries) {
|
|
|
|
if (query->connectionName().compare(oldName, Qt::CaseInsensitive) == 0)
|
|
|
|
query->setConnectionName(newName);
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (SubQueryDesc* query, m_subqueries) {
|
|
|
|
if (query->connectionName().compare(oldName, Qt::CaseInsensitive) == 0)
|
|
|
|
query->setConnectionName(newName);
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::slotQueryTextChanged(const QString& queryName, const QString& queryText)
|
2016-07-07 23:49:54 +03:00
|
|
|
{
|
|
|
|
QueryHolder* holder = dynamic_cast<QueryHolder*>(m_datasources.value(queryName));
|
2024-09-04 17:31:16 +03:00
|
|
|
if (holder) {
|
2016-07-07 23:49:54 +03:00
|
|
|
holder->setQueryText(queryText);
|
|
|
|
}
|
2019-10-16 02:05:25 +03:00
|
|
|
m_varToDataSource.clear();
|
2016-07-07 23:49:54 +03:00
|
|
|
}
|
|
|
|
|
2017-09-19 21:02:55 +03:00
|
|
|
void DataSourceManager::invalidateQueriesContainsVariable(const QString& variableName)
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (!variableIsSystem(variableName)) {
|
2019-10-16 02:05:25 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
if (m_varToDataSource.contains(variableName)) {
|
|
|
|
foreach (QString datasourceName, m_varToDataSource.value(variableName)) {
|
|
|
|
QueryHolder* holder
|
|
|
|
= dynamic_cast<QueryHolder*>(m_datasources.value(datasourceName));
|
|
|
|
if (holder)
|
|
|
|
holder->invalidate(designTime() ? IDataSource::DESIGN_MODE
|
|
|
|
: IDataSource::RENDER_MODE);
|
2019-10-16 02:05:25 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
QVector<QString> datasources;
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (const QString& datasourceName, dataSourceNames()) {
|
|
|
|
QueryHolder* holder
|
|
|
|
= dynamic_cast<QueryHolder*>(m_datasources.value(datasourceName));
|
|
|
|
if (holder) {
|
2021-12-16 00:13:39 +03:00
|
|
|
#if (QT_VERSION < QT_VERSION_CHECK(5, 15, 1))
|
2019-10-16 02:05:25 +03:00
|
|
|
QRegExp rx(QString(Const::NAMED_VARIABLE_RX).arg(variableName));
|
2021-08-24 08:37:40 +03:00
|
|
|
#else
|
2022-07-16 02:33:39 +03:00
|
|
|
QRegularExpression rx = getNamedVariableRegEx(variableName);
|
2021-08-24 08:37:40 +03:00
|
|
|
#endif
|
2024-09-04 17:31:16 +03:00
|
|
|
if (holder->queryText().contains(rx)) {
|
|
|
|
holder->invalidate(designTime() ? IDataSource::DESIGN_MODE
|
|
|
|
: IDataSource::RENDER_MODE);
|
2019-10-16 02:05:25 +03:00
|
|
|
datasources.append(datasourceName);
|
|
|
|
}
|
|
|
|
}
|
2017-09-19 22:00:52 +03:00
|
|
|
}
|
2019-10-16 02:05:25 +03:00
|
|
|
m_varToDataSource.insert(variableName, datasources);
|
2017-09-19 21:02:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataSourceManager::slotVariableHasBeenAdded(const QString& variableName)
|
|
|
|
{
|
|
|
|
invalidateQueriesContainsVariable(variableName);
|
2019-01-31 21:01:55 +03:00
|
|
|
if (variableType(variableName) == VarDesc::Report)
|
|
|
|
m_hasChanges = true;
|
2017-09-19 21:02:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataSourceManager::slotVariableHasBeenChanged(const QString& variableName)
|
|
|
|
{
|
|
|
|
invalidateQueriesContainsVariable(variableName);
|
2019-01-31 21:01:55 +03:00
|
|
|
if (variableType(variableName) == VarDesc::Report)
|
|
|
|
m_hasChanges = true;
|
2017-09-19 21:02:55 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::slotCSVTextChanged(const QString& csvName, const QString& csvText)
|
2019-04-04 04:37:18 +03:00
|
|
|
{
|
|
|
|
CSVHolder* holder = dynamic_cast<CSVHolder*>(m_datasources.value(csvName));
|
2024-09-04 17:31:16 +03:00
|
|
|
if (holder) {
|
2019-04-04 04:37:18 +03:00
|
|
|
holder->setCSVText(csvText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-17 10:11:00 +03:00
|
|
|
void DataSourceManager::clear(ClearMethod method)
|
|
|
|
{
|
2019-10-16 02:05:25 +03:00
|
|
|
m_varToDataSource.clear();
|
|
|
|
|
2016-02-17 10:11:00 +03:00
|
|
|
DataSourcesMap::iterator dit;
|
2024-09-04 17:31:16 +03:00
|
|
|
for (dit = m_datasources.begin(); dit != m_datasources.end();) {
|
2016-02-17 10:11:00 +03:00
|
|
|
bool owned = (*dit)->isOwned() && (*dit)->isRemovable();
|
2024-09-04 17:31:16 +03:00
|
|
|
switch (method) {
|
2016-02-17 10:11:00 +03:00
|
|
|
case All:
|
|
|
|
invalidateLinkedDatasources(dit.key());
|
|
|
|
delete dit.value();
|
2020-06-27 11:23:36 +03:00
|
|
|
dit = m_datasources.erase(dit);
|
2016-02-17 10:11:00 +03:00
|
|
|
break;
|
|
|
|
default:
|
2024-09-04 17:31:16 +03:00
|
|
|
if (owned) {
|
2016-02-17 10:11:00 +03:00
|
|
|
invalidateLinkedDatasources(dit.key());
|
|
|
|
delete dit.value();
|
2020-06-27 11:23:36 +03:00
|
|
|
dit = m_datasources.erase(dit);
|
2016-02-17 10:11:00 +03:00
|
|
|
} else {
|
|
|
|
++dit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-17 10:19:50 +03:00
|
|
|
QList<ConnectionDesc*>::iterator cit = m_connections.begin();
|
2024-09-04 17:31:16 +03:00
|
|
|
while (cit != m_connections.end()) {
|
|
|
|
if ((*cit)->isInternal())
|
|
|
|
QSqlDatabase::removeDatabase((*cit)->name());
|
2016-02-17 10:19:50 +03:00
|
|
|
delete (*cit);
|
|
|
|
cit = m_connections.erase(cit);
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
// TODO: add smart pointes to collections
|
|
|
|
foreach (QueryDesc* desc, m_queries)
|
|
|
|
delete desc;
|
|
|
|
foreach (SubQueryDesc* desc, m_subqueries)
|
|
|
|
delete desc;
|
|
|
|
foreach (ProxyDesc* desc, m_proxies)
|
|
|
|
delete desc;
|
2016-02-17 10:11:00 +03:00
|
|
|
|
|
|
|
m_queries.clear();
|
|
|
|
m_subqueries.clear();
|
|
|
|
m_proxies.clear();
|
2024-09-04 17:31:16 +03:00
|
|
|
// if (method == All)
|
|
|
|
// clearUserVariables();
|
2016-06-07 00:44:21 +03:00
|
|
|
clearReportVariables();
|
2016-04-06 01:36:59 +03:00
|
|
|
|
2016-02-17 10:11:00 +03:00
|
|
|
emit cleared();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataSourceManager::clearGroupFunction()
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (GroupFunction* gf, m_groupFunctions.values()) {
|
2016-02-17 10:11:00 +03:00
|
|
|
delete gf;
|
|
|
|
}
|
|
|
|
m_groupFunctions.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataSourceManager::clearGroupFunctionValues(const QString& bandObjectName)
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (GroupFunction* gf, m_groupFunctions.values(bandObjectName)) {
|
2016-02-17 10:11:00 +03:00
|
|
|
gf->values().clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
GroupFunction* DataSourceManager::addGroupFunction(const QString& name, const QString& expression,
|
|
|
|
const QString& band, const QString& dataBand)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
GroupFunction* gf
|
|
|
|
= m_groupFunctionFactory.createGroupFunction(name, expression, dataBand, this);
|
|
|
|
if (gf) {
|
|
|
|
m_groupFunctions.insert(band, gf);
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
return gf;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
GroupFunction* DataSourceManager::groupFunction(const QString& name, const QString& expression,
|
|
|
|
const QString& band)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (GroupFunction* gf, m_groupFunctions.values(band)) {
|
|
|
|
if ((gf->name().compare(name, Qt::CaseInsensitive) == 0)
|
|
|
|
&& (gf->data().compare(expression, Qt::CaseInsensitive) == 0))
|
|
|
|
return gf;
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QList<GroupFunction*> DataSourceManager::groupFunctionsByBand(const QString& band)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
|
|
|
return m_groupFunctions.values(band);
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
void DataSourceManager::updateChildrenData(const QString& datasourceName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (SubQueryDesc* subquery, m_subqueries) {
|
|
|
|
if (subquery->master().compare(datasourceName, Qt::CaseInsensitive) == 0) {
|
|
|
|
SubQueryHolder* holder
|
|
|
|
= dynamic_cast<SubQueryHolder*>(dataSourceHolder(subquery->queryName()));
|
|
|
|
if (holder)
|
|
|
|
holder->runQuery();
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (ProxyDesc* subproxy, m_proxies) {
|
|
|
|
if (subproxy->master().compare(datasourceName, Qt::CaseInsensitive) == 0) {
|
2016-02-17 10:11:00 +03:00
|
|
|
ProxyHolder* holder = dynamic_cast<ProxyHolder*>(dataSourceHolder(subproxy->name()));
|
|
|
|
holder->filterModel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QString DataSourceManager::extractDataSource(const QString& fieldName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
|
|
|
return fieldName.left(fieldName.indexOf('.'));
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QString DataSourceManager::extractFieldName(const QString& fieldName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
return fieldName.right((fieldName.length() - fieldName.indexOf('.')) - 1);
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
bool DataSourceManager::containsField(const QString& fieldName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
|
|
|
IDataSource* ds = dataSource(extractDataSource(fieldName));
|
|
|
|
if (ds) {
|
2024-09-04 17:31:16 +03:00
|
|
|
return ds->columnIndexByName(extractFieldName(fieldName)) != -1;
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DataSourceManager::containsVariable(const QString& variableName)
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (m_userVariables.containsVariable(variableName))
|
|
|
|
return true;
|
2016-06-07 00:44:21 +03:00
|
|
|
return m_reportVariables.containsVariable(variableName);
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataSourceManager::clearUserVariables()
|
|
|
|
{
|
2016-06-07 00:44:21 +03:00
|
|
|
m_userVariables.clearUserVariables();
|
|
|
|
m_reportVariables.clearUserVariables();
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QVariant DataSourceManager::fieldData(const QString& fieldName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (containsField(fieldName)) {
|
2016-02-17 10:11:00 +03:00
|
|
|
IDataSource* ds = dataSource(extractDataSource(fieldName));
|
2024-09-04 17:31:16 +03:00
|
|
|
if (ds)
|
|
|
|
return ds->data(extractFieldName(fieldName));
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
return QVariant();
|
2019-02-19 02:43:16 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QVariant DataSourceManager::fieldDataByRowIndex(const QString& fieldName, int rowIndex)
|
2019-02-19 02:43:16 +03:00
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (containsField(fieldName)) {
|
2019-02-19 02:43:16 +03:00
|
|
|
IDataSource* ds = dataSource(extractDataSource(fieldName));
|
2024-09-04 17:31:16 +03:00
|
|
|
if (ds) {
|
2019-02-19 02:43:16 +03:00
|
|
|
return ds->dataByRowIndex(extractFieldName(fieldName), rowIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant();
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QVariant DataSourceManager::fieldDataByRowIndex(const QString& fieldName, int rowIndex, int role)
|
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
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (containsField(fieldName)) {
|
|
|
|
IDataSource* ds = dataSource(extractDataSource(fieldName));
|
|
|
|
if (ds) {
|
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
|
|
|
return ds->dataByRowIndex(extractFieldName(fieldName), rowIndex, role);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QVariant DataSourceManager::fieldDataByRowIndex(const QString& fieldName, int rowIndex,
|
|
|
|
const QString& roleName)
|
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
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (containsField(fieldName)) {
|
|
|
|
IDataSource* ds = dataSource(extractDataSource(fieldName));
|
|
|
|
if (ds) {
|
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
|
|
|
return ds->dataByRowIndex(extractFieldName(fieldName), rowIndex, roleName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QVariant DataSourceManager::fieldDataByKey(const QString& datasourceName,
|
|
|
|
const QString& valueFieldName,
|
|
|
|
const QString& keyFieldName, QVariant keyValue)
|
2017-08-31 02:53:34 +03:00
|
|
|
{
|
|
|
|
IDataSource* ds = dataSource(datasourceName);
|
2024-09-04 17:31:16 +03:00
|
|
|
if (ds) {
|
2017-08-31 02:53:34 +03:00
|
|
|
return ds->dataByKeyField(valueFieldName, keyFieldName, keyValue);
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QVariant DataSourceManager::headerData(const QString& fieldName, const QString& roleName)
|
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
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
if (containsField(fieldName)) {
|
|
|
|
IDataSource* ds = dataSource(extractDataSource(fieldName));
|
|
|
|
if (ds) {
|
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
|
|
|
return ds->headerData(extractFieldName(fieldName), roleName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QString DataSourceManager::columnName(const QString& datasourceName, int index)
|
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
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
IDataSource* ds = dataSource(datasourceName);
|
|
|
|
if (ds && !ds->isInvalid() && ds->columnCount() > index) {
|
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
|
|
|
return ds->columnNameByIndex(index);
|
|
|
|
}
|
|
|
|
return QString("unknown");
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
int DataSourceManager::columnCount(const QString& datasourceName)
|
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
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
IDataSource* ds = dataSource(datasourceName);
|
|
|
|
if (ds && !ds->isInvalid()) {
|
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
|
|
|
return ds->columnCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-09-19 21:02:55 +03:00
|
|
|
void DataSourceManager::reopenDatasource(const QString& datasourceName)
|
|
|
|
{
|
|
|
|
QueryHolder* qh = dynamic_cast<QueryHolder*>(dataSourceHolder(datasourceName));
|
2024-09-04 17:31:16 +03:00
|
|
|
if (qh) {
|
|
|
|
qh->invalidate(designTime() ? IDataSource::DESIGN_MODE : IDataSource::RENDER_MODE);
|
2017-09-19 21:02:55 +03:00
|
|
|
invalidateChildren(datasourceName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QVariant DataSourceManager::variable(const QString& variableName)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2016-06-07 00:44:21 +03:00
|
|
|
if (m_userVariables.containsVariable(variableName))
|
|
|
|
return m_userVariables.variable(variableName);
|
|
|
|
return m_reportVariables.variable(variableName);
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
RenderPass DataSourceManager::variablePass(const QString& name)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2016-08-02 01:33:26 +03:00
|
|
|
if (m_userVariables.containsVariable(name))
|
|
|
|
return m_userVariables.variablePass(name);
|
|
|
|
return m_reportVariables.variablePass(name);
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
bool DataSourceManager::variableIsSystem(const QString& name)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2016-06-07 00:44:21 +03:00
|
|
|
if (m_reportVariables.containsVariable(name))
|
2024-09-04 17:31:16 +03:00
|
|
|
return (m_reportVariables.variableType(name) == VarDesc::System);
|
2016-06-07 00:44:21 +03:00
|
|
|
return false;
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2017-09-26 00:19:59 +03:00
|
|
|
bool DataSourceManager::variableIsMandatory(const QString& name)
|
|
|
|
{
|
|
|
|
if (m_reportVariables.containsVariable(name))
|
|
|
|
return m_reportVariables.variableByName(name)->isMandatory();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataSourceManager::setVarableMandatory(const QString& name, bool value)
|
|
|
|
{
|
|
|
|
if (m_reportVariables.containsVariable(name))
|
|
|
|
m_reportVariables.variableByName(name)->setMandatory(value);
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QStringList DataSourceManager::variableNames() { return m_reportVariables.variableNames(); }
|
2016-06-07 00:44:21 +03:00
|
|
|
|
2017-08-31 02:53:34 +03:00
|
|
|
QStringList DataSourceManager::variableNamesByRenderPass(RenderPass pass)
|
|
|
|
{
|
|
|
|
QStringList result;
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (QString variableName, m_reportVariables.variableNames()) {
|
|
|
|
if (m_reportVariables.variablePass(variableName) == pass) {
|
2017-08-31 02:53:34 +03:00
|
|
|
result.append(variableName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
QStringList DataSourceManager::userVariableNames() { return m_userVariables.variableNames(); }
|
2016-02-17 10:11:00 +03:00
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
VarDesc::VarType DataSourceManager::variableType(const QString& name)
|
2016-02-17 10:11:00 +03:00
|
|
|
{
|
2016-06-07 00:44:21 +03:00
|
|
|
if (m_reportVariables.containsVariable(name))
|
|
|
|
return m_reportVariables.variableType(name);
|
|
|
|
return VarDesc::User;
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
|
2017-09-26 00:19:59 +03:00
|
|
|
VariableDataType DataSourceManager::variableDataType(const QString& name)
|
|
|
|
{
|
|
|
|
if (m_reportVariables.containsVariable(name))
|
|
|
|
return m_reportVariables.variableByName(name)->dataType();
|
2017-12-11 16:48:00 +03:00
|
|
|
return Enums::Undefined;
|
2017-09-26 00:19:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void DataSourceManager::setVariableDataType(const QString& name, VariableDataType value)
|
|
|
|
{
|
|
|
|
if (m_reportVariables.containsVariable(name))
|
|
|
|
m_reportVariables.variableByName(name)->setDataType(value);
|
|
|
|
}
|
|
|
|
|
2016-02-17 10:11:00 +03:00
|
|
|
void DataSourceManager::setAllDatasourcesToFirst()
|
|
|
|
{
|
2024-09-04 17:31:16 +03:00
|
|
|
foreach (IDataSourceHolder* ds, m_datasources.values()) {
|
|
|
|
if (ds->dataSource())
|
|
|
|
ds->dataSource()->first();
|
2016-02-17 10:11:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-04 17:31:16 +03:00
|
|
|
} // namespace LimeReport
|