MythTV master
Public Member Functions | Static Public Member Functions | Static Public Attributes | Static Protected Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
DBUtil Class Reference

Aggregates database and DBMS utility functions. More...

#include <libmythbase/dbutil.h>

Inheritance diagram for DBUtil:
[legend]
Collaboration diagram for DBUtil:
[legend]

Public Member Functions

 DBUtil ()=default
 Constructs the DBUtil object. More...
 
 ~DBUtil ()=default
 
QString GetDBMSVersion (void)
 Returns the QString version name of the DBMS or QString() in the event of an error. More...
 
int CompareDBMSVersion (int major, int minor=0, int point=0)
 Compares the version of the active DBMS with the provided version. More...
 

Static Public Member Functions

static MythDBBackupStatus BackupDB (QString &filename, bool disableRotation=false)
 Requests a backup of the database. More...
 
static bool CheckTables (bool repair=false, const QString &options="QUICK")
 Checks database tables. More...
 
static bool RepairTables (const QStringList &tables)
 Repairs database tables. More...
 
static bool IsNewDatabase (void)
 Returns true for a new (empty) database. More...
 
static bool IsBackupInProgress (void)
 Test to see if a DB backup is in progress. More...
 
static int CountClients (void)
 Estimate the number of MythTV programs using the database. More...
 
static bool TryLockSchema (MSqlQuery &query, uint timeout_secs)
 Try to get a lock on the table schemalock. More...
 
static void UnlockSchema (MSqlQuery &query)
 
static bool CheckTimeZoneSupport (void)
 Check if MySQL has working timz zone support. More...
 
static bool CheckTableColumnExists (const QString &tableName, const QString &columnName)
 Checks for the presence of a column in a table in the current database. More...
 

Static Public Attributes

static const int kUnknownVersionNumber = INT_MIN
 

Static Protected Member Functions

static bool CreateTemporaryDBConf (const QString &privateinfo, QString &filename)
 Creates temporary file containing sensitive DB info. More...
 

Private Member Functions

bool QueryDBMSVersion (void)
 Reads and returns the QString version name from the DBMS or returns QString() in the event of an error. More...
 
bool ParseDBMSVersion (void)
 Parses m_versionString to find the major, minor, and point version. More...
 

Static Private Member Functions

static QStringList GetTables (const QStringList &engines=QStringList())
 Retrieves a list of tables from the database. More...
 
static QStringList CheckRepairStatus (MSqlQuery &query)
 Parse the results of a CHECK TABLE or REPAIR TABLE run. More...
 
static QString CreateBackupFilename (const QString &prefix="mythconverg", const QString &extension=".sql")
 Creates a filename to use for the filename. More...
 
static QString GetBackupDirectory ()
 Determines the appropriate path for the database backup. More...
 
static bool DoBackup (const QString &backupScript, QString &filename, bool disableRotation=false)
 Creates a backup of the database by executing the backupScript. More...
 
static bool DoBackup (QString &filename)
 Creates a backup of the database. More...
 

Private Attributes

QString m_versionString
 
int m_versionMajor { -1 }
 
int m_versionMinor { -1 }
 
int m_versionPoint { -1 }
 

Detailed Description

Aggregates database and DBMS utility functions.

This class allows retrieving or comparing the DBMS server version, and backing up the database.

The backup functionality currently requires mysqldump to be installed on the system. This may change in the future to allow backups even when there is no DB client installation on the system.

See also
HouseKeeper::RunHouseKeeping(void)

Definition at line 30 of file dbutil.h.

Constructor & Destructor Documentation

◆ DBUtil()

DBUtil::DBUtil ( void  )
default

Constructs the DBUtil object.

◆ ~DBUtil()

DBUtil::~DBUtil ( )
default

Member Function Documentation

◆ BackupDB()

MythDBBackupStatus DBUtil::BackupDB ( QString &  filename,
bool  disableRotation = false 
)
static

Requests a backup of the database.

If the DatabaseBackupScript exists in the ShareDir, it will be executed. All required database information will be made available as name=value pairs in a temporary file whose filename will be passed to the backup script. The script may parse this file to obtain the required information to run a backup program, such as mysqldump or mysqlhotcopy.

If the DatabaseBackupScript does not exist, a backup will be performed using mysqldump directly. The database password will be passed in a temporary file so it does not have to be specified on the command line.

Care should be taken in calling this function. It has the potential to corrupt in-progress recordings or interfere with playback.

The disableRotation argument should be used only for automatic backups when users could lose important backup files due to a failure loop–for example, a DB upgrade failure and a distro start script that keeps restarting mythbackend even when it exits with an error status.

Parameters
filenameUsed to return the name of the resulting backup file
disableRotationDisable backup rotation
Returns
MythDBBackupStatus indicating the result

Definition at line 186 of file dbutil.cpp.

