From 159605e2f85156af48e70ce8a3c267240bf084af Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Thu, 15 Oct 2015 22:07:14 +0200 Subject: [PATCH] Automatically add % wildcards to filter queries Automatically add a % wildcard to the beginning and the end of a filter query if the user hasn't typed in any wildcards by their own. See issue #415. --- src/sqlitetablemodel.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/sqlitetablemodel.cpp b/src/sqlitetablemodel.cpp index 9501c27e..d1de79fd 100644 --- a/src/sqlitetablemodel.cpp +++ b/src/sqlitetablemodel.cpp @@ -563,10 +563,21 @@ void SqliteTableModel::updateFilter(int column, const QString& value) } else { // Keep the default LIKE operator + // Set the escape character if one has been specified in the settings dialog QString escape_character = PreferencesDialog::getSettingsValue("databrowser", "filter_escape").toString(); if(escape_character == "'") escape_character = "''"; if(escape_character.length()) escape = QString("ESCAPE '%1'").arg(escape_character); + + // Add % wildcards at the start and at the beginning of the filter query, but only if there weren't set any + // wildcards manually. The idea is to assume that a user who's just typing characters expects the wildcards to + // be added but a user who adds them herself knows what she's doing and doesn't want us to mess up her query. + if(!value.contains("%")) + { + val = value; + val.prepend('%'); + val.append('%'); + } } if(val.isEmpty()) val = value; @@ -574,7 +585,7 @@ void SqliteTableModel::updateFilter(int column, const QString& 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(val == "''") + if(val == "''" || val == "'%'" || val == "'%%'") m_mWhere.remove(column); else m_mWhere.insert(column, QString("%1 %2 %3").arg(op).arg(QString(encode(val.toUtf8()))).arg(escape));