Ticket #6161: 00-6161-mythdb-dbsettings-cleanup.2.patch
File 00-6161-mythdb-dbsettings-cleanup.2.patch, 14.7 KB (added by , 15 years ago) |
---|
-
mythtv/libs/libmyth/mythcontext.h
133 133 134 134 void SaveSetting(const QString &key, int newValue); 135 135 void SaveSetting(const QString &key, const QString &newValue); 136 QString GetSetting(const QString &key, const QString &defaultval = "");136 void SaveGlobalSetting(const QString &key, const QString &newValue); 137 137 bool SaveSettingOnHost(const QString &key, const QString &newValue, 138 138 const QString &host); 139 QString GetSetting(const QString &key, const QString &defaultval = ""); 140 QString GetGlobalSetting(const QString &key, const QString &defaultval = ""); 141 QString GetSettingOnHost(const QString &key, const QString &host, 142 const QString &defaultval = ""); 139 143 144 140 145 // Convenience setting query methods 141 146 int GetNumSetting(const QString &key, int defaultval = 0); 142 147 double GetFloatSetting(const QString &key, double defaultval = 0.0); … … 146 151 void GetResolutionSetting(const QString &type, int &width, int &height, 147 152 int index=-1); 148 153 149 QString GetSettingOnHost(const QString &key, const QString &host,150 const QString &defaultval = "");151 154 int GetNumSettingOnHost(const QString &key, const QString &host, 152 155 int defaultval = 0); 153 156 double GetFloatSettingOnHost(const QString &key, const QString &host, -
mythtv/libs/libmyth/mythcontext.cpp
1659 1659 d->m_database->SaveSetting(key, newValue); 1660 1660 } 1661 1661 1662 void MythContext::SaveGlobalSetting(const QString &key, const QString &newValue) 1663 { 1664 d->m_database->SaveGlobalSetting(key, newValue); 1665 } 1666 1662 1667 bool MythContext::SaveSettingOnHost(const QString &key, 1663 1668 const QString &newValue, 1664 1669 const QString &host) … … 1671 1676 return d->m_database->GetSetting(key, defaultval); 1672 1677 } 1673 1678 1679 QString MythContext::GetGlobalSetting(const QString &key, const QString &defaultval) 1680 { 1681 return d->m_database->GetGlobalSetting(key, defaultval); 1682 } 1683 1674 1684 int MythContext::GetNumSetting(const QString &key, int defaultval) 1675 1685 { 1676 1686 return d->m_database->GetNumSetting(key, defaultval); -
mythtv/libs/libmythdb/mythdb.cpp
189 189 return d->ignoreDatabase; 190 190 } 191 191 192 192 /** \fn void MythDB::SaveGlobalSetting(const QString &key, const QString &newValue) 193 * Save setting newValue for key to db. Save to localhost settings table. 194 */ 193 195 void MythDB::SaveSetting(const QString &key, int newValue) 194 196 { 195 197 (void) SaveSettingOnHost(key, 196 198 QString::number(newValue), d->m_localhostname); 197 199 } 198 200 201 /** \fn void MythDB::SaveSetting(const QString &key, const QString &newValue) 202 * Save setting newValue for key to db. Save to localhost settings table. 203 */ 199 204 void MythDB::SaveSetting(const QString &key, const QString &newValue) 200 205 { 201 206 (void) SaveSettingOnHost(key, newValue, d->m_localhostname); 202 207 } 203 208 209 /** \fn void MythDB::SaveGlobalSetting(const QString &key, const QString &newValue) 210 * Save setting newValue for key to db. Save to global settings table. 211 */ 212 void MythDB::SaveGlobalSetting(const QString &key, const QString &newValue) 213 { 214 (void) SaveSettingOnHost(key, newValue, NULL); 215 } 216 217 /** \fn bool MythDB::SaveSettingOnHost(const QString &key, 218 * const QString &newValue, 219 * const QString &host) 220 * Save setting newValue for key to db. 221 * If host is specified then save to host settings table, 222 * otherwise save to global settings table. 223 */ 224 204 225 bool MythDB::SaveSettingOnHost(const QString &key, 205 226 const QString &newValue, 206 227 const QString &host) … … 274 295 return success; 275 296 } 276 297 298 /** \fn QString MythDB::GetSetting(const QString &key, const QString &defaultval) 299 * Get setting for key from db. Return defaultval if not set. 300 * Will try localhost settings first, then global settings table. 301 */ 302 277 303 QString MythDB::GetSetting(const QString &key, const QString &defaultval) 278 304 { 279 bool found = false; 280 QString value; 305 QString retval = GetSettingOnHost(key, d->m_localhostname, defaultval, true); 306 307 return retval; 308 } 281 309 282 if (d->overriddenSettings.contains(key)) { 283 value = d->overriddenSettings[key]; 284 return value; 285 } 310 /** \fn QString MythDB::GetGlobalSetting(const QString &key, const QString &defaultval) 311 * Get setting for key from db. Only try global settings table. 312 */ 313 QString MythDB::GetGlobalSetting(const QString &key, const QString &defaultval) 314 { 315 QString retval = GetSettingOnHost(key, NULL, defaultval, false); 316 317 return retval; 318 } 286 319 287 if (d->useSettingsCache) 288 { 289 d->settingsCacheLock.lock(); 290 if (d->settingsCache.contains(key)) 291 { 292 value = d->settingsCache[key]; 293 d->settingsCacheLock.unlock(); 294 return value; 295 } 296 d->settingsCacheLock.unlock(); 297 } 320 /** \fn int MythDB::GetNumSetting(const QString &key, int defaultval) 321 * Get setting for key from db. Try localhost then global settings table. 322 */ 323 int MythDB::GetNumSetting(const QString &key, int defaultval) 324 { 325 QString val = QString::number(defaultval); 326 QString retval = GetSettingOnHost(key, d->m_localhostname, val, true); 298 327 328 return retval.toInt(); 329 } 299 330 300 if (!d->ignoreDatabase) 301 { 302 MSqlQuery query(MSqlQuery::InitCon()); 303 if (query.isConnected()) 304 { 305 query.prepare("SELECT data FROM settings WHERE value " 306 "= :KEY AND hostname = :HOSTNAME ;"); 307 query.bindValue(":KEY", key); 308 query.bindValue(":HOSTNAME", d->m_localhostname); 309 query.exec(); 331 /** \fn double MythDB::GetFloatSetting(const QString &key, int defaultval) 332 * Get setting for key from db. Try localhost then global settings table. 333 */ 334 double MythDB::GetFloatSetting(const QString &key, double defaultval) 335 { 336 QString val = QString::number(defaultval); 337 QString retval = GetSettingOnHost(key, d->m_localhostname, val, true); 310 338 311 if (query.isActive() && query.size() > 0) 312 { 313 query.next(); 314 value = query.value(0).toString(); 315 found = true; 316 } 317 else 318 { 319 query.prepare("SELECT data FROM settings " 320 "WHERE value = :KEY AND " 321 "hostname IS NULL;"); 322 query.bindValue(":KEY", key); 323 query.exec(); 324 325 if (query.isActive() && query.size() > 0) 326 { 327 query.next(); 328 value = query.value(0).toString(); 329 found = true; 330 } 331 } 332 } 333 else 334 { 335 VERBOSE(VB_IMPORTANT, 336 QString("Database not open while trying to " 337 "load setting: %1") 338 .arg(key)); 339 } 340 } 341 342 if (!found) 343 return d->m_settings->GetSetting(key, defaultval); 344 345 // Store the value (only if we have actually found it in the database) 346 if (!value.isNull() && d->useSettingsCache) 347 { 348 d->settingsCacheLock.lock(); 349 d->settingsCache[key] = value; 350 d->settingsCacheLock.unlock(); 351 } 352 353 return value; 339 return retval.toDouble(); 354 340 } 355 341 356 int MythDB::GetNumSetting(const QString &key, int defaultval) 342 /** \fn int MythDB::GetNumSettingOnHost(const QString &key, const QString &host, 343 * int defaultval) 344 * Get setting for key from db. Try specified host then global settings table. 345 */ 346 int MythDB::GetNumSettingOnHost(const QString &key, const QString &host, 347 int defaultval) 357 348 { 358 349 QString val = QString::number(defaultval); 359 QString retval = GetSetting (key, val);350 QString retval = GetSettingOnHost(key, host, val, true); 360 351 361 352 return retval.toInt(); 362 353 } 363 354 364 double MythDB::GetFloatSetting(const QString &key, double defaultval) 355 /** \fn double MythDB::GetFloatSettingOnHost( 356 * const QString &key, const QString &host, double defaultval) 357 * Get setting for key from db. Try specified host then global settings table. 358 */ 359 double MythDB::GetFloatSettingOnHost( 360 const QString &key, const QString &host, double defaultval) 365 361 { 366 362 QString val = QString::number(defaultval); 367 QString retval = GetSetting (key, val);363 QString retval = GetSettingOnHost(key, host, val, true); 368 364 369 365 return retval.toDouble(); 370 366 } 371 367 368 /** \fn int MythDB::GetNumSetting(const QString &key, int defaultval) 369 * Get setting for key from db. All other Get functions call this one. 370 * If host is set, try localhost settings table. 371 * If tryGlobal is true, try global settings table if not found in localhost 372 * settings table. If false, don't check global settings table. 373 */ 372 374 QString MythDB::GetSettingOnHost(const QString &key, const QString &host, 373 const QString &defaultval )375 const QString &defaultval, bool tryGlobal) 374 376 { 375 377 bool found = false; 376 378 QString value = defaultval; 377 QString myKey = host + ' ' + key; 379 380 QString myKey = key; 381 if (!host.isEmpty()) 382 { 383 myKey = host + ' ' + key; 384 } 378 385 379 386 if (d->overriddenSettings.contains(myKey)) 380 387 { … … 407 414 MSqlQuery query(MSqlQuery::InitCon()); 408 415 if (query.isConnected()) 409 416 { 410 query.prepare("SELECT data FROM settings WHERE value = :VALUE " 411 "AND hostname = :HOSTNAME ;"); 412 query.bindValue(":VALUE", key); 413 query.bindValue(":HOSTNAME", host); 417 if (!host.isEmpty()) 418 { 419 query.prepare("SELECT data FROM settings WHERE value = :VALUE " 420 "AND hostname = :HOSTNAME ;"); 421 query.bindValue(":VALUE", key); 422 query.bindValue(":HOSTNAME", host); 423 424 if (query.exec() && query.isActive() && query.size() > 0) 425 { 426 query.next(); 427 value = query.value(0).toString(); 428 found = true; 429 } 430 } 414 431 415 if ( query.exec() && query.isActive() && query.size() > 0)432 if (host.isEmpty() || (tryGlobal && !found)) 416 433 { 417 query.next(); 418 value = query.value(0).toString(); 419 found = true; 434 query.prepare("SELECT data FROM settings WHERE value = :VALUE " 435 "AND hostname IS NULL;"); 436 query.bindValue(":VALUE", key); 437 438 if (query.exec() && query.isActive() && query.size() > 0) 439 { 440 query.next(); 441 value = query.value(0).toString(); 442 found = true; 443 } 420 444 } 445 421 446 } 422 447 else 423 448 { … … 428 453 } 429 454 } 430 455 456 if (!found) 457 return d->m_settings->GetSetting(key, defaultval); 458 431 459 if (found && d->useSettingsCache) 432 460 { 433 461 d->settingsCacheLock.lock(); 434 d->settingsCache[ host + ' ' + key] = value;462 d->settingsCache[myKey] = value; 435 463 d->settingsCacheLock.unlock(); 436 464 } 437 465 438 466 return value; 439 467 } 440 468 441 int MythDB::GetNumSettingOnHost(const QString &key, const QString &host, 442 int defaultval) 443 { 444 QString val = QString::number(defaultval); 445 QString retval = GetSettingOnHost(key, host, val); 446 447 return retval.toInt(); 448 } 449 450 double MythDB::GetFloatSettingOnHost( 451 const QString &key, const QString &host, double defaultval) 452 { 453 QString val = QString::number(defaultval); 454 QString retval = GetSettingOnHost(key, host, val); 455 456 return retval.toDouble(); 457 } 458 469 /** \fn void MythDB::SetSetting(const QString &key, const QString &newValue) 470 * Set setting in settings cache. Do not save to database. 471 */ 459 472 void MythDB::SetSetting(const QString &key, const QString &newValue) 460 473 { 461 474 d->m_settings->SetSetting(key, newValue); -
mythtv/libs/libmythdb/mythdb.h
35 35 36 36 void SaveSetting(const QString &key, int newValue); 37 37 void SaveSetting(const QString &key, const QString &newValue); 38 void SaveGlobalSetting(const QString &key, const QString &value); 38 39 bool SaveSettingOnHost(const QString &key, const QString &newValue, 39 40 const QString &host); 40 41 41 42 QString GetSetting(const QString &key, const QString &defaultval = ""); 43 QString GetGlobalSetting(const QString &key, const QString &defaultval = ""); 42 44 int GetNumSetting(const QString &key, int defaultval = 0); 43 45 double GetFloatSetting(const QString &key, double defaultval = 0.0); 46 int GetNumSettingOnHost(const QString &key, const QString &host, 47 int defaultval = 0); 48 double GetFloatSettingOnHost(const QString &key, const QString &host, 49 double defaultval = 0.0); 50 QString GetSettingOnHost(const QString &key, const QString &host, 51 const QString &defaultval = "", bool tryGlobal = false); 52 44 53 void GetResolutionSetting(const QString &type, int &width, int &height, 45 54 double& forced_aspect, short &refreshrate, 46 55 int index=-1); 47 56 void GetResolutionSetting(const QString &type, int &width, int &height, 48 57 int index=-1); 49 58 50 QString GetSettingOnHost(const QString &key, const QString &host,51 const QString &defaultval = "");52 int GetNumSettingOnHost(const QString &key, const QString &host,53 int defaultval = 0);54 double GetFloatSettingOnHost(const QString &key, const QString &host,55 double defaultval = 0.0);56 59 57 60 void SetSetting(const QString &key, const QString &newValue); 58 61