mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-12-17 12:40:35 +01:00
Improve the use of the database
This commit is contained in:
@@ -53,37 +53,6 @@ projectDataBase::~projectDataBase() {
|
||||
m_data_base.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief projectDataBase::elementsInfoFromQuery
|
||||
* @param query
|
||||
* @return quickly return all information about elements from @query, ensure that all
|
||||
* record returned by the query can be casted to string.
|
||||
* Each item of the Qvector represent an element and each value of the QStringList represent the recorded value.
|
||||
*/
|
||||
QVector<QStringList> projectDataBase::elementsInfoFromQuery(const QString &query)
|
||||
{
|
||||
QVector<QStringList> result_;
|
||||
|
||||
QSqlQuery query_(query, m_data_base);
|
||||
if (!query_.exec()) {
|
||||
qDebug() << "Query error : " << query_.lastError();
|
||||
}
|
||||
|
||||
while (query_.next())
|
||||
{
|
||||
QStringList record_;
|
||||
auto i=0;
|
||||
while (query_.value(i).isValid())
|
||||
{
|
||||
record_ << query_.value(i).toString();
|
||||
++i;
|
||||
}
|
||||
result_ << record_;
|
||||
}
|
||||
|
||||
return result_;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief projectDataBase::updateDB
|
||||
* Up to date the content of the data base.
|
||||
|
||||
@@ -45,7 +45,6 @@ class projectDataBase : public QObject
|
||||
public:
|
||||
virtual ~projectDataBase() override;
|
||||
|
||||
QVector<QStringList> elementsInfoFromQuery(const QString &query);
|
||||
void updateDB();
|
||||
QETProject *project() const;
|
||||
QSqlQuery newQuery(const QString &query = QString());
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include <QFont>
|
||||
#include <QSqlQuery>
|
||||
#include <QSqlRecord>
|
||||
#include <QSqlError>
|
||||
|
||||
|
||||
/**
|
||||
@@ -183,7 +184,7 @@ void NomenclatureModel::query(const QString &query)
|
||||
m_project->dataBase()->updateDB();
|
||||
if (rm_) {
|
||||
setHeaderString();
|
||||
m_record = m_project->dataBase()->elementsInfoFromQuery(m_query);
|
||||
fillValue();
|
||||
connect(m_project->dataBase(), &projectDataBase::dataBaseUpdated, this, &NomenclatureModel::dataBaseUpdated);
|
||||
}
|
||||
}
|
||||
@@ -276,7 +277,10 @@ void NomenclatureModel::fromXml(const QDomElement &element)
|
||||
*/
|
||||
void NomenclatureModel::dataBaseUpdated()
|
||||
{
|
||||
auto new_record = m_project->dataBase()->elementsInfoFromQuery(m_query);
|
||||
auto original_record = m_record;
|
||||
fillValue();
|
||||
auto new_record = m_record;
|
||||
m_record = original_record;
|
||||
|
||||
//This a very special case, if this nomenclature model is added
|
||||
//befor any element, column count return 0, so in this case we emit column inserted
|
||||
@@ -322,3 +326,25 @@ void NomenclatureModel::setHeaderString()
|
||||
this->setHeaderData(i, Qt::Horizontal, header_name);
|
||||
}
|
||||
}
|
||||
|
||||
void NomenclatureModel::fillValue()
|
||||
{
|
||||
m_record.clear();
|
||||
|
||||
auto query_ = m_project->dataBase()->newQuery(m_query);
|
||||
if (!query_.exec()) {
|
||||
qDebug() << "Query error : " << query_.lastError();
|
||||
}
|
||||
|
||||
while (query_.next())
|
||||
{
|
||||
QStringList record_;
|
||||
auto i=0;
|
||||
while (query_.value(i).isValid())
|
||||
{
|
||||
record_ << query_.value(i).toString();
|
||||
++i;
|
||||
}
|
||||
m_record << record_;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,6 +56,7 @@ class NomenclatureModel : public QAbstractTableModel
|
||||
private:
|
||||
void dataBaseUpdated();
|
||||
void setHeaderString();
|
||||
void fillValue();
|
||||
|
||||
private:
|
||||
QPointer<QETProject> m_project;
|
||||
|
||||
Reference in New Issue
Block a user