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

Code before:
      return -1;

    int idFile = GetFileId(strFilenameAndPath);
    if (idFile < 0)
      return -1;

    std::string strSQL=PrepareSQL("select idEpisode from episode where idFile=%i", idFile);

    CLog::LogFC(LOGDEBUG, LOGDATABASE, "({}), query = {}", CURL::GetRedacted(strFilenameAndPath),
                strSQL);
    pDS->query(strSQL);
    if (pDS->num_rows() > 0)
    {
      if (idEpisode == -1)
        idEpisode = pDS->fv("episode.idEpisode").get_asInt();
      else // use the hint!
      {
        while (!pDS->eof())
        {
          CVideoInfoTag tag;
          int idTmpEpisode = pDS->fv("episode.idEpisode").get_asInt();
          GetEpisodeBasicInfo(strFilenameAndPath, tag, idTmpEpisode);
          if (tag.m_iEpisode == idEpisode && (idSeason == -1 || tag.m_iSeason == idSeason)) {
            // match on the episode hint, and there's no season hint or a season hint match
            idEpisode = idTmpEpisode;
            break;
          }
          pDS->next();
        }
        if (pDS->eof())
          idEpisode = -1;
      }
    }
    else
      idEpisode = -1;

    pDS->close();

    return idEpisode;
  }
  catch (...)
  {
    CLog::LogF(LOGERROR, "({}) failed", strFilenameAndPath);
  }
  return -1;
}

int CVideoDatabase::GetMusicVideoId(const std::string& strFilenameAndPath)
{
  try
  {

Review comment (performance):
Kind of expensive and inefficient because of the loop, and comes in addition to the previous query,

If the hint episode is not provided then query directly episode_view by idFile to retrieve all rows and get the season and episode (c12 c13) for the comparison

That way it may be possible to get rid of the local pDS that deviates from the usual code patterns (not to say that they're good, but they're consistent).

It seems difficult to combine with the query of the next block though. It may be possible by adding a OR condition but then the C++ code has to be more complex to triage the returned rows I'm afraid.

Code after:
    // need this due to the nested GetEpisodeInfo query
    std::unique_ptr<Dataset> pDS;
    pDS.reset(m_pDB->CreateDataset());
    if (nullptr == pDS)
      return -1;

    const int idFile{GetFileId(strFilenameAndPath)};
    int idReturnEpisode{-1};
    if (idFile > 0)
    {
      const std::string strSQL{PrepareSQL("select idEpisode from episode where idFile=%i", idFile)};

      CLog::LogFC(LOGDEBUG, LOGDATABASE, "({}), query = {}", CURL::GetRedacted(strFilenameAndPath),
                  strSQL);
      pDS->query(strSQL);
      if (pDS->num_rows() > 0)
      {
        if (idEpisode == -1)
          idReturnEpisode = pDS->fv("episode.idEpisode").get_asInt();
        else // use the hint!
        {
          while (!pDS->eof())
          {
            CVideoInfoTag tag;
            const int idTmpEpisode{pDS->fv("episode.idEpisode").get_asInt()};
            GetEpisodeBasicInfo(strFilenameAndPath, tag, idTmpEpisode);
            if (tag.m_iEpisode == idEpisode && (idSeason == -1 || tag.m_iSeason == idSeason))
            {
              // match on the episode hint, and there's no season hint or a season hint match
              idReturnEpisode = idTmpEpisode;
              break;
            }
            pDS->next();
          }
        }
      }
    }

    if (idReturnEpisode == -1 && idEpisode != -1 && idSeason != -1)
    {
      // Consider the possibility the path could be bluray://
      // In which case strFilenameAndPath is the basepath (in case of files) or
      // <basepath>/file.iso (in case of ISO)
      std::string path;
      if (!URIUtils::IsDiscImage(strFilenameAndPath))
        path = URIUtils::GetDiscBase(strFilenameAndPath);
      else
        path = strFilenameAndPath;

      const std::string strSQL{PrepareSQL("select strFileName, strPath from episode_view "
                                          "where c%02d='%s' and c%02d=%i and c%02d=%i",