diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2012-01-08 11:52:57 +0100 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2012-01-08 11:53:11 +0100 |
commit | ceed7d755e6f80849c48ecfc3505c1567accf0f4 (patch) | |
tree | ee0b6c16c89b41135db3b3a13fe5ea8caa34f933 | |
parent | 46479785846fb3e8f90704ddf459f9b19c3428ed (diff) | |
download | sonarqube-ceed7d755e6f80849c48ecfc3505c1567accf0f4.tar.gz sonarqube-ceed7d755e6f80849c48ecfc3505c1567accf0f4.zip |
Experimental refactoring to improve support of booleans in MyBatis
13 files changed, 67 insertions, 63 deletions
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 77509b690d6..c016e52a9c6 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 @@ -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); diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/Derby.java b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/Derby.java index 8cd143530d5..701d9dc7cce 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/Derby.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/Derby.java @@ -91,4 +91,11 @@ public class Derby implements Dialect { } } + public String getTrueSqlValue() { + return "true"; + } + + public String getFalseSqlValue() { + return "false"; + } } diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/Dialect.java b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/Dialect.java index 26550498cda..3f11b98797c 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/Dialect.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/Dialect.java @@ -58,4 +58,14 @@ public interface Dialect { String getDefaultDriverClassName(); String getConnectionInitStatement(String schema); + + /** + * @since 2.14 + */ + String getTrueSqlValue(); + + /** + * @since 2.14 + */ + String getFalseSqlValue(); } diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/MsSql.java b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/MsSql.java index 2d4f28b22e2..abcb36896a9 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/MsSql.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/MsSql.java @@ -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"; + } } diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/MySql.java b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/MySql.java index 9f5def8316a..7d025d2b703 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/MySql.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/MySql.java @@ -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"; + } } diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/Oracle.java b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/Oracle.java index 7ecc4b5f0f5..5c0499108a6 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/Oracle.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/Oracle.java @@ -76,4 +76,12 @@ public class Oracle implements Dialect { } return null; } + + public String getTrueSqlValue() { + return "1"; + } + + public String getFalseSqlValue() { + return "0"; + } } diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/PostgreSql.java b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/PostgreSql.java index 4bf34c5d5bf..a02bfd8beac 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/dialect/PostgreSql.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/dialect/PostgreSql.java @@ -73,4 +73,12 @@ public class PostgreSql implements Dialect { } return null; } + + public String getTrueSqlValue() { + return "true"; + } + + public String getFalseSqlValue() { + return "false"; + } } diff --git a/sonar-core/src/main/java/org/sonar/core/resource/ResourceIndexerQuery.java b/sonar-core/src/main/java/org/sonar/core/resource/ResourceIndexerQuery.java index 4c45f846f83..3dc83bf1fd9 100644 --- a/sonar-core/src/main/java/org/sonar/core/resource/ResourceIndexerQuery.java +++ b/sonar-core/src/main/java/org/sonar/core/resource/ResourceIndexerQuery.java @@ -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; diff --git a/sonar-core/src/main/resources/org/sonar/core/duplication/DuplicationMapper-mssql.xml b/sonar-core/src/main/resources/org/sonar/core/duplication/DuplicationMapper-mssql.xml deleted file mode 100644 index 57d2a21257b..00000000000 --- a/sonar-core/src/main/resources/org/sonar/core/duplication/DuplicationMapper-mssql.xml +++ /dev/null @@ -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> diff --git a/sonar-core/src/main/resources/org/sonar/core/duplication/DuplicationMapper-oracle.xml b/sonar-core/src/main/resources/org/sonar/core/duplication/DuplicationMapper-oracle.xml deleted file mode 100644 index 57d2a21257b..00000000000 --- a/sonar-core/src/main/resources/org/sonar/core/duplication/DuplicationMapper-oracle.xml +++ /dev/null @@ -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> diff --git a/sonar-core/src/main/resources/org/sonar/core/duplication/DuplicationMapper.xml b/sonar-core/src/main/resources/org/sonar/core/duplication/DuplicationMapper.xml index 1b68f8afbc1..c1dc43121e4 100644 --- a/sonar-core/src/main/resources/org/sonar/core/duplication/DuplicationMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/duplication/DuplicationMapper.xml @@ -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} diff --git a/sonar-core/src/main/resources/org/sonar/core/resource/ResourceIndexerMapper.xml b/sonar-core/src/main/resources/org/sonar/core/resource/ResourceIndexerMapper.xml index fee6f471405..e5206a164be 100644 --- a/sonar-core/src/main/resources/org/sonar/core/resource/ResourceIndexerMapper.xml +++ b/sonar-core/src/main/resources/org/sonar/core/resource/ResourceIndexerMapper.xml @@ -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 diff --git a/sonar-core/src/test/java/org/sonar/core/persistence/dialect/MySqlTest.java b/sonar-core/src/test/java/org/sonar/core/persistence/dialect/MySqlTest.java index 08498ce1f5b..2a402b1460e 100644 --- a/sonar-core/src/test/java/org/sonar/core/persistence/dialect/MySqlTest.java +++ b/sonar-core/src/test/java/org/sonar/core/persistence/dialect/MySqlTest.java @@ -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")); + } } + |