Browse Source

Add MyBatis stuff for table AUTHORS

tags/3.0
Evgeny Mandrikov 12 years ago
parent
commit
ef9423735e

+ 2
- 0
sonar-core/src/main/java/org/sonar/core/persistence/DaoUtils.java View 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,

+ 4
- 0
sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java View 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);

+ 59
- 0
sonar-core/src/main/java/org/sonar/core/user/AuthorDao.java View File

@@ -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);
}
}

}

+ 80
- 0
sonar-core/src/main/java/org/sonar/core/user/AuthorDto.java View File

@@ -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;
}

}

+ 33
- 0
sonar-core/src/main/java/org/sonar/core/user/AuthorMapper.java View File

@@ -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);

}

+ 25
- 0
sonar-core/src/main/java/org/sonar/core/user/package-info.java View File

@@ -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;


+ 20
- 0
sonar-core/src/main/resources/org/sonar/core/user/AuthorMapper-oracle.xml View File

@@ -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>

+ 17
- 0
sonar-core/src/main/resources/org/sonar/core/user/AuthorMapper.xml View File

@@ -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>

+ 63
- 0
sonar-core/src/test/java/org/sonar/core/user/AuthorDaoTest.java View File

@@ -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");
}
}

+ 8
- 0
sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsert-result.xml View File

@@ -0,0 +1,8 @@
<dataset>

<authors
id="1"
person_id="13"
login="godin" />

</dataset>

+ 2
- 0
sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldInsert.xml View File

@@ -0,0 +1,2 @@
<dataset>
</dataset>

+ 8
- 0
sonar-core/src/test/resources/org/sonar/core/user/AuthorDaoTest/shouldSelect.xml View File

@@ -0,0 +1,8 @@
<dataset>

<authors
id="1"
person_id="13"
login="godin" />

</dataset>

Loading…
Cancel
Save