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 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("SELECT 1 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 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);

Review comment (refactor):
Please simplify and combine as one query, something like:
```
UPDATE streamdetails SET idFile= %idFile
WHERE idFile= %oldIdFile
AND NOT EXISTS(SELECT 1 FROM streamdetails WHERE idFile = %idFile)
```


Code after:
    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};
      std::string strPath;
      if (!m_pDS->eof())
      {
        idPath = m_pDS->fv("idPath").get_asInt();