]> source.dussan.org Git - sonarqube.git/commitdiff
Add MyBatis stuff for table AUTHORS
authorEvgeny Mandrikov <mandrikov@gmail.com>
Tue, 27 Mar 2012 11:26:17 +0000 (17:26 +0600)
committerEvgeny Mandrikov <mandrikov@gmail.com>
Tue, 27 Mar 2012 12:15:32 +0000 (18:15 +0600)
12 files changed:
sonar-core/src/main/java/org/sonar/core/persistence/DaoUtils.java
sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java
sonar-core/src/main/java/org/sonar/core/user/AuthorDao.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/core/user/AuthorDto.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/core/user/AuthorMapper.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/core/user/package-info.java [new file with mode: 0644]
sonar-core/src/main/resources/org/sonar/core/user/AuthorMapper-oracle.xml [new file with mode: 0644]
sonar-core/src/main/resources/org/sonar/core/user/AuthorMapper.xml [new file with mode: 0644]
sonar-core/src/test/java/org/sonar/core/user/AuthorDaoTest.java [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsert-result.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsert.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldSelect.xml [new file with mode: 0644]

index a750228cc37d671beb5c581f2ccf2ab7ba634b57..5295da3660235e3ec7fc51c06fedcd7e5aea5e8c 100644 (file)
@@ -29,6 +29,7 @@ import org.sonar.core.resource.ResourceIndexerDao;
 import org.sonar.core.review.ReviewDao;
 import org.sonar.core.rule.RuleDao;
 import org.sonar.core.template.LoadedTemplateDao;
+import org.sonar.core.user.AuthorDao;
 
 import java.util.Arrays;
 import java.util.Collections;
@@ -42,6 +43,7 @@ public final class DaoUtils {
   public static List<Class<?>> getDaoClasses() {
     return Collections.unmodifiableList(Arrays.asList(
       ActiveDashboardDao.class,
+      AuthorDao.class,
       DashboardDao.class,
       DuplicationDao.class,
       LoadedTemplateDao.class,
index a82ef62da35a4e16fd1f2b5d13f6445dca9c3f85..0c2b1b754150960b3007cdd00cadbbdb64ce4497 100644 (file)
@@ -44,6 +44,8 @@ import org.sonar.core.rule.RuleDto;
 import org.sonar.core.rule.RuleMapper;
 import org.sonar.core.template.LoadedTemplateDto;
 import org.sonar.core.template.LoadedTemplateMapper;
+import org.sonar.core.user.AuthorDto;
+import org.sonar.core.user.AuthorMapper;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -67,6 +69,7 @@ public class MyBatis implements BatchComponent, ServerComponent {
     conf.getVariables().setProperty("_false", database.getDialect().getFalseSqlValue());
 
     loadAlias(conf, "ActiveDashboard", ActiveDashboardDto.class);
+    loadAlias(conf, "Author", AuthorDto.class);
     loadAlias(conf, "Dashboard", DashboardDto.class);
     loadAlias(conf, "DuplicationUnit", DuplicationUnitDto.class);
     loadAlias(conf, "LoadedTemplate", LoadedTemplateDto.class);
@@ -82,6 +85,7 @@ public class MyBatis implements BatchComponent, ServerComponent {
     loadAlias(conf, "WidgetProperty", WidgetPropertyDto.class);
 
     loadMapper(conf, ActiveDashboardMapper.class);
+    loadMapper(conf, AuthorMapper.class);
     loadMapper(conf, DashboardMapper.class);
     loadMapper(conf, DuplicationMapper.class);
     loadMapper(conf, LoadedTemplateMapper.class);
diff --git a/sonar-core/src/main/java/org/sonar/core/user/AuthorDao.java b/sonar-core/src/main/java/org/sonar/core/user/AuthorDao.java
new file mode 100644 (file)
index 0000000..574bdaa
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.core.user;
+
+import org.apache.ibatis.session.SqlSession;
+import org.sonar.api.BatchComponent;
+import org.sonar.api.ServerComponent;
+import org.sonar.core.persistence.MyBatis;
+
+/**
+ * @since 2.15
+ */
+public class AuthorDao implements BatchComponent, ServerComponent {
+
+  private final MyBatis mybatis;
+
+  public AuthorDao(MyBatis mybatis) {
+    this.mybatis = mybatis;
+  }
+
+  public AuthorDto select(String login) {
+    SqlSession session = mybatis.openSession();
+    try {
+      AuthorMapper mapper = session.getMapper(AuthorMapper.class);
+      return mapper.select(login);
+    } finally {
+      MyBatis.closeQuietly(session);
+    }
+  }
+
+  public void insert(AuthorDto authorDto) {
+    SqlSession session = mybatis.openSession();
+    try {
+      AuthorMapper mapper = session.getMapper(AuthorMapper.class);
+      mapper.insert(authorDto);
+      session.commit();
+    } finally {
+      MyBatis.closeQuietly(session);
+    }
+  }
+
+}
diff --git a/sonar-core/src/main/java/org/sonar/core/user/AuthorDto.java b/sonar-core/src/main/java/org/sonar/core/user/AuthorDto.java
new file mode 100644 (file)
index 0000000..63e22bb
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.core.user;
+
+import java.util.Date;
+
+/**
+ * @since 2.15
+ */
+public class AuthorDto {
+
+  private Integer id;
+  private Integer personId;
+  private String login;
+  private Date createdAt;
+  private Date updatedAt;
+
+  public Integer getId() {
+    return id;
+  }
+
+  public AuthorDto setId(Integer id) {
+    this.id = id;
+    return this;
+  }
+
+  public Integer getPersonId() {
+    return personId;
+  }
+
+  public AuthorDto setPersonId(Integer personId) {
+    this.personId = personId;
+    return this;
+  }
+
+  public String getLogin() {
+    return login;
+  }
+
+  public AuthorDto setLogin(String login) {
+    this.login = login;
+    return this;
+  }
+
+  public Date getCreatedAt() {
+    return createdAt;
+  }
+
+  public AuthorDto setCreatedAt(Date createdAt) {
+    this.createdAt = createdAt;
+    return this;
+  }
+
+  public Date getUpdatedAt() {
+    return updatedAt;
+  }
+
+  public AuthorDto setUpdatedAt(Date updatedAt) {
+    this.updatedAt = updatedAt;
+    return this;
+  }
+
+}
diff --git a/sonar-core/src/main/java/org/sonar/core/user/AuthorMapper.java b/sonar-core/src/main/java/org/sonar/core/user/AuthorMapper.java
new file mode 100644 (file)
index 0000000..ac3b23e
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.core.user;
+
+import org.apache.ibatis.annotations.Param;
+
+/**
+ * @since 2.15
+ */
+public interface AuthorMapper {
+
+  AuthorDto select(@Param("login") String login);
+
+  void insert(AuthorDto authorDto);
+
+}
diff --git a/sonar-core/src/main/java/org/sonar/core/user/package-info.java b/sonar-core/src/main/java/org/sonar/core/user/package-info.java
new file mode 100644 (file)
index 0000000..06cd9c9
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+
+@ParametersAreNonnullByDefault
+package org.sonar.core.user;
+
+import javax.annotation.ParametersAreNonnullByDefault;
+
diff --git a/sonar-core/src/main/resources/org/sonar/core/user/AuthorMapper-oracle.xml b/sonar-core/src/main/resources/org/sonar/core/user/AuthorMapper-oracle.xml
new file mode 100644 (file)
index 0000000..7043980
--- /dev/null
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+
+<mapper namespace="org.sonar.core.user.AuthorMapper">
+
+  <select id="select" parameterType="string" resultType="Author">
+    SELECT id, person_id AS "personId", login, created_at AS "createdAt", updated_at AS "updatedAt"
+    FROM authors WHERE login=#{login}
+  </select>
+
+  <insert id="insert" parameterType="Author" keyColumn="id" useGeneratedKeys="true" keyProperty="id">
+    <selectKey order="BEFORE" resultType="Long" keyProperty="id">
+      select dashboards_seq.NEXTVAL from DUAL
+    </selectKey>
+    INSERT INTO authors (id, person_id, login, created_at, updated_at)
+    VALUES (#{id}, #{personId, jdbcType=INTEGER}, #{login, jdbcType=VARCHAR},
+    #{createdAt, jdbcType=TIMESTAMP}, #{updatedAt, jdbcType=TIMESTAMP})
+  </insert>
+
+</mapper>
diff --git a/sonar-core/src/main/resources/org/sonar/core/user/AuthorMapper.xml b/sonar-core/src/main/resources/org/sonar/core/user/AuthorMapper.xml
new file mode 100644 (file)
index 0000000..18f5ffe
--- /dev/null
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+
+<mapper namespace="org.sonar.core.user.AuthorMapper">
+
+  <select id="select" parameterType="string" resultType="Author">
+    SELECT id, person_id AS "personId", login, created_at AS "createdAt", updated_at AS "updatedAt"
+    FROM authors WHERE login=#{login}
+  </select>
+
+  <insert id="insert" parameterType="Author" useGeneratedKeys="true" keyProperty="id">
+    INSERT INTO authors (person_id, login, created_at, updated_at)
+    VALUES (#{personId, jdbcType=INTEGER}, #{login, jdbcType=VARCHAR},
+    #{createdAt, jdbcType=TIMESTAMP}, #{updatedAt, jdbcType=TIMESTAMP})
+  </insert>
+
+</mapper>
diff --git a/sonar-core/src/test/java/org/sonar/core/user/AuthorDaoTest.java b/sonar-core/src/test/java/org/sonar/core/user/AuthorDaoTest.java
new file mode 100644 (file)
index 0000000..ed09443
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2012 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.core.user;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.core.persistence.DaoTestCase;
+
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
+import static org.junit.Assert.assertThat;
+
+public class AuthorDaoTest extends DaoTestCase {
+
+  private AuthorDao dao;
+
+  @Before
+  public void setUp() {
+    dao = new AuthorDao(getMyBatis());
+  }
+
+  @Test
+  public void shouldSelect() {
+    setupData("shouldSelect");
+
+    AuthorDto authorDto = dao.select("godin");
+    assertThat(authorDto.getId(), is(1));
+    assertThat(authorDto.getPersonId(), is(13));
+    assertThat(authorDto.getLogin(), is("godin"));
+
+    assertThat(dao.select("simon"), is(nullValue()));
+  }
+
+  @Test
+  public void shouldInsert() {
+    setupData("shouldInsert");
+
+    AuthorDto authorDto = new AuthorDto()
+        .setLogin("godin")
+        .setPersonId(13);
+
+    dao.insert(authorDto);
+
+    checkTables("shouldInsert", new String[] {"created_at", "updated_at"}, "authors");
+  }
+}
diff --git a/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsert-result.xml b/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsert-result.xml
new file mode 100644 (file)
index 0000000..a129400
--- /dev/null
@@ -0,0 +1,8 @@
+<dataset>
+
+  <authors
+    id="1"
+    person_id="13"
+    login="godin" />
+
+</dataset>
diff --git a/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsert.xml b/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsert.xml
new file mode 100644 (file)
index 0000000..fb0854f
--- /dev/null
@@ -0,0 +1,2 @@
+<dataset>
+</dataset>
diff --git a/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldSelect.xml b/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldSelect.xml
new file mode 100644 (file)
index 0000000..a129400
--- /dev/null
@@ -0,0 +1,8 @@
+<dataset>
+
+  <authors
+    id="1"
+    person_id="13"
+    login="godin" />
+
+</dataset>