All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.db.qualityprofile.ActiveRuleMapper.xml Maven / Gradle / Ivy

The newest version!
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd">

<mapper namespace="org.sonar.db.qualityprofile.ActiveRuleMapper">

  <sql id="activeRuleColumns">
    a.id,
    a.profile_id as "profileId",
    a.rule_id as "ruleId",
    a.failure_level as "severity",
    a.inheritance as "inheritance",
    r.plugin_rule_key as "rulefield",
    r.plugin_name as "repository",
    rp.kee as "ruleProfileUuid",
    a.created_at as "createdAt",
    a.updated_at as "updatedAt"
  </sql>

  <sql id="orgActiveRuleColumns">
    a.id,
    a.profile_id as "profileId",
    a.rule_id as "ruleId",
    a.failure_level as "severity",
    a.inheritance as "inheritance",
    r.plugin_rule_key as "rulefield",
    r.plugin_name as "repository",
    rp.kee as "ruleProfileUuid",
    a.created_at as "createdAt",
    a.updated_at as "updatedAt",
    oqp.organization_uuid as "organizationUuid",
    oqp.uuid as "profileUuid"
  </sql>

  <sql id="activeRuleKeyJoin">
    inner join rules_profiles rp on rp.id = a.profile_id
    inner join rules r on r.id = a.rule_id
  </sql>

  <insert id="insert" parameterType="ActiveRule" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
    insert into active_rules (
      profile_id,
      rule_id,
      failure_level,
      inheritance,
      created_at,
      updated_at
    ) values (
      #{profileId, jdbcType=BIGINT},
      #{ruleId, jdbcType=BIGINT},
      #{severity, jdbcType=INTEGER},
      #{inheritance, jdbcType=VARCHAR},
      #{createdAt, jdbcType=BIGINT},
      #{updatedAt, jdbcType=BIGINT}
    )
  </insert>

  <update id="update" parameterType="ActiveRule">
    update active_rules
    set
      failure_level = #{severity, jdbcType=INTEGER},
      inheritance = #{inheritance, jdbcType=VARCHAR},
      updated_at = #{updatedAt, jdbcType=BIGINT}
    where
      id = #{id, jdbcType=BIGINT}
  </update>

  <delete id="delete" parameterType="int">
    delete from active_rules
    where
      id=#{id, jdbcType=BIGINT}
  </delete>

  <delete id="deleteByRuleProfileUuids" parameterType="String">
    delete from active_rules
    where exists (
      select 1
      from rules_profiles rp
      where rp.id = profile_id
      and rp.kee in
        <foreach collection="rulesProfileUuids" open="(" close=")" item="rulesProfileUuid" separator=",">#{rulesProfileUuid, jdbcType=VARCHAR}</foreach>
      )
  </delete>

  <delete id="deleteByIds" parameterType="Integer">
    delete from active_rules
    where
      id in
        <foreach collection="ids" open="(" close=")" item="id" separator=",">#{id, jdbcType=INTEGER}</foreach>
  </delete>

  <select id="selectByKey" parameterType="map" resultType="ActiveRule">
    select
    <include refid="activeRuleColumns"/>
    from active_rules a
    <include refid="activeRuleKeyJoin"/>
    where
    rp.kee = #{ruleProfileUuid, jdbcType=VARCHAR}
    and r.plugin_rule_key = #{rule, jdbcType=VARCHAR}
    and r.plugin_name = #{repository, jdbcType=VARCHAR}
  </select>

  <select id="selectByKeys" parameterType="map" resultType="ActiveRule">
    select
    <include refid="activeRuleColumns"/>
    from active_rules a
    <include refid="activeRuleKeyJoin"/>
    where
    <foreach collection="keys" item="key" open="(" separator=" or " close=")">
      (rp.kee = #{key.ruleProfileUuid, jdbcType=VARCHAR}
      AND r.plugin_rule_key = #{key.ruleKey.rule, jdbcType=VARCHAR}
      AND r.plugin_name = #{key.ruleKey.repository, jdbcType=VARCHAR}
      )
    </foreach>
  </select>

  <select id="selectByProfileUuid" parameterType="string" resultType="org.sonar.db.qualityprofile.OrgActiveRuleDto">
    select
    <include refid="orgActiveRuleColumns"/>
    from active_rules a
    inner join rules_profiles rp on rp.id = a.profile_id
    inner join org_qprofiles oqp on oqp.rules_profile_uuid = rp.kee
    inner join rules r on r.id = a.rule_id and r.status != 'REMOVED'
    where oqp.uuid = #{id, jdbcType=VARCHAR}
  </select>

  <select id="selectByRuleProfileUuid" parameterType="string" resultType="org.sonar.db.qualityprofile.ActiveRuleDto">
    select
    <include refid="activeRuleColumns"/>
    from active_rules a
    <include refid="activeRuleKeyJoin"/>
    where
      rp.kee = #{ruleProfileUuid, jdbcType=VARCHAR}
  </select>

  <select id="selectByRuleIdsAndRuleProfileUuids" parameterType="map" resultType="org.sonar.db.qualityprofile.ActiveRuleDto">
    select
    <include refid="activeRuleColumns"/>
    from active_rules a
    <include refid="activeRuleKeyJoin"/>
    where
    r.id in <foreach collection="ruleIds" item="ruleId" separator="," open="(" close=")">#{ruleId, jdbcType=BIGINT}</foreach>
    and rp.kee in <foreach collection="ruleProfileUuids" item="ruleProfileUuid" separator="," open="(" close=")">#{ruleProfileUuid, jdbcType=VARCHAR}</foreach>
  </select>

  <select id="selectByRuleId" parameterType="map" resultType="org.sonar.db.qualityprofile.OrgActiveRuleDto">
    select
    <include refid="orgActiveRuleColumns"/>
    from active_rules a
    inner join rules_profiles rp on rp.id = a.profile_id
    inner join org_qprofiles oqp on oqp.rules_profile_uuid = rp.kee
    inner join rules r on r.id = a.rule_id
    where
    a.rule_id = #{ruleId, jdbcType=BIGINT}
    and oqp.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
  </select>

  <select id="selectByRuleIdOfAllOrganizations" parameterType="Integer" resultType="ActiveRule">
    select
    <include refid="activeRuleColumns"/>
    from active_rules a
    <include refid="activeRuleKeyJoin"/>
    where
      a.rule_id = #{ruleId, jdbcType=BIGINT}
  </select>

  <select id="selectByRuleIds" parameterType="List" resultType="org.sonar.db.qualityprofile.OrgActiveRuleDto">
    select
    <include refid="orgActiveRuleColumns"/>
    from active_rules a
    inner join rules_profiles rp on rp.id = a.profile_id
    inner join org_qprofiles oqp on oqp.rules_profile_uuid = rp.kee
    inner join rules r on r.id = a.rule_id
    where
    a.rule_id in
    <foreach collection="ruleIds" item="ruleId" separator="," open="(" close=")">
      #{ruleId, jdbcType=BIGINT}
    </foreach>
    and oqp.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
  </select>

  <!-- Parameters -->

  <sql id="activeRuleParamColumns">
    p.id,
    p.active_rule_id as activeRuleId,
    p.rules_parameter_id as rulesParameterId,
    p.rules_parameter_key as kee,
    p.value as value
  </sql>

  <insert id="insertParameter" parameterType="ActiveRuleParam" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
    insert into active_rule_parameters (
      active_rule_id,
      rules_parameter_id,
      rules_parameter_key,
      value
    ) values (
      #{activeRuleId, jdbcType=BIGINT},
      #{rulesParameterId, jdbcType=BIGINT},
      #{key, jdbcType=VARCHAR},
      #{value, jdbcType=VARCHAR}
    )
  </insert>

  <update id="updateParameter" parameterType="ActiveRuleParam">
    UPDATE active_rule_parameters SET
    value=#{value, jdbcType=VARCHAR}
    WHERE id=#{id, jdbcType=BIGINT}
  </update>

  <delete id="deleteParameters" parameterType="int">
    DELETE FROM active_rule_parameters WHERE active_rule_id=#{id, jdbcType=BIGINT}
  </delete>

  <delete id="deleteParametersByRuleProfileUuids" parameterType="String">
    delete from active_rule_parameters
    where exists (
      select 1 from active_rules ar
      inner join rules_profiles rp on rp.id = ar.profile_id
      where ar.id = active_rule_id
      and rp.kee in
        <foreach collection="rulesProfileUuids" open="(" close=")" item="rulesProfileUuid" separator=",">
        #{rulesProfileUuid, jdbcType=VARCHAR}
        </foreach>
    )
  </delete>

  <delete id="deleteParameter" parameterType="int">
    DELETE FROM active_rule_parameters WHERE id=#{id, jdbcType=BIGINT}
  </delete>

  <delete id="deleteParamsByActiveRuleIds" parameterType="Integer">
    delete from active_rule_parameters
    where
      active_rule_id in
    <foreach collection="activeRuleIds" open="(" close=")" item="activeRuleId" separator=",">#{activeRuleId, jdbcType=INTEGER}</foreach>
  </delete>

  <select id="selectParamsByActiveRuleId" parameterType="Integer" resultType="ActiveRuleParam">
    select
    <include refid="activeRuleParamColumns"/>
    from active_rule_parameters p
    <where>
      p.active_rule_id=#{id, jdbcType=BIGINT}
    </where>
  </select>

  <select id="selectParamsByActiveRuleIds" parameterType="map" resultType="ActiveRuleParam">
    select
    <include refid="activeRuleParamColumns"/>
    from active_rule_parameters p
    <where>
      <foreach collection="ids" item="id" open="(" separator=" or " close=")">
        p.active_rule_id=#{id, jdbcType=BIGINT}
      </foreach>
    </where>
  </select>

  <select id="countActiveRulesByQuery" resultType="KeyLongValue" parameterType="map">
    select oqp.uuid as "key", count(ar.id) as "value"
    from active_rules ar
    inner join rules_profiles rp on rp.id = ar.profile_id
    inner join org_qprofiles oqp on oqp.rules_profile_uuid = rp.kee
    inner join rules r on r.id = ar.rule_id
    <where>
      oqp.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
      and <foreach collection="profileUuids" item="profileUuid" open="(" separator=" or " close=")">
        oqp.uuid = #{profileUuid, jdbcType=VARCHAR}
      </foreach>
      <choose>
        <when test="ruleStatus == null">
          and r.status != 'REMOVED'
        </when>
        <otherwise>
          and r.status = #{ruleStatus, jdbcType=VARCHAR}
        </otherwise>
      </choose>
      <if test="inheritance != null">
        and ar.inheritance = #{inheritance, jdbcType=VARCHAR}
      </if>
    </where>
    group by oqp.uuid
  </select>

  <select id="scrollAllForIndexing" resultType="org.sonar.db.qualityprofile.IndexedActiveRuleDto" fetchSize="${_scrollFetchSize}" resultSetType="FORWARD_ONLY">
    <include refid="scrollAllForIndexingSql"/>
  </select>

  <select id="scrollByIdsForIndexing" parameterType="map" resultType="org.sonar.db.qualityprofile.IndexedActiveRuleDto" fetchSize="${_scrollFetchSize}" resultSetType="FORWARD_ONLY">
    <include refid="scrollAllForIndexingSql"/>
    where ar.id in
    <foreach collection="ids" open="(" close=")" item="id" separator=",">#{id, jdbcType=BIGINT}</foreach>
  </select>

  <select id="scrollByRuleProfileUuidForIndexing" parameterType="String" resultType="org.sonar.db.qualityprofile.IndexedActiveRuleDto" fetchSize="${_scrollFetchSize}" resultSetType="FORWARD_ONLY">
    <include refid="scrollAllForIndexingSql"/>
    where rp.kee = #{ruleProfileUuid, jdbcType=VARCHAR}
  </select>

  <sql id="scrollAllForIndexingSql">
    select
    ar.id as "id",
    ar.failure_level as "severity",
    ar.inheritance as "inheritance",
    r.id as "ruleId",
    r.plugin_name as "repository",
    r.plugin_rule_key as "key",
    rp.kee as "ruleProfileUuid"
    from active_rules ar
    inner join rules_profiles rp on rp.id = ar.profile_id
    inner join rules r on r.id = ar.rule_id
  </sql>
</mapper>





© 2015 - 2024 Weber Informatics LLC | Privacy Policy