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

Code before:
  if (idFile < 0)
    return false;

  try
  {
    assert(m_pDB->in_transaction());
    std::string sql = PrepareSQL("UPDATE movie SET idFile=%i WHERE idFile=%i", idFile, oldIdFile);
    m_pDS->exec(sql);

    sql = PrepareSQL("UPDATE videoversion SET idFile=%i WHERE idFile=%i AND media_type='movie'",
                     idFile, oldIdFile);
    m_pDS->exec(sql);

    sql = PrepareSQL("UPDATE art SET media_id=%i WHERE media_id=%i AND media_type='videoversion'",
                     idFile, oldIdFile);
    m_pDS->exec(sql);
    sql = PrepareSQL("SELECT idFile FROM streamdetails WHERE idFile = %i", idFile);
    m_pDS->query(sql);
    if (m_pDS->eof())
    {
      sql = PrepareSQL("UPDATE streamdetails SET idFile=%i WHERE idFile=%i", idFile, oldIdFile);
      m_pDS->exec(sql);
    }
    DeleteFile(oldIdFile);

    return true;
  }
  catch (...)
  {
    CLog::Log(LOGERROR, "{} ({}) failed", __FUNCTION__, idFile);
  }
  return false;
}

void CVideoDatabase::DeleteFile(int idFile)
{
  // First check not multiple references to file (eg. episodes)
  std::string sql{PrepareSQL("SELECT idFile FROM movie WHERE idFile = %i UNION SELECT idFile FROM "
                             "episode WHERE idFile = %i",
                             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 path{-1};
    std::string strPath;
    if (!m_pDS->eof())

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

Code after:
    return false;

  try
  {
    std::string sql = PrepareSQL("UPDATE movie SET idFile=%i WHERE idFile=%i AND idMovie=%i",
                                 idFile, oldIdFile, idMovie);
    m_pDS->exec(sql);

    sql = PrepareSQL(
        "UPDATE videoversion SET idFile=%i WHERE idFile=%i AND media_type='movie' AND idMedia=%i",
        idFile, oldIdFile, idMovie);
    m_pDS->exec(sql);

    sql = PrepareSQL("UPDATE art SET media_id=%i WHERE media_id=%i AND media_type='videoversion'",
                     idFile, oldIdFile);
    m_pDS->exec(sql);

    sql = PrepareSQL("UPDATE streamdetails SET idFile=%i WHERE idFile=%i AND NOT EXISTS (SELECT 1 "
                     "FROM streamdetails WHERE idFile=%i)",
                     idFile, oldIdFile, idFile);
    m_pDS->exec(sql);

    return DeleteFile(oldIdFile);
  }
  catch (...)
  {
    CLog::LogF(LOGERROR, " idFile {}, fileAndPath {}, idMovie {}, oldIdFile {} - failed", idFile,
               fileAndPath, idMovie, oldIdFile);
  }
  return false;
}

bool CVideoDatabase::DeleteFile(int idFile)
{
  try
  {
    // First check no other references to file (eg. other episodes)
    std::string sql{PrepareSQL("SELECT idFile FROM movie WHERE idFile = %i "
                               "UNION SELECT idFile FROM 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};