xbmc
DatabaseQuery.h
1 /*
2  * Copyright (C) 2013-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include <memory>
12 #include <set>
13 #include <string>
14 #include <vector>
15 
16 #define DATABASEQUERY_RULE_VALUE_SEPARATOR " / "
17 
18 class CDatabase;
19 class CVariant;
20 class TiXmlNode;
21 
23 {
24 public:
26  virtual ~CDatabaseQueryRule() = default;
27 
28  enum SEARCH_OPERATOR
29  {
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
47  };
48 
49  enum FIELD_TYPE
50  {
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
59  };
60 
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;
65 
66  static std::string GetLocalizedOperator(SEARCH_OPERATOR oper);
67  static void GetAvailableOperators(std::vector<std::string>& operatorList);
68 
69  std::string GetParameter() const;
70  void SetParameter(const std::string& value);
71  void SetParameter(const std::vector<std::string>& values);
72 
73  virtual std::string GetWhereClause(const CDatabase& db, const std::string& strType) const;
74 
75  int m_field;
76  SEARCH_OPERATOR m_operator;
77  std::vector<std::string> m_parameter;
78 
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
97  {
98  return "";
99  }
100 
101  static SEARCH_OPERATOR TranslateOperator(const char* oper);
102  static std::string TranslateOperator(SEARCH_OPERATOR oper);
103 };
104 
106 
107 typedef std::vector<std::shared_ptr<CDatabaseQueryRule>> CDatabaseQueryRules;
108 typedef std::vector<std::shared_ptr<CDatabaseQueryRuleCombination>> CDatabaseQueryRuleCombinations;
109 
111 {
112 public:
113  virtual ~IDatabaseQueryRuleFactory() = default;
114  virtual CDatabaseQueryRule* CreateRule() const = 0;
115  virtual CDatabaseQueryRuleCombination* CreateCombination() const = 0;
116 };
117 
119 {
120 public:
121  virtual ~CDatabaseQueryRuleCombination() = default;
122 
123  typedef enum
124  {
125  CombinationOr = 0,
126  CombinationAnd
127  } Combination;
128 
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;
134 
135  std::string GetWhereClause(const CDatabase& db, const std::string& strType) const;
136  std::string TranslateCombinationType() const;
137 
138  Combination GetType() const { return m_type; }
139  void SetType(Combination combination) { m_type = combination; }
140 
141  bool empty() const { return m_combinations.empty() && m_rules.empty(); }
142 
143 protected:
144  friend class CGUIDialogSmartPlaylistEditor;
145  friend class CGUIDialogMediaFilter;
146 
147  Combination m_type = CombinationAnd;
148  CDatabaseQueryRuleCombinations m_combinations;
149  CDatabaseQueryRules m_rules;
150 };
Definition: DatabaseQuery.h:118
Definition: DatabaseQuery.h:22
Definition: Variant.h:29
Definition: GUIDialogSmartPlaylistEditor.h:16
Definition: DatabaseQuery.h:110
Definition: Database.h:26
Definition: GUIDialogMediaFilter.h:28