]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3876 Backslash characters are duplicated on PostgreSQL 9.1
authorSimon Brandhof <simon.brandhof@gmail.com>
Mon, 15 Oct 2012 11:19:03 +0000 (13:19 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Mon, 15 Oct 2012 11:20:32 +0000 (13:20 +0200)
12 files changed:
pom.xml
sonar-core/src/main/java/org/sonar/core/persistence/DefaultDatabase.java
sonar-core/src/main/java/org/sonar/core/persistence/dialect/AbstractDialect.java
sonar-core/src/main/java/org/sonar/core/persistence/dialect/Dialect.java
sonar-core/src/main/java/org/sonar/core/persistence/dialect/H2.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/test/java/org/sonar/core/persistence/dialect/OracleTest.java
sonar-core/src/test/java/org/sonar/core/persistence/dialect/PostgreSqlTest.java
sonar-server/src/main/webapp/WEB-INF/gems/gems/activerecord-jdbc-adapter-1.1.3/lib/arjdbc/postgresql/adapter.rb

diff --git a/pom.xml b/pom.xml
index 3b26e406fc6519ddcdcc7972e215135796a307c1..c3b410ecbd07c0e2be98313bd6a5c4219cf5a183 100644 (file)
--- a/pom.xml
+++ b/pom.xml
       <dependency>
         <groupId>postgresql</groupId>
         <artifactId>postgresql</artifactId>
-        <!-- do not upgrade to version 9.0-801.jdbc4, not compatible with Java 5 -->
-        <version>9.0-801.jdbc3</version>
+        <!-- do not upgrade to version 9.1-901.jdbc4, not compatible with Java 5 -->
+        <version>9.1-901.jdbc3</version>
       </dependency>
       <dependency>
         <groupId>net.sourceforge.jtds</groupId>
index ae05c0b10d3f8c34a142772e5529025347ecbb50..54744b10b6f3aa2e9c98dd78a34b8c9dbe687500 100644 (file)
@@ -112,15 +112,9 @@ public class DefaultDatabase implements Database {
 
   private void initDatasource() throws Exception {// NOSONAR this exception is thrown by BasicDataSourceFactory
     // but it's correctly caught by start()
-
     LOG.info("Create JDBC datasource to url " + properties.getProperty(DatabaseProperties.PROP_URL, DEFAULT_URL));
     datasource = (BasicDataSource) BasicDataSourceFactory.createDataSource(extractCommonsDbcpProperties(properties));
-
-    String initStatement = dialect.getConnectionInitStatement(getSchema());
-    if (StringUtils.isNotBlank(initStatement)) {
-      datasource.setConnectionInitSqls(Arrays.asList(initStatement));
-    }
-
+    datasource.setConnectionInitSqls(dialect.getConnectionInitStatements(getSchema()));
     datasource.setValidationQuery(dialect.getValidationQuery());
   }
 
index 0efe69d12b5ff801253eed72c6d09c2464f8799e..469784ca892b1a9ab24a21c0864e92f099264aa2 100644 (file)
@@ -19,6 +19,9 @@
  */
 package org.sonar.core.persistence.dialect;
 
+import java.util.Collections;
+import java.util.List;
+
 /**
  * @since 3.2
  */
@@ -63,4 +66,8 @@ abstract class AbstractDialect implements Dialect {
   public final String getValidationQuery() {
     return validationQuery;
   }
+
+  public List<String> getConnectionInitStatements(String schema) {
+    return Collections.emptyList();
+  }
 }
index 0994f3dcf4fda6ed3cbe8492373ffa95d3067ba5..ca78184e096d8ab043825c1e3286ec02b8496b93 100644 (file)
@@ -19,6 +19,8 @@
  */
 package org.sonar.core.persistence.dialect;
 
+import java.util.List;
+
 /**
  * @since 1.12
  */
@@ -52,7 +54,7 @@ public interface Dialect {
    */
   String getDefaultDriverClassName();
 
-  String getConnectionInitStatement(String schema);
+  List<String> getConnectionInitStatements(String schema);
 
   /**
    * @since 2.14
index 0c80c7dc445cd54f36884fadde7dadef8c0a9c89..721c35174882994b1d09fb5b6c535188165817fb 100644 (file)
@@ -41,7 +41,4 @@ public class H2 extends AbstractDialect {
     return StringUtils.startsWithIgnoreCase(jdbcConnectionURL, "jdbc:h2:");
   }
 
-  public String getConnectionInitStatement(String schema) {
-    return null;
-  }
 }
index a7b30451e3f92523ebe96742588dd3396b09dc43..ce93a5034f0f7649148164fa0fcdf9005736b425 100644 (file)
@@ -61,9 +61,5 @@ public class MsSql extends AbstractDialect {
       }
     }
   }
-
-  public String getConnectionInitStatement(String schema) {
-    return null;
-  }
 }
 
index 1e0d86d73e25a79503a57f85969dd5a2e578b26e..ff20d9272d40c8b20538c29a166a02aaaa6624c8 100644 (file)
@@ -53,8 +53,4 @@ public class MySql extends AbstractDialect {
       registerColumnType(Types.BLOB, "blob");
     }
   }
-
-  public String getConnectionInitStatement(String schema) {
-    return null;
-  }
 }
index ce21be04c52f4e8ffe8260c116072107bdd6ec27..9d66cae54d6b58e4a7a4e5a7a35d69fb066a36d2 100644 (file)
  */
 package org.sonar.core.persistence.dialect;
 
+import com.google.common.collect.Lists;
 import org.apache.commons.lang.StringUtils;
 import org.hibernate.dialect.Oracle10gDialect;
 import org.sonar.api.database.DatabaseProperties;
 
 import java.sql.Types;
+import java.util.List;
 
 /**
  * @since 1.12
@@ -58,10 +60,12 @@ public class Oracle extends AbstractDialect {
     }
   }
 
-  public String getConnectionInitStatement(String schema) {
+  @Override
+  public List<String> getConnectionInitStatements(String schema) {
+    List<String> statements = Lists.newArrayList();
     if (StringUtils.isNotBlank(schema)) {
-      return "ALTER SESSION SET CURRENT_SCHEMA = \"" + schema + "\"";
+      statements.add("ALTER SESSION SET CURRENT_SCHEMA = \"" + schema + "\"");
     }
-    return null;
+    return statements;
   }
 }
index 0db4659e653df6c48b496ef309bac128a0afd2e1..51719490553ec80c3c60691b2490cd247fc7fcd8 100644 (file)
  */
 package org.sonar.core.persistence.dialect;
 
+import com.google.common.collect.Lists;
 import org.apache.commons.lang.StringUtils;
 import org.hibernate.dialect.PostgreSQLDialect;
 
 import java.sql.Types;
+import java.util.List;
 
 /**
  * @since 1.12
@@ -55,10 +57,12 @@ public class PostgreSql extends AbstractDialect {
     }
   }
 
-  public String getConnectionInitStatement(String schema) {
+  @Override
+  public List<String> getConnectionInitStatements(String schema) {
+    List<String> statements = Lists.newArrayList("SET standard_conforming_strings=on", "SET backslash_quote=off");
     if (StringUtils.isNotBlank(schema)) {
-      return "SET SEARCH_PATH TO " + schema;
+      statements.add("SET SEARCH_PATH TO " + schema);
     }
-    return null;
+    return statements;
   }
 }
index 34d1594f2c57828a5a5833a9b286840d0368fb63..f39fbbc2b8232c92f93d7b4a78db4a61cb760188 100644 (file)
@@ -21,6 +21,8 @@ package org.sonar.core.persistence.dialect;
 
 import org.junit.Test;
 
+import java.util.List;
+
 import static org.fest.assertions.Assertions.assertThat;
 
 public class OracleTest {
@@ -38,14 +40,14 @@ public class OracleTest {
    */
   @Test
   public void shouldChangeOracleSchema() {
-    String initStatement = oracle.getConnectionInitStatement("my_schema");
+    List<String> initStatements = oracle.getConnectionInitStatements("my_schema");
 
-    assertThat(initStatement).isEqualTo("ALTER SESSION SET CURRENT_SCHEMA = \"my_schema\"");
+    assertThat(initStatements).containsExactly("ALTER SESSION SET CURRENT_SCHEMA = \"my_schema\"");
   }
 
   @Test
   public void shouldNotChangeOracleSchemaByDefault() {
-    assertThat(oracle.getConnectionInitStatement(null)).isNull();
+    assertThat(oracle.getConnectionInitStatements(null)).isEmpty();
   }
 
   @Test
index dfc35ac88c47213205ae052268b44479b17d787b..40f067ea71055a526222ad8a07ecc84a6437d914 100644 (file)
  */
 package org.sonar.core.persistence.dialect;
 
-import org.hamcrest.core.Is;
+import org.fest.assertions.Condition;
 import org.junit.Test;
 
+import java.util.List;
+
 import static org.fest.assertions.Assertions.assertThat;
-import static org.junit.Assert.assertThat;
 
 public class PostgreSqlTest {
 
@@ -37,14 +38,16 @@ public class PostgreSqlTest {
 
   @Test
   public void should_avoid_conflict_with_other_schemas() {
-    String initStatement = postgreSql.getConnectionInitStatement("my_schema");
+    List<String> initStatements = postgreSql.getConnectionInitStatements("my_schema");
 
-    assertThat(initStatement, Is.is("SET SEARCH_PATH TO my_schema"));
+    assertThat(initStatements).contains("SET SEARCH_PATH TO my_schema");
   }
 
   @Test
   public void shouldNotChangePostgreSearchPathByDefault() {
-    assertThat(postgreSql.getConnectionInitStatement(null)).isNull();
+    for (String statement : postgreSql.getConnectionInitStatements(null)) {
+      assertThat(statement).doesNotMatch("SET SEARCH_PATH .*");
+    }
   }
 
   @Test
index 28a42647bc59967418a31612af53e6909dbe2fd0..2f442f829ccbc87df26731a95495b5267f7893c7 100644 (file)
@@ -465,6 +465,15 @@ module ::ArJdbc
       end
     end
 
+    #sonar
+    # standard_conforming_strings is forced to true in JDBC connection pool (see org.sonar.core.persistence.dialect.PostgreSql)
+    # so double backslashing must be removed
+    def quote_string(s)
+      s.gsub(/'/, "''") # ' (for ruby-mode)
+    end
+    #/sonar
+
+
     def escape_bytea(s)
       if s
         result = ''