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

Code before:
}

int CVideoDatabase::GetFileIdByEpisode(int idEpisode)
{
  if (!m_pDB || !m_pDS)
    return -1;

  int idFile = -1;

  try
  {
    m_pDS->query(PrepareSQL("SELECT idFile FROM episode WHERE idEpisode = %i", idEpisode));

    if (!m_pDS->eof())
    {
      idFile = m_pDS->fv("idFile").get_asInt();
    }

    m_pDS->close();
  }
  catch (...)
  {
    CLog::Log(LOGERROR, "{} failed for episode {}", __FUNCTION__, idEpisode);
  }

  return idFile;
}

void CVideoDatabase::GetSameVideoItems(const CFileItem& item, CFileItemList& items)
{
  if (!m_pDB || !m_pDS)
    return;

  std::vector<int> itemIds;

  int dbId = item.GetVideoInfoTag()->m_iDbId;
  MediaType mediaType = item.GetVideoInfoTag()->m_type;

  try
  {
    // get items with same unique ids (imdb, tmdb, etc.) as the specified item, these are
    // the different versions of the item
    // note: old records may have the type 'unknown'
    // note 2: for type 'tmdb' the same value may be used for a movie and a tv episode, only
    // distinguished by media_type.
    // @todo make the (value,type) pairs truly unique
    m_pDS->query(PrepareSQL("SELECT DISTINCT media_id "
                            "FROM uniqueid "
                            "WHERE (media_type, value, type) IN "
                            "  (SELECT media_type, value, type "
                            "  FROM uniqueid WHERE media_id = %i AND media_type = '%s') ",

Review comment (suggestion):
I think this can be implemented far more simple using `GetSingleValueInt`method.

```c++
  const std::string query{PrepareSQL("SELECT idFile FROM episode WHERE idEpisode = %i", idEpisode)};
  const int idFile{GetSingleValueInt(query, m_pDS)};
  return idFile > 0 ? idFile : -1;
```

Code after:
  return idFile;
}

void CVideoDatabase::GetSameVideoItems(const CFileItem& item, CFileItemList& items)
{
  if (!m_pDB || !m_pDS)
    return;

  std::vector<int> itemIds;

  int dbId = item.GetVideoInfoTag()->m_iDbId;
  MediaType mediaType = item.GetVideoInfoTag()->m_type;

  try
  {
    // get items with same unique ids (imdb, tmdb, etc.) as the specified item, these are
    // the different versions of the item
    // note: old records may have the type 'unknown'
    // note 2: for type 'tmdb' the same value may be used for a movie and a tv episode, only
    // distinguished by media_type.
    // @todo make the (value,type) pairs truly unique
    m_pDS->query(PrepareSQL("SELECT DISTINCT media_id "
                            "FROM uniqueid "
                            "WHERE (media_type, value, type) IN "
                            "  (SELECT media_type, value, type "
                            "  FROM uniqueid WHERE media_id = %i AND media_type = '%s') ",
                            dbId, mediaType.c_str()));

    while (!m_pDS->eof())
    {
      itemIds.push_back(m_pDS->fv("media_id").get_asInt());
      m_pDS->next();
    }

    m_pDS->close();

    VideoDbContentType itemType = item.GetVideoContentType();

    // get items with same title (and year if exists) as the specified item, these are
    // potentially different versions of the item
    if (itemType == VideoDbContentType::MOVIES)
    {
      if (item.GetVideoInfoTag()->HasYear())
        m_pDS->query(
            PrepareSQL("SELECT idMovie FROM movie WHERE c%02d = '%s' AND premiered LIKE '%i%%'",
                       VIDEODB_ID_TITLE, item.GetVideoInfoTag()->GetTitle().c_str(),
                       item.GetVideoInfoTag()->GetYear()));
      else
        m_pDS->query(
            PrepareSQL("SELECT idMovie FROM movie WHERE c%02d = '%s' AND LENGTH(premiered) < 4",
                       VIDEODB_ID_TITLE, item.GetVideoInfoTag()->GetTitle().c_str()));