Kodi Documentation 23.0
Kodi is an open source media player and entertainment hub.
Loading...
Searching...
No Matches
CDatabase Class Referenceabstract

#include <Database.h>

Inheritance diagram for CDatabase:
ADDON::CAddonDatabase CMusicDatabase CTextureDatabase CVideoDatabase CViewDatabase KODI::GAME::CGameDatabase PVR::CPVRDatabase PVR::CPVREpgDatabase

Classes

struct  DatasetFieldInfo
 
class  DatasetLayout
 
class  ExistsSubQuery
 
class  Filter
 

Public Types

enum class  ConnectionState { STATE_ERROR , STATE_DATABASE_NOT_FOUND , STATE_CONNECTED }
 

Public Member Functions

 CDatabase (const std::string &dbType)
 
virtual ~CDatabase ()
 
bool IsOpen () const
 
bool Open (const DatabaseSettings &db)
 
virtual void Close ()
 
const std::string & GetType () const
 
bool Compress (bool bForce=true)
 
void Interrupt ()
 
void BeginTransaction ()
 
virtual bool CommitTransaction ()
 
void RollbackTransaction ()
 
bool InTransaction () const
 
void CopyDB (const std::string &latestDb)
 
void DropAnalytics ()
 
std::string PrepareSQL (std::string_view sqlFormat,...) const
 
std::string GetSingleValue (const std::string &strTable, const std::string &strColumn, const std::string &strWhereClause=std::string(), const std::string &strOrderBy=std::string()) const
 Get a single value from a table.
 
std::string GetSingleValue (const std::string &query) const
 
std::string GetSingleValue (const std::string &query, dbiplus::Dataset &ds) const
 Get a single value from a query on a dataset.
 
int GetSingleValueInt (const std::string &strTable, const std::string &strColumn, const std::string &strWhereClause=std::string(), const std::string &strOrderBy=std::string()) const
 Get a single integer value from a table.
 
int GetSingleValueInt (const std::string &query) const
 
int GetSingleValueInt (const std::string &query, dbiplus::Dataset &ds) const
 Get a single integer value from a query on a dataset.
 
bool DeleteValues (const std::string &strTable, const Filter &filter=Filter())
 Delete values from a table.
 
bool ExecuteQuery (const std::string &strQuery)
 Execute a query that does not return any result. Note that if BeginMultipleExecute() has been called, the query will be queued until CommitMultipleExecute() is called.
 
bool ResultQuery (const std::string &strQuery) const
 Execute a query that returns a result.
 
bool BeginMultipleExecute ()
 Start a multiple execution queue. Any ExecuteQuery() function following this call will be queued rather than executed until CommitMultipleExecute() is performed. NOTE: Queries that rely on any queued execute query will not function as expected during this period!
 
bool CommitMultipleExecute ()
 Commit the multiple execution queue to the database. Queries are performed within a transaction, and the transaction is rolled back should any one query fail.
 
bool QueueInsertQuery (const std::string &strQuery)
 Put an INSERT or REPLACE query in the queue.
 
bool CommitInsertQueries ()
 Commit all queries in the queue.
 
size_t GetInsertQueriesCount () const
 Get the number of INSERT queries in the queue.
 
bool QueueDeleteQuery (const std::string &strQuery)
 Put a DELETE query in the queue.
 
bool CommitDeleteQueries ()
 Commit all queued DELETE queries.
 
size_t GetDeleteQueriesCount () const
 Get the number of DELETE queries in the queue.
 
virtual bool GetFilter (CDbUrl &dbUrl, Filter &filter, SortDescription &sorting)
 
virtual bool BuildSQL (const std::string &strBaseDir, const std::string &strQuery, Filter &filter, std::string &strSQL, CDbUrl &dbUrl)
 
virtual bool BuildSQL (const std::string &strBaseDir, const std::string &strQuery, Filter &filter, std::string &strSQL, CDbUrl &dbUrl, SortDescription &sorting)
 
ConnectionState Connect (const std::string &dbName, const DatabaseSettings &db, bool create)
 
