| 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 |
#include "Database.h" |
| 10 |
|
| 11 |
#include "DatabaseManager.h" |
| 12 |
#include "DbUrl.h" |
| 13 |
#include "ServiceBroker.h" |
| 14 |
#include "filesystem/SpecialProtocol.h" |
| 15 |
#if defined(HAS_MYSQL) || defined(HAS_MARIADB) |
| 16 |
#include "mysqldataset.h" |
| 17 |
#endif |
| 18 |
#include "profiles/ProfileManager.h" |
| 19 |
#include "settings/AdvancedSettings.h" |
| 20 |
#include "settings/SettingsComponent.h" |
| 21 |
#include "sqlitedataset.h" |
| 22 |
#include "utils/SortUtils.h" |
| 23 |
#include "utils/StringUtils.h" |
| 24 |
#include "utils/log.h" |
| 25 |
|
| 26 |
#ifdef TARGET_POSIX |
| 27 |
#include "platform/posix/ConvUtils.h" |
| 28 |
#endif |
| 29 |
|
| 30 |
#include <memory> |
| 31 |
|
| 32 |
using namespace dbiplus; |
| 33 |
|
| 34 |
#define MAX_COMPRESS_COUNT 20 |
| 35 |
|
| 36 |
void CDatabase::Filter::AppendField(const std::string& strField) |
| 37 |
{ |
| 38 |
if (strField.empty()) |
| 39 |
return; |
| 40 |
|
| 41 |
if (fields.empty() || fields == "*") |
| 42 |
fields = strField; |
| 43 |
else |
| 44 |
fields += ", " + strField; |
| 45 |
} |
| 46 |
|
| 47 |
void CDatabase::Filter::AppendJoin(const std::string& strJoin) |
| 48 |
{ |
| 49 |
if (strJoin.empty()) |
| 50 |
return; |
| 51 |
|
| 52 |
if (join.empty()) |
| 53 |
join = strJoin; |
| 54 |
else |
| 55 |
join += " " + strJoin; |
| 56 |
} |
| 57 |
|
| 58 |
void CDatabase::Filter::AppendWhere(const std::string& strWhere, bool combineWithAnd /* = true */) |
| 59 |
{ |
| 60 |
if (strWhere.empty()) |
| 61 |
return; |
| 62 |
|
| 63 |
if (where.empty()) |
| 64 |
where = strWhere; |
| 65 |
else |
| 66 |
{ |
| 67 |
where = "(" + where + ") "; |
| 68 |
where += combineWithAnd ? "AND" : "OR"; |
| 69 |
where += " (" + strWhere + ")"; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
void CDatabase::Filter::AppendOrder(const std::string& strOrder) |
| 74 |
{ |
| 75 |
if (strOrder.empty()) |
| 76 |
return; |
| 77 |
|
| 78 |
if (order.empty()) |
| 79 |
order = strOrder; |
| 80 |
else |
| 81 |
order += ", " + strOrder; |
| 82 |
} |
| 83 |
|
| 84 |
void CDatabase::Filter::AppendGroup(const std::string& strGroup) |
| 85 |
{ |
| 86 |
if (strGroup.empty()) |
| 87 |
return; |
| 88 |
|
| 89 |
if (group.empty()) |
| 90 |
group = strGroup; |
| 91 |
else |
| 92 |
group += ", " + strGroup; |
| 93 |
} |
| 94 |
|
| 95 |
void CDatabase::ExistsSubQuery::AppendJoin(const std::string& strJoin) |
| 96 |
{ |
| 97 |
if (strJoin.empty()) |
| 98 |
return; |
| 99 |
|
| 100 |
if (join.empty()) |
| 101 |
join = strJoin; |
| 102 |
else |
| 103 |
join += " " + strJoin; |
| 104 |
} |
| 105 |
|
| 106 |
void CDatabase::ExistsSubQuery::AppendWhere(const std::string& strWhere, |
| 107 |
bool combineWithAnd /* = true */) |
| 108 |
{ |
| 109 |
if (strWhere.empty()) |
| 110 |
return; |
| 111 |
|
| 112 |
if (where.empty()) |
| 113 |
where = strWhere; |
| 114 |
else |
| 115 |
{ |
| 116 |
where += combineWithAnd ? " AND " : " OR "; |
| 117 |
where += strWhere; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
bool CDatabase::ExistsSubQuery::BuildSQL(std::string& strSQL) |
| 122 |
{ |
| 123 |
if (tablename.empty()) |
| 124 |
return false; |
| 125 |
strSQL = "EXISTS (SELECT 1 FROM " + tablename; |
| 126 |
if (!join.empty()) |
| 127 |
strSQL += " " + join; |
| 128 |
std::string strWhere; |
| 129 |
if (!param.empty()) |
| 130 |
strWhere = param; |
| 131 |
if (!where.empty()) |
| 132 |
{ |
| 133 |
if (!strWhere.empty()) |
| 134 |
strWhere += " AND "; |
| 135 |
strWhere += where; |
| 136 |
} |
| 137 |
if (!strWhere.empty()) |
| 138 |
strSQL += " WHERE " + strWhere; |
| 139 |
|
| 140 |
strSQL += ")"; |
| 141 |
return true; |
| 142 |
} |
| 143 |
|
| 144 |
CDatabase::DatasetLayout::DatasetLayout(size_t totalfields) |
| 145 |
{ |
| 146 |
m_fields.resize(totalfields, DatasetFieldInfo(false, false, -1)); |
| 147 |
} |
| 148 |
|
| 149 |
void CDatabase::DatasetLayout::SetField(int fieldNo, |
| 150 |
const std::string& strField, |
| 151 |
bool bOutput /*= false*/) |
| 152 |
{ |
| 153 |
if (fieldNo >= 0 && fieldNo < static_cast<int>(m_fields.size())) |
| 154 |
{ |
| 155 |
m_fields[fieldNo].strField = strField; |
| 156 |
m_fields[fieldNo].fetch = true; |
| 157 |
m_fields[fieldNo].output = bOutput; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
void CDatabase::DatasetLayout::AdjustRecordNumbers(int offset) |
| 162 |
{ |
| 163 |
int recno = 0; |
| 164 |
for (auto& field : m_fields) |
| 165 |
{ |
| 166 |
if (field.fetch) |
| 167 |
{ |
| 168 |
field.recno = recno + offset; |
| 169 |
++recno; |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
bool CDatabase::DatasetLayout::GetFetch(int fieldno) |
| 175 |
{ |
| 176 |
if (fieldno >= 0 && fieldno < static_cast<int>(m_fields.size())) |
| 177 |
return m_fields[fieldno].fetch; |
| 178 |
return false; |
| 179 |
} |
| 180 |
|
| 181 |
void CDatabase::DatasetLayout::SetFetch(int fieldno, bool bFetch /*= true*/) |
| 182 |
{ |
| 183 |
if (fieldno >= 0 && fieldno < static_cast<int>(m_fields.size())) |
| 184 |
m_fields[fieldno].fetch = bFetch; |
| 185 |
} |
| 186 |
|
| 187 |
bool CDatabase::DatasetLayout::GetOutput(int fieldno) |
| 188 |
{ |
| 189 |
if (fieldno >= 0 && fieldno < static_cast<int>(m_fields.size())) |
| 190 |
return m_fields[fieldno].output; |
| 191 |
return false; |
| 192 |
} |
| 193 |
|
| 194 |
int CDatabase::DatasetLayout::GetRecNo(int fieldno) |
| 195 |
{ |
| 196 |
if (fieldno >= 0 && fieldno < static_cast<int>(m_fields.size())) |
| 197 |
return m_fields[fieldno].recno; |
| 198 |
return -1; |
| 199 |
} |
| 200 |
|
| 201 |
const std::string CDatabase::DatasetLayout::GetFields() |
| 202 |
{ |
| 203 |
std::string strSQL; |
| 204 |
for (const auto& field : m_fields) |
| 205 |
{ |
| 206 |
if (!field.strField.empty() && field.fetch) |
| 207 |
{ |
| 208 |
if (strSQL.empty()) |
| 209 |
strSQL = field.strField; |
| 210 |
else |
| 211 |
strSQL += ", " + field.strField; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
return strSQL; |
| 216 |
} |
| 217 |
|
| 218 |
bool CDatabase::DatasetLayout::HasFilterFields() |
| 219 |
{ |
| 220 |
for (const auto& field : m_fields) |
| 221 |
{ |
| 222 |
if (field.fetch) |
| 223 |
return true; |
| 224 |
} |
| 225 |
return false; |
| 226 |
} |
| 227 |
|
| 228 |
CDatabase::CDatabase() |
| 229 |
: m_profileManager(*CServiceBroker::GetSettingsComponent()->GetProfileManager()) |
| 230 |
{ |
| 231 |
m_openCount = 0; |
| 232 |
m_sqlite = true; |
| 233 |
m_multipleExecute = false; |
| 234 |
} |
| 235 |
|
| 236 |
CDatabase::~CDatabase(void) |
| 237 |
{ |
| 238 |
Close(); |
| 239 |
} |
| 240 |
|
| 241 |
void CDatabase::Split(const std::string& strFileNameAndPath, |
| 242 |
std::string& strPath, |
| 243 |
std::string& strFileName) |
| 244 |
{ |
| 245 |
strFileName = ""; |
| 246 |
strPath = ""; |
| 247 |
int i = strFileNameAndPath.size() - 1; |
| 248 |
while (i > 0) |
| 249 |
{ |
| 250 |
char ch = strFileNameAndPath[i]; |
| 251 |
if (ch == ':' || ch == '/' || ch == '\\') |
| 252 |
break; |
| 253 |
else |
| 254 |
i--; |
| 255 |
} |
| 256 |
strPath = strFileNameAndPath.substr(0, i); |
| 257 |
strFileName = strFileNameAndPath.substr(i); |
| 258 |
} |
| 259 |
|
| 260 |
std::string CDatabase::PrepareSQL(std::string strStmt, ...) const |
| 261 |
{ |
| 262 |
std::string strResult = ""; |
| 263 |
|
| 264 |
if (nullptr != m_pDB) |
| 265 |
{ |
| 266 |
va_list args; |
| 267 |
va_start(args, strStmt); |
| 268 |
strResult = m_pDB->vprepare(strStmt.c_str(), args); |
| 269 |
va_end(args); |
| 270 |
} |
| 271 |
|
| 272 |
return strResult; |
| 273 |
} |
| 274 |
|
| 275 |
std::string CDatabase::GetSingleValue(const std::string& query, |
| 276 |
const std::unique_ptr<Dataset>& ds) const |
| 277 |
{ |
| 278 |
std::string ret; |
| 279 |
try |
| 280 |
{ |
| 281 |
if (!m_pDB || !ds) |
| 282 |
return ret; |
| 283 |
|
| 284 |
if (ds->query(query) && ds->num_rows() > 0) |
| 285 |
ret = ds->fv(0).get_asString(); |
| 286 |
|
| 287 |
ds->close(); |
| 288 |
} |
| 289 |
catch (...) |
| 290 |
{ |
| 291 |
CLog::Log(LOGERROR, "{} - failed on query '{}'", __FUNCTION__, query); |
| 292 |
} |
| 293 |
return ret; |
| 294 |
} |
| 295 |
|
| 296 |
std::string CDatabase::GetSingleValue(const std::string& strTable, |
| 297 |
const std::string& strColumn, |
| 298 |
const std::string& strWhereClause /* = std::string() */, |
| 299 |
const std::string& strOrderBy /* = std::string() */) const |
| 300 |
{ |
| 301 |
std::string query = PrepareSQL("SELECT %s FROM %s", strColumn.c_str(), strTable.c_str()); |
| 302 |
if (!strWhereClause.empty()) |
| 303 |
query += " WHERE " + strWhereClause; |
| 304 |
if (!strOrderBy.empty()) |
| 305 |
query += " ORDER BY " + strOrderBy; |
| 306 |
query += " LIMIT 1"; |
| 307 |
return GetSingleValue(query, m_pDS); |
| 308 |
} |
| 309 |
|
| 310 |
std::string CDatabase::GetSingleValue(const std::string& query) const |
| 311 |
{ |
| 312 |
return GetSingleValue(query, m_pDS); |
| 313 |
} |
| 314 |
|
| 315 |
int CDatabase::GetSingleValueInt(const std::string& query, const std::unique_ptr<Dataset>& ds) const |
| 316 |
{ |
| 317 |
int ret = 0; |
| 318 |
try |
| 319 |
{ |
| 320 |
if (!m_pDB || !ds) |
| 321 |
return ret; |
| 322 |
|
| 323 |
if (ds->query(query) && ds->num_rows() > 0) |
| 324 |
ret = ds->fv(0).get_asInt(); |
| 325 |
|
| 326 |
ds->close(); |
| 327 |
} |
| 328 |
catch (...) |
| 329 |
{ |
| 330 |
CLog::Log(LOGERROR, "{} - failed on query '{}'", __FUNCTION__, query); |
| 331 |
} |
| 332 |
return ret; |
| 333 |
} |
| 334 |
|
| 335 |
int CDatabase::GetSingleValueInt(const std::string& strTable, |
| 336 |
const std::string& strColumn, |
| 337 |
const std::string& strWhereClause /* = std::string() */, |
| 338 |
const std::string& strOrderBy /* = std::string() */) const |
| 339 |
{ |
| 340 |
std::string strResult = GetSingleValue(strTable, strColumn, strWhereClause, strOrderBy); |
| 341 |
return static_cast<int>(strtol(strResult.c_str(), NULL, 10)); |
| 342 |
} |
| 343 |
|
| 344 |
int CDatabase::GetSingleValueInt(const std::string& query) const |
| 345 |
{ |
| 346 |
return GetSingleValueInt(query, m_pDS); |
| 347 |
} |
| 348 |
|
| 349 |
bool CDatabase::DeleteValues(const std::string& strTable, const Filter& filter /* = Filter() */) |
| 350 |
{ |
| 351 |
std::string strQuery; |
| 352 |
BuildSQL(PrepareSQL("DELETE FROM %s ", strTable.c_str()), filter, strQuery); |
| 353 |
return ExecuteQuery(strQuery); |
| 354 |
} |
| 355 |
|
| 356 |
bool CDatabase::BeginMultipleExecute() |
| 357 |
{ |
| 358 |
m_multipleExecute = true; |
| 359 |
m_multipleQueries.clear(); |
| 360 |
return true; |
| 361 |
} |
| 362 |
|
| 363 |
bool CDatabase::CommitMultipleExecute() |
| 364 |
{ |
| 365 |
m_multipleExecute = false; |
| 366 |
BeginTransaction(); |
| 367 |
for (const auto& i : m_multipleQueries) |
| 368 |
{ |
| 369 |
if (!ExecuteQuery(i)) |
| 370 |
{ |
| 371 |
RollbackTransaction(); |
| 372 |
return false; |
| 373 |
} |
| 374 |
} |
| 375 |
m_multipleQueries.clear(); |
| 376 |
return CommitTransaction(); |
| 377 |
} |
| 378 |
|
| 379 |
bool CDatabase::ExecuteQuery(const std::string& strQuery) |
| 380 |
{ |
| 381 |
if (m_multipleExecute) |
| 382 |
{ |
| 383 |
m_multipleQueries.push_back(strQuery); |
| 384 |
return true; |
| 385 |
} |
| 386 |
|
| 387 |
bool bReturn = false; |
| 388 |
|
| 389 |
try |
| 390 |
{ |
| 391 |
if (nullptr == m_pDB) |
| 392 |
return bReturn; |
| 393 |
if (nullptr == m_pDS) |
| 394 |
return bReturn; |
| 395 |
m_pDS->exec(strQuery); |
| 396 |
bReturn = true; |
| 397 |
} |
| 398 |
catch (...) |
| 399 |
{ |
| 400 |
CLog::Log(LOGERROR, "{} - failed to execute query '{}'", __FUNCTION__, strQuery); |
| 401 |
} |
| 402 |
|
| 403 |
return bReturn; |
| 404 |
} |
| 405 |
|
| 406 |
bool CDatabase::ResultQuery(const std::string& strQuery) const |
| 407 |
{ |
| 408 |
bool bReturn = false; |
| 409 |
|
| 410 |
try |
| 411 |
{ |
| 412 |
if (nullptr == m_pDB) |
| 413 |
return bReturn; |
| 414 |
if (nullptr == m_pDS) |
| 415 |
return bReturn; |
| 416 |
|
| 417 |
std::string strPreparedQuery = PrepareSQL(strQuery); |
| 418 |
|
| 419 |
bReturn = m_pDS->query(strPreparedQuery); |
| 420 |
} |
| 421 |
catch (...) |
| 422 |
{ |
| 423 |
CLog::Log(LOGERROR, "{} - failed to execute query '{}'", __FUNCTION__, strQuery); |
| 424 |
} |
| 425 |
|
| 426 |
return bReturn; |
| 427 |
} |
| 428 |
|
| 429 |
bool CDatabase::QueueInsertQuery(const std::string& strQuery) |
| 430 |
{ |
| 431 |
if (strQuery.empty()) |
| 432 |
return false; |
| 433 |
|
| 434 |
if (!m_bMultiInsert) |
| 435 |
{ |
| 436 |
if (nullptr == m_pDB) |
| 437 |
return false; |
| 438 |
if (nullptr == m_pDS2) |
| 439 |
return false; |
| 440 |
|
| 441 |
m_bMultiInsert = true; |
| 442 |
m_pDS2->insert(); |
| 443 |
} |
| 444 |
|
| 445 |
m_pDS2->add_insert_sql(strQuery); |
| 446 |
|
| 447 |
return true; |
| 448 |
} |
| 449 |
|
| 450 |
bool CDatabase::CommitInsertQueries() |
| 451 |
{ |
| 452 |
bool bReturn = true; |
| 453 |
|
| 454 |
if (m_bMultiInsert) |
| 455 |
{ |
| 456 |
try |
| 457 |
{ |
| 458 |
m_bMultiInsert = false; |
| 459 |
m_pDS2->post(); |
| 460 |
m_pDS2->clear_insert_sql(); |
| 461 |
} |
| 462 |
catch (...) |
| 463 |
{ |
| 464 |
bReturn = false; |
| 465 |
CLog::Log(LOGERROR, "{} - failed to execute queries", __FUNCTION__); |
| 466 |
} |
| 467 |
} |
| 468 |
|
| 469 |
return bReturn; |
| 470 |
} |
| 471 |
|
| 472 |
size_t CDatabase::GetInsertQueriesCount() |
| 473 |
{ |
| 474 |
return m_pDS2->insert_sql_count(); |
| 475 |
} |
| 476 |
|
| 477 |
bool CDatabase::QueueDeleteQuery(const std::string& strQuery) |
| 478 |
{ |
| 479 |
if (strQuery.empty() || !m_pDB || !m_pDS) |
| 480 |
return false; |
| 481 |
|
| 482 |
m_bMultiDelete = true; |
| 483 |
m_pDS->del(); |
| 484 |
m_pDS->add_delete_sql(strQuery); |
| 485 |
return true; |
| 486 |
} |
| 487 |
|
| 488 |
bool CDatabase::CommitDeleteQueries() |
| 489 |
{ |
| 490 |
bool bReturn = true; |
| 491 |
|
| 492 |
if (m_bMultiDelete) |
| 493 |
{ |
| 494 |
try |
| 495 |
{ |
| 496 |
m_bMultiDelete = false; |
| 497 |
m_pDS->deletion(); |
| 498 |
m_pDS->clear_delete_sql(); |
| 499 |
} |
| 500 |
catch (...) |
| 501 |
{ |
| 502 |
bReturn = false; |
| 503 |
CLog::Log(LOGERROR, "{} - failed to execute queries", __FUNCTION__); |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
return bReturn; |
| 508 |
} |
| 509 |
|
| 510 |
size_t CDatabase::GetDeleteQueriesCount() |
| 511 |
{ |
| 512 |
return m_pDS->delete_sql_count(); |
| 513 |
} |
| 514 |
|
| 515 |
bool CDatabase::Open() |
| 516 |
{ |
| 517 |
DatabaseSettings db_fallback; |
| 518 |
return Open(db_fallback); |
| 519 |
} |
| 520 |
|
| 521 |
bool CDatabase::Open(const DatabaseSettings& settings) |
| 522 |
{ |
| 523 |
if (IsOpen()) |
| 524 |
{ |
| 525 |
m_openCount++; |
| 526 |
return true; |
| 527 |
} |
| 528 |
|
| 529 |
// check our database manager to see if this database can be opened |
| 530 |
if (!CServiceBroker::GetDatabaseManager().CanOpen(GetBaseDBName())) |
| 531 |
return false; |
| 532 |
|
| 533 |
DatabaseSettings dbSettings = settings; |
| 534 |
InitSettings(dbSettings); |
| 535 |
|
| 536 |
std::string dbName = dbSettings.name; |
| 537 |
dbName += std::to_string(GetSchemaVersion()); |
| 538 |
return Connect(dbName, dbSettings, false); |
| 539 |
} |
| 540 |
|
| 541 |
void CDatabase::InitSettings(DatabaseSettings& dbSettings) |
| 542 |
{ |
| 543 |
m_sqlite = true; |
| 544 |
|
| 545 |
#if defined(HAS_MYSQL) || defined(HAS_MARIADB) |
| 546 |
if (dbSettings.type == "mysql") |
| 547 |
{ |
| 548 |
// check we have all information before we cancel the fallback |
| 549 |
if (!(dbSettings.host.empty() || dbSettings.user.empty() || dbSettings.pass.empty())) |
| 550 |
m_sqlite = false; |
| 551 |
else |
| 552 |
CLog::Log(LOGINFO, "Essential mysql database information is missing. Require at least host, " |
| 553 |
"user and pass defined."); |
| 554 |
} |
| 555 |
else |
| 556 |
#else |
| 557 |
if (dbSettings.type == "mysql") |
| 558 |
CLog::Log( |
| 559 |
LOGERROR, |
| 560 |
"MySQL library requested but MySQL support is not compiled in. Falling back to sqlite3."); |
| 561 |
#endif |
| 562 |
{ |
| 563 |
dbSettings.type = "sqlite3"; |
| 564 |
if (dbSettings.host.empty()) |
| 565 |
dbSettings.host = CSpecialProtocol::TranslatePath(m_profileManager.GetDatabaseFolder()); |
| 566 |
} |
| 567 |
|
| 568 |
// use separate, versioned database |
| 569 |
if (dbSettings.name.empty()) |
| 570 |
dbSettings.name = GetBaseDBName(); |
| 571 |
} |
| 572 |
|
| 573 |
void CDatabase::CopyDB(const std::string& latestDb) |
| 574 |
{ |
| 575 |
m_pDB->copy(latestDb.c_str()); |
| 576 |
} |
| 577 |
|
| 578 |
void CDatabase::DropAnalytics() |
| 579 |
{ |
| 580 |
m_pDB->drop_analytics(); |
| 581 |
} |
| 582 |
|
| 583 |
bool CDatabase::Connect(const std::string& dbName, const DatabaseSettings& dbSettings, bool create) |
| 584 |
{ |
| 585 |
// create the appropriate database structure |
| 586 |
if (dbSettings.type == "sqlite3") |
| 587 |
{ |
| 588 |
m_pDB = std::make_unique<SqliteDatabase>(); |
| 589 |
} |
| 590 |
#if defined(HAS_MYSQL) || defined(HAS_MARIADB) |
| 591 |
else if (dbSettings.type == "mysql") |
| 592 |
{ |
| 593 |
m_pDB = std::make_unique<MysqlDatabase>(); |
| 594 |
} |
| 595 |
#endif |
| 596 |
else |
| 597 |
{ |
| 598 |
CLog::Log(LOGERROR, "Unable to determine database type: {}", dbSettings.type); |
| 599 |
return false; |
| 600 |
} |
| 601 |
|
| 602 |
// host name is always required |
| 603 |
m_pDB->setHostName(dbSettings.host.c_str()); |
| 604 |
|
| 605 |
if (!dbSettings.port.empty()) |
| 606 |
m_pDB->setPort(dbSettings.port.c_str()); |
| 607 |
|
| 608 |
if (!dbSettings.user.empty()) |
| 609 |
m_pDB->setLogin(dbSettings.user.c_str()); |
| 610 |
|
| 611 |
if (!dbSettings.pass.empty()) |
| 612 |
m_pDB->setPasswd(dbSettings.pass.c_str()); |
| 613 |
|
| 614 |
// database name is always required |
| 615 |
m_pDB->setDatabase(dbName.c_str()); |
| 616 |
|
| 617 |
// set configuration regardless if any are empty |
| 618 |
m_pDB->setConfig(dbSettings.key.c_str(), dbSettings.cert.c_str(), dbSettings.ca.c_str(), |
| 619 |
dbSettings.capath.c_str(), dbSettings.ciphers.c_str(), dbSettings.compression); |
| 620 |
|
| 621 |
// create the datasets |
| 622 |
m_pDS.reset(m_pDB->CreateDataset()); |
| 623 |
m_pDS2.reset(m_pDB->CreateDataset()); |
| 624 |
|
| 625 |
if (m_pDB->connect(create) != DB_CONNECTION_OK) |
| 626 |
return false; |
| 627 |
|
| 628 |
try |
| 629 |
{ |
| 630 |
// test if db already exists, if not we need to create the tables |
| 631 |
if (!m_pDB->exists() && create) |
| 632 |
{ |
| 633 |
if (dbSettings.type == "sqlite3") |
| 634 |
{ |
| 635 |
// Modern file systems have a cluster/block size of 4k. |
| 636 |
// To gain better performance when performing write |
| 637 |
// operations to the database, set the page size of the |
| 638 |
// database file to 4k. |
| 639 |
// This needs to be done before any table is created. |
| 640 |
m_pDS->exec("PRAGMA page_size=4096\n"); |
| 641 |
|
| 642 |
// Also set the memory cache size to 16k |
| 643 |
m_pDS->exec("PRAGMA default_cache_size=4096\n"); |
| 644 |
} |
| 645 |
CreateDatabase(); |
| 646 |
} |
| 647 |
|
| 648 |
// sqlite3 post connection operations |
| 649 |
if (dbSettings.type == "sqlite3") |
| 650 |
{ |
| 651 |
m_pDS->exec("PRAGMA cache_size=4096\n"); |
| 652 |
m_pDS->exec("PRAGMA synchronous='NORMAL'\n"); |
| 653 |
m_pDS->exec("PRAGMA count_changes='OFF'\n"); |
| 654 |
} |
| 655 |
} |
| 656 |
catch (DbErrors& error) |
| 657 |
{ |
| 658 |
CLog::Log(LOGERROR, "{} failed with '{}'", __FUNCTION__, error.getMsg()); |
| 659 |
m_openCount = 1; // set to open so we can execute Close() |
| 660 |
Close(); |
| 661 |
return false; |
| 662 |
} |
| 663 |
|
| 664 |
m_openCount = 1; // our database is open |
| 665 |
return true; |
| 666 |
} |
| 667 |
|
| 668 |
int CDatabase::GetDBVersion() |
| 669 |
{ |
| 670 |
m_pDS->query("SELECT idVersion FROM version\n"); |
| 671 |
if (m_pDS->num_rows() > 0) |
| 672 |
return m_pDS->fv("idVersion").get_asInt(); |
| 673 |
return 0; |
| 674 |
} |
| 675 |
|
| 676 |
bool CDatabase::IsOpen() |
| 677 |
{ |
| 678 |
return m_openCount > 0; |
| 679 |
} |
| 680 |
|
| 681 |
void CDatabase::Close() |
| 682 |
{ |
| 683 |
if (m_openCount == 0) |
| 684 |
return; |
| 685 |
|
| 686 |
if (m_openCount > 1) |
| 687 |
{ |
| 688 |
m_openCount--; |
| 689 |
return; |
| 690 |
} |
| 691 |
|
| 692 |
m_openCount = 0; |
| 693 |
m_multipleExecute = false; |
| 694 |
|
| 695 |
if (nullptr == m_pDB) |
| 696 |
return; |
| 697 |
if (nullptr != m_pDS) |
| 698 |
m_pDS->close(); |
| 699 |
m_pDB->disconnect(); |
| 700 |
m_pDB.reset(); |
| 701 |
m_pDS.reset(); |
| 702 |
m_pDS2.reset(); |
| 703 |
} |
| 704 |
|
| 705 |
bool CDatabase::Compress(bool bForce /* =true */) |
| 706 |
{ |
| 707 |
if (!m_sqlite) |
| 708 |
return true; |
| 709 |
|
| 710 |
try |
| 711 |
{ |
| 712 |
if (nullptr == m_pDB) |
| 713 |
return false; |
| 714 |
if (nullptr == m_pDS) |
| 715 |
return false; |
| 716 |
if (!bForce) |
| 717 |
{ |
| 718 |
m_pDS->query("select iCompressCount from version"); |
| 719 |
if (!m_pDS->eof()) |
| 720 |
{ |
| 721 |
int iCount = m_pDS->fv(0).get_asInt(); |
| 722 |
if (iCount > MAX_COMPRESS_COUNT) |
| 723 |
iCount = -1; |
| 724 |
m_pDS->close(); |
| 725 |
std::string strSQL = PrepareSQL("update version set iCompressCount=%i\n", ++iCount); |
| 726 |
m_pDS->exec(strSQL); |
| 727 |
if (iCount != 0) |
| 728 |
return true; |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
if (!m_pDS->exec("vacuum\n")) |
| 733 |
return false; |
| 734 |
} |
| 735 |
catch (...) |
| 736 |
{ |
| 737 |
CLog::Log(LOGERROR, "{} - Compressing the database failed", __FUNCTION__); |
| 738 |
return false; |
| 739 |
} |
| 740 |
return true; |
| 741 |
} |
| 742 |
|
| 743 |
void CDatabase::Interrupt() |
| 744 |
{ |
| 745 |
m_pDS->interrupt(); |
| 746 |
} |
| 747 |
|
| 748 |
void CDatabase::BeginTransaction() |
| 749 |
{ |
| 750 |
try |
| 751 |
{ |
| 752 |
if (nullptr != m_pDB) |
| 753 |
m_pDB->start_transaction(); |
| 754 |
} |
| 755 |
catch (...) |
| 756 |
{ |
| 757 |
CLog::Log(LOGERROR, "database:begintransaction failed"); |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
bool CDatabase::CommitTransaction() |
| 762 |
{ |
| 763 |
try |
| 764 |
{ |
| 765 |
if (nullptr != m_pDB) |
| 766 |
m_pDB->commit_transaction(); |
| 767 |
} |
| 768 |
catch (...) |
| 769 |
{ |
| 770 |
CLog::Log(LOGERROR, "database:committransaction failed"); |
| 771 |
return false; |
| 772 |
} |
| 773 |
return true; |
| 774 |
} |
| 775 |
|
| 776 |
void CDatabase::RollbackTransaction() |
| 777 |
{ |
| 778 |
try |
| 779 |
{ |
| 780 |
if (nullptr != m_pDB) |
| 781 |
m_pDB->rollback_transaction(); |
| 782 |
} |
| 783 |
catch (...) |
| 784 |
{ |
| 785 |
CLog::Log(LOGERROR, "database:rollbacktransaction failed"); |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
bool CDatabase::CreateDatabase() |
| 790 |
{ |
| 791 |
BeginTransaction(); |
| 792 |
try |
| 793 |
{ |
| 794 |
CLog::Log(LOGINFO, "creating version table"); |
| 795 |
m_pDS->exec("CREATE TABLE version (idVersion integer, iCompressCount integer)\n"); |
| 796 |
std::string strSQL = PrepareSQL("INSERT INTO version (idVersion,iCompressCount) values(%i,0)\n", |
| 797 |
GetSchemaVersion()); |
| 798 |
m_pDS->exec(strSQL); |
| 799 |
|
| 800 |
CreateTables(); |
| 801 |
CreateAnalytics(); |
| 802 |
} |
| 803 |
catch (...) |
| 804 |
{ |
| 805 |
CLog::Log(LOGERROR, "{} unable to create database:{}", __FUNCTION__, (int)GetLastError()); |
| 806 |
RollbackTransaction(); |
| 807 |
return false; |
| 808 |
} |
| 809 |
|
| 810 |
return CommitTransaction(); |
| 811 |
} |
| 812 |
|
| 813 |
void CDatabase::UpdateVersionNumber() |
| 814 |
{ |
| 815 |
std::string strSQL = PrepareSQL("UPDATE version SET idVersion=%i\n", GetSchemaVersion()); |
| 816 |
m_pDS->exec(strSQL); |
| 817 |
} |
| 818 |
|
| 819 |
bool CDatabase::BuildSQL(const std::string& strQuery, |
| 820 |
const Filter& filter, |
| 821 |
std::string& strSQL) const |
| 822 |
{ |
| 823 |
strSQL = strQuery; |
| 824 |
|
| 825 |
if (!filter.join.empty()) |
| 826 |
strSQL += filter.join; |
| 827 |
if (!filter.where.empty()) |
| 828 |
strSQL += " WHERE " + filter.where; |
| 829 |
if (!filter.group.empty()) |
| 830 |
strSQL += " GROUP BY " + filter.group; |
| 831 |
if (!filter.order.empty()) |
| 832 |
strSQL += " ORDER BY " + filter.order; |
| 833 |
if (!filter.limit.empty()) |
| 834 |
strSQL += " LIMIT " + filter.limit; |
| 835 |
|
| 836 |
return true; |
| 837 |
} |
| 838 |
|
| 839 |
bool CDatabase::BuildSQL(const std::string& strBaseDir, |
| 840 |
const std::string& strQuery, |
| 841 |
Filter& filter, |
| 842 |
std::string& strSQL, |
| 843 |
CDbUrl& dbUrl) |
| 844 |
{ |
| 845 |
SortDescription sorting; |
| 846 |
return BuildSQL(strBaseDir, strQuery, filter, strSQL, dbUrl, sorting); |
| 847 |
} |
| 848 |
|
| 849 |
bool CDatabase::BuildSQL(const std::string& strBaseDir, |
| 850 |
const std::string& strQuery, |
| 851 |
Filter& filter, |
| 852 |
std::string& strSQL, |
| 853 |
CDbUrl& dbUrl, |
| 854 |
SortDescription& sorting /* = SortDescription() */) |
| 855 |
{ |
| 856 |
// parse the base path to get additional filters |
| 857 |
dbUrl.Reset(); |
| 858 |
if (!dbUrl.FromString(strBaseDir) || !GetFilter(dbUrl, filter, sorting)) |
| 859 |
return false; |
| 860 |
|
| 861 |
return BuildSQL(strQuery, filter, strSQL); |
| 862 |
} |