Referenced by V2Myth::BackupDatabase(), and SchemaUpgradeWizard::BackupDB().

◆ CheckRepairStatus()

QStringList DBUtil::CheckRepairStatus ( MSqlQuery query)
staticprivate

Parse the results of a CHECK TABLE or REPAIR TABLE run.

This function reads the records returned by a CHECK TABLE or REPAIR TABLE run and determines the status of the table(s). The query should have columns Table, Msg_type, and Msg_text.

The function properly handles multiple records for a single table. If the last record for a given table shows a status (Msg_type) of OK (Msg_text), the table is considered OK, even if an error or warning appeared before (this could be the case, for example, when an empty table is crashed).

Parameters
queryAn already-executed CHECK TABLE or REPAIR TABLE query whose results should be parsed.
Returns
A list of names of not-OK (errored or crashed) tables
See also
DBUtil::CheckTables(const bool, const QString)
DBUtil::RepairTables(const QStringList)

Definition at line 380 of file dbutil.cpp.

Referenced by CheckTables(), and RepairTables().

◆ CheckTableColumnExists()

bool DBUtil::CheckTableColumnExists ( const QString &  tableName,
const QString &  columnName 
)
static

Checks for the presence of a column in a table in the current database.

This function will check a table in the current database for the presence of a named column.

Parameters
tableNameName of table to check
columnNameName of column to look for
Returns
true if column exists in the table; false if it does not
See also
CheckTableColumnExists(const QString &tableName, const QString &columnName)

Definition at line 889 of file dbutil.cpp.

Referenced by doUpgradeTVDatabaseSchema().

◆ CheckTables()

bool DBUtil::CheckTables ( bool  repair = false,
const QString &  options = "QUICK" 
)
static

Checks database tables.

This function will check database tables.

Parameters
repairRepair any tables whose status is not OK
optionsOptions to be passed to CHECK TABLE; defaults to QUICK
Returns
false if any tables have status other than OK; if repair is true, returns true if those tables were repaired successfully
See also
DBUtil::RepairTables(const QStringList)

Definition at line 275 of file dbutil.cpp.

Referenced by V2Myth::CheckDatabase().

◆ CheckTimeZoneSupport()

bool DBUtil::CheckTimeZoneSupport ( void  )
static

Check if MySQL has working timz zone support.

Definition at line 865 of file dbutil.cpp.

Referenced by V2Myth::GetBackendInfo(), main(), and run_backend().

◆ CompareDBMSVersion()

int DBUtil::CompareDBMSVersion ( int  major,
int  minor = 0,
int  point = 0 
)

Compares the version of the active DBMS with the provided version.

Returns negative, 0, or positive if the active DBMS version is less than, equal to, or greater than the provided version or returns DBUtil::kUnknownVersionNumber if the version cannot be determined.

Parameters
majorThe major version number (i.e. 5 in "5.0.22")
minorThe minor version number (i.e. 0 in "5.0.22")
pointThe point version number (i.e. 22 in "5.0.22")
Returns
negative, 0, or positive or DBUtil::kUnknownVersionNumber for error

Definition at line 53 of file dbutil.cpp.

Referenced by SchemaUpgradeWizard::PromptForUpgrade().

◆ CountClients()

int DBUtil::CountClients ( void  )
static

Estimate the number of MythTV programs using the database.

Definition at line 804 of file dbutil.cpp.

Referenced by SchemaUpgradeWizard::PromptForUpgrade().

◆ CreateBackupFilename()

QString DBUtil::CreateBackupFilename ( const QString &  prefix = "mythconverg",
const QString &  extension = ".sql" 
)
staticprivate

Creates a filename to use for the filename.

The filename is a concatenation of the given prefix, a hyphen, the current date/time, and the extension.

Parameters
prefixThe prefix (i.e. a database name) which should appear before the date/time
extensionThe extension to use for the file, including a dot, if desired
Returns
QString name

Definition at line 467 of file dbutil.cpp.

Referenced by DoBackup().

◆ CreateTemporaryDBConf()

bool DBUtil::CreateTemporaryDBConf ( const QString &  privateinfo,
QString &  filename 
)
staticprotected

Creates temporary file containing sensitive DB info.

So we don't have to specify the password on the command line, use –defaults-extra-file to specify a temporary file with a [client] and [mysqldump] section that provides the password. This will fail if the user's ~/.my.cnf (which is read after the –defaults-extra-file) specifies a different password that's incorrect for dbUserName

Definition at line 519 of file dbutil.cpp.

Referenced by DoBackup().

◆ DoBackup() [1/2]

bool DBUtil::DoBackup ( const QString &  backupScript,
QString &  filename,
bool  disableRotation = false 
)
staticprivate

Creates a backup of the database by executing the backupScript.

This function executes the specified backup script to create a database backup. This is the preferred approach for creating the backup.

