From 44a635167d199485dc5210577c4fd0905f85c459 Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Tue, 9 Apr 2013 19:28:21 +0200 Subject: [PATCH] Allow different comparison operators in the new filter row Allow other comparison methods than just the LIKE operator when using the new filter row. --- src/sqlitetablemodel.cpp | 35 +++++++++++++++++++++++++++++++++-- src/sqlitetablemodel.h | 2 +- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/sqlitetablemodel.cpp b/src/sqlitetablemodel.cpp index 4689f1c2..87e48350 100644 --- a/src/sqlitetablemodel.cpp +++ b/src/sqlitetablemodel.cpp @@ -225,13 +225,44 @@ void SqliteTableModel::buildQuery() setQuery(sql); } -void SqliteTableModel::updateFilter(int column, QString value) +void SqliteTableModel::updateFilter(int column, const QString& value) { + // Check for any special comparison operators at the beginning of the value string. If there are none default to LIKE. + QString op = "LIKE"; + QString val; + if(value.left(2) == ">=" || value.left(2) == "<=" || value.left(2) == "<>") + { + bool ok; + value.mid(2).toFloat(&ok); + if(ok) + { + op = value.left(2); + val = value.mid(2); + } + } else if(value.left(1) == ">" || value.left(1) == "<") { + bool ok; + value.mid(1).toFloat(&ok); + if(ok) + { + op = value.left(1); + val = value.mid(1); + } + } else { + if(value.left(1) == "=") + { + op = "="; + val = value.mid(1); + } else { + val = value; + } + val = QString("'%1'").arg(val.replace("'", "")); + } + // If the value was set to an empty string remove any filter for this column. Otherwise insert a new filter rule or replace the old one if there is already one if(value.isEmpty()) m_mWhere.remove(column); else - m_mWhere.insert(column, QString("LIKE '%1'").arg(value.replace('\'', ""))); // TODO: Add some code here to detect fancy filter values like '>5' and change the operator in these cases + m_mWhere.insert(column, QString("%1 %2").arg(op).arg(val)); // Build the new query buildQuery(); diff --git a/src/sqlitetablemodel.h b/src/sqlitetablemodel.h index c9d86c36..7644e0a1 100644 --- a/src/sqlitetablemodel.h +++ b/src/sqlitetablemodel.h @@ -35,7 +35,7 @@ public: signals: public slots: - void updateFilter(int column, QString value); + void updateFilter(int column, const QString& value); private: void fetchData(unsigned int from, unsigned to);