Merge pull request #4676 from jmarshallnz/dont_set_scraper_on_tvshow_on_nfo
[vuplus_xbmc] / xbmc / dbwrappers / DatabaseQuery.h
1 #pragma once
2 /*
3  *      Copyright (C) 2013 Team XBMC
4  *      http://xbmc.org
5  *
6  *  This Program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2, or (at your option)
9  *  any later version.
10  *
11  *  This Program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with XBMC; see the file COPYING.  If not, see
18  *  <http://www.gnu.org/licenses/>.
19  *
20  */
21
22 #include <set>
23 #include <vector>
24 #include <boost/shared_ptr.hpp>
25
26 #include "utils/StdString.h"
27
28 class CDatabase;
29 class CVariant;
30 class TiXmlNode;
31
32 class CDatabaseQueryRule
33 {
34 public:
35   CDatabaseQueryRule();
36   virtual ~CDatabaseQueryRule() { };
37
38   enum SEARCH_OPERATOR { OPERATOR_START = 0,
39                          OPERATOR_CONTAINS,
40                          OPERATOR_DOES_NOT_CONTAIN,
41                          OPERATOR_EQUALS,
42                          OPERATOR_DOES_NOT_EQUAL,
43                          OPERATOR_STARTS_WITH,
44                          OPERATOR_ENDS_WITH,
45                          OPERATOR_GREATER_THAN,
46                          OPERATOR_LESS_THAN,
47                          OPERATOR_AFTER,
48                          OPERATOR_BEFORE,
49                          OPERATOR_IN_THE_LAST,
50                          OPERATOR_NOT_IN_THE_LAST,
51                          OPERATOR_TRUE,
52                          OPERATOR_FALSE,
53                          OPERATOR_BETWEEN,
54                          OPERATOR_END
55                        };
56
57   enum FIELD_TYPE { TEXT_FIELD = 0,
58                     NUMERIC_FIELD,
59                     DATE_FIELD,
60                     PLAYLIST_FIELD,
61                     SECONDS_FIELD,
62                     BOOLEAN_FIELD,
63                     TEXTIN_FIELD
64                   };
65
66   virtual bool Load(const TiXmlNode *node, const std::string &encoding = "UTF-8");
67   virtual bool Load(const CVariant &obj);
68   virtual bool Save(TiXmlNode *parent) const;
69   virtual bool Save(CVariant &obj) const;
70
71   static CStdString           GetLocalizedOperator(SEARCH_OPERATOR oper);
72   static void                 GetAvailableOperators(std::vector<std::string> &operatorList);
73
74   CStdString                  GetParameter() const;
75   void                        SetParameter(const CStdString &value);
76   void                        SetParameter(const std::vector<CStdString> &values);
77
78   virtual CStdString          GetWhereClause(const CDatabase &db, const CStdString& strType) const;
79
80   int                         m_field;
81   SEARCH_OPERATOR             m_operator;
82   std::vector<CStdString>     m_parameter;
83
84 protected:
85   virtual CStdString          GetField(int field, const CStdString& type) const=0;
86   virtual FIELD_TYPE          GetFieldType(int field) const=0;
87   virtual int                 TranslateField(const char *field) const=0;
88   virtual CStdString          TranslateField(int field) const=0;
89   CStdString                  ValidateParameter(const CStdString &parameter) const;
90   virtual CStdString          FormatParameter(const CStdString &negate, const CStdString &oper, const CDatabase &db, const CStdString &type) const;
91   virtual CStdString          FormatWhereClause(const CStdString &negate, const CStdString &oper, const CStdString &param,
92                                                 const CDatabase &db, const CStdString &type) const;
93   virtual SEARCH_OPERATOR     GetOperator(const CStdString &type) const { return m_operator; };
94   virtual CStdString          GetOperatorString(SEARCH_OPERATOR op) const;
95   virtual CStdString          GetBooleanQuery(const CStdString &negate, const CStdString &strType) const { return ""; }
96
97   static SEARCH_OPERATOR      TranslateOperator(const char *oper);
98   static CStdString           TranslateOperator(SEARCH_OPERATOR oper);
99 };
100
101 class CDatabaseQueryRuleCombination;
102
103 typedef std::vector< boost::shared_ptr<CDatabaseQueryRule> > CDatabaseQueryRules;
104 typedef std::vector< boost::shared_ptr<CDatabaseQueryRuleCombination> > CDatabaseQueryRuleCombinations;
105
106 class IDatabaseQueryRuleFactory
107 {
108 public:
109   virtual CDatabaseQueryRule *CreateRule() const=0;
110   virtual CDatabaseQueryRuleCombination *CreateCombination() const=0;
111 };
112
113 class CDatabaseQueryRuleCombination
114 {
115 public:
116   CDatabaseQueryRuleCombination();
117   virtual ~CDatabaseQueryRuleCombination() { };
118
119   typedef enum {
120     CombinationOr = 0,
121     CombinationAnd
122   } Combination;
123
124   void clear();
125   virtual bool Load(const TiXmlNode *node, const std::string &encoding = "UTF-8") { return false; }
126   virtual bool Load(const CVariant &obj, const IDatabaseQueryRuleFactory *factory);
127   virtual bool Save(TiXmlNode *parent) const;
128   virtual bool Save(CVariant &obj) const;
129
130   CStdString GetWhereClause(const CDatabase &db, const CStdString& strType) const;
131   std::string TranslateCombinationType() const;
132
133   Combination GetType() const { return m_type; }
134   void SetType(Combination combination) { m_type = combination; }
135
136   bool empty() const { return m_combinations.empty() && m_rules.empty(); }
137
138 protected:
139   friend class CGUIDialogSmartPlaylistEditor;
140   friend class CGUIDialogMediaFilter;
141
142   Combination m_type;
143   CDatabaseQueryRuleCombinations m_combinations;
144   CDatabaseQueryRules m_rules;
145 };