[database] introduce CreateDatabase() to the baseclass that calls CreateTables()...
authorJonathan Marshall <jmarshall@xbmc.org>
Sat, 1 Feb 2014 01:29:04 +0000 (14:29 +1300)
committerJonathan Marshall <jmarshall@xbmc.org>
Wed, 5 Feb 2014 20:27:31 +0000 (09:27 +1300)
16 files changed:
xbmc/TextureDatabase.cpp
xbmc/TextureDatabase.h
xbmc/addons/AddonDatabase.cpp
xbmc/addons/AddonDatabase.h
xbmc/dbwrappers/Database.cpp
xbmc/dbwrappers/Database.h
xbmc/epg/EpgDatabase.cpp
xbmc/epg/EpgDatabase.h
xbmc/music/MusicDatabase.cpp
xbmc/music/MusicDatabase.h
xbmc/pvr/PVRDatabase.cpp
xbmc/pvr/PVRDatabase.h
xbmc/video/VideoDatabase.cpp
xbmc/video/VideoDatabase.h
xbmc/view/ViewDatabase.cpp
xbmc/view/ViewDatabase.h

index 34d27fa..bf0a8e4 100644 (file)
@@ -161,12 +161,8 @@ bool CTextureDatabase::Open()
   return CDatabase::Open();
 }
 
-bool CTextureDatabase::CreateTables()
+void CTextureDatabase::CreateTables()
 {
-  try
-  {
-    CDatabase::CreateTables();
-
     CLog::Log(LOGINFO, "create texture table");
     m_pDS->exec("CREATE TABLE texture (id integer primary key, url text, cachedurl text, imagehash text, lasthashcheck text)");
 
@@ -185,14 +181,6 @@ bool CTextureDatabase::CreateTables()
     // TODO: Should the path index be a covering index? (we need only retrieve texture)
     CLog::Log(LOGINFO, "create path index");
     m_pDS->exec("CREATE INDEX idxPath ON path(url, type)");
-  }
-  catch (...)
-  {
-    CLog::Log(LOGERROR, "%s unable to create tables", __FUNCTION__);
-    return false;
-  }
-
-  return true;
 }
 
 bool CTextureDatabase::UpdateOldVersion(int version)
index 75aa45c..1d67ad0 100644 (file)
@@ -123,7 +123,7 @@ protected:
    */
   unsigned int GetURLHash(const CStdString &url) const;
 
-  virtual bool CreateTables();
+  virtual void CreateTables();
   virtual bool UpdateOldVersion(int version);
   virtual int GetMinVersion() const { return 13; };
   const char *GetBaseDBName() const { return "Textures"; };
index 524895a..af5d9b7 100644 (file)
@@ -44,12 +44,8 @@ bool CAddonDatabase::Open()
   return CDatabase::Open();
 }
 
-bool CAddonDatabase::CreateTables()
+void CAddonDatabase::CreateTables()
 {
-  try
-  {
-    CDatabase::CreateTables();
-
     CLog::Log(LOGINFO, "create addon table");
     m_pDS->exec("CREATE TABLE addon (id integer primary key, type text,"
                 "name text, summary text, description text, stars integer,"
@@ -94,14 +90,6 @@ bool CAddonDatabase::CreateTables()
     CLog::Log(LOGINFO, "create package table");
     m_pDS->exec("CREATE TABLE package (id integer primary key, addonID text, filename text, hash text)\n");
     m_pDS->exec("CREATE UNIQUE INDEX idxPackage ON package(filename)");
-  }
-  catch (...)
-  {
-    CLog::Log(LOGERROR, "%s unable to create tables", __FUNCTION__);
-    return false;
-  }
-
-  return true;
 }
 
 bool CAddonDatabase::UpdateOldVersion(int version)
index 0901a0b..62fc666 100644 (file)
@@ -135,7 +135,7 @@ public:
   */
   bool RemovePackage(const CStdString& packageFileName);
 protected:
-  virtual bool CreateTables();
+  virtual void CreateTables();
   virtual bool UpdateOldVersion(int version);
   virtual int GetMinVersion() const { return 16; }
   const char *GetBaseDBName() const { return "Addons"; }
index 1deb4c9..23db348 100644 (file)
@@ -489,7 +489,7 @@ bool CDatabase::Connect(const CStdString &dbName, const DatabaseSettings &dbSett
         //  Also set the memory cache size to 16k
         m_pDS->exec("PRAGMA default_cache_size=4096\n");
       }
-      CreateTables();
+      CreateDatabase();
     }
 
     // sqlite3 post connection operations
@@ -673,15 +673,26 @@ bool CDatabase::InTransaction()
   return m_pDB->in_transaction();
 }
 
