aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-core
diff options
context:
space:
mode:
authorEvgeny Mandrikov <mandrikov@gmail.com>2012-03-27 17:26:17 +0600
committerEvgeny Mandrikov <mandrikov@gmail.com>2012-03-27 18:15:32 +0600
commitef9423735e55a57d4c888dd1f69ec2e4c6efd174 (patch)
treec51cfaa9dfda48a8aea1b203091f5a1bf669d055 /sonar-core
parente4fa987e3d3426c01f0b8e3c34cb6c4acfbe85bd (diff)
downloadsonarqube-ef9423735e55a57d4c888dd1f69ec2e4c6efd174.tar.gz
sonarqube-ef9423735e55a57d4c888dd1f69ec2e4c6efd174.zip
Add MyBatis stuff for table AUTHORS
Diffstat (limited to 'sonar-core')
-rw-r--r--sonar-core/src/main/java/org/sonar/core/persistence/DaoUtils.java2
-rw-r--r--sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java4
-rw-r--r--sonar-core/src/main/java/org/sonar/core/user/AuthorDao.java59
-rw-r--r--sonar-core/src/main/java/org/sonar/core/user/AuthorDto.java80
-rw-r--r--sonar-core/src/main/java/org/sonar/core/user/AuthorMapper.java33
-rw-r--r--sonar-core/src/main/java/org/sonar/core/user/package-info.java25
-rw-r--r--sonar-core/src/main/resources/org/sonar/core/user/AuthorMapper-oracle.xml20
-rw-r--r--sonar-core/src/main/resources/org/sonar/core/user/AuthorMapper.xml17
-rw-r--r--sonar-core/src/test/java/org/sonar/core/user/AuthorDaoTest.java63
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsert-result.xml8
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsert.xml2
-rw-r--r--sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldSelect.xml8
12 files changed, 321 insertions, 0 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DaoUtils.java b/sonar-core/src/main/java/org/sonar/core/persistence/DaoUtils.java
index a750228cc37..5295da36602 100644
--- a/sonar-core/src/main/java/org/sonar/core/persistence/DaoUtils.java
+++ b/sonar-core/src/main/java/org/sonar/core/persistence/DaoUtils.java
@@ -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,
diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java b/sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java
index a82ef62da35..0c2b1b75415 100644
--- a/sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java
+++ b/sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java
@@ -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
index 00000000000..574bdaa6270
--- /dev/null
+++ b/sonar-core/src/main/java/org/sonar/core/user/AuthorDao.java
@@ -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
index 00000000000..63e22bbcff7
--- /dev/null
+++ b/sonar-core/src/main/java/org/sonar/core/user/AuthorDto.java
@@ -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
index 00000000000..ac3b23ebc0f
--- /dev/null
+++ b/sonar-core/src/main/java/org/sonar/core/user/AuthorMapper.java
@@ -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
index 00000000000..06cd9c97ba1
--- /dev/null
+++ b/sonar-core/src/main/java/org/sonar/core/user/package-info.java
@@ -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
index 00000000000..70439809d16
--- /dev/null
+++ b/sonar-core/src/main/resources/org/sonar/core/user/AuthorMapper-oracle.xml
@@ -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
index 00000000000..18f5ffe016a
--- /dev/null
+++ b/sonar-core/src/main/resources/org/sonar/core/user/AuthorMapper.xml
@@ -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
index 00000000000..ed0944362b0
--- /dev/null
+++ b/sonar-core/src/test/java/org/sonar/core/user/AuthorDaoTest.java
@@ -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
index 00000000000..a129400b266
--- /dev/null
+++ b/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsert-result.xml
@@ -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
index 00000000000..fb0854fccbe
--- /dev/null
+++ b/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsert.xml
@@ -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
index 00000000000..a129400b266
--- /dev/null
+++ b/sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldSelect.xml
@@ -0,0 +1,8 @@
+<dataset>
+
+ <authors
+ id="1"
+ person_id="13"
+ login="godin" />
+
+</dataset>