bool SetArtForItem (int mediaId, const std::string &mediaType, const std::string &artType, const std::string &url)
 Sets art for a library item.
 
virtual bool SetArtForItem (int mediaId, const std::string &mediaType, const KODI::ART::Artwork &art)
 Sets each piece of art, a map of art type to url, for a library item.
 
virtual bool GetArtForItem (int mediaId, const std::string &mediaType, KODI::ART::Artwork &art)
 Adds the art a library item has to art, a map of art type to url.
 
std::string GetArtForItem (int mediaId, const std::string &mediaType, const std::string &artType)
 The url of one type of art for a library item, empty if it has none.
 
bool RemoveArtForItem (int mediaId, const std::string &mediaType, const std::string &artType)
 
bool RemoveArtForItem (int mediaId, const std::string &mediaType, const std::set< std::string, std::less<> > &artTypes)
 
bool GetArtTypes (const std::string &mediaType, std::vector< std::string > &artTypes)
 Adds the distinct types of art held for a type of item to artTypes.
 
bool SetArtForItem (int mediaId, KODI::MEDIA::TYPE mediaType, const std::string &artType, const std::string &url)
 The art functions above, for an item whose table is a media type.
 
bool SetArtForItem (int mediaId, KODI::MEDIA::TYPE mediaType, const KODI::ART::Artwork &art)
 
bool GetArtForItem (int mediaId, KODI::MEDIA::TYPE mediaType, KODI::ART::Artwork &art)
 
std::string GetArtForItem (int mediaId, KODI::MEDIA::TYPE mediaType, const std::string &artType)
 
bool RemoveArtForItem (int mediaId, KODI::MEDIA::TYPE mediaType, const std::set< std::string, std::less<> > &artTypes)
 

Protected Member Functions

void Split (const std::string &strFileNameAndPath, std::string &strPath, std::string &strFileName) const
 
virtual bool Open ()
 
bool CreateDatabase ()
 Create database tables and analytics as needed. Calls CreateTables() and CreateAnalytics() on child classes.
 
virtual void CreateTables ()=0
 
virtual void CreateAnalytics ()=0
 
virtual void UpdateTables (int version)
 
void AddAutoIncrement (const std::string &table)
 Rebuild a table so that its integer primary key never hands out a deleted row's id. For UpdateTables() only: the analytics must be dropped. MySQL already declares every integer primary key auto_increment, so there it does nothing.
 
virtual int GetMinSchemaVersion () const
 
virtual int GetSchemaVersion () const =0
 
virtual const char * GetBaseDBName () const =0
 
int GetDBVersion ()
 
bool BuildSQL (std::string_view strQuery, const Filter &filter, std::string &strSQL) const
 

Protected Attributes

bool m_sqlite {true}
 whether we use sqlite (defaults to true)
 
std::unique_ptr< dbiplus::Database > m_pDB
 
std::unique_ptr< dbiplus::Dataset > m_pDS
 
std::unique_ptr< dbiplus::Dataset > m_pDS2
 
const CProfileManager & m_profileManager
 

Friends

class CDatabaseManager
 

Member Enumeration Documentation

◆ ConnectionState

enum class CDatabase::ConnectionState
strong
Enumerator
STATE_ERROR 
STATE_DATABASE_NOT_FOUND 
STATE_CONNECTED 

Constructor & Destructor Documentation

◆ CDatabase()

CDatabase::CDatabase ( const std::string & dbType)
explicit

◆ ~CDatabase()

CDatabase::~CDatabase ( void )
virtual

Member Function Documentation

◆ AddAutoIncrement()

void CDatabase::AddAutoIncrement ( const std::string & table)
protected

Rebuild a table so that its integer primary key never hands out a deleted row's id. For UpdateTables() only: the analytics must be dropped. MySQL already declares every integer primary key auto_increment, so there it does nothing.

◆ BeginMultipleExecute()

bool CDatabase::BeginMultipleExecute ( )

