mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-12-18 05:00:33 +01:00
CSV export : improve database and query
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
/*
|
/*
|
||||||
Copyright 2006-2019 The QElectroTech Team
|
Copyright 2006-2019 The QElectroTech Team
|
||||||
This file is part of QElectroTech.
|
This file is part of QElectroTech.
|
||||||
|
|
||||||
@@ -30,6 +30,7 @@
|
|||||||
#include <QSqlQuery>
|
#include <QSqlQuery>
|
||||||
#include <QSqlError>
|
#include <QSqlError>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
|
#include <QSqlRecord>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief BOMExportDialog::BOMExportDialog
|
* @brief BOMExportDialog::BOMExportDialog
|
||||||
@@ -42,6 +43,13 @@ BOMExportDialog::BOMExportDialog(QETProject *project, QWidget *parent) :
|
|||||||
m_project(project)
|
m_project(project)
|
||||||
{
|
{
|
||||||
ui->setupUi(this);
|
ui->setupUi(this);
|
||||||
|
|
||||||
|
m_export_info.insert("pos", tr("Position"));
|
||||||
|
m_export_info.insert("folio_title", tr("Titre du folio"));
|
||||||
|
m_export_info.insert("folio_pos", tr("Position de folio"));
|
||||||
|
m_export_info.insert("folio_num", tr("Numéro de folio"));
|
||||||
|
m_export_info.insert("designation_qty", tr("Quantité (Numéro d'article)"));
|
||||||
|
|
||||||
setUpItems();
|
setUpItems();
|
||||||
createDataBase();
|
createDataBase();
|
||||||
fillSavedQuery();
|
fillSavedQuery();
|
||||||
@@ -92,7 +100,44 @@ int BOMExportDialog::exec()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return r;
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief BOMExportDialog::selectedKeys
|
||||||
|
* @return the current keys of selected infos to be exported
|
||||||
|
*/
|
||||||
|
QStringList BOMExportDialog::selectedKeys() const
|
||||||
|
{
|
||||||
|
//Made a string list with the colomns (keys) choosen by the user
|
||||||
|
QStringList keys;
|
||||||
|
int row = 0;
|
||||||
|
while (auto *item = ui->m_choosen_list->item(row))
|
||||||
|
{
|
||||||
|
keys.append(item->data(Qt::UserRole).toString());
|
||||||
|
++row;
|
||||||
|
}
|
||||||
|
|
||||||
|
return keys;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief BOMExportDialog::translatedKeys
|
||||||
|
* @param key
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
QString BOMExportDialog::translatedKeys(const QString &key) const
|
||||||
|
{
|
||||||
|
if (QETApp::elementInfoKeys().contains(key)) {
|
||||||
|
return QETApp::elementTranslatedInfoKey(key);
|
||||||
|
}
|
||||||
|
else if (m_export_info.keys().contains(key)) {
|
||||||
|
return m_export_info.value(key);
|
||||||
|
} else {
|
||||||
|
auto str(key);
|
||||||
|
str.replace("_", "-");
|
||||||
|
return QETApp::elementTranslatedInfoKey(str);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -107,13 +152,13 @@ void BOMExportDialog::setUpItems()
|
|||||||
item->setData(Qt::UserRole+1, key); //We store the real key before replace "-" by "_" to easily retrieve it in the element information
|
item->setData(Qt::UserRole+1, key); //We store the real key before replace "-" by "_" to easily retrieve it in the element information
|
||||||
item->setData(Qt::UserRole, key.replace("-", "_")); //We must to replace "-" by "_" because "-" is a sql keyword.
|
item->setData(Qt::UserRole, key.replace("-", "_")); //We must to replace "-" by "_" because "-" is a sql keyword.
|
||||||
}
|
}
|
||||||
QStringList other_keys({"pos", "folio_title", "folio_pos", "folio_num", "designation_qty"});
|
|
||||||
QStringList other_translated({tr("Position"), tr("Titre du folio"), tr("Position de folio"), tr("Numéro de folio"), tr("Quantité (Numéro d'article)")});
|
for (auto key : m_export_info.keys())
|
||||||
for(int i=0 ; i<other_keys.size() ; ++i)
|
{
|
||||||
{
|
auto item = new QListWidgetItem(m_export_info.value(key), ui->m_var_list);
|
||||||
auto item = new QListWidgetItem(other_translated.at(i), ui->m_var_list);
|
item->setData(Qt::UserRole, key);
|
||||||
item->setData(Qt::UserRole, other_keys.at(i));
|
item->setData(Qt::UserRole+1, key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -188,6 +233,7 @@ QString BOMExportDialog::getBom()
|
|||||||
QString data; //The string to be returned
|
QString data; //The string to be returned
|
||||||
if (ui->m_include_header_cb->isChecked()) {
|
if (ui->m_include_header_cb->isChecked()) {
|
||||||
data = headers();
|
data = headers();
|
||||||
|
data += "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
QSqlQuery query (queryStr() , m_data_base);
|
QSqlQuery query (queryStr() , m_data_base);
|
||||||
@@ -195,18 +241,29 @@ QString BOMExportDialog::getBom()
|
|||||||
qDebug() << "Query error : " << query.lastError();
|
qDebug() << "Query error : " << query.lastError();
|
||||||
}
|
}
|
||||||
|
|
||||||
QStringList record;
|
QStringList record;
|
||||||
while (query.next())
|
while (query.next())
|
||||||
{
|
{
|
||||||
auto i=0;
|
if (ui->m_edit_sql_query_cb->isChecked()) //In case of custom query, we only append each value to @record
|
||||||
while (query.value(i).isValid())
|
{
|
||||||
{
|
auto i=0;
|
||||||
record << query.value(i).toString();
|
while (query.value(i).isValid())
|
||||||
++i;
|
{
|
||||||
}
|
record << query.value(i).toString();
|
||||||
data += record.join(";") + "\n";
|
++i;
|
||||||
record.clear();
|
}
|
||||||
}
|
}
|
||||||
|
else //In case of query made with the gui, we ensure that returned values are in the same order as list created by user
|
||||||
|
{
|
||||||
|
QSqlRecord sql_record = query.record();
|
||||||
|
for (auto key : selectedKeys()) {
|
||||||
|
record << sql_record.value(key).toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
data += record.join(";") + "\n";
|
||||||
|
record.clear();
|
||||||
|
}
|
||||||
|
|
||||||
m_data_base.close();
|
m_data_base.close();
|
||||||
return data;
|
return data;
|
||||||
@@ -222,23 +279,14 @@ QString BOMExportDialog::headers() const
|
|||||||
|
|
||||||
if (!ui->m_edit_sql_query_cb->isChecked())
|
if (!ui->m_edit_sql_query_cb->isChecked())
|
||||||
{
|
{
|
||||||
//Made a string list with the colomns (keys) choosen by the user
|
for (auto key : selectedKeys())
|
||||||
QStringList keys;
|
{
|
||||||
int row = 0;
|
if (!header_string.isEmpty()) {
|
||||||
while (auto *item = ui->m_choosen_list->item(row))
|
header_string += ";";
|
||||||
{
|
}
|
||||||
keys.append(item->data(Qt::UserRole).toString());
|
header_string += translatedKeys(key);
|
||||||
++row;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
for (int i=0 ; i<keys.size() ; i++)
|
|
||||||
{
|
|
||||||
if(!header_string.isEmpty()) {
|
|
||||||
header_string += ";";
|
|
||||||
}
|
|
||||||
header_string += QETApp::elementTranslatedInfoKey(keys.at(i));
|
|
||||||
}
|
|
||||||
header_string += "\n";
|
header_string += "\n";
|
||||||
}
|
}
|
||||||
else if (!queryStr().isEmpty()) //Try to retreive the header according to the sql query
|
else if (!queryStr().isEmpty()) //Try to retreive the header according to the sql query
|
||||||
@@ -289,6 +337,7 @@ bool BOMExportDialog::createDataBase()
|
|||||||
++row;
|
++row;
|
||||||
}
|
}
|
||||||
keys << "element_type" << "element_subtype";
|
keys << "element_type" << "element_subtype";
|
||||||
|
keys.removeAll("designation_qty");
|
||||||
|
|
||||||
QString table("CREATE TABLE bom(");
|
QString table("CREATE TABLE bom(");
|
||||||
bool first = true;
|
bool first = true;
|
||||||
@@ -298,14 +347,9 @@ bool BOMExportDialog::createDataBase()
|
|||||||
first = false;
|
first = false;
|
||||||
} else {
|
} else {
|
||||||
table += ",";
|
table += ",";
|
||||||
}
|
}
|
||||||
if (string == "designation_qty") {
|
|
||||||
table += string += " INT";
|
|
||||||
|
|
||||||
} else {
|
table += string += " VARCHAR(512)";
|
||||||
table += string += " VARCHAR(512)";
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
table += ");";
|
table += ");";
|
||||||
m_data_base.exec(table);
|
m_data_base.exec(table);
|
||||||
@@ -325,14 +369,6 @@ bool BOMExportDialog::createDataBase()
|
|||||||
m_insert_query = QSqlQuery(m_data_base);
|
m_insert_query = QSqlQuery(m_data_base);
|
||||||
m_insert_query.prepare(insert);
|
m_insert_query.prepare(insert);
|
||||||
|
|
||||||
QString update("UPDATE bom SET designation_qty = :bind_qty WHERE designation IS NOT NULL AND designation = :bind_ref");
|
|
||||||
m_update_qty_query = QSqlQuery(m_data_base);
|
|
||||||
m_update_qty_query.prepare(update);
|
|
||||||
|
|
||||||
QString count ("SELECT COUNT(designation) FROM bom WHERE designation = :bind_ref");
|
|
||||||
m_count_ref_query = QSqlQuery(m_data_base);
|
|
||||||
m_count_ref_query.prepare(count);
|
|
||||||
|
|
||||||
populateDataBase();
|
populateDataBase();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -362,32 +398,10 @@ void BOMExportDialog::populateDataBase()
|
|||||||
|
|
||||||
m_insert_query.bindValue(":element_type", elmt->linkTypeToString());
|
m_insert_query.bindValue(":element_type", elmt->linkTypeToString());
|
||||||
m_insert_query.bindValue(":element_subtype", elmt->kindInformations()["type"].toString());
|
m_insert_query.bindValue(":element_subtype", elmt->kindInformations()["type"].toString());
|
||||||
m_insert_query.bindValue(":designation_qty", 0);
|
|
||||||
|
|
||||||
if (!m_insert_query.exec()) {
|
if (!m_insert_query.exec()) {
|
||||||
qDebug() << "BOMExportDialog::populateDataBase insert error : " << m_insert_query.lastError();
|
qDebug() << "BOMExportDialog::populateDataBase insert error : " << m_insert_query.lastError();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!elmt->elementInformations().value("designation").toString().isEmpty())
|
|
||||||
{
|
|
||||||
QString ref = elmt->elementInformations().value("designation").toString();
|
|
||||||
m_count_ref_query.bindValue(":bind_ref", ref);
|
|
||||||
if (m_count_ref_query.exec())
|
|
||||||
{
|
|
||||||
if (m_count_ref_query.first())
|
|
||||||
{
|
|
||||||
int c = m_count_ref_query.value(0).toInt();
|
|
||||||
m_update_qty_query.bindValue(":bind_ref", ref);
|
|
||||||
m_update_qty_query.bindValue(":bind_qty", c);
|
|
||||||
if (!m_update_qty_query.exec()) {
|
|
||||||
qDebug() << "BOMExportDialog::populateDataBase update qty query error : " << m_insert_query.lastError();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
qDebug() << "BOMExportDialog::populateDataBase count ref query : " << m_count_ref_query.lastError();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -423,9 +437,6 @@ QHash<QString, QString> BOMExportDialog::elementInfoToString(Element *elmt) cons
|
|||||||
else if (key == "folio_num") {
|
else if (key == "folio_num") {
|
||||||
hash.insert(key, elmt->diagram()->border_and_titleblock.finalfolio());
|
hash.insert(key, elmt->diagram()->border_and_titleblock.finalfolio());
|
||||||
}
|
}
|
||||||
else if (key == "designation_qty") {
|
|
||||||
hash.insert(key, key);
|
|
||||||
}
|
|
||||||
else if (key == "label") {
|
else if (key == "label") {
|
||||||
hash.insert(key, elmt->actualLabel());
|
hash.insert(key, elmt->actualLabel());
|
||||||
}
|
}
|
||||||
@@ -448,13 +459,8 @@ QString BOMExportDialog::queryStr() const
|
|||||||
return ui->m_sql_query->text();
|
return ui->m_sql_query->text();
|
||||||
}
|
}
|
||||||
//Made a string list with the colomns (keys) choosen by the user
|
//Made a string list with the colomns (keys) choosen by the user
|
||||||
QStringList keys;
|
QStringList keys = selectedKeys();
|
||||||
int row = 0;
|
keys.removeAll("designation_qty");
|
||||||
while (auto *item = ui->m_choosen_list->item(row))
|
|
||||||
{
|
|
||||||
keys.append(item->data(Qt::UserRole).toString());
|
|
||||||
++row;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString select ="SELECT ";
|
QString select ="SELECT ";
|
||||||
QString order_by = " ORDER BY ";
|
QString order_by = " ORDER BY ";
|
||||||
@@ -473,6 +479,7 @@ QString BOMExportDialog::queryStr() const
|
|||||||
}
|
}
|
||||||
|
|
||||||
QString from = " FROM bom";
|
QString from = " FROM bom";
|
||||||
|
QString count = ui->m_format_as_bom_rb->isChecked() ? QString(", COUNT(*) AS designation_qty ") : QString();
|
||||||
QString where;
|
QString where;
|
||||||
if (ui->m_button_rb->isChecked()) {
|
if (ui->m_button_rb->isChecked()) {
|
||||||
where = " WHERE element_subtype = 'commutator'";
|
where = " WHERE element_subtype = 'commutator'";
|
||||||
@@ -495,7 +502,7 @@ QString BOMExportDialog::queryStr() const
|
|||||||
}
|
}
|
||||||
|
|
||||||
QString group_by = ui->m_format_as_bom_rb->isChecked() ? " GROUP BY designation" : "";
|
QString group_by = ui->m_format_as_bom_rb->isChecked() ? " GROUP BY designation" : "";
|
||||||
QString q(select + column + from + where + where_bom + group_by + order_by);
|
QString q(select + column + count + from + where + where_bom + group_by + order_by);
|
||||||
return q;
|
return q;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -38,7 +38,9 @@ class BOMExportDialog : public QDialog
|
|||||||
explicit BOMExportDialog(QETProject *project, QWidget *parent = nullptr);
|
explicit BOMExportDialog(QETProject *project, QWidget *parent = nullptr);
|
||||||
~BOMExportDialog() override;
|
~BOMExportDialog() override;
|
||||||
|
|
||||||
virtual int exec() override;
|
virtual int exec() override;
|
||||||
|
QStringList selectedKeys() const;
|
||||||
|
QString translatedKeys(const QString &key) const;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void on_m_add_pb_clicked();
|
void on_m_add_pb_clicked();
|
||||||
@@ -70,10 +72,9 @@ class BOMExportDialog : public QDialog
|
|||||||
Ui::BOMExportDialog *ui;
|
Ui::BOMExportDialog *ui;
|
||||||
QETProject *m_project = nullptr;
|
QETProject *m_project = nullptr;
|
||||||
QSqlDatabase m_data_base;
|
QSqlDatabase m_data_base;
|
||||||
QSqlQuery m_insert_query,
|
QSqlQuery m_insert_query;
|
||||||
m_update_qty_query,
|
|
||||||
m_count_ref_query;
|
|
||||||
QString m_custom_query;
|
QString m_custom_query;
|
||||||
|
QHash <QString, QString> m_export_info;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // BOMExportDialog_H
|
#endif // BOMExportDialog_H
|
||||||
|
|||||||
@@ -329,7 +329,7 @@
|
|||||||
<string>Inclure les en-têtes</string>
|
<string>Inclure les en-têtes</string>
|
||||||
</property>
|
</property>
|
||||||
<property name="checked">
|
<property name="checked">
|
||||||
<bool>false</bool>
|
<bool>true</bool>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
|
|||||||
Reference in New Issue
Block a user