diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2023-05-24 12:32:00 -0500 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-06-01 20:02:59 +0000 |
commit | 7b73b663030d67ddf6cf01914379f1c5a671bdf6 (patch) | |
tree | ed7872e6207c808a1b3ee928ff386d19e715b24a /server/sonar-db-dao/src/main | |
parent | 92c132a13f46f23a5ecc5dee5dfdfb619e6fa65f (diff) | |
download | sonarqube-7b73b663030d67ddf6cf01914379f1c5a671bdf6.tar.gz sonarqube-7b73b663030d67ddf6cf01914379f1c5a671bdf6.zip |
SONAR-18856 Move Entity queries to dedicated DAO
Diffstat (limited to 'server/sonar-db-dao/src/main')
10 files changed, 197 insertions, 119 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java b/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java index a58e3b8d522..44a996e4b41 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java @@ -39,6 +39,7 @@ import org.sonar.db.component.ComponentKeyUpdaterDao; import org.sonar.db.component.ProjectLinkDao; import org.sonar.db.component.SnapshotDao; import org.sonar.db.duplication.DuplicationDao; +import org.sonar.db.entity.EntityDao; import org.sonar.db.es.EsQueueDao; import org.sonar.db.event.EventComponentChangeDao; import org.sonar.db.event.EventDao; @@ -118,6 +119,7 @@ public class DaoModule extends Module { ComponentKeyUpdaterDao.class, DefaultQProfileDao.class, DuplicationDao.class, + EntityDao.class, EsQueueDao.class, EventDao.class, EventComponentChangeDao.class, diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java b/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java index 4179b7dc7e1..80add385af8 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java @@ -39,6 +39,7 @@ import org.sonar.db.component.ComponentKeyUpdaterDao; import org.sonar.db.component.ProjectLinkDao; import org.sonar.db.component.SnapshotDao; import org.sonar.db.duplication.DuplicationDao; +import org.sonar.db.entity.EntityDao; import org.sonar.db.es.EsQueueDao; import org.sonar.db.event.EventComponentChangeDao; import org.sonar.db.event.EventDao; @@ -178,6 +179,7 @@ public class DbClient { private final ScannerAnalysisCacheDao scannerAnalysisCacheDao; private final ScimUserDao scimUserDao; private final ScimGroupDao scimGroupDao; + private final EntityDao entityDao; public DbClient(Database database, MyBatis myBatis, DBSessions dbSessions, Dao... daos) { this.database = database; @@ -263,6 +265,7 @@ public class DbClient { scannerAnalysisCacheDao = getDao(map, ScannerAnalysisCacheDao.class); scimUserDao = getDao(map, ScimUserDao.class); scimGroupDao = getDao(map, ScimGroupDao.class); + entityDao = getDao(map, EntityDao.class); } public DbSession openSession(boolean batch) { @@ -582,5 +585,9 @@ public class DbClient { public ScimGroupDao scimGroupDao() { return scimGroupDao; } + + public EntityDao entityDao() { + return entityDao; + } } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java index a36feeb394e..f2aeefdcc27 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java @@ -65,6 +65,7 @@ import org.sonar.db.component.ViewsSnapshotDto; import org.sonar.db.duplication.DuplicationMapper; import org.sonar.db.duplication.DuplicationUnitDto; import org.sonar.db.entity.EntityDto; +import org.sonar.db.entity.EntityMapper; import org.sonar.db.es.EsQueueMapper; import org.sonar.db.event.EventComponentChangeMapper; import org.sonar.db.event.EventDto; @@ -271,6 +272,7 @@ public class MyBatis { LiveMeasureMapper.class, DefaultQProfileMapper.class, DuplicationMapper.class, + EntityMapper.class, EsQueueMapper.class, EventMapper.class, EventComponentChangeMapper.class, diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/entity/EntityDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/entity/EntityDao.java new file mode 100644 index 00000000000..f0268ec9de5 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/entity/EntityDao.java @@ -0,0 +1,65 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.entity; + +import java.util.Collection; +import java.util.List; +import java.util.Optional; +import javax.annotation.Nullable; +import org.apache.ibatis.session.ResultHandler; +import org.sonar.db.Dao; +import org.sonar.db.DbSession; + +import static java.util.Collections.emptyList; +import static org.sonar.db.DatabaseUtils.executeLargeInputs; + +public class EntityDao implements Dao { + public Optional<EntityDto> selectByUuid(DbSession dbSession, String uuid) { + return Optional.ofNullable(mapper(dbSession).selectByUuid(uuid)); + } + + public List<EntityDto> selectByUuids(DbSession dbSession, Collection<String> uuids) { + if (uuids.isEmpty()) { + return emptyList(); + } + + return executeLargeInputs(uuids, partition -> mapper(dbSession).selectByUuids(partition)); + } + + public Optional<EntityDto> selectByKey(DbSession dbSession, String key) { + return Optional.ofNullable(mapper(dbSession).selectByKey(key)); + } + + public List<EntityDto> selectByKeys(DbSession dbSession, Collection<String> keys) { + if (keys.isEmpty()) { + return emptyList(); + } + return executeLargeInputs(keys, partition -> mapper(dbSession).selectByKeys(partition)); + } + + public void scrollForIndexing(DbSession session, @Nullable String entityUuid, ResultHandler<EntityDto> handler) { + mapper(session).scrollForIndexing(entityUuid, handler); + } + + private static EntityMapper mapper(DbSession session) { + return session.getMapper(EntityMapper.class); + } + +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/entity/EntityDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/entity/EntityDto.java index 03e0b564dc3..aa031c0787a 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/entity/EntityDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/entity/EntityDto.java @@ -20,6 +20,7 @@ package org.sonar.db.entity; import java.util.Objects; +import javax.annotation.CheckForNull; /** * Represents a project, an application, a portfolio or a sub-portfolio. diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/entity/EntityMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/entity/EntityMapper.java new file mode 100644 index 00000000000..1ca0041731a --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/entity/EntityMapper.java @@ -0,0 +1,41 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.entity; + +import java.util.Collection; +import java.util.List; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.session.ResultHandler; + +public interface EntityMapper { + @CheckForNull + EntityDto selectByUuid(String uuid); + + List<EntityDto> selectByUuids(@Param("uuids") Collection<String> uuids); + + @CheckForNull + EntityDto selectByKey(String key); + + List<EntityDto> selectByKeys(@Param("keys") Collection<String> keys); + + void scrollForIndexing(@Param("entityUuid") @Nullable String entityUuid, ResultHandler<EntityDto> handler); +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java index 1d354a8487a..44ef0ff0e8b 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectDao.java @@ -19,18 +19,15 @@ */ package org.sonar.db.project; -import java.util.Collection; import java.util.List; import java.util.Optional; import java.util.Set; import javax.annotation.Nullable; -import org.apache.ibatis.session.ResultHandler; import org.sonar.api.utils.System2; import org.sonar.db.Dao; import org.sonar.db.DbSession; import org.sonar.db.audit.AuditPersister; import org.sonar.db.audit.model.ComponentNewValue; -import org.sonar.db.entity.EntityDto; import static java.util.Collections.emptyList; import static org.sonar.db.DatabaseUtils.executeLargeInputs; @@ -146,31 +143,4 @@ public class ProjectDao implements Dao { public long getNclocSum(DbSession dbSession, @Nullable String projectUuidToExclude) { return Optional.ofNullable(mapper(dbSession).getNclocSum(projectUuidToExclude)).orElse(0L); } - - public Optional<EntityDto> selectEntityByUuid(DbSession dbSession, String uuid) { - return Optional.ofNullable(mapper(dbSession).selectEntityByUuid(uuid)); - } - - public List<EntityDto> selectEntitiesByUuids(DbSession dbSession, Collection<String> uuids) { - if (uuids.isEmpty()) { - return emptyList(); - } - - return executeLargeInputs(uuids, partition -> mapper(dbSession).selectEntitiesByUuids(partition)); - } - - public Optional<EntityDto> selectEntityByKey(DbSession dbSession, String key) { - return Optional.ofNullable(mapper(dbSession).selectEntityByKey(key)); - } - - public List<EntityDto> selectEntitiesByKeys(DbSession dbSession, Collection<String> keys) { - if (keys.isEmpty()) { - return emptyList(); - } - return executeLargeInputs(keys, partition -> mapper(dbSession).selectEntitiesByKeys(partition)); - } - - public void scrollEntitiesForIndexing(DbSession session, @Nullable String entityUuid, ResultHandler<EntityDto> handler) { - mapper(session).scrollEntitiesForIndexing(entityUuid, handler); - } } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectMapper.java index 6ffcf920733..c4ddb661141 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectMapper.java @@ -73,16 +73,4 @@ public interface ProjectMapper { @CheckForNull Long getNclocSum(@Nullable @Param("projectUuidToExclude") String projectUuidToExclude); - - @CheckForNull - EntityDto selectEntityByUuid(String uuid); - - List<EntityDto> selectEntitiesByUuids(@Param("uuids") Collection<String> uuids); - - @CheckForNull - EntityDto selectEntityByKey(String key); - - List<EntityDto> selectEntitiesByKeys(@Param("keys") Collection<String> keys); - - void scrollEntitiesForIndexing(@Param("entityUuid") @Nullable String entityUuid, ResultHandler<EntityDto> handler); } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/entity/EntityMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/entity/EntityMapper.xml new file mode 100644 index 00000000000..794d65709ff --- /dev/null +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/entity/EntityMapper.xml @@ -0,0 +1,79 @@ +<?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.entity.EntityMapper"> + <sql id="entityProjectColumns"> + p.uuid as uuid, p.kee as kee, p.name as name, p.private as isPrivate, p.qualifier as qualifier, null as authUuid + </sql> + + <sql id="entityPortfolioColumns"> + p.uuid as uuid, p.kee as kee, p.name as name, p.private as isPrivate, + case when p.parent_uuid is null then 'VW' else 'SVW' end as qualifier, + case when p.root_uuid != p.uuid then p.root_uuid else null end as authUuid + </sql> + + <select id="selectByUuid" parameterType="string" resultType="Entity"> + (select <include refid="entityProjectColumns"/> + from projects p + where p.uuid = #{uuid,jdbcType=VARCHAR}) + UNION + (select <include refid="entityPortfolioColumns"/> + from portfolios p + where p.uuid = #{uuid,jdbcType=VARCHAR}) + </select> + + <select id="selectByUuids" resultType="Entity"> + (select <include refid="entityProjectColumns"/> + from projects p + where p.uuid in + <foreach collection="uuids" open="(" close=")" item="uuid" separator=","> + #{uuid,jdbcType=VARCHAR} + </foreach>) + UNION + (select <include refid="entityPortfolioColumns"/> + from portfolios p + where p.uuid in + <foreach collection="uuids" open="(" close=")" item="uuid" separator=","> + #{uuid,jdbcType=VARCHAR} + </foreach>) + </select> + + <select id="selectByKey" parameterType="string" resultType="Entity"> + (select <include refid="entityProjectColumns"/> + from projects p + where p.kee = #{key,jdbcType=VARCHAR}) + UNION + (select <include refid="entityPortfolioColumns"/> + from portfolios p + where p.kee = #{key,jdbcType=VARCHAR}) + </select> + + <select id="selectByKeys" parameterType="string" resultType="Entity"> + (select <include refid="entityProjectColumns"/> + from projects p + where p.kee in + <foreach collection="keys" open="(" close=")" item="kee" separator=","> + #{kee,jdbcType=VARCHAR} + </foreach>) + UNION + (select <include refid="entityPortfolioColumns"/> + from portfolios p + where p.kee in + <foreach collection="keys" open="(" close=")" item="kee" separator=","> + #{kee,jdbcType=VARCHAR} + </foreach>) + </select> + + <select id="scrollForIndexing" parameterType="map" resultType="Entity" fetchSize="${_scrollFetchSize}" resultSetType="FORWARD_ONLY"> + (select <include refid="entityProjectColumns"/> + from projects p + <if test="entityUuid != null"> + where p.uuid = #{entityUuid,jdbcType=VARCHAR} + </if>) + UNION + (select <include refid="entityPortfolioColumns"/> + from portfolios p + <if test="entityUuid != null"> + where p.uuid = #{entityUuid,jdbcType=VARCHAR} + </if>) + </select> +</mapper>
\ No newline at end of file diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml index e623ae82be7..36bb101f9ae 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/project/ProjectMapper.xml @@ -201,81 +201,4 @@ and uuid <> #{projectUuidToExclude,jdbcType=VARCHAR} </if> </select> - - <sql id="entityProjectColumns"> - p.uuid as uuid, p.kee as kee, p.name as name, p.private as isPrivate, p.qualifier as qualifier, null as authUuid - </sql> - - <sql id="entityPortfolioColumns"> - p.uuid as uuid, p.kee as kee, p.name as name, p.private as isPrivate, - case when p.parent_uuid is null then 'VW' else 'SVW' end as qualifier, - case when p.root_uuid != p.uuid then p.root_uuid else null end as authUuid - </sql> - - <select id="selectEntityByUuid" parameterType="string" resultType="Entity"> - (select <include refid="entityProjectColumns"/> - from projects p - where p.uuid = #{uuid,jdbcType=VARCHAR}) - UNION - (select <include refid="entityPortfolioColumns"/> - from portfolios p - where p.uuid = #{uuid,jdbcType=VARCHAR}) - </select> - - <select id="selectEntitiesByUuids" resultType="Entity"> - (select <include refid="entityProjectColumns"/> - from projects p - where p.uuid in - <foreach collection="uuids" open="(" close=")" item="uuid" separator=","> - #{uuid,jdbcType=VARCHAR} - </foreach>) - UNION - (select <include refid="entityPortfolioColumns"/> - from portfolios p - where p.uuid in - <foreach collection="uuids" open="(" close=")" item="uuid" separator=","> - #{uuid,jdbcType=VARCHAR} - </foreach>) - </select> - - <select id="selectEntityByKey" parameterType="string" resultType="Entity"> - (select <include refid="entityProjectColumns"/> - from projects p - where p.kee = #{key,jdbcType=VARCHAR}) - UNION - (select <include refid="entityPortfolioColumns"/> - from portfolios p - where p.kee = #{key,jdbcType=VARCHAR}) - </select> - - <select id="selectEntitiesByKeys" parameterType="string" resultType="Entity"> - (select <include refid="entityProjectColumns"/> - from projects p - where p.kee in - <foreach collection="keys" open="(" close=")" item="kee" separator=","> - #{kee,jdbcType=VARCHAR} - </foreach>) - UNION - (select <include refid="entityPortfolioColumns"/> - from portfolios p - where p.kee in - <foreach collection="keys" open="(" close=")" item="kee" separator=","> - #{kee,jdbcType=VARCHAR} - </foreach>) - </select> - - <select id="scrollEntitiesForIndexing" parameterType="map" resultType="Entity" fetchSize="${_scrollFetchSize}" resultSetType="FORWARD_ONLY"> - (select <include refid="entityProjectColumns"/> - from projects p - <if test="entityUuid != null"> - where p.uuid = #{entityUuid,jdbcType=VARCHAR} - </if>) - UNION - (select <include refid="entityPortfolioColumns"/> - from portfolios p - <if test="entityUuid != null"> - where p.uuid = #{entityUuid,jdbcType=VARCHAR} - </if>) - </select> - </mapper> |