Start a multiple execution queue. Any ExecuteQuery() function following this call will be queued rather than executed until CommitMultipleExecute() is performed. NOTE: Queries that rely on any queued execute query will not function as expected during this period!

Returns
true if we could start a multiple execution queue, false otherwise.
See also
CommitMultipleExecute, ExecuteQuery

◆ BeginTransaction()

void CDatabase::BeginTransaction ( )

◆ BuildSQL() [1/3]

bool CDatabase::BuildSQL ( const std::string & strBaseDir,
const std::string & strQuery,
Filter & filter,
std::string & strSQL,
CDbUrl & dbUrl )
virtual

◆ BuildSQL() [2/3]

bool CDatabase::BuildSQL ( const std::string & strBaseDir,
const std::string & strQuery,
Filter & filter,
std::string & strSQL,
CDbUrl & dbUrl,
SortDescription & sorting )
virtual

◆ BuildSQL() [3/3]

bool CDatabase::BuildSQL ( std::string_view strQuery,
const Filter & filter,
std::string & strSQL ) const
protected

◆ Close()

void CDatabase::Close ( )
virtual

Reimplemented in PVR::CPVRDatabase, and PVR::CPVREpgDatabase.

◆ CommitDeleteQueries()

bool CDatabase::CommitDeleteQueries ( )

Commit all queued DELETE queries.

Returns
True if all queries were executed successfully, false otherwise.

◆ CommitInsertQueries()

bool CDatabase::CommitInsertQueries ( )

Commit all queries in the queue.

Returns
True if all queries were executed successfully, false otherwise.

◆ CommitMultipleExecute()

bool CDatabase::CommitMultipleExecute ( )

Commit the multiple execution queue to the database. Queries are performed within a transaction, and the transaction is rolled back should any one query fail.

Returns
True if the queries were executed successfully, false otherwise.
See also
BeginMultipleExecute, ExecuteQuery

◆ CommitTransaction()

bool CDatabase::CommitTransaction ( )
virtual

Reimplemented in CMusicDatabase, and CVideoDatabase.

◆ Compress()

bool CDatabase::Compress ( bool bForce = true)

◆ Connect()

CDatabase::ConnectionState CDatabase::Connect ( const std::string & dbName,
const DatabaseSettings & db,
bool create )

◆ CopyDB()

void CDatabase::CopyDB ( const std::string & latestDb)

◆ CreateAnalytics()

virtual void CDatabase::CreateAnalytics ( )
protectedpure virtual

◆ CreateDatabase()

bool CDatabase::CreateDatabase ( )
protected

Create database tables and analytics as needed. Calls CreateTables() and CreateAnalytics() on child classes.

◆ CreateTables()

virtual void CDatabase::CreateTables ( )
protectedpure virtual

◆ DeleteValues()

bool CDatabase::DeleteValues ( const std::string & strTable,
const Filter & filter = Filter() )

Delete values from a table.

Parameters
strTableThe table to delete the values from.
filterThe Filter to apply to this query.
Returns
True if the query was executed successfully, false otherwise.

◆ DropAnalytics()

void CDatabase::DropAnalytics ( )

◆ ExecuteQuery()

bool CDatabase::ExecuteQuery ( const std::string & strQuery)

Execute a query that does not return any result. Note that if BeginMultipleExecute() has been called, the query will be queued until CommitMultipleExecute() is called.

Parameters
strQueryThe query to execute.
Returns
True if the query was executed successfully, false otherwise.
See also
BeginMultipleExecute, CommitMultipleExecute

◆ GetArtForItem() [1/4]

std::string CDatabase::GetArtForItem ( int mediaId,
const std::string & mediaType,
const std::string & artType )

The url of one type of art for a library item, empty if it has none.

◆ GetArtForItem() [2/4]

bool CDatabase::GetArtForItem ( int mediaId,
const std::string & mediaType,
KODI::ART::Artwork & art )
virtual

Adds the art a library item has to art, a map of art type to url.

Returns
false on a database error

Reimplemented in CMusicDatabase.

◆ GetArtForItem() [3/4]

