xbmc
Database.h
1 /*
2  * Copyright (C) 2005-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 namespace dbiplus
12 {
13 class Database;
14 class Dataset;
15 } // namespace dbiplus
16 
17 #include <memory>
18 #include <string>
19 #include <vector>
20 
21 class DatabaseSettings; // forward
22 class CDbUrl;
23 class CProfileManager;
24 struct SortDescription;
25 
26 class CDatabase
27 {
28 public:
29  class Filter
30  {
31  public:
32  Filter() : fields("*") {}
33  explicit Filter(const char* w) : fields("*"), where(w) {}
34  explicit Filter(const std::string& w) : fields("*"), where(w) {}
35 
36  void AppendField(const std::string& strField);
37  void AppendJoin(const std::string& strJoin);
38  void AppendWhere(const std::string& strWhere, bool combineWithAnd = true);
39  void AppendOrder(const std::string& strOrder);
40  void AppendGroup(const std::string& strGroup);
41 
42  std::string fields;
43  std::string join;
44  std::string where;
45  std::string order;
46  std::string group;
47  std::string limit;
48  };
49 
51  {
52  DatasetFieldInfo(bool fetch, bool output, int recno)
53  : fetch(fetch), output(output), recno(recno)
54  {
55  }
56 
57  bool fetch;
58  bool output;
59  int recno;
60  std::string strField;
61  };
62 
64  {
65  public:
66  DatasetLayout(size_t totalfields);
67  void SetField(int fieldNo, const std::string& strField, bool bOutput = false);
68  void AdjustRecordNumbers(int offset);
69  bool GetFetch(int fieldno);
70  void SetFetch(int fieldno, bool bFetch = true);
71  bool GetOutput(int fieldno);
72  int GetRecNo(int fieldno);
73  const std::string GetFields();
74  bool HasFilterFields();
75 
76  private:
77  std::vector<DatasetFieldInfo> m_fields;
78  };
79 
81  {
82  public:
83  explicit ExistsSubQuery(const std::string& table) : tablename(table) {}
84  ExistsSubQuery(const std::string& table, const std::string& parameter)
85  : tablename(table), param(parameter)
86  {
87  }
88  void AppendJoin(const std::string& strJoin);
89  void AppendWhere(const std::string& strWhere, bool combineWithAnd = true);
90  bool BuildSQL(std::string& strSQL);
91 
92  std::string tablename;
93  std::string param;
94  std::string join;
95  std::string where;
96  };
97 
98  CDatabase();
99  virtual ~CDatabase(void);
100  bool IsOpen();
101  virtual void Close();
102  bool Compress(bool bForce = true);
103  void Interrupt();
104 
105  bool Open(const DatabaseSettings& db);
106 
107  void BeginTransaction();
108  virtual bool CommitTransaction();
109  void RollbackTransaction();
110  void CopyDB(const std::string& latestDb);
111  void DropAnalytics();
112 
113  std::string PrepareSQL(std::string strStmt, ...) const;
114 
124  std::string GetSingleValue(const std::string& strTable,
125  const std::string& strColumn,
126  const std::string& strWhereClause = std::string(),
127  const std::string& strOrderBy = std::string());
128  std::string GetSingleValue(const std::string& query);
129 
135  std::string GetSingleValue(const std::string& query, std::unique_ptr<dbiplus::Dataset>& ds);
136 
146  int GetSingleValueInt(const std::string& strTable,
147  const std::string& strColumn,
148  const std::string& strWhereClause = std::string(),
149  const std::string& strOrderBy = std::string());
150  int GetSingleValueInt(const std::string& query);
151 
157  int GetSingleValueInt(const std::string& query, std::unique_ptr<dbiplus::Dataset>& ds);
158 
165  bool DeleteValues(const std::string& strTable, const Filter& filter = Filter());
166 
175  bool ExecuteQuery(const std::string& strQuery);
176 
183  bool ResultQuery(const std::string& strQuery) const;
184 
194  bool BeginMultipleExecute();
195 
203  bool CommitMultipleExecute();
204 
210  bool QueueInsertQuery(const std::string& strQuery);
211 
216  bool CommitInsertQueries();
217 
222  size_t GetInsertQueriesCount();
223 
229  bool QueueDeleteQuery(const std::string& strQuery);
230 
235  bool CommitDeleteQueries();
236 
241  size_t GetDeleteQueriesCount();
242 
243  virtual bool GetFilter(CDbUrl& dbUrl, Filter& filter, SortDescription& sorting) { return true; }
244  virtual bool BuildSQL(const std::string& strBaseDir,
245  const std::string& strQuery,
246  Filter& filter,
247  std::string& strSQL,
248  CDbUrl& dbUrl);
249  virtual bool BuildSQL(const std::string& strBaseDir,
250  const std::string& strQuery,
251  Filter& filter,
252  std::string& strSQL,
253  CDbUrl& dbUrl,
254  SortDescription& sorting);
255 
256  bool Connect(const std::string& dbName, const DatabaseSettings& db, bool create);
257 
258 protected:
259  friend class CDatabaseManager;
260 
261  void Split(const std::string& strFileNameAndPath, std::string& strPath, std::string& strFileName);
262 
263  virtual bool Open();
264 
268  bool CreateDatabase();
269 
270  /* \brief Create tables for the current database schema.
271  Will be called on database creation.
272  */
273  virtual void CreateTables() = 0;
274 
275  /* \brief Create views, indices and triggers for the current database schema.
276  Will be called on database creation and database update.
277  */
278  virtual void CreateAnalytics() = 0;
279 
280  /* \brief Update database tables to the current version.
281  Note that analytics (views, indices, triggers) are not present during this
282  function, so don't rely on them.
283  */
284  virtual void UpdateTables(int version) {}
285 
286  /* \brief The minimum schema version that we support updating from.
287  */
288  virtual int GetMinSchemaVersion() const { return 0; }
289 
290  /* \brief The current schema version.
291  */
292  virtual int GetSchemaVersion() const = 0;
293  virtual const char* GetBaseDBName() const = 0;
294 
295  int GetDBVersion();
296 
297  bool BuildSQL(const std::string& strQuery, const Filter& filter, std::string& strSQL);
298 
299  bool m_sqlite;
300 
301  std::unique_ptr<dbiplus::Database> m_pDB;
302  std::unique_ptr<dbiplus::Dataset> m_pDS;
303  std::unique_ptr<dbiplus::Dataset> m_pDS2;
304 
305 protected:
306  // Construction parameters
307  const CProfileManager& m_profileManager;
308 
309 private:
310  void InitSettings(DatabaseSettings& dbSettings);
311  void UpdateVersionNumber();
312 
313  bool m_bMultiInsert =
314  false;
315  bool m_bMultiDelete =
316  false;
317  unsigned int m_openCount;
318 
319  bool m_multipleExecute;
320  std::vector<std::string> m_multipleQueries;
321 };
Definition: Database.h:80
Definition: Database.h:29
Definition: Database.h:11
Database manager class for handling database updating.
Definition: DatabaseManager.h:28
Definition: AdvancedSettings.h:38
Definition: Database.h:50
Definition: Database.h:63
Definition: Database.h:26
Definition: ProfileManager.h:25
Definition: SmartPlayList.cpp:137
bool m_sqlite
whether we use sqlite (defaults to true)
Definition: Database.h:299
Definition: DbUrl.h:18
Definition: SortUtils.h:176