-bool CDatabase::CreateTables()
+bool CDatabase::CreateDatabase()
 {
-
+  BeginTransaction();
+  try
+  {
     CLog::Log(LOGINFO, "creating version table");
     m_pDS->exec("CREATE TABLE version (idVersion integer, iCompressCount integer)\n");
     CStdString strSQL=PrepareSQL("INSERT INTO version (idVersion,iCompressCount) values(%i,0)\n", GetMinVersion());
     m_pDS->exec(strSQL.c_str());
 
-    return true;
+    CreateTables();
+  }
+  catch (...)
+  {
+    CLog::Log(LOGERROR, "%s unable to create database:%i", __FUNCTION__, (int)GetLastError());
+    RollbackTransaction();
+    return false;
+  }
+  CommitTransaction();
+  return true;
 }
 
 bool CDatabase::UpdateVersionNumber()
index a270f08..835793d 100644 (file)
@@ -169,8 +169,17 @@ protected:
   uint32_t ComputeCRC(const CStdString &text);
 
   virtual bool Open();
-  virtual bool CreateTables();
-  virtual void CreateViews() {};
+
+  /*! \brief Create database tables and analytics as needed.
+   Calls CreateTables() on child classes.
+   */
+  bool CreateDatabase();
+
+  /* \brief Create tables for the current database schema.
+   Will be called on database creation.
+   */
+  virtual void CreateTables()=0;
+
   virtual bool UpdateOldVersion(int version) { return true; };
 
   virtual int GetMinVersion() const=0;
index 91699a0..90d1f57 100644 (file)
@@ -37,16 +37,8 @@ bool CEpgDatabase::Open(void)
   return CDatabase::Open(g_advancedSettings.m_databaseEpg);
 }
 
-bool CEpgDatabase::CreateTables(void)
+void CEpgDatabase::CreateTables(void)
 {
-  bool bReturn(false);
-
-  try
-  {
-    CDatabase::CreateTables();
-
-    BeginTransaction();
-
     CLog::Log(LOGINFO, "EpgDB - %s - creating tables", __FUNCTION__);
 
     CLog::Log(LOGDEBUG, "EpgDB - %s - creating table 'epg'", __FUNCTION__);
@@ -91,20 +83,6 @@ bool CEpgDatabase::CreateTables(void)
           "sLastScan varchar(20)"
         ")"
     );
-
-    CommitTransaction();
-
-    bReturn = true;
-  }
-  catch (...)
-  {
-    CLog::Log(LOGERROR, "EpgDB - %s - unable to create EPG tables:%i",
-        __FUNCTION__, (int)GetLastError());
-    RollbackTransaction();
-    bReturn = false;
-  }
-
-  return bReturn;
 }
 
 bool CEpgDatabase::UpdateOldVersion(int iVersion)
index e8a428b..d6130ad 100644 (file)
@@ -148,9 +148,8 @@ namespace EPG
   protected:
     /*!
      * @brief Create the EPG database tables.
-     * @return True if the tables were created successfully, false otherwise.
      */
-    virtual bool CreateTables(void);
+    virtual void CreateTables();
 
     /*!
      * @brief Update an old version of the database.
index 0f14c1a..81e19e4 100644 (file)
@@ -110,13 +110,8 @@ bool CMusicDatabase::Open()
   return CDatabase::Open(g_advancedSettings.m_databaseMusic);
 }
 
-bool CMusicDatabase::CreateTables()
+void CMusicDatabase::CreateTables()
 {
-  BeginTransaction();
-  try
-  {
-    CDatabase::CreateTables();
-
     CLog::Log(LOGINFO, "create artist table");
     m_pDS->exec("CREATE TABLE artist ( idArtist integer primary key, "
                 " strArtist varchar(256), strMusicBrainzArtistID text, "
@@ -264,15 +259,6 @@ bool CMusicDatabase::CreateTables()
 
     // Add 'Karaoke' genre
     AddGenre( "Karaoke" );
-  }
-  catch (...)
-  {
-    CLog::Log(LOGERROR, "%s unable to create tables:%i", __FUNCTION__, (int)GetLastError());
-    RollbackTransaction();
-    return false;
-  }
-  CommitTransaction();
-  return true;
 }
 
 void CMusicDatabase::CreateViews()
index 86bd366..0c42b83 100644 (file)
@@ -468,7 +468,7 @@ protected:
   std::map<CStdString, int> m_thumbCache;
   std::map<CStdString, CAlbum> m_albumCache;
 
-  virtual bool CreateTables();
+  virtual void CreateTables();
   virtual int GetMinVersion() const;
 
   const char *GetBaseDBName() const { return "MyMusic"; };
index 22cefb3..0f45c6a 100644 (file)
@@ -42,16 +42,8 @@ bool CPVRDatabase::Open()
   return CDatabase::Open(g_advancedSettings.m_databaseTV);
 }
 
-bool CPVRDatabase::CreateTables()
+void CPVRDatabase::CreateTables()
 {
-  bool bReturn(false);
-
-  try
-  {
-    if (!CDatabase::CreateTables())
-      return false;
-
-    BeginTransaction();
     CLog::Log(LOGINFO, "PVR - %s - creating tables", __FUNCTION__);
 
     CLog::Log(LOGDEBUG, "PVR - %s - creating table 'clients'", __FUNCTION__);
@@ -159,30 +151,15 @@ bool CPVRDatabase::CreateTables()
         ")"
     );
 
-    CommitTransaction();
-    bReturn = true;
-  }
-  catch (...)
-  {
-    CLog::Log(LOGERROR, "PVR - %s - unable to create PVR database tables (error %i)", __FUNCTION__, (int)GetLastError());
-    RollbackTransaction();
-    bReturn = false;
-  }
-
-  if (bReturn)
-  {
     // disable all PVR add-on when started the first time
     ADDON::VECADDONS addons;
-    if ((bReturn = CAddonMgr::Get().GetAddons(ADDON_PVRDLL, addons, true)) == false)
+    if (!CAddonMgr::Get().GetAddons(ADDON_PVRDLL, addons, true))
       CLog::Log(LOGERROR, "PVR - %s - failed to get add-ons from the add-on manager", __FUNCTION__);
     else
     {
       for (IVECADDONS it = addons.begin(); it != addons.end(); it++)
         CAddonMgr::Get().DisableAddon(it->get()->ID());
     }
-  }
-
-  return bReturn;
 }
 
 bool CPVRDatabase::UpdateOldVersion(int iVersion)
index b2c9c52..363332e 100644 (file)
@@ -221,7 +221,7 @@ namespace PVR
      * @brief Create the PVR database tables.
      * @return True if the tables were created successfully, false otherwise.
      */
