diff options
author | Uwe Klotz <uwe_klotz@web.de> | 2017-01-31 19:21:21 +0100 |
---|---|---|
committer | Uwe Klotz <uwe_klotz@web.de> | 2017-01-31 19:30:25 +0100 |
commit | 372a2784a84891cc1134ac8c072fb264c5b5ddfa (patch) | |
tree | 7e2df6a0ef080d1731056a67408a18bc502bd80d /src | |
parent | 845994e3ff64ccb50b35e3605b4b405833e3f6fe (diff) |
Reduce code duplication
Diffstat (limited to 'src')
-rw-r--r-- | src/library/dao/directorydao.cpp | 5 | ||||
-rw-r--r-- | src/library/queryutil.h | 28 | ||||
-rw-r--r-- | src/test/queryutiltest.cpp | 3 | ||||
-rw-r--r-- | src/util/db/sqllikewildcardescaper.cpp | 28 | ||||
-rw-r--r-- | src/util/db/sqllikewildcardescaper.h | 2 |
5 files changed, 26 insertions, 40 deletions
diff --git a/src/library/dao/directorydao.cpp b/src/library/dao/directorydao.cpp index dfc907ee7d..a0f43bfd78 100644 --- a/src/library/dao/directorydao.cpp +++ b/src/library/dao/directorydao.cpp @@ -7,6 +7,8 @@ #include "library/dao/directorydao.h" #include "library/queryutil.h" +#include "util/db/sqllikewildcardescaper.h" + DirectoryDAO::DirectoryDAO(QSqlDatabase& database) : m_database(database) { } @@ -106,10 +108,9 @@ QSet<TrackId> DirectoryDAO::relocateDirectory(const QString& oldFolder, return QSet<TrackId>(); } - FieldEscaper escaper(m_database); // on Windows the absolute path starts with the drive name // we also need to check for that - QString startsWithOldFolder = escaper.escapeStringForLike( + QString startsWithOldFolder = SqlLikeWildcardEscaper::escapeStringForLike( QDir(oldFolder).absolutePath() + "/", '%') + "%"; // Also update information in the track_locations table. This is where mixxx diff --git a/src/library/queryutil.h b/src/library/queryutil.h index 878772fb5e..a0742ac83b 100644 --- a/src/library/queryutil.h +++ b/src/library/queryutil.h @@ -4,6 +4,7 @@ #include <QtDebug> #include <QtSql> + #define LOG_FAILED_QUERY(query) qDebug() << __FILE__ << __LINE__ << "FAILED QUERY [" \ << (query).executedQuery() << "]" << (query).lastError() @@ -65,14 +66,12 @@ class ScopedTransaction { bool m_active; }; -class FieldEscaper { +class FieldEscaper final { public: FieldEscaper(const QSqlDatabase& database) : m_database(database), m_stringField("string", QVariant::String) { } - virtual ~FieldEscaper() { - } // Escapes a string for use in a SQL query by wrapping with quotes and // escaping embedded quote characters. @@ -87,6 +86,7 @@ class FieldEscaper { return result; } + private: void escapeStringsInPlace(QStringList* pEscapeStrings) const { QMutableStringListIterator it(*pEscapeStrings); while (it.hasNext()) { @@ -94,28 +94,8 @@ class FieldEscaper { } } - // Escapes a string for use in a LIKE operation by prefixing instances of - // LIKE wildcard characters (% and _) with escapeCharacter. This allows the - // caller to then attach wildcard characters to the string. This does NOT - // escape the string in the same way that escapeString() does. - QString escapeStringForLike(const QString& escapeString, const QChar escapeCharacter) const { - QString escapeCharacterStr(escapeCharacter); - QString result = escapeString; - // Replace instances of escapeCharacter with two escapeCharacters. - result = result.replace( - escapeCharacter, escapeCharacterStr + escapeCharacterStr); - // Replace instances of % or _ with $escapeCharacter%. - if (escapeCharacter != '%') { - result = result.replace("%", escapeCharacterStr + "%"); - } - if (escapeCharacter != '_') { - result = result.replace("_", escapeCharacterStr + "_"); - } - return result; - } - - private: const QSqlDatabase& m_database; mutable QSqlField m_stringField; }; + #endif /* QUERYUTIL_H */ diff --git a/src/test/queryutiltest.cpp b/src/test/queryutiltest.cpp index 6e090e2941..f2830e50cd 100644 --- a/src/test/queryutiltest.cpp +++ b/src/test/queryutiltest.cpp @@ -4,6 +4,7 @@ #include <QTemporaryFile> #include "library/queryutil.h" +#include "util/db/sqllikewildcardescaper.h" class QueryUtilTest : public testing::Test {}; @@ -41,5 +42,5 @@ TEST_F(QueryUtilTest, FieldEscaperEscapesForLike) { FieldEscaper f(db); EXPECT_STREQ(qPrintable(QString("xx44xx4%yy4_yy")), - qPrintable(f.escapeStringForLike("xx4xx%yy_yy", '4'))); + qPrintable(SqlLikeWildcardEscaper::escapeStringForLike("xx4xx%yy_yy", '4'))); } diff --git a/src/util/db/sqllikewildcardescaper.cpp b/src/util/db/sqllikewildcardescaper.cpp index ef7abd3dbd..7acdbfc1e3 100644 --- a/src/util/db/sqllikewildcardescaper.cpp +++ b/src/util/db/sqllikewildcardescaper.cpp @@ -3,18 +3,22 @@ #include "util/db/sqllikewildcards.h" -QString SqlLikeWildcardEscaper::apply(const QString& escapeString, QChar escapeCharacter) { - QString escapeCharacterStr(escapeCharacter); - QString result = escapeString; - // Replace instances of escapeCharacter with two escapeCharacters. - result = result.replace( - escapeCharacter, escapeCharacterStr + escapeCharacterStr); - // Replace instances of % or _ with $escapeCharacter%. - if (escapeCharacter != '%') { - result = result.replace("%", escapeCharacterStr + "%"); +//static +QString SqlLikeWildcardEscaper::escapeStringForLike( + const QString& unescapedString, + QChar escapeCharacter) { + QString escapePrefix(escapeCharacter); + QString escapedString = unescapedString; + // Duplicate all occurrences of escapeCharacter, i.e. prefix escapeCharacter + // with itself + escapedString.replace(escapeCharacter, escapePrefix + escapeCharacter); + // Prefix all occurrences of LIKE wildcards other than the escapeCharacter + // itself with escapeCharacter + if (escapeCharacter != kSqlLikeMatchAll) { + escapedString.replace(kSqlLikeMatchAll, escapePrefix + kSqlLikeMatchAll); } - if (escapeCharacter != '_') { - result = result.replace("_", escapeCharacterStr + "_"); + if (escapeCharacter != kSqlLikeMatchOne) { + escapedString.replace(kSqlLikeMatchOne, escapePrefix + kSqlLikeMatchOne); } - return result; + return escapedString; } diff --git a/src/util/db/sqllikewildcardescaper.h b/src/util/db/sqllikewildcardescaper.h index 56a10e135f..06ebc88fb2 100644 --- a/src/util/db/sqllikewildcardescaper.h +++ b/src/util/db/sqllikewildcardescaper.h @@ -12,7 +12,7 @@ class SqlLikeWildcardEscaper final { // LIKE wildcard characters (% and _) with escapeCharacter. This allows the // caller to then attach wildcard characters to the string. This does NOT // escape the string in the same way that SqlStringFormatter does! - static QString apply(const QString& escapeString, QChar escapeCharacter); + static QString apply(const QString& unescapedString, QChar escapeCharacter); private: SqlLikeWildcardEscaper() = delete; // utility class |