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

Code before:
                             "episode WHERE idFile = %i UNION SELECT idFile FROM "
                             "videoversion WHERE idFile = %i",
                             idFile, idFile, idFile)};
  m_pDS->query(sql);
  if (m_pDS->eof())
  {
    // Get idPath
    sql = PrepareSQL("SELECT path.idPath, path.strPath FROM path JOIN files ON path.idPath = "
                     "files.idPath WHERE idFile = %i",
                     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::IsBluray(strPath))
    {
      sql = PrepareSQL("SELECT 1 FROM files WHERE idPath = %i", idPath);
      m_pDS->query(sql);
      if (m_pDS->eof())
      {
        sql = PrepareSQL("DELETE FROM path WHERE idPath = %i", idPath);
        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)

Review comment (suggestion):
Suggestion: combine the queries.
```
DELETE FROM path
WHERE idPath = %i
AND NOT EXISTS (SELECT 1 FROM files WHERE files.idPath = %i)
```

Code after:
      sql = PrepareSQL("SELECT path.idPath, path.strPath FROM path "
                       "JOIN files ON path.idPath = files.idPath "
                       "WHERE idFile = %i",
                       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