[Test] Added tests for CUtil::SplitParams
[xbmc.git] / xbmc / dbwrappers / DatabaseQuery.h
bloba15f377782cfd50771e1481d7dd89885112b0880
1 /*
2 * Copyright (C) 2013-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #pragma once
11 #include <memory>
12 #include <set>
13 #include <string>
14 #include <vector>
16 #define DATABASEQUERY_RULE_VALUE_SEPARATOR " / "
18 class CDatabase;
19 class CVariant;
20 class TiXmlNode;
22 class CDatabaseQueryRule
24 public:
25 CDatabaseQueryRule();
26 virtual ~CDatabaseQueryRule() = default;
28 enum SEARCH_OPERATOR
30 OPERATOR_START = 0,
31 OPERATOR_CONTAINS,
32 OPERATOR_DOES_NOT_CONTAIN,
33 OPERATOR_EQUALS,
34 OPERATOR_DOES_NOT_EQUAL,
35 OPERATOR_STARTS_WITH,
36 OPERATOR_ENDS_WITH,
37 OPERATOR_GREATER_THAN,
38 OPERATOR_LESS_THAN,
39 OPERATOR_AFTER,
40 OPERATOR_BEFORE,
41 OPERATOR_IN_THE_LAST,
42 OPERATOR_NOT_IN_THE_LAST,
43 OPERATOR_TRUE,
44 OPERATOR_FALSE,
45 OPERATOR_BETWEEN,
46 OPERATOR_END
49 enum FIELD_TYPE
51 TEXT_FIELD = 0,
52 REAL_FIELD,
53 NUMERIC_FIELD,
54 DATE_FIELD,
55 PLAYLIST_FIELD,
56 SECONDS_FIELD,
57 BOOLEAN_FIELD,
58 TEXTIN_FIELD
61 virtual bool Load(const TiXmlNode* node, const std::string& encoding = "UTF-8");
62 virtual bool Load(const CVariant& obj);
63 virtual bool Save(TiXmlNode* parent) const;
64 virtual bool Save(CVariant& obj) const;
66 static std::string GetLocalizedOperator(SEARCH_OPERATOR oper);
67 static void GetAvailableOperators(std::vector<std::string>& operatorList);
69 std::string GetParameter() const;
70 void SetParameter(const std::string& value);
71 void SetParameter(const std::vector<std::string>& values);
73 virtual std::string GetWhereClause(const CDatabase& db, const std::string& strType) const;
75 int m_field;
76 SEARCH_OPERATOR m_operator;
77 std::vector<std::string> m_parameter;
79 protected:
80 virtual std::string GetField(int field, const std::string& type) const = 0;
81 virtual FIELD_TYPE GetFieldType(int field) const = 0;
82 virtual int TranslateField(const char* field) const = 0;
83 virtual std::string TranslateField(int field) const = 0;
84 std::string ValidateParameter(const std::string& parameter) const;
85 virtual std::string FormatParameter(const std::string& negate,
86 const std::string& oper,
87 const CDatabase& db,
88 const std::string& type) const;
89 virtual std::string FormatWhereClause(const std::string& negate,
90 const std::string& oper,
91 const std::string& param,
92 const CDatabase& db,
93 const std::string& type) const;
94 virtual SEARCH_OPERATOR GetOperator(const std::string& type) const { return m_operator; }
95 virtual std::string GetOperatorString(SEARCH_OPERATOR op) const;
96 virtual std::string GetBooleanQuery(const std::string& negate, const std::string& strType) const
98 return "";
101 static SEARCH_OPERATOR TranslateOperator(const char* oper);
102 static std::string TranslateOperator(SEARCH_OPERATOR oper);
105 class CDatabaseQueryRuleCombination;
107 typedef std::vector<std::shared_ptr<CDatabaseQueryRule>> CDatabaseQueryRules;
108 typedef std::vector<std::shared_ptr<CDatabaseQueryRuleCombination>> CDatabaseQueryRuleCombinations;
110 class IDatabaseQueryRuleFactory
112 public:
113 virtual ~IDatabaseQueryRuleFactory() = default;
114 virtual CDatabaseQueryRule* CreateRule() const = 0;
115 virtual CDatabaseQueryRuleCombination* CreateCombination() const = 0;
118 class CDatabaseQueryRuleCombination
120 public:
121 virtual ~CDatabaseQueryRuleCombination() = default;
123 typedef enum
125 CombinationOr = 0,
126 CombinationAnd
127 } Combination;
129 void clear();
130 virtual bool Load(const TiXmlNode* node, const std::string& encoding = "UTF-8") { return false; }
131 virtual bool Load(const CVariant& obj, const IDatabaseQueryRuleFactory* factory);
132 virtual bool Save(TiXmlNode* parent) const;
133 virtual bool Save(CVariant& obj) const;
135 std::string GetWhereClause(const CDatabase& db, const std::string& strType) const;
136 std::string TranslateCombinationType() const;
138 Combination GetType() const { return m_type; }
139 void SetType(Combination combination) { m_type = combination; }
141 bool empty() const { return m_combinations.empty() && m_rules.empty(); }
143 protected:
144 friend class CGUIDialogSmartPlaylistEditor;
145 friend class CGUIDialogMediaFilter;
147 Combination m_type = CombinationAnd;
148 CDatabaseQueryRuleCombinations m_combinations;
149 CDatabaseQueryRules m_rules;