Compare commits

...

2 Commits

Author SHA1 Message Date
nou 305c1d1f55 Deffered SQL query when database is visible 2026-03-21 20:33:47 +01:00
nou 95808b094d Fix buidling query 2026-03-21 20:31:44 +01:00
2 changed files with 30 additions and 7 deletions
+27 -7
View File
@@ -157,13 +157,25 @@ void FITSFileModel::filesUnmarked(const QModelIndexList &indexes)
}
}
void FITSFileModel::load()
{
if(!m_loaded)
{
m_loaded = true;
prepareQuery();
}
}
void FITSFileModel::prepareQuery()
{
if(!m_loaded)return;
QString cols;
QString join;
QStringList where;
QString sql = m_columns.size() ? "SELECT f.file," : "SELECT f.file";
QVariantList bindValues;
QVariantList bindValuesJoin;
for(int i=0; i<m_value.size(); i++)
{
if(m_key[i] == "file")
@@ -195,9 +207,9 @@ void FITSFileModel::prepareQuery()
}
else
{
join += QString(" JOIN fits_headers AS s%1 ON f.id=s%1.id_file AND s%1.key=? AND s%1.value LIKE ?").arg(i);
bindValues.append(m_key[i]);
bindValues.append(m_value[i]);
join += QString(" JOIN fits_headers AS s%1 ON f.id=s%1.id_file AND s%1.key=? AND s%1.value LIKE ? ").arg(i);
bindValuesJoin.append(m_key[i]);
bindValuesJoin.append(m_value[i]);
}
}
int i=0;
@@ -205,7 +217,7 @@ void FITSFileModel::prepareQuery()
{
cols += QString("GROUP_CONCAT(h%1.value) AS h%1_value,").arg(i);
join += QString(" LEFT JOIN fits_headers AS h%1 ON f.id=h%1.id_file AND h%1.key=?").arg(i);
bindValues.append(column);
bindValuesJoin.append(column);
i++;
}
cols.chop(1);
@@ -217,11 +229,13 @@ void FITSFileModel::prepareQuery()
QSqlQuery query(m_database->db());
query.prepare(sql);
for(int i = 0; i < bindValues.size(); i++)
query.bindValue(i, bindValues[i]);
for(auto &val : bindValuesJoin)
query.addBindValue(val);
for(auto &val : bindValues)
query.addBindValue(val);
if(!query.exec())
qWarning() << "Failed to exectute query" << query.lastQuery();
qWarning() << "Failed to exectute query" << query.lastQuery() << bindValuesJoin << bindValues;
setQuery(std::move(query));
setHeaderData(0, Qt::Horizontal, tr("File name"));
@@ -424,6 +438,7 @@ bool DataBaseView::exportCSV(const QString &path)
if(!csv.open(QIODevice::WriteOnly | QIODevice::Text))
return false;
m_model->load();
QSqlQuery sql(m_model->query().lastQuery());
int colCount = m_model->columnCount();
QStringList header;
@@ -452,3 +467,8 @@ bool DataBaseView::exportCSV(const QString &path)
}
return true;
}
void DataBaseView::visible(bool visible)
{
if(visible)m_model->load();
}
+3
View File
@@ -30,6 +30,7 @@ class FITSFileModel : public QSqlQueryModel
QStringList m_limit;
QSet<QString> m_markedFiles;
Database *m_database;
bool m_loaded = false;
public:
explicit FITSFileModel(Database *database, QObject *parent = nullptr);
void sort(int column, Qt::SortOrder order) override;
@@ -38,6 +39,7 @@ public:
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
void filesMarked(const QModelIndexList &indexes);
void filesUnmarked(const QModelIndexList &indexes);
void load();
protected:
void prepareQuery();
};
@@ -74,6 +76,7 @@ public slots:
void itemActivated(const QModelIndex &index);
void applyFilter();
bool exportCSV(const QString &path);
void visible(bool visible);
signals:
void loadFile(QString file);
};