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

Code before:
  {
    if (nullptr == m_pDB)
      return -1;
    if (nullptr == m_pDS)
      return -1;

    // need this due to the nested GetEpisodeInfo query
    std::unique_ptr<Dataset> pDS;
    pDS.reset(m_pDB->CreateDataset());
    if (nullptr == pDS)
      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 (...)

Review comment (suggestion):
pDS remains open after this block. it's not a good idea to rely on dataset self-protection logic to close automatically when it's used again.

Code after:
  return -1;
}

int CVideoDatabase::GetEpisodeId(const std::string& strFilenameAndPath, int idEpisode, int idSeason) // input value is episode/season number hint - for multiparters
{
  try
  {
    if (nullptr == m_pDB)
      return -1;

    // 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://