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

@@ -57,6 +57,8 @@ public class MyBatis implements BatchComponent, ServerComponent {
conf.setEnvironment(new Environment("production", createTransactionFactory(), database.getDataSource()));
conf.setUseGeneratedKeys(true);
conf.setLazyLoadingEnabled(false);
conf.getVariables().setProperty("_true", database.getDialect().getTrueSqlValue());
conf.getVariables().setProperty("_false", database.getDialect().getFalseSqlValue());

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

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

@@ -91,4 +91,11 @@ public class Derby implements Dialect {
}
}

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

@@ -58,4 +58,14 @@ public interface Dialect {
String getDefaultDriverClassName();

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

@@ -76,5 +76,13 @@ public class MsSql implements Dialect {
public String getConnectionInitStatement(String schema) {
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

@@ -69,4 +69,12 @@ public class MySql implements Dialect {
public String getConnectionInitStatement(String schema) {
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

@@ -76,4 +76,12 @@ public class Oracle implements Dialect {
}
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

@@ -73,4 +73,12 @@ public class PostgreSql implements Dialect {
}
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

@@ -20,10 +20,6 @@
package org.sonar.core.resource;

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 String[] scopes = null;
private String[] qualifiers = null;

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

@@ -1,24 +0,0 @@
<?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

@@ -1,24 +0,0 @@
<?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

@@ -9,7 +9,7 @@
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 = TRUE
AND snapshot.islast = ${_true}
AND snapshot.project_id = res.id
<if test="last_project_snapshot_id != null">
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

@@ -11,10 +11,10 @@
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
<where>
p.enabled=#{_true}
p.enabled=${_true}
and p.copy_resource_id is null
and p.id=s.project_id
and s.islast=#{_true}
and s.islast=${_true}
<if test="scopes != null">
and p.scope in
<foreach item="scope" index="index" collection="scopes" open="(" separator="," close=")">#{scope}</foreach>
@@ -36,14 +36,11 @@
<select id="selectRootProjectIds" parameterType="map" resultType="int">
select distinct root_project_id
from snapshots
where islast=#{_true}
where islast=${_true}
and scope='PRJ'
and qualifier in ('TRK', 'VW', 'SVW')
</select>




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

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

@@ -26,13 +26,21 @@ import static org.junit.Assert.assertThat;

public class MySqlTest {

private MySql mySql = new MySql();

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