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.

OrganizationMapper.xml 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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.organization.OrganizationMapper">
  4. <sql id="selectColumns">
  5. org.uuid as "uuid",
  6. org.kee as "key",
  7. org.name as "name",
  8. org.description as "description",
  9. org.default_quality_gate_uuid as "defaultQualityGateUuid",
  10. org.url as "url",
  11. org.avatar_url as "avatarUrl",
  12. org.subscription as "subscription",
  13. org.created_at as "createdAt",
  14. org.updated_at as "updatedAt"
  15. </sql>
  16. <sql id="defaultTemplatesColumns">
  17. org.default_perm_template_project as "projectUuid",
  18. org.default_perm_template_app as "applicationsUuid",
  19. org.default_perm_template_port as "portfoliosUuid"
  20. </sql>
  21. <select id="selectByUuid" resultType="Organization">
  22. select
  23. <include refid="selectColumns"/>
  24. from organizations org
  25. where
  26. org.uuid = #{uuid, jdbcType=VARCHAR}
  27. </select>
  28. <select id="selectDefaultTemplatesByUuid" resultType="org.sonar.db.permission.template.DefaultTemplates">
  29. select
  30. <include refid="defaultTemplatesColumns"/>
  31. from organizations org
  32. where
  33. org.uuid = #{uuid, jdbcType=VARCHAR}
  34. and org.default_perm_template_project is not null
  35. </select>
  36. <select id="selectByKey" resultType="Organization">
  37. select
  38. <include refid="selectColumns"/>
  39. from organizations org
  40. where
  41. org.kee = #{key, jdbcType=VARCHAR}
  42. </select>
  43. <select id="selectByUuids" resultType="Organization">
  44. select
  45. <include refid="selectColumns"/>
  46. from organizations org
  47. where
  48. org.uuid in
  49. <foreach collection="uuids" open="(" close=")" item="uuid" separator=",">
  50. #{uuid, jdbcType=VARCHAR}
  51. </foreach>
  52. </select>
  53. <select id="countByQuery" resultType="int">
  54. select count(org.uuid)
  55. <include refid="sqlSelectByQuery" />
  56. </select>
  57. <select id="selectByQuery" parameterType="map" resultType="Organization">
  58. select
  59. <include refid="selectColumns"/>
  60. <include refid="sqlSelectByQuery" />
  61. order by
  62. org.created_at desc
  63. limit #{pagination.pageSize,jdbcType=INTEGER} offset #{pagination.offset,jdbcType=INTEGER}
  64. </select>
  65. <select id="selectByQuery" parameterType="map" resultType="Organization" databaseId="mssql">
  66. select * from (
  67. select row_number() over(order by org.created_at desc) as number,
  68. <include refid="selectColumns"/>
  69. <include refid="sqlSelectByQuery" />
  70. ) as query
  71. where
  72. query.number between #{pagination.startRowNumber,jdbcType=INTEGER} and #{pagination.endRowNumber,jdbcType=INTEGER}
  73. order by
  74. query.createdAt desc
  75. </select>
  76. <select id="selectByQuery" parameterType="map" resultType="Organization" databaseId="oracle">
  77. select * from (
  78. select rownum as rn, t.* from (
  79. select
  80. <include refid="selectColumns"/>
  81. <include refid="sqlSelectByQuery" />
  82. ORDER BY org.created_at desc
  83. ) t
  84. ) t
  85. where
  86. t.rn between #{pagination.startRowNumber,jdbcType=INTEGER} and #{pagination.endRowNumber,jdbcType=INTEGER}
  87. </select>
  88. <sql id="sqlSelectByQuery">
  89. from organizations org
  90. <where>
  91. <if test="query.keys != null">
  92. org.kee in
  93. <foreach collection="query.keys" open="(" close=")" item="key" separator=",">
  94. #{key, jdbcType=VARCHAR}
  95. </foreach>
  96. </if>
  97. <if test="query.withAnalyses">
  98. and exists(
  99. select 1
  100. from snapshots s
  101. inner join components p on p.uuid = s.component_uuid
  102. where p.organization_uuid = org.uuid
  103. and p.enabled = ${_true}
  104. and s.islast = ${_true}
  105. )
  106. </if>
  107. <if test="query.analyzedAfter != null">
  108. and exists(
  109. select 1
  110. from snapshots s
  111. inner join components p on p.uuid = s.component_uuid
  112. where p.organization_uuid = org.uuid
  113. and p.enabled = ${_true}
  114. and s.islast = ${_true}
  115. and s.created_at &gt;= #{query.analyzedAfter,jdbcType=BIGINT}
  116. )
  117. </if>
  118. </where>
  119. </sql>
  120. <select id="selectByPermission" parameterType="map" resultType="Organization">
  121. select
  122. <include refid="selectColumns"/>
  123. from organizations org
  124. inner join user_roles ur on ur.user_uuid = #{userUuid,jdbcType=VARCHAR}
  125. and ur.component_uuid is null
  126. and ur.role = #{permission,jdbcType=VARCHAR}
  127. union
  128. select
  129. <include refid="selectColumns"/>
  130. from organizations org
  131. inner join group_roles g on g.component_uuid is null
  132. and g.role = #{permission,jdbcType=VARCHAR}
  133. inner join groups_users gu on
  134. gu.group_uuid = g.group_uuid
  135. and gu.user_uuid= #{userUuid,jdbcType=VARCHAR}
  136. </select>
  137. <select id="selectAllUuids" resultType="String">
  138. select uuid from organizations
  139. </select>
  140. <select id="selectDefaultGroupUuidByUuid" resultType="string">
  141. select org.default_group_uuid
  142. from organizations org
  143. where
  144. org.uuid = #{uuid, jdbcType=VARCHAR}
  145. </select>
  146. <select id="selectNewProjectPrivateByUuid" resultType="Boolean">
  147. select org.new_project_private
  148. from organizations org
  149. where
  150. org.uuid = #{uuid, jdbcType=VARCHAR}
  151. </select>
  152. <select id="selectOrganizationsWithNcloc" resultType="org.sonar.db.organization.OrganizationWithNclocDto">
  153. select o.uuid as id, o.kee as kee, o.name as name, t.ncloc as ncloc
  154. from organizations o
  155. left outer join (
  156. select orgUuid, sum(sumncloc.maxncloc) as ncloc from (
  157. select b.project_uuid, p.organization_uuid as orgUuid, max(lm.value) as maxncloc
  158. from live_measures lm
  159. inner join metrics m on m.uuid = lm.metric_uuid
  160. inner join components p on p.uuid = lm.component_uuid
  161. inner join project_branches b on b.uuid = p.uuid
  162. where
  163. m.name = #{ncloc, jdbcType=VARCHAR}
  164. and p.enabled = ${_true}
  165. and p.private = ${_true}
  166. and p.scope = 'PRJ'
  167. and p.qualifier = 'TRK'
  168. and p.copy_component_uuid is null
  169. and p.organization_uuid in <foreach collection="organizationUuids" open="(" close=")" item="uuid" separator=",">#{uuid, jdbcType=VARCHAR}</foreach>
  170. and b.branch_type = #{branchType, jdbcType=VARCHAR}
  171. group by b.project_uuid, p.organization_uuid
  172. ) sumncloc
  173. group by orgUuid
  174. ) t on t.orgUuid = o.uuid
  175. where
  176. o.uuid in <foreach collection="organizationUuids" open="(" close=")" item="uuid" separator=",">#{uuid, jdbcType=VARCHAR}</foreach>
  177. </select>
  178. <insert id="insert" parameterType="map" useGeneratedKeys="false">
  179. insert into organizations
  180. (
  181. uuid,
  182. kee,
  183. name,
  184. description,
  185. url,
  186. avatar_url,
  187. new_project_private,
  188. default_quality_gate_uuid,
  189. subscription,
  190. created_at,
  191. updated_at
  192. )
  193. values
  194. (
  195. #{organization.uuid, jdbcType=VARCHAR},
  196. #{organization.key, jdbcType=VARCHAR},
  197. #{organization.name, jdbcType=VARCHAR},
  198. #{organization.description, jdbcType=VARCHAR},
  199. #{organization.url, jdbcType=VARCHAR},
  200. #{organization.avatarUrl, jdbcType=VARCHAR},
  201. #{newProjectPrivate, jdbcType=BOOLEAN},
  202. #{organization.defaultQualityGateUuid, jdbcType=VARCHAR},
  203. #{organization.subscription, jdbcType=VARCHAR},
  204. #{organization.createdAt, jdbcType=BIGINT},
  205. #{organization.updatedAt, jdbcType=BIGINT}
  206. )
  207. </insert>
  208. <update id="update" parameterType="Organization">
  209. update organizations
  210. set
  211. kee = #{organization.key, jdbcType=VARCHAR},
  212. name = #{organization.name, jdbcType=VARCHAR},
  213. description = #{organization.description, jdbcType=VARCHAR},
  214. url = #{organization.url, jdbcType=VARCHAR},
  215. default_quality_gate_uuid = #{organization.defaultQualityGateUuid, jdbcType=VARCHAR},
  216. subscription = #{organization.subscription, jdbcType=VARCHAR},
  217. avatar_url = #{organization.avatarUrl, jdbcType=VARCHAR},
  218. updated_at = #{organization.updatedAt, jdbcType=BIGINT}
  219. where
  220. uuid = #{organization.uuid, jdbcType=VARCHAR}
  221. </update>
  222. <update id="updateDefaultTemplates">
  223. update organizations
  224. set
  225. default_perm_template_project = #{defaultTemplates.projectUuid, jdbcType=VARCHAR},
  226. default_perm_template_app = #{defaultTemplates.applicationsUuid, jdbcType=VARCHAR},
  227. default_perm_template_port = #{defaultTemplates.portfoliosUuid, jdbcType=VARCHAR},
  228. updated_at = #{now, jdbcType=BIGINT}
  229. where
  230. uuid = #{organizationUuid, jdbcType=VARCHAR}
  231. </update>
  232. <update id="updateDefaultGroupUuid">
  233. update organizations
  234. set
  235. default_group_uuid = #{defaultGroupUuid, jdbcType=VARCHAR},
  236. updated_at = #{now, jdbcType=BIGINT}
  237. where
  238. uuid = #{organizationUuid, jdbcType=VARCHAR}
  239. </update>
  240. <update id="updateDefaultQualityGate">
  241. update organizations
  242. set
  243. default_quality_gate_uuid = #{defaultQualityGateUuid, jdbcType=INTEGER},
  244. updated_at = #{now, jdbcType=BIGINT}
  245. where
  246. uuid = #{organizationUuid, jdbcType=VARCHAR}
  247. </update>
  248. <update id="updateNewProjectPrivate">
  249. update organizations
  250. set
  251. new_project_private = #{newProjectPrivate, jdbcType=INTEGER},
  252. updated_at = #{now, jdbcType=BIGINT}
  253. where
  254. uuid = #{organizationUuid, jdbcType=VARCHAR}
  255. </update>
  256. <delete id="deleteByUuid">
  257. delete from organizations
  258. where
  259. uuid = #{uuid, jdbcType=VARCHAR}
  260. </delete>
  261. </mapper>