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

Code before:
  }
  catch (...)
  {
    CLog::LogF(LOGERROR, "failed for movie {}", idMovie);
  }

  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' AND value != '') ",
                   dbId, mediaType.c_str()));

    while (!m_pDS->eof())
    {
      itemIds.emplace_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(

Review comment (suggestion):
If I remember correctly that query caused a lengthy discussion shortly after videoversions merge, and I will need to look closer.
Until then it's safer to validate the inputs from scraper and nfo inputs so the case doesn't happen in the first place, and to remove the existing cases from the db (db bump needed)


Code after:

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' AND value != '') ",
                   dbId, mediaType.c_str()));

    while (!m_pDS->eof())
    {
      itemIds.emplace_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()));