std::string CDatabase::GetArtForItem ( int mediaId,
KODI::MEDIA::TYPE mediaType,
const std::string & artType )
inline

◆ GetArtForItem() [4/4]

bool CDatabase::GetArtForItem ( int mediaId,
KODI::MEDIA::TYPE mediaType,
KODI::ART::Artwork & art )
inline

◆ GetArtTypes()

bool CDatabase::GetArtTypes ( const std::string & mediaType,
std::vector< std::string > & artTypes )

Adds the distinct types of art held for a type of item to artTypes.

Returns
false on a database error

◆ GetBaseDBName()

virtual const char * CDatabase::GetBaseDBName ( ) const
protectedpure virtual

◆ GetDBVersion()

int CDatabase::GetDBVersion ( )
protected

◆ GetDeleteQueriesCount()

size_t CDatabase::GetDeleteQueriesCount ( ) const

Get the number of DELETE queries in the queue.

Returns
The number of queries.

◆ GetFilter()

virtual bool CDatabase::GetFilter ( CDbUrl & dbUrl,
Filter & filter,
SortDescription & sorting )
inlinevirtual

Reimplemented in CMusicDatabase, and CVideoDatabase.

◆ GetInsertQueriesCount()

size_t CDatabase::GetInsertQueriesCount ( ) const

Get the number of INSERT queries in the queue.

Returns
The number of queries.

◆ GetMinSchemaVersion()

virtual int CDatabase::GetMinSchemaVersion ( ) const
inlineprotectedvirtual

Reimplemented in ADDON::CAddonDatabase, and CMusicDatabase.

◆ GetSchemaVersion()

virtual int CDatabase::GetSchemaVersion ( ) const
protectedpure virtual

◆ GetSingleValue() [1/3]

std::string CDatabase::GetSingleValue ( const std::string & query) const

◆ GetSingleValue() [2/3]

std::string CDatabase::GetSingleValue ( const std::string & query,
dbiplus::Dataset & ds ) const

Get a single value from a query on a dataset.

Parameters
querythe query in question.
dsthe dataset to use for the query.
Returns
the value from the query, empty on failure.

◆ GetSingleValue() [3/3]

std::string CDatabase::GetSingleValue ( const std::string & strTable,
const std::string & strColumn,
const std::string & strWhereClause = std::string(),
const std::string & strOrderBy = std::string() ) const

Get a single value from a table.

Remarks
The values of the strWhereClause and strOrderBy parameters have to be FormatSQL'ed when used.
Parameters
strTableThe table to get the value from. Cannot be a complex expression with joins for example.
strColumnThe column to get.
strWhereClauseIf set, use this WHERE clause.
strOrderByIf set, use this ORDER BY clause.
Returns
The requested value or an empty string if it wasn't found.

◆ GetSingleValueInt() [1/3]

int CDatabase::GetSingleValueInt ( const std::string & query) const

◆ GetSingleValueInt() [2/3]

int CDatabase::GetSingleValueInt ( const std::string & query,
dbiplus::Dataset & ds ) const

Get a single integer value from a query on a dataset.

Parameters
querythe query in question.
dsthe dataset to use for the query.
Returns
the value from the query, 0 on failure.

◆ GetSingleValueInt() [3/3]

int CDatabase::GetSingleValueInt ( const std::string & strTable,
const std::string & strColumn,
const std::string & strWhereClause = std::string(),
const std::string & strOrderBy = std::string() ) const

Get a single integer value from a table.

Remarks
The values of the strWhereClause and strOrderBy parameters have to be FormatSQL'ed when used.
Parameters
strTableThe table to get the value from. Cannot be a complex expression with joins for example.
strColumnThe column to get.
strWhereClauseIf set, use this WHERE clause.
strOrderByIf set, use this ORDER BY clause.
Returns
The requested value or 0 if it wasn't found.

◆ GetType()

const std::string & CDatabase::GetType ( ) const
inline

◆ Interrupt()

void CDatabase::Interrupt ( )

◆ InTransaction()

bool CDatabase::InTransaction ( ) const

