Skip to content

Commit

Permalink
DBManager: ORDER BY parameter added to getRecord(s) (#770)
Browse files Browse the repository at this point in the history
-> All instances are now sorted in ascending order using the instance ID
-> The web interface gets an incorrect instance order, which causes further problems
  • Loading branch information
SJunkies authored Jul 12, 2020
1 parent 8c188d7 commit 9110b3e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
8 changes: 5 additions & 3 deletions include/db/DBManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,20 @@ class DBManager : public QObject
/// @param[in] conditions condition to search for (WHERE)
/// @param[out] results results of query
/// @param[in] tColumns target columns to search in (optional) if not provided returns all columns
/// @param[in] tOrder target order columns with order by ASC/DESC (optional)
/// @return True on success else false
///
bool getRecord(const VectorPair& conditions, QVariantMap& results, const QStringList& tColumns = QStringList()) const;
bool getRecord(const VectorPair& conditions, QVariantMap& results, const QStringList& tColumns = QStringList(), const QStringList& tOrder = QStringList()) const;

///
/// @brief Get data of multiple records, you need to specify the columns. This search is without conditions. Good to grab all data from db
/// @param[in] conditions condition to search for (WHERE)
/// @param[out] results results of query
/// @param[in] tColumns target columns to search in (optional) if not provided returns all columns
/// @param[in] tColumns target columns to search in (optional) if not provided returns all columns
/// @param[in] tOrder target order columns with order by ASC/DESC (optional)
/// @return True on success else false
///
bool getRecords(QVector<QVariantMap>& results, const QStringList& tColumns = QStringList()) const;
bool getRecords(QVector<QVariantMap>& results, const QStringList& tColumns = QStringList(), const QStringList& tOrder = QStringList()) const;

///
/// @brief Delete a record determined by conditions
Expand Down
2 changes: 1 addition & 1 deletion include/db/InstanceTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class InstanceTable : public DBManager
inline QVector<QVariantMap> getAllInstances(const bool& justEnabled = false)
{
QVector<QVariantMap> results;
getRecords(results);
getRecords(results, QStringList(), QStringList() << "instance ASC");
if(justEnabled)
{
for (auto it = results.begin(); it != results.end();)
Expand Down
23 changes: 18 additions & 5 deletions libsrc/db/DBManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ bool DBManager::updateRecord(const VectorPair& conditions, const QVariantMap& co
return true;
}

bool DBManager::getRecord(const VectorPair& conditions, QVariantMap& results, const QStringList& tColumns) const
bool DBManager::getRecord(const VectorPair& conditions, QVariantMap& results, const QStringList& tColumns, const QStringList& tOrder) const
{
QSqlDatabase idb = getDB();
QSqlQuery query(idb);
Expand All @@ -195,18 +195,24 @@ bool DBManager::getRecord(const VectorPair& conditions, QVariantMap& results, co
if(!tColumns.isEmpty())
sColumns = tColumns.join(", ");

QString sOrder("");
if(!tOrder.isEmpty())
{
sOrder = " ORDER BY ";
sOrder.append(tOrder.join(", "));
}
// prep conditions
QStringList prepCond;
QVariantList bindVal;
if(!conditions.isEmpty())
prepCond << "WHERE";
prepCond << " WHERE";

for(const auto& pair : conditions)
{
prepCond << pair.first+"=?";
bindVal << pair.second;
}
query.prepare(QString("SELECT %1 FROM %2 %3").arg(sColumns,_table).arg(prepCond.join(" ")));
query.prepare(QString("SELECT %1 FROM %2%3%4").arg(sColumns,_table).arg(prepCond.join(" ")).arg(sOrder));
doAddBindValue(query, bindVal);

if(!query.exec())
Expand All @@ -227,7 +233,7 @@ bool DBManager::getRecord(const VectorPair& conditions, QVariantMap& results, co
return true;
}

bool DBManager::getRecords(QVector<QVariantMap>& results, const QStringList& tColumns) const
bool DBManager::getRecords(QVector<QVariantMap>& results, const QStringList& tColumns, const QStringList& tOrder) const
{
QSqlDatabase idb = getDB();
QSqlQuery query(idb);
Expand All @@ -237,7 +243,14 @@ bool DBManager::getRecords(QVector<QVariantMap>& results, const QStringList& tCo
if(!tColumns.isEmpty())
sColumns = tColumns.join(", ");

query.prepare(QString("SELECT %1 FROM %2").arg(sColumns,_table));
QString sOrder("");
if(!tOrder.isEmpty())
{
sOrder = " ORDER BY ";
sOrder.append(tOrder.join(", "));
}

query.prepare(QString("SELECT %1 FROM %2%3").arg(sColumns,_table,sOrder));

if(!query.exec())
{
Expand Down

0 comments on commit 9110b3e

Please sign in to comment.