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

Code before:
  {
    CLog::LogF(LOGERROR, "unable to update unique ids of ({})", mediaType);
  }
  return -1;
}

int CVideoDatabase::AddUniqueIDs(int mediaId, const char *mediaType, const CVideoInfoTag& details)
{
  int uniqueid = -1;
  try
  {
    if (nullptr == m_pDB)
      return -1;
    if (nullptr == m_pDS)
      return -1;

    for (const auto& [type, value] : details.GetUniqueIDs())
    {
      int id;

      if (value.empty())
      {
        CLog::LogF(LOGWARNING, "Skipped insert empty {} uniqueId for {} with mediaId {}", type,
                   mediaType, mediaId);
        continue;
      }

      std::string strSQL = PrepareSQL(
          "SELECT uniqueid_id FROM uniqueid WHERE media_id=%i AND media_type='%s' AND type = '%s'",
          mediaId, mediaType, type.c_str());
      m_pDS->query(strSQL);
      if (m_pDS->num_rows() == 0)
      {
        m_pDS->close();
        // doesn't exists, add it
        strSQL = PrepareSQL("INSERT INTO uniqueid (media_id, media_type, value, type) VALUES (%i, "
                            "'%s', '%s', '%s')",
                            mediaId, mediaType, value.c_str(), type.c_str());
        m_pDS->exec(strSQL);
        id = static_cast<int>(m_pDS->lastinsertid());
      }
      else
      {
        id = m_pDS->fv(0).get_asInt();
        m_pDS->close();
        strSQL = PrepareSQL("UPDATE uniqueid SET value = '%s', type = '%s' WHERE uniqueid_id = %i",
                            value.c_str(), type.c_str(), id);
        m_pDS->exec(strSQL);
      }
      if (type == details.GetDefaultUniqueID())
        uniqueid = id;

Review comment (suggestion):
Does the job but I think the caller of the scraper should be responsible of the validation the scraper's output. That way all code can only be in contact with "good" uniqueid data.
That means a change in the whereabouts of InfoTagVideo::setUniqueID/setUniqueIDs of the Python API or more centrally CVideoInfoTag::SetUniqueIDs. Eliminating a uniqueid provider can impact the default uniqueid provider so that'd be another small adjustment. Trimming as suggested by copilot wouldn't hurt.
I could whip something up for that, it'd be a good opportunity to add some unit tests.


Code after:
  {
    CLog::LogF(LOGERROR, "unable to update unique ids of ({})", mediaType);
  }
  return -1;
}

int CVideoDatabase::AddUniqueIDs(int mediaId, const char *mediaType, const CVideoInfoTag& details)
{
  int uniqueid = -1;
  try
  {
    if (nullptr == m_pDB)
      return -1;
    if (nullptr == m_pDS)
      return -1;

    for (const auto& [type, value] : details.GetUniqueIDs())
    {
      int id;
      std::string strSQL = PrepareSQL(
          "SELECT uniqueid_id FROM uniqueid WHERE media_id=%i AND media_type='%s' AND type = '%s'",
          mediaId, mediaType, type.c_str());
      m_pDS->query(strSQL);
      if (m_pDS->num_rows() == 0)
      {
        m_pDS->close();
        // doesn't exists, add it
        strSQL = PrepareSQL("INSERT INTO uniqueid (media_id, media_type, value, type) VALUES (%i, "
                            "'%s', '%s', '%s')",
                            mediaId, mediaType, value.c_str(), type.c_str());
        m_pDS->exec(strSQL);
        id = static_cast<int>(m_pDS->lastinsertid());
      }
      else
      {
        id = m_pDS->fv(0).get_asInt();
        m_pDS->close();
        strSQL = PrepareSQL("UPDATE uniqueid SET value = '%s', type = '%s' WHERE uniqueid_id = %i",
                            value.c_str(), type.c_str(), id);
        m_pDS->exec(strSQL);
      }
      if (type == details.GetDefaultUniqueID())
        uniqueid = id;
    }
    return uniqueid;

  }
  catch (...)
  {
    CLog::LogF(LOGERROR, "({}, {}) failed", mediaId, mediaType);
  }