◆ IsOpen()

bool CDatabase::IsOpen ( ) const

◆ Open() [1/2]

◆ Open() [2/2]

bool CDatabase::Open ( const DatabaseSettings & db)

◆ PrepareSQL()

std::string CDatabase::PrepareSQL ( std::string_view sqlFormat,
... ) const

◆ QueueDeleteQuery()

bool CDatabase::QueueDeleteQuery ( const std::string & strQuery)

Put a DELETE query in the queue.

Parameters
strQueryThe query to queue.
Returns
True if the query was added successfully, false otherwise.

◆ QueueInsertQuery()

bool CDatabase::QueueInsertQuery ( const std::string & strQuery)

Put an INSERT or REPLACE query in the queue.

Parameters
strQueryThe query to queue.
Returns
True if the query was added successfully, false otherwise.

◆ RemoveArtForItem() [1/3]

bool CDatabase::RemoveArtForItem ( int mediaId,
const std::string & mediaType,
const std::set< std::string, std::less<> > & artTypes )

◆ RemoveArtForItem() [2/3]

bool CDatabase::RemoveArtForItem ( int mediaId,
const std::string & mediaType,
const std::string & artType )

◆ RemoveArtForItem() [3/3]

bool CDatabase::RemoveArtForItem ( int mediaId,
KODI::MEDIA::TYPE mediaType,
const std::set< std::string, std::less<> > & artTypes )
inline

◆ ResultQuery()

bool CDatabase::ResultQuery ( const std::string & strQuery) const

Execute a query that returns a result.

Remarks
Call m_pDS->close(); to clean up the dataset when done.
Parameters
strQueryThe query to execute.
Returns
True if the query was executed successfully, false otherwise.

◆ RollbackTransaction()

void CDatabase::RollbackTransaction ( )

◆ SetArtForItem() [1/4]

bool CDatabase::SetArtForItem ( int mediaId,
const std::string & mediaType,
const KODI::ART::Artwork & art )
virtual

Sets each piece of art, a map of art type to url, for a library item.

Returns
false once a piece cannot be set, leaving the rest unset

Reimplemented in CMusicDatabase.

◆ SetArtForItem() [2/4]

bool CDatabase::SetArtForItem ( int mediaId,
const std::string & mediaType,
const std::string & artType,
const std::string & url )

Sets art for a library item.

Parameters
mediaIdthe id in the item's table.
mediaTypethe type of the item, which corresponds to the table it resides in.
artTypethe type of art to set, e.g. "thumb"; a type of a parent's art, such as "tvshow.poster", is not stored.
urlthe url to the art (the original url, not a cached url).
Returns
false on a database error

◆ SetArtForItem() [3/4]

bool CDatabase::SetArtForItem ( int mediaId,
KODI::MEDIA::TYPE mediaType,
const KODI::ART::Artwork & art )
inline

◆ SetArtForItem() [4/4]

bool CDatabase::SetArtForItem ( int mediaId,
KODI::MEDIA::TYPE mediaType,
const std::string & artType,
const std::string & url )
inline

The art functions above, for an item whose table is a media type.

◆ Split()

void CDatabase::Split ( const std::string & strFileNameAndPath,
std::string & strPath,
std::string & strFileName ) const
protected

◆ UpdateTables()

virtual void CDatabase::UpdateTables ( int version)
inlineprotectedvirtual

Friends And Related Symbol Documentation

◆ CDatabaseManager

friend class CDatabaseManager
friend

Member Data Documentation

◆ m_pDB

std::unique_ptr<dbiplus::Database> CDatabase::m_pDB
protected

◆ m_pDS

std::unique_ptr<dbiplus::Dataset> CDatabase::m_pDS
protected

◆ m_pDS2

std::unique_ptr<dbiplus::Dataset> CDatabase::m_pDS2
protected

◆ m_profileManager

const CProfileManager& CDatabase::m_profileManager
protected

◆ m_sqlite

bool CDatabase::m_sqlite {true}
protected

whether we use sqlite (defaults to true)


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