Escape special characters for like sql searches

This commit is contained in:
emeric
2021-05-02 15:10:32 +02:00
parent a6398738bb
commit 444d4af3c6
27 changed files with 261 additions and 68 deletions
+1
View File
@@ -13,6 +13,7 @@ add_library(lmsdatabase SHARED
impl/Track.cpp
impl/TrackBookmark.cpp
impl/User.cpp
impl/Utils.cpp
)
target_include_directories(lmsdatabase INTERFACE
+9 -9
View File
@@ -27,7 +27,7 @@
#include "database/User.hpp"
#include "utils/Logger.hpp"
#include "SqlQuery.hpp"
#include "Utils.hpp"
namespace Database
{
@@ -83,7 +83,7 @@ Wt::Dbo::Query<T>
createQuery(Session& session,
const std::string& queryStr,
const std::set<IdType>& clusterIds,
const std::vector<std::string>& keywords,
const std::vector<std::string_view>& keywords,
std::optional<TrackArtistLinkType> linkType)
{
session.checkSharedLocked();
@@ -100,16 +100,16 @@ createQuery(Session& session,
std::vector<std::string> clauses;
std::vector<std::string> sortClauses;
for (const std::string& keyword : keywords)
for (std::string_view keyword : keywords)
{
clauses.push_back("a.name LIKE ?");
query.bind("%%" + keyword + "%%");
clauses.push_back("a.name LIKE ? ESCAPE '" ESCAPE_CHAR_STR "'");
query.bind("%%" + escapeLikeKeyword(keyword) + "%%");
}
for (const std::string& keyword : keywords)
for (std::string_view keyword : keywords)
{
sortClauses.push_back("a.sort_name LIKE ?");
query.bind("%%" + keyword + "%%");
sortClauses.push_back("a.sort_name LIKE ? ESCAPE '" ESCAPE_CHAR_STR "'");
query.bind("%%" + escapeLikeKeyword(keyword) + "%%");
}
query.where("(" + StringUtils::joinStrings(clauses, " AND ") + ") OR (" + StringUtils::joinStrings(sortClauses, " AND ") + ")");
@@ -267,7 +267,7 @@ Artist::getByClusters(Session& session, const std::set<IdType>& clusters, SortMe
std::vector<Artist::pointer>
Artist::getByFilter(Session& session,
const std::set<IdType>& clusters,
const std::vector<std::string>& keywords,
const std::vector<std::string_view>& keywords,
std::optional<TrackArtistLinkType> linkType,
SortMethod sortMethod,
std::optional<Range> range,
+5 -4
View File
@@ -28,6 +28,7 @@
#include "database/User.hpp"
#include "utils/Logger.hpp"
#include "SqlQuery.hpp"
#include "Utils.hpp"
namespace Database
{
@@ -38,14 +39,14 @@ Wt::Dbo::Query<T>
createQuery(Session& session,
const std::string& queryStr,
const std::set<IdType>& clusterIds,
const std::vector<std::string>& keywords)
const std::vector<std::string_view>& keywords)
{
auto query {session.getDboSession().query<T>(queryStr)};
query.join("track t ON t.release_id = r.id");
for (const std::string& keyword : keywords)
query.where("r.name LIKE ?").bind("%%" + keyword + "%%");
for (std::string_view keyword : keywords)
query.where("r.name LIKE ? ESCAPE '" ESCAPE_CHAR_STR "'").bind("%%" + escapeLikeKeyword(keyword) + "%%");
if (!clusterIds.empty())
{
@@ -300,7 +301,7 @@ Release::getByClusters(Session& session, const std::set<IdType>& clusters)
std::vector<Release::pointer>
Release::getByFilter(Session& session,
const std::set<IdType>& clusterIds,
const std::vector<std::string>& keywords,
const std::vector<std::string_view>& keywords,
std::optional<Range> range,
bool& moreResults)
{
+5 -4
View File
@@ -31,6 +31,7 @@
#include "SqlQuery.hpp"
#include "StringViewTraits.hpp"
#include "Utils.hpp"
namespace Database {
@@ -40,14 +41,14 @@ Wt::Dbo::Query<T>
createQuery(Session& session,
const std::string& queryStr,
const std::set<IdType>& clusterIds,
const std::vector<std::string>& keywords)
const std::vector<std::string_view>& keywords)
{
session.checkSharedLocked();
auto query {session.getDboSession().query<T>(queryStr)};
for (const std::string& keyword : keywords)
query.where("t.name LIKE ?").bind("%%" + keyword + "%%");
for (std::string_view keyword : keywords)
query.where("t.name LIKE ? ESCAPE '" ESCAPE_CHAR_STR "'").bind("%%" + escapeLikeKeyword(keyword) + "%%");
if (!clusterIds.empty())
{
@@ -333,7 +334,7 @@ Track::hasTrackFeatures() const
std::vector<Track::pointer>
Track::getByFilter(Session& session,
const std::set<IdType>& clusterIds,
const std::vector<std::string>& keywords,
const std::vector<std::string_view>& keywords,
std::optional<Range> range,
bool& moreResults)
{
+33
View File
@@ -0,0 +1,33 @@
/*
* Copyright (C) 2021 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Utils.hpp"
#include "utils/String.hpp"
namespace Database
{
std::string
escapeLikeKeyword(std::string_view keyword)
{
return StringUtils::escapeString(keyword, "*_", escapeChar);
}
} // namespace Database
+33
View File
@@ -0,0 +1,33 @@
/*
* Copyright (C) 2021 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <string>
#include <string_view>
#include <vector>
namespace Database
{
#define ESCAPE_CHAR_STR "\\"
static constexpr char escapeChar {'\\'};
std::string escapeLikeKeyword(std::string_view keywords);
} // namespace Database
@@ -21,6 +21,7 @@
#include <optional>
#include <string>
#include <string_view>
#include <unordered_set>
#include <vector>
@@ -69,7 +70,7 @@ class Artist : public Wt::Dbo::Dbo<Artist>
);
static std::vector<pointer> getByFilter(Session& session,
const std::set<IdType>& clusters, // if non empty, at least one artist that belongs to these clusters
const std::vector<std::string>& keywords, // if non empty, name must match all of these keywords (name + sort name fields)
const std::vector<std::string_view>& keywords, // if non empty, name must match all of these keywords (name + sort name fields)
std::optional<TrackArtistLinkType> linkType, // if set, only artists that have produced at least one track with this link type
SortMethod sortMethod,
std::optional<Range> range,
@@ -66,7 +66,7 @@ class Release : public Wt::Dbo::Dbo<Release>
static std::vector<pointer> getByClusters(Session& session, const std::set<IdType>& clusters);
static std::vector<pointer> getByFilter(Session& session,
const std::set<IdType>& clusters, // if non empty, at least one release that belongs to these clusters
const std::vector<std::string>& keywords, // if non empty, name must match all of these keywords
const std::vector<std::string_view>& keywords, // if non empty, name must match all of these keywords
std::optional<Range> range,
bool& moreExpected);
static std::vector<IdType> getAllIdsWithClusters(Session& session, std::optional<std::size_t> limit = {});
+1 -1
View File
@@ -71,7 +71,7 @@ class Track : public Wt::Dbo::Dbo<Track>
const std::set<IdType>& clusters); // tracks that belong to these clusters
static std::vector<pointer> getByFilter(Session& session,
const std::set<IdType>& clusters, // if non empty, tracks that belong to these clusters
const std::vector<std::string>& keywords, // if non empty, name must match all of these keywords
const std::vector<std::string_view>& keywords, // if non empty, name must match all of these keywords
std::optional<Range> range,
bool& moreExpected);
static std::vector<pointer> getByNameAndReleaseName(Session& session, std::string_view trackName, std::string_view releaseName);