You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

QualityProfileMapper.xml 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd">
  3. <mapper namespace="org.sonar.db.qualityprofile.QualityProfileMapper">
  4. <sql id="qProfileColumns">
  5. oqp.uuid as kee,
  6. oqp.organization_uuid as organizationUuid,
  7. oqp.parent_uuid as parentKee,
  8. oqp.last_used as lastUsed,
  9. oqp.user_updated_at as userUpdatedAt,
  10. rp.uuid as rulesProfileUuid,
  11. rp.name as name,
  12. rp.language as language,
  13. rp.rules_updated_at as rulesUpdatedAt,
  14. rp.is_built_in as isBuiltIn
  15. </sql>
  16. <sql id="ruleProfileColumns">
  17. rp.uuid as uuid,
  18. rp.name as name,
  19. rp.language as language,
  20. rp.rules_updated_at as rulesUpdatedAt,
  21. rp.is_built_in as isBuiltIn
  22. </sql>
  23. <insert id="insertRuleProfile" parameterType="map" useGeneratedKeys="false">
  24. insert into rules_profiles (
  25. uuid,
  26. name,
  27. language,
  28. created_at,
  29. updated_at,
  30. rules_updated_at,
  31. is_built_in
  32. ) values (
  33. #{dto.uuid, jdbcType=VARCHAR},
  34. #{dto.name, jdbcType=VARCHAR},
  35. #{dto.language, jdbcType=VARCHAR},
  36. #{now, jdbcType=TIMESTAMP},
  37. #{now, jdbcType=TIMESTAMP},
  38. #{dto.rulesUpdatedAt, jdbcType=VARCHAR},
  39. #{dto.isBuiltIn, jdbcType=BOOLEAN}
  40. )
  41. </insert>
  42. <insert id="insertOrgQProfile" parameterType="map" useGeneratedKeys="false">
  43. insert into org_qprofiles (
  44. uuid,
  45. organization_uuid,
  46. rules_profile_uuid,
  47. parent_uuid,
  48. last_used,
  49. user_updated_at,
  50. created_at,
  51. updated_at
  52. ) values (
  53. #{dto.uuid, jdbcType=VARCHAR},
  54. #{dto.organizationUuid, jdbcType=VARCHAR},
  55. #{dto.rulesProfileUuid, jdbcType=VARCHAR},
  56. #{dto.parentUuid, jdbcType=VARCHAR},
  57. #{dto.lastUsed, jdbcType=BIGINT},
  58. #{dto.userUpdatedAt, jdbcType=BIGINT},
  59. #{now, jdbcType=BIGINT},
  60. #{now, jdbcType=BIGINT}
  61. )
  62. </insert>
  63. <update id="updateRuleProfile" parameterType="map">
  64. update rules_profiles
  65. set
  66. name = #{dto.name, jdbcType=VARCHAR},
  67. language = #{dto.language, jdbcType=VARCHAR},
  68. updated_at = #{now, jdbcType=TIMESTAMP},
  69. rules_updated_at = #{dto.rulesUpdatedAt, jdbcType=VARCHAR},
  70. is_built_in = #{dto.isBuiltIn, jdbcType=BOOLEAN}
  71. where
  72. uuid = #{dto.uuid, jdbcType=VARCHAR}
  73. </update>
  74. <update id="updateOrgQProfile" parameterType="map">
  75. update org_qprofiles
  76. set
  77. parent_uuid = #{dto.parentUuid, jdbcType=VARCHAR},
  78. last_used = #{dto.lastUsed, jdbcType=BIGINT},
  79. user_updated_at = #{dto.userUpdatedAt, jdbcType=BIGINT},
  80. updated_at = #{now, jdbcType=BIGINT}
  81. where
  82. uuid = #{dto.uuid, jdbcType=VARCHAR}
  83. </update>
  84. <update id="updateLastUsedDate" parameterType="map">
  85. update org_qprofiles
  86. set
  87. last_used = #{lastUsedDate, jdbcType=BIGINT},
  88. updated_at = #{now, jdbcType=BIGINT}
  89. where
  90. uuid = #{uuid, jdbcType=VARCHAR}
  91. and (last_used is null or last_used &lt; #{lastUsedDate, jdbcType=BIGINT})
  92. </update>
  93. <delete id="deleteRuleProfilesByUuids" parameterType="String">
  94. delete from rules_profiles
  95. where uuid in
  96. <foreach collection="uuids" open="(" close=")" item="uuid" separator=",">#{uuid, jdbcType=VARCHAR}</foreach>
  97. </delete>
  98. <delete id="deleteOrgQProfilesByUuids" parameterType="String">
  99. delete from org_qprofiles
  100. where uuid in
  101. <foreach collection="uuids" open="(" close=")" item="uuid" separator=",">#{uuid, jdbcType=VARCHAR}</foreach>
  102. </delete>
  103. <select id="selectBuiltInRuleProfiles" resultType="org.sonar.db.qualityprofile.RulesProfileDto">
  104. select <include refid="ruleProfileColumns"/>
  105. from rules_profiles rp
  106. where rp.is_built_in = ${_true}
  107. </select>
  108. <select id="selectRuleProfile" resultType="org.sonar.db.qualityprofile.RulesProfileDto">
  109. select <include refid="ruleProfileColumns"/>
  110. from rules_profiles rp
  111. where rp.uuid = #{uuid, jdbcType=VARCHAR}
  112. </select>
  113. <select id="selectOrderedByOrganizationUuid" parameterType="map" resultType="org.sonar.db.qualityprofile.QProfileDto">
  114. select
  115. <include refid="qProfileColumns"/>
  116. from org_qprofiles oqp
  117. inner join rules_profiles rp on oqp.rules_profile_uuid = rp.uuid
  118. where
  119. oqp.organization_uuid = #{organizationUuid,jdbcType=VARCHAR}
  120. order by rp.name, rp.language
  121. </select>
  122. <select id="selectDefaultProfile" parameterType="map" resultType="org.sonar.db.qualityprofile.QProfileDto">
  123. select
  124. <include refid="qProfileColumns"/>
  125. from org_qprofiles oqp
  126. inner join rules_profiles rp on oqp.rules_profile_uuid = rp.uuid
  127. inner join default_qprofiles dp on dp.qprofile_uuid = oqp.uuid
  128. where
  129. dp.language = #{language, jdbcType=VARCHAR}
  130. and dp.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
  131. and rp.language = dp.language
  132. and oqp.organization_uuid = dp.organization_uuid
  133. </select>
  134. <select id="selectDefaultBuiltInProfilesWithoutActiveRules" parameterType="map" resultType="org.sonar.db.qualityprofile.QProfileDto">
  135. SELECT
  136. <include refid="qProfileColumns"/>
  137. FROM org_qprofiles oqp
  138. INNER JOIN rules_profiles rp ON oqp.rules_profile_uuid = rp.uuid
  139. INNER JOIN default_qprofiles dp ON dp.qprofile_uuid = oqp.uuid
  140. WHERE
  141. rp.is_built_in = ${_true}
  142. AND rp.language IN <foreach collection="languages" open="(" close=")" item="language" separator=",">#{language, jdbcType=VARCHAR}</foreach>
  143. AND NOT EXISTS (
  144. SELECT 1 FROM active_rules ar
  145. INNER JOIN rules r ON r.uuid = ar.rule_uuid AND r.status &lt;&gt; 'REMOVED'
  146. WHERE profile_uuid = rp.uuid
  147. )
  148. </select>
  149. <select id="selectDefaultProfiles" parameterType="map" resultType="org.sonar.db.qualityprofile.QProfileDto">
  150. select
  151. <include refid="qProfileColumns"/>
  152. from org_qprofiles oqp
  153. inner join rules_profiles rp on oqp.rules_profile_uuid = rp.uuid
  154. inner join default_qprofiles dp on dp.qprofile_uuid = oqp.uuid
  155. where
  156. dp.language in <foreach collection="languages" open="(" close=")" item="language" separator=",">#{language, jdbcType=VARCHAR}</foreach>
  157. and dp.organization_uuid = oqp.organization_uuid
  158. and rp.language = dp.language
  159. and oqp.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
  160. </select>
  161. <select id="selectBuiltInRuleProfilesWithActiveRules" resultType="org.sonar.db.qualityprofile.RulesProfileDto">
  162. select <include refid="ruleProfileColumns"/>
  163. from rules_profiles rp
  164. where rp.is_built_in = ${_true}
  165. AND EXISTS (
  166. SELECT 1 FROM active_rules ar
  167. INNER JOIN rules r ON r.uuid = ar.rule_uuid AND r.status &lt;&gt; 'REMOVED'
  168. WHERE profile_uuid=rp.uuid
  169. )
  170. </select>
  171. <select id="selectByNameAndLanguage" parameterType="map" resultType="org.sonar.db.qualityprofile.QProfileDto">
  172. select
  173. <include refid="qProfileColumns"/>
  174. from org_qprofiles oqp
  175. inner join rules_profiles rp on oqp.rules_profile_uuid = rp.uuid
  176. where
  177. rp.name = #{name, jdbcType=VARCHAR}
  178. and rp.language = #{language, jdbcType=VARCHAR}
  179. and oqp.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
  180. </select>
  181. <select id="selectByRuleProfileUuid" parameterType="map" resultType="org.sonar.db.qualityprofile.QProfileDto">
  182. select
  183. <include refid="qProfileColumns"/>
  184. from org_qprofiles oqp
  185. inner join rules_profiles rp on oqp.rules_profile_uuid = rp.uuid
  186. where
  187. rp.uuid = #{ruleProfileUuid, jdbcType=VARCHAR}
  188. and oqp.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
  189. </select>
  190. <select id="selectByNameAndLanguages" parameterType="map" resultType="org.sonar.db.qualityprofile.QProfileDto">
  191. select
  192. <include refid="qProfileColumns"/>
  193. from org_qprofiles oqp
  194. inner join rules_profiles rp on oqp.rules_profile_uuid = rp.uuid
  195. where
  196. rp.name = #{name, jdbcType=VARCHAR}
  197. and rp.language in <foreach collection="languages" open="(" close=")" item="language" separator=",">#{language, jdbcType=VARCHAR}</foreach>
  198. and oqp.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
  199. </select>
  200. <select id="selectByUuid" parameterType="string" resultType="org.sonar.db.qualityprofile.QProfileDto">
  201. select
  202. <include refid="qProfileColumns"/>
  203. from org_qprofiles oqp
  204. inner join rules_profiles rp on oqp.rules_profile_uuid = rp.uuid
  205. where
  206. oqp.uuid = #{uuid, jdbcType=VARCHAR}
  207. </select>
  208. <select id="selectByUuids" parameterType="map" resultType="org.sonar.db.qualityprofile.QProfileDto">
  209. select
  210. <include refid="qProfileColumns"/>
  211. from org_qprofiles oqp
  212. inner join rules_profiles rp on oqp.rules_profile_uuid = rp.uuid
  213. where
  214. oqp.uuid in <foreach collection="uuids" open="(" close=")" item="uuid" separator=",">#{uuid, jdbcType=VARCHAR}</foreach>
  215. </select>
  216. <select id="selectByLanguage" parameterType="String" resultType="org.sonar.db.qualityprofile.QProfileDto">
  217. select
  218. <include refid="qProfileColumns"/>
  219. from org_qprofiles oqp
  220. inner join rules_profiles rp on oqp.rules_profile_uuid = rp.uuid
  221. where
  222. rp.language = #{language, jdbcType=VARCHAR}
  223. and oqp.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
  224. </select>
  225. <!-- the join on "org_qprofiles parent" is required to benefit from the index on uuid -->
  226. <select id="selectChildren" parameterType="string" resultType="org.sonar.db.qualityprofile.QProfileDto">
  227. select <include refid="qProfileColumns"/>
  228. from org_qprofiles oqp
  229. inner join rules_profiles rp on oqp.rules_profile_uuid = rp.uuid
  230. inner join org_qprofiles parent on parent.uuid = oqp.parent_uuid
  231. where
  232. parent.uuid in <foreach collection="uuids" item="uuid" open="(" close=")" separator=",">#{uuid, jdbcType=VARCHAR}</foreach>
  233. order by rp.name
  234. </select>
  235. <select id="countProjectsByOrganizationAndProfiles" resultType="KeyLongValue" parameterType="map">
  236. select pqp.profile_key as "key", count(pj.uuid) as "value"
  237. from components pj
  238. inner join project_qprofiles pqp on pqp.project_uuid = pj.uuid
  239. inner join org_qprofiles oqp on oqp.uuid = pqp.profile_key
  240. where
  241. pj.enabled = ${_true}
  242. and pj.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
  243. and oqp.organization_uuid = pj.organization_uuid
  244. and <foreach collection="profileUuids" item="profileUuid" open="(" separator=" or " close=")">
  245. oqp.uuid = #{profileUuid, jdbcType=VARCHAR}
  246. </foreach>
  247. group by pqp.profile_key
  248. </select>
  249. <select id="selectAssociatedToProjectUuidAndLanguage" parameterType="map" resultType="org.sonar.db.qualityprofile.QProfileDto">
  250. select
  251. <include refid="qProfileColumns"/>
  252. from org_qprofiles oqp
  253. inner join rules_profiles rp on oqp.rules_profile_uuid = rp.uuid
  254. inner join project_qprofiles pqp ON pqp.profile_key = oqp.uuid
  255. where
  256. rp.language = #{language, jdbcType=VARCHAR}
  257. and pqp.project_uuid = #{projectUuid, jdbcType=VARCHAR}
  258. and oqp.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
  259. </select>
  260. <select id="selectAssociatedToProjectUuidAndLanguages" parameterType="map" resultType="org.sonar.db.qualityprofile.QProfileDto">
  261. select
  262. <include refid="qProfileColumns"/>
  263. from org_qprofiles oqp
  264. inner join rules_profiles rp on oqp.rules_profile_uuid = rp.uuid
  265. inner join project_qprofiles pqp ON pqp.profile_key = oqp.uuid
  266. where
  267. rp.language in <foreach collection="languages" open="(" close=")" item="language" separator=",">#{language, jdbcType=VARCHAR}</foreach>
  268. and pqp.project_uuid = #{projectUuid, jdbcType=VARCHAR}
  269. and oqp.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
  270. </select>
  271. <insert id="insertProjectProfileAssociation" useGeneratedKeys="false">
  272. insert into project_qprofiles (
  273. uuid,
  274. project_uuid,
  275. profile_key
  276. ) values (
  277. #{uuid, jdbcType=VARCHAR},
  278. #{projectUuid, jdbcType=VARCHAR},
  279. #{profileUuid, jdbcType=VARCHAR}
  280. )
  281. </insert>
  282. <update id="updateProjectProfileAssociation">
  283. update project_qprofiles
  284. set
  285. profile_key = #{profileUuid, jdbcType=VARCHAR}
  286. where
  287. project_uuid = #{projectUuid, jdbcType=VARCHAR}
  288. and profile_key = #{oldProfileUuid, jdbcType=VARCHAR}
  289. </update>
  290. <delete id="deleteProjectProfileAssociation">
  291. delete from project_qprofiles
  292. where
  293. project_uuid = #{projectUuid, jdbcType=VARCHAR}
  294. and profile_key=#{profileUuid, jdbcType=VARCHAR}
  295. </delete>
  296. <delete id="deleteProjectAssociationByProfileUuids" parameterType="String">
  297. delete from project_qprofiles
  298. where profile_key in
  299. <foreach collection="profileUuids" open="(" close=")" item="profileUuid" separator=",">
  300. #{profileUuid, jdbcType=VARCHAR}
  301. </foreach>
  302. </delete>
  303. <select id="selectSelectedProjects" resultType="org.sonar.db.qualityprofile.ProjectQprofileAssociationDto">
  304. select
  305. pj.uuid as projectUuid,
  306. pj.kee as projectKey,
  307. pj.name as projectName,
  308. pp.profile_key as profileKey
  309. from components pj
  310. inner join project_qprofiles pp ON pp.project_uuid = pj.uuid and pp.profile_key = #{profileUuid, jdbcType=VARCHAR}
  311. where
  312. pj.scope = 'PRJ'
  313. and pj.qualifier = 'TRK'
  314. and pj.main_branch_project_uuid is null
  315. and upper(pj.name) like #{nameQuery, jdbcType=VARCHAR}
  316. and pj.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
  317. order by pj.name ASC
  318. </select>
  319. <select id="selectDeselectedProjects" resultType="org.sonar.db.qualityprofile.ProjectQprofileAssociationDto">
  320. SELECT pj.uuid as projectUuid, pj.kee as projectKey, pj.name as projectName, pp.profile_key as profileKey
  321. FROM components pj
  322. LEFT JOIN project_qprofiles pp ON pp.project_uuid = pj.uuid
  323. AND pp.profile_key = #{profileUuid, jdbcType=VARCHAR}
  324. WHERE pj.scope='PRJ' AND pj.qualifier='TRK' AND pj.main_branch_project_uuid is null
  325. AND UPPER(pj.name) LIKE #{nameQuery, jdbcType=VARCHAR}
  326. AND pp.profile_key IS NULL
  327. AND pj.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
  328. ORDER BY pj.name ASC
  329. </select>
  330. <select id="selectProjectAssociations" resultType="org.sonar.db.qualityprofile.ProjectQprofileAssociationDto">
  331. SELECT pj.uuid as projectUuid, pj.kee as projectKey, pj.name as projectName, pp.profile_key as profileKey
  332. FROM components pj
  333. LEFT JOIN project_qprofiles pp ON pp.project_uuid = pj.uuid
  334. AND pp.profile_key = #{profileUuid, jdbcType=VARCHAR}
  335. WHERE pj.scope='PRJ' AND pj.qualifier='TRK' AND pj.main_branch_project_uuid is null
  336. AND UPPER(pj.name) LIKE #{nameQuery, jdbcType=VARCHAR}
  337. AND pj.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
  338. ORDER BY pj.name ASC
  339. </select>
  340. <select id="selectUuidsOfCustomRuleProfiles" parameterType="map" resultType="string">
  341. select oqp.rules_profile_uuid
  342. from org_qprofiles oqp
  343. inner join organizations o on o.uuid = oqp.organization_uuid
  344. inner join rules_profiles rp on rp.uuid = oqp.rules_profile_uuid
  345. where
  346. rp.language = #{language, jdbcType=VARCHAR}
  347. and rp.name = #{name, jdbcType=VARCHAR}
  348. and rp.is_built_in = ${_false}
  349. </select>
  350. <update id="renameRuleProfiles" parameterType="map">
  351. update rules_profiles
  352. set
  353. name = #{newName, jdbcType=VARCHAR},
  354. updated_at = #{updatedAt, jdbcType=TIMESTAMP}
  355. where
  356. uuid in <foreach collection="uuids" open="(" close=")" item="uuid" separator=",">#{uuid, jdbcType=VARCHAR}</foreach>
  357. </update>
  358. <select id="selectQProfilesByRuleProfileUuid" parameterType="string" resultType="org.sonar.db.qualityprofile.QProfileDto">
  359. select
  360. <include refid="qProfileColumns"/>
  361. from org_qprofiles oqp
  362. inner join rules_profiles rp on oqp.rules_profile_uuid = rp.uuid
  363. where
  364. rp.uuid= #{rulesProfileUuid, jdbcType=VARCHAR}
  365. </select>
  366. </mapper>