-    bool CreateTables();
+    void CreateTables();
 
     bool DeleteChannelsFromGroup(const CPVRChannelGroup &group);
     bool DeleteChannelsFromGroup(const CPVRChannelGroup &group, const std::vector<int> &channelsToDelete);
index 7d399d1..9d339f5 100644 (file)
@@ -81,7 +81,7 @@ bool CVideoDatabase::Open()
   return CDatabase::Open(g_advancedSettings.m_databaseVideo);
 }
 
-bool CVideoDatabase::CreateTables()
+void CVideoDatabase::CreateTables()
 {
   /* indexes should be added on any columns that are used in in  */
   /* a where or a join. primary key on a column is the same as a */
@@ -94,11 +94,6 @@ bool CVideoDatabase::CreateTables()
   /* advantage of a index that has been created on ( idGenre, idMovie ) */
   /*, hower on on ( idMovie, idGenre ) will be considered for use       */
 
-  BeginTransaction();
-  try
-  {
-    CDatabase::CreateTables();
-
     CLog::Log(LOGINFO, "create bookmark table");
     m_pDS->exec("CREATE TABLE bookmark ( idBookmark integer primary key, idFile integer, timeInSeconds double, totalTimeInSeconds double, thumbNailImage text, player text, playerState text, type integer)\n");
     m_pDS->exec("CREATE INDEX ix_bookmark ON bookmark (idFile, type)");
@@ -349,15 +344,6 @@ bool CVideoDatabase::CreateTables()
 
     // we create views last to ensure all indexes are rolled in
     CreateViews();
-  }
-  catch (...)
-  {
-    CLog::Log(LOGERROR, "%s unable to create tables:%i", __FUNCTION__, (int)GetLastError());
-    RollbackTransaction();
-    return false;
-  }
-  CommitTransaction();
-  return true;
 }
 
 void CVideoDatabase::CreateViews()
index 44f94fa..71cf93e 100644 (file)
@@ -785,7 +785,7 @@ protected:
   CStdString GetValueString(const CVideoInfoTag &details, int min, int max, const SDbTableOffsets *offsets) const;
 
 private:
-  virtual bool CreateTables();
+  virtual void CreateTables();
   virtual bool UpdateOldVersion(int version);
 
   /*! \brief (Re)Create the generic database views for movies, tvshows,
index 45d79e5..168241e 100644 (file)
@@ -48,12 +48,8 @@ bool CViewDatabase::Open()
   return CDatabase::Open();
 }
 
-bool CViewDatabase::CreateTables()
+void CViewDatabase::CreateTables()
 {
-  try
-  {
-    CDatabase::CreateTables();
-
     CLog::Log(LOGINFO, "create view table");
     m_pDS->exec("CREATE TABLE view ("
                   "idView integer primary key,"
@@ -68,15 +64,6 @@ bool CViewDatabase::CreateTables()
     m_pDS->exec("CREATE INDEX idxViews ON view(path)");
     CLog::Log(LOGINFO, "create view - window index");
     m_pDS->exec("CREATE INDEX idxViewsWindow ON view(window)");
-  }
-  catch (...)
-  {
-    CLog::Log(LOGERROR, "%s unable to create tables:%u",
-              __FUNCTION__, GetLastError());
-    return false;
-  }
-
-  return true;
 }
 
 bool CViewDatabase::UpdateOldVersion(int version)
index 191a20f..05fb5dc 100644 (file)
@@ -34,7 +34,7 @@ public:
   bool ClearViewStates(int windowID);
 
 protected:
-  virtual bool CreateTables();
+  virtual void CreateTables();
   virtual bool UpdateOldVersion(int version);
   virtual int GetMinVersion() const { return 6; };
   const char *GetBaseDBName() const { return "ViewModes"; };