kodi
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()) const;
128  std::string GetSingleValue(const std::string& query) const;
129 
135  std::string GetSingleValue(const std::string& query,
136  const std::unique_ptr<dbiplus::Dataset>& ds) const;
137 
147  int GetSingleValueInt(const std::string& strTable,
148  const std::string& strColumn,
149  const std::string& strWhereClause = std::string(),
150  const std::string& strOrderBy = std::string()) const;
151  int GetSingleValueInt(const std::string& query) const;
152 
158  int GetSingleValueInt(const std::string& query,
159  const std::unique_ptr<dbiplus::Dataset>& ds) const;
160 
167  bool DeleteValues(const std::string& strTable, const Filter& filter = Filter());
168 
177  bool ExecuteQuery(const std::string& strQuery);
178 
185  bool ResultQuery(const std::string& strQuery) const;
186 
196  bool BeginMultipleExecute();
197 
205  bool CommitMultipleExecute();
206 
212  bool QueueInsertQuery(const std::string& strQuery);
213 
218  bool CommitInsertQueries();
219 
224  size_t GetInsertQueriesCount();
225 
231  bool QueueDeleteQuery(const std::string& strQuery);
232 
237  bool CommitDeleteQueries();
238 
243  size_t GetDeleteQueriesCount();
244 
245  virtual bool GetFilter(CDbUrl& dbUrl, Filter& filter, SortDescription& sorting) { return true; }
246  virtual bool BuildSQL(const std::string& strBaseDir,
247  const std::string& strQuery,
248  Filter& filter,
249  std::string& strSQL,
250  CDbUrl& dbUrl);
251  virtual bool BuildSQL(const std::string& strBaseDir,
252  const std::string& strQuery,
253  Filter& filter,
254  std::string& strSQL,
255  CDbUrl& dbUrl,
256  SortDescription& sorting);
257 
258  bool Connect(const std::string& dbName, const DatabaseSettings& db, bool create);
259 
260 protected:
261  friend class CDatabaseManager;
262 
263  void Split(const std::string& strFileNameAndPath, std::string& strPath, std::string& strFileName);
264 
265  virtual bool Open();
266 
270  bool CreateDatabase();
271 
272  /* \brief Create tables for the current database schema.
273  Will be called on database creation.
274  */
275  virtual void CreateTables() = 0;
276 
277  /* \brief Create views, indices and triggers for the current database schema.
278  Will be called on database creation and database update.
279  */
280  virtual void CreateAnalytics() = 0;
281 
282  /* \brief Update database tables to the current version.
283  Note that analytics (views, indices, triggers) are not present during this
284  function, so don't rely on them.
285  */
286  virtual void UpdateTables(int version) {}
287 
288  /* \brief The minimum schema version that we support updating from.
289  */
290  virtual int GetMinSchemaVersion() const { return 0; }
291 
292  /* \brief The current schema version.
293  */
294  virtual int GetSchemaVersion() const = 0;
295  virtual const char* GetBaseDBName() const = 0;
296 
297  int GetDBVersion();
298 
299  bool BuildSQL(const std::string& strQuery, const Filter& filter, std::string& strSQL) const;
300 
301  bool m_sqlite;
302 
303  std::unique_ptr<dbiplus::Database> m_pDB;
304  std::unique_ptr<dbiplus::Dataset> m_pDS;
305  std::unique_ptr<dbiplus::Dataset> m_pDS2;
306 
307 protected:
308  // Construction parameters
309  const CProfileManager& m_profileManager;
310 
311 private:
312  void InitSettings(DatabaseSettings& dbSettings);
313  void UpdateVersionNumber();
314 
315  bool m_bMultiInsert =
316  false;
317  bool m_bMultiDelete =
318  false;
319  unsigned int m_openCount;
320 
321  bool m_multipleExecute;
322  std::vector<std::string> m_multipleQueries;
323 };
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:32
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:301
Definition: DbUrl.h:18
Definition: SortUtils.h:176