]> source.dussan.org Git - sonarqube.git/commitdiff
Experimental refactoring to improve support of booleans in MyBatis
authorsimonbrandhof <simon.brandhof@gmail.com>
Sun, 8 Jan 2012 10:52:57 +0000 (11:52 +0100)
committersimonbrandhof <simon.brandhof@gmail.com>
Sun, 8 Jan 2012 10:53:11 +0000 (11:53 +0100)
13 files changed:
sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java
sonar-core/src/main/java/org/sonar/core/persistence/dialect/Derby.java
sonar-core/src/main/java/org/sonar/core/persistence/dialect/Dialect.java
sonar-core/src/main/java/org/sonar/core/persistence/dialect/MsSql.java
sonar-core/src/main/java/org/sonar/core/persistence/dialect/MySql.java
sonar-core/src/main/java/org/sonar/core/persistence/dialect/Oracle.java
sonar-core/src/main/java/org/sonar/core/persistence/dialect/PostgreSql.java
sonar-core/src/main/java/org/sonar/core/resource/ResourceIndexerQuery.java
sonar-core/src/main/resources/org/sonar/core/duplication/DuplicationMapper-mssql.xml [deleted file]
sonar-core/src/main/resources/org/sonar/core/duplication/DuplicationMapper-oracle.xml [deleted file]
sonar-core/src/main/resources/org/sonar/core/duplication/DuplicationMapper.xml
sonar-core/src/main/resources/org/sonar/core/resource/ResourceIndexerMapper.xml
sonar-core/src/test/java/org/sonar/core/persistence/dialect/MySqlTest.java

index 77509b690d6c437b52dd6efa16f9eb33c77c7d49..c016e52a9c64f13e792d7c996129f80b8495b1f8 100644 (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);
index 8cd143530d56e878c5aeb245f375fc0a5ca67345..701d9dc7cce46eebd8d01e9e6272c83da54a2b37 100644 (file)
@@ -91,4 +91,11 @@ public class Derby implements Dialect {
     }
   }
 
+  public String getTrueSqlValue() {
+    return "true";
+  }
+
+  public String getFalseSqlValue() {
+    return "false";
+  }
 }
index 26550498cdab8b327dfd4d2d1c025f2d76e5b23a..3f11b98797c9e6784d55662f1667f357e62b7c46 100644 (file)
@@ -58,4 +58,14 @@ public interface Dialect {
   String getDefaultDriverClassName();
 
   String getConnectionInitStatement(String schema);
+
+  /**
+   * @since 2.14
+   */
+  String getTrueSqlValue();
+
+  /**
+   * @since 2.14
+   */
+  String getFalseSqlValue();
 }
index 2d4f28b22e20d25266e45d72e7aa67f76ee58598..abcb36896a937515a0efb01feb6a2c21b9bd1de7 100644 (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";
+  }
 }
 
index 9f5def8316aafdbe0386a537adb4c377fcbacdab..7d025d2b703cc7bd0bc6cb1d62d59eb2475071f2 100644 (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";
+  }
 }
index 7ecc4b5f0f5cec07421c22c94d5c85c2edb61124..5c0499108a685d2288ffd518ebfa50df47927036 100644 (file)
@@ -76,4 +76,12 @@ public class Oracle implements Dialect {
     }
     return null;
   }
+
+  public String getTrueSqlValue() {
+    return "1";
+  }
+
+  public String getFalseSqlValue() {
+    return "0";
+  }
 }
index 4bf34c5d5bf2235799e321eac696a306b9c541b7..a02bfd8beac7ca0f191c81cc6287138691e95291 100644 (file)
@@ -73,4 +73,12 @@ public class PostgreSql implements Dialect {
     }
     return null;
   }
+
+  public String getTrueSqlValue() {
+    return "true";
+  }
+
+  public String getFalseSqlValue() {
+    return "false";
+  }
 }
index 4c45f846f83cdd676c911ade494f93449d6d7734..3dc83bf1fd9c10ba167ceed310a867dbceed5b60 100644 (file)
 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 (file)
index 57d2a21..0000000
+++ /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 (file)
index 57d2a21..0000000
+++ /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>
index 1b68f8afbc1ac307c3fe2c1ef88040ba01a7e9a2..c1dc43121e4330058a949e7672d83177d7288ee5 100644 (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}
index fee6f471405f40f4f2cfcfad69ae83500c5c46cb..e5206a164be5b761a2d3fe6d1454cc28e5269515 100644 (file)
     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>
   <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
index 08498ce1f5b24adb10a6b63c7f388a652bc44ffe..2a402b1460e677665fedf9c128890d179d7846a8 100644 (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"));
+  }
 }
+