Definition at line 563 of file dbutil.cpp.

Referenced by BackupDB().

◆ DoBackup() [2/2]

bool DBUtil::DoBackup ( QString &  filename)
staticprivate

Creates a backup of the database.

This fallback function is used only if the database backup script cannot be found.

Definition at line 660 of file dbutil.cpp.

◆ GetBackupDirectory()

QString DBUtil::GetBackupDirectory ( void  )
staticprivate

Determines the appropriate path for the database backup.

The function requests the special "DB Backups" storage group. In the event the group is not defined, the StorageGroup will fall back to using the "Default" group. For users upgrading from version 0.20 or before (which do not support Storage Groups), the StorageGroup will fall back to using the old RecordFilePrefix.

Definition at line 482 of file dbutil.cpp.

Referenced by DoBackup().

◆ GetDBMSVersion()

QString DBUtil::GetDBMSVersion ( void  )

Returns the QString version name of the DBMS or QString() in the event of an error.

Definition at line 34 of file dbutil.cpp.

Referenced by SchemaUpgradeWizard::PromptForUpgrade().

◆ GetTables()

QStringList DBUtil::GetTables ( const QStringList &  engines = QStringList())
staticprivate

Retrieves a list of tables from the database.

Returns
QStringList containing table names

Definition at line 424 of file dbutil.cpp.

Referenced by CheckTables(), and IsNewDatabase().

◆ IsBackupInProgress()

bool DBUtil::IsBackupInProgress ( void  )
static

Test to see if a DB backup is in progress.

Definition at line 88 of file dbutil.cpp.

◆ IsNewDatabase()

bool DBUtil::IsNewDatabase ( void  )
static

Returns true for a new (empty) database.

Definition at line 74 of file dbutil.cpp.

Referenced by BackupDB(), SchemaUpgradeWizard::Compare(), and doUpgradeTVDatabaseSchema().

◆ ParseDBMSVersion()

bool DBUtil::ParseDBMSVersion ( void  )
private

Parses m_versionString to find the major, minor, and point version.

Definition at line 780 of file dbutil.cpp.

Referenced by CompareDBMSVersion().

◆ QueryDBMSVersion()

bool DBUtil::QueryDBMSVersion ( void  )
private

Reads and returns the QString version name from the DBMS or returns QString() in the event of an error.

Definition at line 749 of file dbutil.cpp.

Referenced by GetDBMSVersion(), and ParseDBMSVersion().

◆ RepairTables()

bool DBUtil::RepairTables ( const QStringList &  tables)
static

Repairs database tables.

This function will repair MyISAM database tables.

Care should be taken in calling this function. It should only be called when no clients are accessing the database, and in the event the MySQL server crashes, it is critical that a REPAIR TABLE is run on the table that was being processed at the time of the server crash before any other operations are performed on that table, or the table may be destroyed. It is up to the caller of this function to guarantee the safety of performing database repairs.

Parameters
tablesList of tables to repair
Returns
false if errors were encountered repairing tables
See also
DBUtil::CheckTables(const bool, const QString)

Definition at line 333 of file dbutil.cpp.

Referenced by CheckTables().

◆ TryLockSchema()

bool DBUtil::TryLockSchema ( MSqlQuery query,
uint  timeout_secs 
)
static

Try to get a lock on the table schemalock.

Prevents multiple upgrades by different programs of the same schema.

Definition at line 846 of file dbutil.cpp.

Referenced by UpgradeMusicDatabaseSchema(), and UpgradeTVDatabaseSchema().

◆ UnlockSchema()

void DBUtil::UnlockSchema ( MSqlQuery query)
static

Definition at line 853 of file dbutil.cpp.

Referenced by UpgradeMusicDatabaseSchema(), and UpgradeTVDatabaseSchema().

Member Data Documentation

◆ kUnknownVersionNumber

const int DBUtil::kUnknownVersionNumber = INT_MIN
static

Definition at line 58 of file dbutil.h.

Referenced by CompareDBMSVersion().

◆ m_versionMajor

int DBUtil::m_versionMajor { -1 }
private

Definition at line 81 of file dbutil.h.

Referenced by CompareDBMSVersion(), and ParseDBMSVersion().

◆ m_versionMinor

int DBUtil::m_versionMinor { -1 }
private

Definition at line 82 of file dbutil.h.

Referenced by CompareDBMSVersion(), and ParseDBMSVersion().

◆ m_versionPoint

int DBUtil::m_versionPoint { -1 }
private

Definition at line 83 of file dbutil.h.

Referenced by CompareDBMSVersion(), and ParseDBMSVersion().

◆ m_versionString

QString DBUtil::m_versionString
private

Definition at line 79 of file dbutil.h.

Referenced by GetDBMSVersion(), ParseDBMSVersion(), and QueryDBMSVersion().


The documentation for this class was generated from the following files: