Browse Source

Experimental refactoring to improve support of booleans in MyBatis

tags/2.14
simonbrandhof 12 years ago
parent
commit
ceed7d755e

+ 2
- 0
sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java View File

conf.setEnvironment(new Environment("production", createTransactionFactory(), database.getDataSource())); conf.setEnvironment(new Environment("production", createTransactionFactory(), database.getDataSource()));
conf.setUseGeneratedKeys(true); conf.setUseGeneratedKeys(true);
conf.setLazyLoadingEnabled(false); conf.setLazyLoadingEnabled(false);
conf.getVariables().setProperty("_true", database.getDialect().getTrueSqlValue());
conf.getVariables().setProperty("_false", database.getDialect().getFalseSqlValue());


loadAlias(conf, "ActiveDashboard", ActiveDashboardDto.class); loadAlias(conf, "ActiveDashboard", ActiveDashboardDto.class);
loadAlias(conf, "Dashboard", DashboardDto.class); loadAlias(conf, "Dashboard", DashboardDto.class);

+ 7
- 0
sonar-core/src/main/java/org/sonar/core/persistence/dialect/Derby.java View File

} }
} }


public String getTrueSqlValue() {
return "true";
}

public String getFalseSqlValue() {
return "false";
}
} }

+ 10
- 0
sonar-core/src/main/java/org/sonar/core/persistence/dialect/Dialect.java View File

String getDefaultDriverClassName(); String getDefaultDriverClassName();


String getConnectionInitStatement(String schema); String getConnectionInitStatement(String schema);

/**
* @since 2.14
*/
String getTrueSqlValue();

/**
* @since 2.14
*/
String getFalseSqlValue();
} }

+ 8
- 0
sonar-core/src/main/java/org/sonar/core/persistence/dialect/MsSql.java View File

public String getConnectionInitStatement(String schema) { public String getConnectionInitStatement(String schema) {
return null; return null;
} }

public String getTrueSqlValue() {
return "1";
}

public String getFalseSqlValue() {
return "0";
}
} }



+ 8
- 0
sonar-core/src/main/java/org/sonar/core/persistence/dialect/MySql.java View File

public String getConnectionInitStatement(String schema) { public String getConnectionInitStatement(String schema) {
return null; return null;
} }

public String getTrueSqlValue() {
return "true";
}

public String getFalseSqlValue() {
return "false";
}
} }

+ 8
- 0
sonar-core/src/main/java/org/sonar/core/persistence/dialect/Oracle.java View File

} }
return null; return null;
} }

public String getTrueSqlValue() {
return "1";
}

public String getFalseSqlValue() {
return "0";
}
} }

+ 8
- 0
sonar-core/src/main/java/org/sonar/core/persistence/dialect/PostgreSql.java View File

} }
return null; return null;
} }

public String getTrueSqlValue() {
return "true";
}

public String getFalseSqlValue() {
return "false";
}
} }

+ 0
- 4
sonar-core/src/main/java/org/sonar/core/resource/ResourceIndexerQuery.java View File

package org.sonar.core.resource; package org.sonar.core.resource;


final class ResourceIndexerQuery { final class ResourceIndexerQuery {
// Workaround to inject booleans into mybatis mappers. It avoids declaring mappers
// dedicated to Oracle and SQLServer
public final boolean _true = true; //NOSONAR

private Integer rootProjectId = null; private Integer rootProjectId = null;
private String[] scopes = null; private String[] scopes = null;
private String[] qualifiers = null; private String[] qualifiers = null;

+ 0
- 24
sonar-core/src/main/resources/org/sonar/core/duplication/DuplicationMapper-mssql.xml View File

<?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.duplication.DuplicationMapper">

<select id="selectCandidates" parameterType="map" resultType="DuplicationUnit">
SELECT DISTINCT to_blocks.hash hash, res.kee resourceKey, to_blocks.index_in_file indexInFile, to_blocks.start_line startLine, to_blocks.end_line endLine
FROM duplications_index to_blocks, duplications_index from_blocks, snapshots snapshot, projects res
WHERE from_blocks.snapshot_id = #{resource_snapshot_id}
AND to_blocks.hash = from_blocks.hash
AND to_blocks.snapshot_id = snapshot.id
AND snapshot.islast = 1
AND snapshot.project_id = res.id
<if test="last_project_snapshot_id != null">
AND to_blocks.project_snapshot_id != #{last_project_snapshot_id}
</if>
</select>

<insert id="batchInsert" parameterType="DuplicationUnit" useGeneratedKeys="false">
INSERT INTO duplications_index (snapshot_id, project_snapshot_id, hash, index_in_file, start_line, end_line)
VALUES (#{snapshotId}, #{projectSnapshotId}, #{hash}, #{indexInFile}, #{startLine}, #{endLine})
</insert>

</mapper>

+ 0
- 24
sonar-core/src/main/resources/org/sonar/core/duplication/DuplicationMapper-oracle.xml View File

<?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.duplication.DuplicationMapper">

<select id="selectCandidates" parameterType="map" resultType="DuplicationUnit">
SELECT DISTINCT to_blocks.hash hash, res.kee resourceKey, to_blocks.index_in_file indexInFile, to_blocks.start_line startLine, to_blocks.end_line endLine
FROM duplications_index to_blocks, duplications_index from_blocks, snapshots snapshot, projects res
WHERE from_blocks.snapshot_id = #{resource_snapshot_id}
AND to_blocks.hash = from_blocks.hash
AND to_blocks.snapshot_id = snapshot.id
AND snapshot.islast = 1
AND snapshot.project_id = res.id
<if test="last_project_snapshot_id != null">
AND to_blocks.project_snapshot_id != #{last_project_snapshot_id}
</if>
</select>

<insert id="batchInsert" parameterType="DuplicationUnit" useGeneratedKeys="false">
INSERT INTO duplications_index (snapshot_id, project_snapshot_id, hash, index_in_file, start_line, end_line)
VALUES (#{snapshotId}, #{projectSnapshotId}, #{hash}, #{indexInFile}, #{startLine}, #{endLine})
</insert>

</mapper>

+ 1
- 1
sonar-core/src/main/resources/org/sonar/core/duplication/DuplicationMapper.xml View File

WHERE from_blocks.snapshot_id = #{resource_snapshot_id} WHERE from_blocks.snapshot_id = #{resource_snapshot_id}
AND to_blocks.hash = from_blocks.hash AND to_blocks.hash = from_blocks.hash
AND to_blocks.snapshot_id = snapshot.id AND to_blocks.snapshot_id = snapshot.id
AND snapshot.islast = TRUE
AND snapshot.islast = ${_true}
AND snapshot.project_id = res.id AND snapshot.project_id = res.id
<if test="last_project_snapshot_id != null"> <if test="last_project_snapshot_id != null">
AND to_blocks.project_snapshot_id != #{last_project_snapshot_id} AND to_blocks.project_snapshot_id != #{last_project_snapshot_id}

+ 3
- 6
sonar-core/src/main/resources/org/sonar/core/resource/ResourceIndexerMapper.xml View File

select p.name as "name", p.id as "id", p.scope as "scope", p.qualifier as "qualifier", s.root_project_id as "rootId" select p.name as "name", p.id as "id", p.scope as "scope", p.qualifier as "qualifier", s.root_project_id as "rootId"
from projects p, snapshots s from projects p, snapshots s
<where> <where>
p.enabled=#{_true}
p.enabled=${_true}
and p.copy_resource_id is null and p.copy_resource_id is null
and p.id=s.project_id and p.id=s.project_id
and s.islast=#{_true}
and s.islast=${_true}
<if test="scopes != null"> <if test="scopes != null">
and p.scope in and p.scope in
<foreach item="scope" index="index" collection="scopes" open="(" separator="," close=")">#{scope}</foreach> <foreach item="scope" index="index" collection="scopes" open="(" separator="," close=")">#{scope}</foreach>
<select id="selectRootProjectIds" parameterType="map" resultType="int"> <select id="selectRootProjectIds" parameterType="map" resultType="int">
select distinct root_project_id select distinct root_project_id
from snapshots from snapshots
where islast=#{_true}
where islast=${_true}
and scope='PRJ' and scope='PRJ'
and qualifier in ('TRK', 'VW', 'SVW') and qualifier in ('TRK', 'VW', 'SVW')
</select> </select>





<select id="selectMasterIndexByResourceId" parameterType="int" resultType="ResourceIndex"> <select id="selectMasterIndexByResourceId" parameterType="int" resultType="ResourceIndex">
select kee as "key", resource_id as "resourceId" select kee as "key", resource_id as "resourceId"
from resource_index from resource_index

+ 12
- 4
sonar-core/src/test/java/org/sonar/core/persistence/dialect/MySqlTest.java View File



public class MySqlTest { public class MySqlTest {


private MySql mySql = new MySql();

@Test @Test
public void matchesJdbcURL() { public void matchesJdbcURL() {
assertThat(new MySql().matchesJdbcURL("jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8"), is(true));
assertThat(new MySql().matchesJdbcURL("JDBC:MYSQL://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8"), is(true));
assertThat(mySql.matchesJdbcURL("jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8"), is(true));
assertThat(mySql.matchesJdbcURL("JDBC:MYSQL://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8"), is(true));


assertThat(new MySql().matchesJdbcURL("jdbc:hsql:foo"), is(false));
assertThat(new MySql().matchesJdbcURL("jdbc:oracle:foo"), is(false));
assertThat(mySql.matchesJdbcURL("jdbc:hsql:foo"), is(false));
assertThat(mySql.matchesJdbcURL("jdbc:oracle:foo"), is(false));
} }


@Test
public void testBooleanSqlValues() {
assertThat(mySql.getTrueSqlValue(), is("true"));
assertThat(mySql.getFalseSqlValue(), is("false"));
}
} }


Loading…
Cancel
Save