| 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 |
|
| 50 |
struct DatasetFieldInfo |
| 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 |
|
| 63 |
class DatasetLayout |
| 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 |
|
| 80 |
class ExistsSubQuery |
| 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 |
|
| 115 |
/*! |
| 116 |
* @brief Get a single value from a table. |
| 117 |
* @remarks The values of the strWhereClause and strOrderBy parameters have to be FormatSQL'ed when used. |
| 118 |
* @param strTable The table to get the value from. |
| 119 |
* @param strColumn The column to get. |
| 120 |
* @param strWhereClause If set, use this WHERE clause. |
| 121 |
* @param strOrderBy If set, use this ORDER BY clause. |
| 122 |
* @return The requested value or an empty string if it wasn't found. |
| 123 |
*/ |
| 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 |
|
| 130 |
/*! \brief Get a single value from a query on a dataset. |
| 131 |
\param query the query in question. |
| 132 |
\param ds the dataset to use for the query. |
| 133 |
\return the value from the query, empty on failure. |
| 134 |
*/ |
| 135 |
std::string GetSingleValue(const std::string& query, |
| 136 |
const std::unique_ptr<dbiplus::Dataset>& ds) const; |
| 137 |
|
| 138 |
/*! |
| 139 |
* @brief Get a single integer value from a table. |
| 140 |
* @remarks The values of the strWhereClause and strOrderBy parameters have to be FormatSQL'ed when used. |
| 141 |
* @param strTable The table to get the value from. |
| 142 |
* @param strColumn The column to get. |
| 143 |
* @param strWhereClause If set, use this WHERE clause. |
| 144 |
* @param strOrderBy If set, use this ORDER BY clause. |
| 145 |
* @return The requested value or 0 if it wasn't found. |
| 146 |
*/ |
| 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 |
|
| 153 |
/*! \brief Get a single integer value from a query on a dataset. |
| 154 |
\param query the query in question. |
| 155 |
\param ds the dataset to use for the query. |
| 156 |
\return the value from the query, 0 on failure. |
| 157 |
*/ |
| 158 |
int GetSingleValueInt(const std::string& query, |
| 159 |
const std::unique_ptr<dbiplus::Dataset>& ds) const; |
| 160 |
|
| 161 |
/*! |
| 162 |
* @brief Delete values from a table. |
| 163 |
* @param strTable The table to delete the values from. |
| 164 |
* @param filter The Filter to apply to this query. |
| 165 |
* @return True if the query was executed successfully, false otherwise. |
| 166 |
*/ |
| 167 |
bool DeleteValues(const std::string& strTable, const Filter& filter = Filter()); |
| 168 |
|
| 169 |
/*! |
| 170 |
* @brief Execute a query that does not return any result. |
| 171 |
* Note that if BeginMultipleExecute() has been called, the |
| 172 |
* query will be queued until CommitMultipleExecute() is called. |
| 173 |
* @param strQuery The query to execute. |
| 174 |
* @return True if the query was executed successfully, false otherwise. |
| 175 |
* @sa BeginMultipleExecute, CommitMultipleExecute |
| 176 |
*/ |
| 177 |
bool ExecuteQuery(const std::string& strQuery); |
| 178 |
|
| 179 |
/*! |
| 180 |
* @brief Execute a query that returns a result. |
| 181 |
* @remarks Call m_pDS->close(); to clean up the dataset when done. |
| 182 |
* @param strQuery The query to execute. |
| 183 |
* @return True if the query was executed successfully, false otherwise. |
| 184 |
*/ |
| 185 |
bool ResultQuery(const std::string& strQuery) const; |
| 186 |
|
| 187 |
/*! |
| 188 |
* @brief Start a multiple execution queue. Any ExecuteQuery() function |
| 189 |
* following this call will be queued rather than executed until |
| 190 |
* CommitMultipleExecute() is performed. |
| 191 |
* NOTE: Queries that rely on any queued execute query will not |
| 192 |
* function as expected during this period! |
| 193 |
* @return true if we could start a multiple execution queue, false otherwise. |
| 194 |
* @sa CommitMultipleExecute, ExecuteQuery |
| 195 |
*/ |
| 196 |
bool BeginMultipleExecute(); |
| 197 |
|
| 198 |
/*! |
| 199 |
* @brief Commit the multiple execution queue to the database. |
| 200 |
* Queries are performed within a transaction, and the transaction |
| 201 |
* is rolled back should any one query fail. |
| 202 |
* @return True if the queries were executed successfully, false otherwise. |
| 203 |
* @sa BeginMultipleExecute, ExecuteQuery |
| 204 |
*/ |
| 205 |
bool CommitMultipleExecute(); |
| 206 |
|
| 207 |
/*! |
| 208 |
* @brief Put an INSERT or REPLACE query in the queue. |
| 209 |
* @param strQuery The query to queue. |
| 210 |
* @return True if the query was added successfully, false otherwise. |
| 211 |
*/ |
| 212 |
bool QueueInsertQuery(const std::string& strQuery); |
| 213 |
|
| 214 |
/*! |
| 215 |
* @brief Commit all queries in the queue. |
| 216 |
* @return True if all queries were executed successfully, false otherwise. |
| 217 |
*/ |
| 218 |
bool CommitInsertQueries(); |
| 219 |
|
| 220 |
/*! |
| 221 |
* @brief Get the number of INSERT queries in the queue. |
| 222 |
* @return The number of queries. |
| 223 |
*/ |
| 224 |
size_t GetInsertQueriesCount(); |
| 225 |
|
| 226 |
/*! |
| 227 |
* @brief Put a DELETE query in the queue. |
| 228 |
* @param strQuery The query to queue. |
| 229 |
* @return True if the query was added successfully, false otherwise. |
| 230 |
*/ |
| 231 |
bool QueueDeleteQuery(const std::string& strQuery); |
| 232 |
|
| 233 |
/*! |
| 234 |
* @brief Commit all queued DELETE queries. |
| 235 |
* @return True if all queries were executed successfully, false otherwise. |
| 236 |
*/ |
| 237 |
bool CommitDeleteQueries(); |
| 238 |
|
| 239 |
/*! |
| 240 |
* @brief Get the number of DELETE queries in the queue. |
| 241 |
* @return The number of queries. |
| 242 |
*/ |
| 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 |
|
| 267 |
/*! \brief Create database tables and analytics as needed. |
| 268 |
Calls CreateTables() and CreateAnalytics() on child classes. |
| 269 |
*/ |
| 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; ///< \brief whether we use sqlite (defaults to true) |
| 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; /*!< True if there are any queries in the insert queue, false otherwise */ |
| 317 |
bool m_bMultiDelete = |
| 318 |
false; /*!< True if there are any queries in the delete queue, false otherwise */ |
| 319 |
unsigned int m_openCount; |
| 320 |
|
| 321 |
bool m_multipleExecute; |
| 322 |
std::vector<std::string> m_multipleQueries; |
| 323 |
}; |