]> source.dussan.org Git - sonarqube.git/commitdiff
Add ComponentDao#insertBatch() for batch inserts
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 15 Apr 2016 20:05:22 +0000 (22:05 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 15 Apr 2016 20:05:22 +0000 (22:05 +0200)
sonar-db/src/main/java/org/sonar/db/component/ComponentDao.java
sonar-db/src/main/java/org/sonar/db/component/ComponentMapper.java
sonar-db/src/main/resources/org/sonar/db/component/ComponentMapper.xml
sonar-db/src/test/java/org/sonar/db/component/ComponentDaoTest.java

index ebd4b6e6dade6f39aa0c442ca4b8ba70a7a752be..d58b2c12f0b164cee474976bf4b085871432462d 100644 (file)
@@ -278,6 +278,10 @@ public class ComponentDao implements Dao {
     mapper(session).insert(item);
   }
 
+  public void insertBatch(DbSession session, ComponentDto item) {
+    mapper(session).insertBatch(item);
+  }
+
   public void insert(DbSession session, Collection<ComponentDto> items) {
     for (ComponentDto item : items) {
       insert(session, item);
index ac40231765ea5f35f21d750cffba4f2b3be193d1..bc5e291c5c788a294e2df4b307239faa713f78bc 100644 (file)
@@ -144,6 +144,8 @@ public interface ComponentMapper {
 
   void insert(ComponentDto componentDto);
 
+  void insertBatch(ComponentDto componentDto);
+
   void update(ComponentDto componentDto);
 
   void delete(long componentId);
index d310528ad8abb9291e249095812689b871e75563..91641a7a37902e8eab2985b47408ceee317a7c87 100644 (file)
     </if>
   </sql>
 
-  <insert id="insert" parameterType="Component" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
+  <sql id="insertSql">
     INSERT INTO projects (kee, deprecated_kee, uuid, project_uuid, module_uuid, module_uuid_path, name, long_name,
     qualifier, scope, language, description, root_id, path, copy_resource_id, person_id, enabled,
     created_at, authorization_updated_at)
     #{developerId,jdbcType=BIGINT},
     #{enabled,jdbcType=BOOLEAN},
     #{createdAt,jdbcType=TIMESTAMP}, #{authorizationUpdatedAt,jdbcType=BIGINT})
+  </sql>
+
+  <insert id="insert" parameterType="Component" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
+    <include refid="insertSql"/>
+  </insert>
+
+  <insert id="insertBatch" parameterType="Component" useGeneratedKeys="false">
+    <include refid="insertSql"/>
   </insert>
 
   <update id="update" parameterType="Component" useGeneratedKeys="false">
index 8a4de8f8cdeb5f360e9da642696bd7accdaf8363..8af397c8ae25280ecf8e9c444a2bf33fda265f33 100644 (file)
@@ -34,7 +34,6 @@ import org.sonar.db.RowNotFoundException;
 
 import static com.google.common.collect.Lists.newArrayList;
 import static com.google.common.collect.Sets.newHashSet;
-import static java.util.Collections.singleton;
 import static java.util.Collections.singletonList;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.guava.api.Assertions.assertThat;
@@ -556,7 +555,6 @@ public class ComponentDaoTest {
     db.prepareDbUnit(getClass(), "empty.xml");
 
     ComponentDto componentDto = new ComponentDto()
-      .setId(1L)
       .setUuid("GHIJ")
       .setProjectUuid("ABCD")
       .setModuleUuid("EFGH")
@@ -584,6 +582,40 @@ public class ComponentDaoTest {
     db.assertDbUnit(getClass(), "insert-result.xml", "projects");
   }
 
+  @Test
+  public void insertBatch() {
+    try (DbSession batchSession = db.myBatis().openSession(true)) {
+      db.prepareDbUnit(getClass(), "empty.xml");
+
+      ComponentDto componentDto = new ComponentDto()
+        .setUuid("GHIJ")
+        .setProjectUuid("ABCD")
+        .setModuleUuid("EFGH")
+        .setModuleUuidPath(".ABCD.EFGH.")
+        .setKey("org.struts:struts-core:src/org/struts/RequestContext.java")
+        .setDeprecatedKey("org.struts:struts-core:src/org/struts/RequestContext.java")
+        .setName("RequestContext.java")
+        .setLongName("org.struts.RequestContext")
+        .setQualifier("FIL")
+        .setScope("FIL")
+        .setLanguage("java")
+        .setDescription("description")
+        .setPath("src/org/struts/RequestContext.java")
+        .setParentProjectId(3L)
+        .setCopyResourceId(5L)
+        .setDeveloperId(7L)
+        .setEnabled(true)
+        .setCreatedAt(DateUtils.parseDate("2014-06-18"))
+        .setAuthorizationUpdatedAt(123456789L);
+
+      underTest.insertBatch(batchSession, componentDto);
+      batchSession.commit();
+
+      assertThat(componentDto.getId()).isNull();
+      db.assertDbUnit(getClass(), "insert-result.xml", "projects");
+    }
+  }
+
   @Test
   public void insert_disabled_component() {
     db.prepareDbUnit(getClass(), "empty.xml");