Repository: xbmc/xbmc
File: xbmc/video/VideoDatabase.cpp (C++)

Code before:
    int path{-1};
    std::string strPath;
    if (!m_pDS->eof())
    {
      path = m_pDS->fv("idPath").get_asInt();
      strPath = m_pDS->fv("strPath").get_asString();
    }

    // Remove file and any associated bookmarks and streamdetails
    sql = PrepareSQL("DELETE FROM files WHERE idFile = %i", idFile);
    m_pDS->exec(sql);
    sql = PrepareSQL("DELETE FROM bookmark WHERE idFile = %i", idFile);
    m_pDS->exec(sql);
    DeleteStreamDetails(idFile);

    // Delete path if orphan (and not base directory - needs to remain to prevent re-adding on library update)
    if (path >= 0 && URIUtils::IsBlurayPath(strPath))
    {
      sql = PrepareSQL("SELECT idFile FROM files JOIN path ON files.idPath = path.idPath WHERE "
                       "files.idPath = %i",
                       path);
      m_pDS->query(sql);
      if (m_pDS->eof())
      {
        sql = PrepareSQL("DELETE FROM path WHERE idPath = %i", path);
        m_pDS->exec(sql);
      }
    }
  }
}

int CVideoDatabase::SetDetailsForEpisode(CVideoInfoTag& details,
                                         const std::map<std::string, std::string>& artwork,
                                         int idShow,
                                         int idEpisode /* = -1 */)
{
  const auto filePath = details.GetPath();

  try
  {
    BeginTransaction();
    if (idEpisode < 0)
      idEpisode = GetEpisodeId(filePath);

    if (idEpisode > 0)
      DeleteEpisode(idEpisode, true); // true to keep the table entry
    else
    {
      // only add a new episode if we don't already have a valid idEpisode
      // (DeleteEpisode is called with bKeepId == true so the episode won't
      // be removed from the episode table)

Review comment (suggestion):


Another opportunity to use `GetSingleValueInt()`?

if (GetSingle ... == 0)


Code after:
                       idFile);
      m_pDS->query(sql);
      int idPath{-1};
      std::string strPath;
      if (!m_pDS->eof())
      {
        idPath = m_pDS->fv("idPath").get_asInt();
        strPath = m_pDS->fv("strPath").get_asString();
      }

      // Associated bookmarks and streamdetails deleted by delete trigger
      sql = PrepareSQL("DELETE FROM files WHERE idFile = %i", idFile);
      m_pDS->exec(sql);

      // Delete path if orphan (and not base directory - needs to remain to prevent re-adding on library update)
      if (idPath >= 0 && URIUtils::IsBlurayPath(strPath))
      {
        sql = PrepareSQL("DELETE FROM path WHERE idPath = %i "
                         "AND NOT EXISTS (SELECT 1 FROM files WHERE files.idPath = %i)",
                         idPath, idPath);
        m_pDS->exec(sql);
      }
    }
    return true;
  }
  catch (...)
  {
    CLog::LogF(LOGERROR, "({}) failed", idFile);
  }
  return false;
}

int CVideoDatabase::SetDetailsForEpisode(CVideoInfoTag& details,
                                         const std::map<std::string, std::string>& artwork,
                                         int idShow,
                                         int idEpisode /* = -1 */)
{
  const auto filePath = details.GetPath();

  try
  {
    BeginTransaction();
    if (idEpisode < 0)
      idEpisode = GetEpisodeId(filePath);

    if (idEpisode > 0)
      DeleteEpisode(idEpisode, true); // true to keep the table entry
    else
    {
      // only add a new episode if we don't already have a valid idEpisode
      // (DeleteEpisode is called with bKeepId == true so the episode won't