]> source.dussan.org Git - sonarqube.git/commitdiff
Move org.sonar.jpa.dialect to org.sonar.persistence.dialect and refactor DialectRepos...
authorSimon Brandhof <simon.brandhof@gmail.com>
Fri, 16 Dec 2011 18:39:40 +0000 (19:39 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Fri, 16 Dec 2011 18:39:40 +0000 (19:39 +0100)
42 files changed:
sonar-core/src/main/java/org/sonar/jpa/dialect/Derby.java [deleted file]
sonar-core/src/main/java/org/sonar/jpa/dialect/Dialect.java [deleted file]
sonar-core/src/main/java/org/sonar/jpa/dialect/DialectRepository.java [deleted file]
sonar-core/src/main/java/org/sonar/jpa/dialect/MsSql.java [deleted file]
sonar-core/src/main/java/org/sonar/jpa/dialect/MySql.java [deleted file]
sonar-core/src/main/java/org/sonar/jpa/dialect/Oracle.java [deleted file]
sonar-core/src/main/java/org/sonar/jpa/dialect/OracleSequenceGenerator.java [deleted file]
sonar-core/src/main/java/org/sonar/jpa/dialect/PostgreSQLSequenceGenerator.java [deleted file]
sonar-core/src/main/java/org/sonar/jpa/dialect/PostgreSql.java [deleted file]
sonar-core/src/main/java/org/sonar/jpa/session/AbstractDatabaseConnector.java
sonar-core/src/main/java/org/sonar/jpa/session/DatabaseConnector.java
sonar-core/src/main/java/org/sonar/persistence/Database.java
sonar-core/src/main/java/org/sonar/persistence/DefaultDatabase.java
sonar-core/src/main/java/org/sonar/persistence/dialect/Derby.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/persistence/dialect/Dialect.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/persistence/dialect/DialectUtils.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/persistence/dialect/MsSql.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/persistence/dialect/MySql.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/persistence/dialect/Oracle.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/persistence/dialect/OracleSequenceGenerator.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/persistence/dialect/PostgreSQLSequenceGenerator.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/persistence/dialect/PostgreSql.java [new file with mode: 0644]
sonar-core/src/test/java/org/sonar/jpa/dialect/DerbyTest.java [deleted file]
sonar-core/src/test/java/org/sonar/jpa/dialect/DialectRepositoryTest.java [deleted file]
sonar-core/src/test/java/org/sonar/jpa/dialect/MsSqlTest.java [deleted file]
sonar-core/src/test/java/org/sonar/jpa/dialect/MySqlTest.java [deleted file]
sonar-core/src/test/java/org/sonar/jpa/dialect/OracleSequenceGeneratorTest.java [deleted file]
sonar-core/src/test/java/org/sonar/jpa/dialect/OracleTest.java [deleted file]
sonar-core/src/test/java/org/sonar/jpa/dialect/PostgreSQLSequenceGeneratorTest.java [deleted file]
sonar-core/src/test/java/org/sonar/jpa/dialect/PostgreSqlTest.java [deleted file]
sonar-core/src/test/java/org/sonar/persistence/DatabaseCommands.java
sonar-core/src/test/java/org/sonar/persistence/DefaultDatabaseTest.java
sonar-core/src/test/java/org/sonar/persistence/InMemoryDatabase.java
sonar-core/src/test/java/org/sonar/persistence/dialect/DerbyTest.java [new file with mode: 0644]
sonar-core/src/test/java/org/sonar/persistence/dialect/DialectUtilsTest.java [new file with mode: 0644]
sonar-core/src/test/java/org/sonar/persistence/dialect/MsSqlTest.java [new file with mode: 0644]
sonar-core/src/test/java/org/sonar/persistence/dialect/MySqlTest.java [new file with mode: 0644]
sonar-core/src/test/java/org/sonar/persistence/dialect/OracleSequenceGeneratorTest.java [new file with mode: 0644]
sonar-core/src/test/java/org/sonar/persistence/dialect/OracleTest.java [new file with mode: 0644]
sonar-core/src/test/java/org/sonar/persistence/dialect/PostgreSQLSequenceGeneratorTest.java [new file with mode: 0644]
sonar-core/src/test/java/org/sonar/persistence/dialect/PostgreSqlTest.java [new file with mode: 0644]
sonar-server/src/test/java/org/sonar/server/platform/ServerFileSystemTest.java

diff --git a/sonar-core/src/main/java/org/sonar/jpa/dialect/Derby.java b/sonar-core/src/main/java/org/sonar/jpa/dialect/Derby.java
deleted file mode 100644 (file)
index cebb9e0..0000000
+++ /dev/null
@@ -1,94 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
- */
-package org.sonar.jpa.dialect;
-
-import org.apache.commons.lang.StringUtils;
-import org.hibernate.dialect.DerbyDialect;
-import org.hibernate.id.IdentityGenerator;
-import org.sonar.api.database.DatabaseProperties;
-
-import java.sql.Types;
-
-/**
- * @since 1.12
- */
-public class Derby implements Dialect {
-
-  public static final String ID = "derby";
-
-  public String getId() {
-    return ID;
-  }
-
-  public String getActiveRecordDialectCode() {
-    return "derby";
-  }
-
-  public String getActiveRecordJdbcAdapter() {
-    return "jdbc";
-  }
-
-  public Class<? extends org.hibernate.dialect.Dialect> getHibernateDialectClass() {
-    return DerbyWithDecimalDialect.class;
-  }
-
-  public boolean matchesJdbcURL(String jdbcConnectionURL) {
-    return StringUtils.startsWithIgnoreCase(jdbcConnectionURL, "jdbc:derby:");
-  }
-
-  public String getDefaultDriverClassName() {
-    return "org.apache.derby.jdbc.ClientDriver";
-  }
-
-  public String getConnectionInitStatement(String schema) {
-    return null;
-  }
-
-  public static class DerbyWithDecimalDialect extends DerbyDialect {
-    public DerbyWithDecimalDialect() {
-      super();
-      registerColumnType(Types.DOUBLE, "decimal");
-      registerColumnType(Types.VARCHAR, DatabaseProperties.MAX_TEXT_SIZE, "clob");
-      registerColumnType(Types.VARBINARY, "blob");
-
-      // Not possible to do alter column types in Derby
-      registerColumnType(Types.BIGINT, "integer");
-
-      registerColumnType(Types.BIT, "boolean");
-    }
-
-    @Override
-    public String toBooleanValueString(boolean bool) {
-      return bool ? "true" : "false";
-    }
-
-    /**
-     * To be compliant with Oracle, we define on each model (ch.hortis.sonar.model classes)
-     * a sequence generator. It works on mySQL because strategy = GenerationType.AUTO, so
-     * it equals GenerationType.IDENTITY.
-     * But on derby, AUTO becomes TABLE instead of IDENTITY. So we explicitly change this behavior.
-     */
-    @Override
-    public Class getNativeIdentifierGeneratorClass() {
-      return IdentityGenerator.class;
-    }
-  }
-
-}
diff --git a/sonar-core/src/main/java/org/sonar/jpa/dialect/Dialect.java b/sonar-core/src/main/java/org/sonar/jpa/dialect/Dialect.java
deleted file mode 100644 (file)
index a33df55..0000000
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
- */
-package org.sonar.jpa.dialect;
-
-/**
- * @since 1.12
- */
-public interface Dialect {
-
-  /**
-   * @return the sonar dialect Id to be matched with the sonar.jdbc.dialect property when provided
-   */
-  String getId();
-
-  /**
-   * @return the hibernate dialect class to be used
-   */
-  Class<? extends org.hibernate.dialect.Dialect> getHibernateDialectClass();
-
-  /**
-   * @return the activerecord dialect to be used
-   */
-  String getActiveRecordDialectCode();
-
-  /**
-   * @return the activerecord-jdbc adapter. See the property 'adapter' in database.yml
-   */
-  String getActiveRecordJdbcAdapter();
-
-  /**
-   * Used to autodetect a dialect for a given driver URL
-   *
-   * @param jdbcConnectionURL a jdbc driver url such as jdbc:mysql://localhost:3306/sonar
-   * @return true if the dialect supports surch url
-   */
-  boolean matchesJdbcURL(String jdbcConnectionURL);
-
-  /**
-   * @since 2.13
-   */
-  String getDefaultDriverClassName();
-
-  String getConnectionInitStatement(String schema);
-}
diff --git a/sonar-core/src/main/java/org/sonar/jpa/dialect/DialectRepository.java b/sonar-core/src/main/java/org/sonar/jpa/dialect/DialectRepository.java
deleted file mode 100644 (file)
index 80394a8..0000000
+++ /dev/null
@@ -1,93 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
- */
-package org.sonar.jpa.dialect;
-
-import com.google.common.base.Predicate;
-import com.google.common.collect.Iterators;
-import org.apache.commons.lang.StringUtils;
-import org.sonar.api.utils.SonarException;
-
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.List;
-import java.util.NoSuchElementException;
-
-/**
- * @since 1.12
- */
-public final class DialectRepository {
-
-  private DialectRepository() {
-  }
-
-  private static List<Dialect> builtInDialects = getSupportedDialects();
-
-  public static Dialect find(final String dialectId, final String jdbcConnectionUrl) {
-    Dialect match = StringUtils.isNotEmpty(dialectId) ? findById(dialectId) : findByJdbcUrl(jdbcConnectionUrl);
-    if (match == null) {
-      throw new SonarException("Unable to determine database dialect to use within sonar with dialect " + dialectId + " jdbc url " + jdbcConnectionUrl);
-    }
-    return match;
-  }
-
-  private static Dialect findByJdbcUrl(final String jdbcConnectionUrl) {
-    Dialect match = findDialect(builtInDialects, new Predicate<Dialect>() {
-      public boolean apply(Dialect dialect) {
-        return dialect.matchesJdbcURL(StringUtils.trimToEmpty(jdbcConnectionUrl));
-      }
-    });
-    return match;
-  }
-
-  private static Dialect findById(final String dialectId) {
-    Dialect match = findDialect(builtInDialects, new Predicate<Dialect>() {
-      public boolean apply(Dialect dialect) {
-        return dialect.getId().equals(dialectId);
-      }
-    });
-    // maybe a class name if no match
-    match = match == null ? getDialectByClassname(dialectId) : match;
-    return match;
-  }
-
-  private static Dialect findDialect(Collection<Dialect> dialects, Predicate<Dialect> predicate) {
-    try {
-      return Iterators.find(dialects.iterator(), predicate);
-    } catch (NoSuchElementException ex) {
-      return null;
-    }
-  }
-
-  private static Dialect getDialectByClassname(String dialectId) {
-    try {
-      Class<? extends Dialect> dialectClass = (Class<? extends Dialect>) DialectRepository.class.getClassLoader().loadClass(dialectId);
-      return dialectClass.newInstance();
-    } catch (ClassNotFoundException e) {
-      // dialectId was not a class name :)
-    } catch (Exception e) {
-      throw new SonarException("Unable to instantiate dialect class", e);
-    }
-    return null;
-  }
-
-  private static List<Dialect> getSupportedDialects() {
-    return Arrays.asList(new Derby(), new MySql(), new Oracle(), new PostgreSql(), new MsSql());
-  }
-}
diff --git a/sonar-core/src/main/java/org/sonar/jpa/dialect/MsSql.java b/sonar-core/src/main/java/org/sonar/jpa/dialect/MsSql.java
deleted file mode 100644 (file)
index 8cafb79..0000000
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
- */
-package org.sonar.jpa.dialect;
-
-import org.apache.commons.lang.StringUtils;
-import org.hibernate.HibernateException;
-import org.hibernate.dialect.SQLServerDialect;
-import org.sonar.api.database.DatabaseProperties;
-
-import java.sql.Types;
-
-public class MsSql implements Dialect {
-
-  public static final String ID = "mssql";
-
-  public String getId() {
-    return ID;
-  }
-
-  public String getActiveRecordDialectCode() {
-    return "sqlserver";
-  }
-
-  public String getActiveRecordJdbcAdapter() {
-    return "jdbc";
-  }
-
-  public Class<? extends org.hibernate.dialect.Dialect> getHibernateDialectClass() {
-    return MsSqlDialect.class;
-  }
-
-  public boolean matchesJdbcURL(String jdbcConnectionURL) {
-    return StringUtils.startsWithIgnoreCase(jdbcConnectionURL, "jdbc:microsoft:sqlserver:")
-        || StringUtils.startsWithIgnoreCase(jdbcConnectionURL, "jdbc:jtds:sqlserver:");
-  }
-
-  public static class MsSqlDialect extends SQLServerDialect {
-    public MsSqlDialect() {
-      super();
-      registerColumnType(Types.DOUBLE, "decimal");
-      registerColumnType(Types.VARCHAR, 255, "nvarchar($l)");
-      registerColumnType(Types.VARCHAR, DatabaseProperties.MAX_TEXT_SIZE, "nvarchar(max)");
-      registerColumnType(Types.CHAR, "nchar(1)");
-      registerColumnType(Types.CLOB, "nvarchar(max)");
-    }
-
-    public String getTypeName(int code, int length, int precision, int scale) throws HibernateException {
-      if (code != 2005) {
-        return super.getTypeName(code, length, precision, scale);
-      } else {
-        return "ntext";
-      }
-    }
-  }
-
-  public String getDefaultDriverClassName() {
-    return "net.sourceforge.jtds.jdbc.Driver";
-  }
-
-  public String getConnectionInitStatement(String schema) {
-    return null;
-  }
-}
-
diff --git a/sonar-core/src/main/java/org/sonar/jpa/dialect/MySql.java b/sonar-core/src/main/java/org/sonar/jpa/dialect/MySql.java
deleted file mode 100644 (file)
index 4287617..0000000
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
- */
-package org.sonar.jpa.dialect;
-
-import org.apache.commons.lang.StringUtils;
-import org.hibernate.dialect.MySQLDialect;
-import org.sonar.api.database.DatabaseProperties;
-
-import java.sql.Types;
-
-/**
- * @since 1.12
- */
-public class MySql implements Dialect {
-
-  public static final String ID = "mysql";
-
-  public String getId() {
-    return ID;
-  }
-
-  public String getActiveRecordDialectCode() {
-    return "mysql";
-  }
-
-  public String getActiveRecordJdbcAdapter() {
-    return "jdbc";
-  }
-
-  public Class<? extends org.hibernate.dialect.Dialect> getHibernateDialectClass() {
-    return MySqlWithDecimalDialect.class;
-  }
-
-  public boolean matchesJdbcURL(String jdbcConnectionURL) {
-    return StringUtils.startsWithIgnoreCase(jdbcConnectionURL, "jdbc:mysql:");
-  }
-
-  public static class MySqlWithDecimalDialect extends MySQLDialect {
-    public MySqlWithDecimalDialect() {
-      super();
-      registerColumnType(Types.DOUBLE, "decimal precision");
-      registerColumnType(Types.VARCHAR, DatabaseProperties.MAX_TEXT_SIZE, "mediumtext");
-      registerColumnType(Types.CLOB, "mediumtext");
-      registerColumnType(Types.BLOB, "blob");
-    }
-  }
-
-  public String getDefaultDriverClassName() {
-    return "com.mysql.jdbc.Driver";
-  }
-
-  public String getConnectionInitStatement(String schema) {
-    return null;
-  }
-}
diff --git a/sonar-core/src/main/java/org/sonar/jpa/dialect/Oracle.java b/sonar-core/src/main/java/org/sonar/jpa/dialect/Oracle.java
deleted file mode 100644 (file)
index eb92ca5..0000000
+++ /dev/null
@@ -1,79 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
- */
-package org.sonar.jpa.dialect;
-
-import org.apache.commons.lang.StringUtils;
-import org.hibernate.dialect.Oracle10gDialect;
-import org.sonar.api.database.DatabaseProperties;
-
-import java.sql.Types;
-
-/**
- * @since 1.12
- */
-public class Oracle implements Dialect {
-
-  public static final String ID = "oracle";
-
-  public String getId() {
-    return ID;
-  }
-
-  public String getActiveRecordDialectCode() {
-    return "oracle";
-  }
-
-  public String getActiveRecordJdbcAdapter() {
-    return "oracle_enhanced";
-  }
-
-  public Class<? extends org.hibernate.dialect.Dialect> getHibernateDialectClass() {
-    return Oracle10gWithDecimalDialect.class;
-  }
-
-  public boolean matchesJdbcURL(String jdbcConnectionURL) {
-    return StringUtils.startsWithIgnoreCase(jdbcConnectionURL, "jdbc:oracle:");
-  }
-
-  public static class Oracle10gWithDecimalDialect extends Oracle10gDialect {
-    public Oracle10gWithDecimalDialect() {
-      super();
-      registerColumnType(Types.DOUBLE, "number($p,$s)");
-      registerColumnType(Types.VARCHAR, DatabaseProperties.MAX_TEXT_SIZE, "clob");
-      registerColumnType(Types.VARBINARY, "blob");
-    }
-
-    @Override
-    public Class getNativeIdentifierGeneratorClass() {
-      return OracleSequenceGenerator.class;
-    }
-  }
-
-  public String getDefaultDriverClassName() {
-    return "oracle.jdbc.OracleDriver";
-  }
-
-  public String getConnectionInitStatement(String schema) {
-    if (StringUtils.isNotBlank(schema)) {
-      return "ALTER SESSION SET CURRENT_SCHEMA = \"" + schema + "\"";
-    }
-    return null;
-  }
-}
diff --git a/sonar-core/src/main/java/org/sonar/jpa/dialect/OracleSequenceGenerator.java b/sonar-core/src/main/java/org/sonar/jpa/dialect/OracleSequenceGenerator.java
deleted file mode 100644 (file)
index 5450f27..0000000
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
- */
-package org.sonar.jpa.dialect;
-
-import org.apache.commons.lang.StringUtils;
-import org.hibernate.MappingException;
-import org.hibernate.dialect.Dialect;
-import org.hibernate.id.PersistentIdentifierGenerator;
-import org.hibernate.id.SequenceGenerator;
-import org.hibernate.type.Type;
-
-import java.util.Properties;
-
-/**
- * @since 1.10
- */
-public class OracleSequenceGenerator extends SequenceGenerator {
-
-  public static final String SEQUENCE_NAME_SUFFIX = "_SEQ";
-
-  @Override
-  public void configure(Type type, Properties params, Dialect dialect)
-    throws MappingException {
-
-    String tableName = params.getProperty(PersistentIdentifierGenerator.TABLE);
-
-    if (tableName != null) {
-      StringBuilder sequenceNameBuilder = new StringBuilder();
-
-      sequenceNameBuilder.append(tableName);
-      sequenceNameBuilder.append(SEQUENCE_NAME_SUFFIX);
-
-      params.setProperty(SEQUENCE, StringUtils.upperCase(sequenceNameBuilder.toString()));
-    }
-
-    super.configure(type, params, dialect);
-  }
-
-}
diff --git a/sonar-core/src/main/java/org/sonar/jpa/dialect/PostgreSQLSequenceGenerator.java b/sonar-core/src/main/java/org/sonar/jpa/dialect/PostgreSQLSequenceGenerator.java
deleted file mode 100644 (file)
index a04390c..0000000
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
- */
-package org.sonar.jpa.dialect;
-
-import org.hibernate.MappingException;
-import org.hibernate.dialect.Dialect;
-import org.hibernate.id.PersistentIdentifierGenerator;
-import org.hibernate.id.SequenceGenerator;
-import org.hibernate.type.Type;
-
-import java.util.Properties;
-
-/**
- * if the underlying database is PostgreSQL, the sequence
- * naming convention is different and includes the primary key
- * column name
- *
- * @since 1.10
- */
-public class PostgreSQLSequenceGenerator extends SequenceGenerator {
-
-  public static final String SEQUENCE_NAME_SEPARATOR = "_";
-  public static final String SEQUENCE_NAME_SUFFIX = "seq";
-
-  @Override
-  public void configure(Type type, Properties params, Dialect dialect)
-      throws MappingException {
-
-    String tableName = params.getProperty(PersistentIdentifierGenerator.TABLE);
-    String columnName = params.getProperty(PersistentIdentifierGenerator.PK);
-
-    if (tableName != null && columnName != null) {
-      StringBuilder sequenceNameBuilder = new StringBuilder();
-
-      sequenceNameBuilder.append(tableName);
-      sequenceNameBuilder.append(SEQUENCE_NAME_SEPARATOR);
-      sequenceNameBuilder.append(columnName);
-      sequenceNameBuilder.append(SEQUENCE_NAME_SEPARATOR);
-      sequenceNameBuilder.append(SEQUENCE_NAME_SUFFIX);
-
-      params.setProperty(SEQUENCE, sequenceNameBuilder.toString());
-    }
-
-    super.configure(type, params, dialect);
-  }
-
-}
diff --git a/sonar-core/src/main/java/org/sonar/jpa/dialect/PostgreSql.java b/sonar-core/src/main/java/org/sonar/jpa/dialect/PostgreSql.java
deleted file mode 100644 (file)
index 495dfef..0000000
+++ /dev/null
@@ -1,76 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
- */
-package org.sonar.jpa.dialect;
-
-import org.apache.commons.lang.StringUtils;
-import org.hibernate.dialect.PostgreSQLDialect;
-
-import java.sql.Types;
-
-/**
- * @since 1.12
- */
-public class PostgreSql implements Dialect {
-
-  public static final String ID = "postgresql";
-
-  public String getId() {
-    return ID;
-  }
-
-  public String getActiveRecordDialectCode() {
-    return "postgre";
-  }
-
-  public String getActiveRecordJdbcAdapter() {
-    return "jdbc";
-  }
-
-  public Class<? extends org.hibernate.dialect.Dialect> getHibernateDialectClass() {
-    return PostgreSQLWithDecimalDialect.class;
-  }
-
-  public boolean matchesJdbcURL(String jdbcConnectionURL) {
-    return StringUtils.startsWithIgnoreCase(jdbcConnectionURL, "jdbc:postgresql:");
-  }
-
-  public static class PostgreSQLWithDecimalDialect extends PostgreSQLDialect {
-    public PostgreSQLWithDecimalDialect() {
-      super();
-      registerColumnType(Types.DOUBLE, "numeric($p,$s)");
-    }
-
-    @Override
-    public Class getNativeIdentifierGeneratorClass() {
-      return PostgreSQLSequenceGenerator.class;
-    }
-  }
-
-  public String getDefaultDriverClassName() {
-    return "org.postgresql.Driver";
-  }
-
-  public String getConnectionInitStatement(String schema) {
-    if (StringUtils.isNotBlank(schema)) {
-      return "SET SEARCH_PATH TO " + schema;
-    }
-    return null;
-  }
-}
index 6dbcfd8daa1bc7ba5fca90de97bd7fde80deb1ed..e0a5d739d6822d5b96db858f79531ef8d2b99fe4 100644 (file)
@@ -22,9 +22,9 @@ package org.sonar.jpa.session;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.sonar.api.utils.Logs;
-import org.sonar.jpa.dialect.Dialect;
 import org.sonar.jpa.entity.SchemaMigration;
 import org.sonar.persistence.Database;
+import org.sonar.persistence.dialect.Dialect;
 
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
index 72abcf6755a45a799bbddfa4cd6ce1d838eb244c..2f793c3e75dcb275b5fcb4a9e691ffa44e88cf48 100644 (file)
@@ -19,7 +19,7 @@
  */
 package org.sonar.jpa.session;
 
-import org.sonar.jpa.dialect.Dialect;
+import org.sonar.persistence.dialect.Dialect;
 
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
@@ -37,5 +37,5 @@ public interface DatabaseConnector {
   EntityManager createEntityManager();
 
   int getDatabaseVersion();
-  
+
 }
index 9fd31c7b652983a4c69e654f6a072d505461a436..95b9b10b739ca2539cd781e737a9f697d6d99e97 100644 (file)
  */
 package org.sonar.persistence;
 
-import org.sonar.jpa.dialect.Dialect;
+import org.sonar.persistence.dialect.Dialect;
 
 import javax.sql.DataSource;
 import java.util.Properties;
 
 /**
- *
  * @since 2.12
  */
 public interface Database {
   Database start();
+
   Database stop();
 
   /**
index 9f7c6af21c3d1ccf197b3e15d2f4916101ff8420..f18e404485ec3fe66251bba2202ffe4e11968fa0 100644 (file)
@@ -27,8 +27,8 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.sonar.api.config.Settings;
 import org.sonar.api.database.DatabaseProperties;
-import org.sonar.jpa.dialect.*;
 import org.sonar.jpa.session.CustomHibernateConnectionProvider;
+import org.sonar.persistence.dialect.*;
 
 import javax.sql.DataSource;
 import java.sql.SQLException;
@@ -80,7 +80,7 @@ public class DefaultDatabase implements Database {
   }
 
   private void initDialect() {
-    dialect = DialectRepository.find(properties.getProperty("sonar.jdbc.dialect"), properties.getProperty("sonar.jdbc.url"));
+    dialect = DialectUtils.find(properties.getProperty("sonar.jdbc.dialect"), properties.getProperty("sonar.jdbc.url"));
     if (dialect == null) {
       throw new IllegalStateException("Can not guess the JDBC dialect. Please check the property sonar.jdbc.url.");
     }
diff --git a/sonar-core/src/main/java/org/sonar/persistence/dialect/Derby.java b/sonar-core/src/main/java/org/sonar/persistence/dialect/Derby.java
new file mode 100644 (file)
index 0000000..0377f2a
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.persistence.dialect;
+
+import org.apache.commons.lang.StringUtils;
+import org.hibernate.dialect.DerbyDialect;
+import org.hibernate.id.IdentityGenerator;
+import org.sonar.api.database.DatabaseProperties;
+
+import java.sql.Types;
+
+/**
+ * @since 1.12
+ */
+public class Derby implements Dialect {
+
+  public static final String ID = "derby";
+
+  public String getId() {
+    return ID;
+  }
+
+  public String getActiveRecordDialectCode() {
+    return "derby";
+  }
+
+  public String getActiveRecordJdbcAdapter() {
+    return "jdbc";
+  }
+
+  public Class<? extends org.hibernate.dialect.Dialect> getHibernateDialectClass() {
+    return DerbyWithDecimalDialect.class;
+  }
+
+  public boolean matchesJdbcURL(String jdbcConnectionURL) {
+    return StringUtils.startsWithIgnoreCase(jdbcConnectionURL, "jdbc:derby:");
+  }
+
+  public String getDefaultDriverClassName() {
+    return "org.apache.derby.jdbc.ClientDriver";
+  }
+
+  public String getConnectionInitStatement(String schema) {
+    return null;
+  }
+
+  public static class DerbyWithDecimalDialect extends DerbyDialect {
+    public DerbyWithDecimalDialect() {
+      super();
+      registerColumnType(Types.DOUBLE, "decimal");
+      registerColumnType(Types.VARCHAR, DatabaseProperties.MAX_TEXT_SIZE, "clob");
+      registerColumnType(Types.VARBINARY, "blob");
+
+      // Not possible to do alter column types in Derby
+      registerColumnType(Types.BIGINT, "integer");
+
+      registerColumnType(Types.BIT, "boolean");
+    }
+
+    @Override
+    public String toBooleanValueString(boolean bool) {
+      return bool ? "true" : "false";
+    }
+
+    /**
+     * To be compliant with Oracle, we define on each model (ch.hortis.sonar.model classes)
+     * a sequence generator. It works on mySQL because strategy = GenerationType.AUTO, so
+     * it equals GenerationType.IDENTITY.
+     * But on derby, AUTO becomes TABLE instead of IDENTITY. So we explicitly change this behavior.
+     */
+    @Override
+    public Class getNativeIdentifierGeneratorClass() {
+      return IdentityGenerator.class;
+    }
+  }
+
+}
diff --git a/sonar-core/src/main/java/org/sonar/persistence/dialect/Dialect.java b/sonar-core/src/main/java/org/sonar/persistence/dialect/Dialect.java
new file mode 100644 (file)
index 0000000..8ba4d9b
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.persistence.dialect;
+
+/**
+ * @since 1.12
+ */
+public interface Dialect {
+
+  /**
+   * @return the sonar dialect Id to be matched with the sonar.jdbc.dialect property when provided
+   */
+  String getId();
+
+  /**
+   * @return the hibernate dialect class to be used
+   */
+  Class<? extends org.hibernate.dialect.Dialect> getHibernateDialectClass();
+
+  /**
+   * @return the activerecord dialect to be used
+   */
+  String getActiveRecordDialectCode();
+
+  /**
+   * @return the activerecord-jdbc adapter. See the property 'adapter' in database.yml
+   */
+  String getActiveRecordJdbcAdapter();
+
+  /**
+   * Used to autodetect a dialect for a given driver URL
+   *
+   * @param jdbcConnectionURL a jdbc driver url such as jdbc:mysql://localhost:3306/sonar
+   * @return true if the dialect supports surch url
+   */
+  boolean matchesJdbcURL(String jdbcConnectionURL);
+
+  /**
+   * @since 2.13
+   */
+  String getDefaultDriverClassName();
+
+  String getConnectionInitStatement(String schema);
+}
diff --git a/sonar-core/src/main/java/org/sonar/persistence/dialect/DialectUtils.java b/sonar-core/src/main/java/org/sonar/persistence/dialect/DialectUtils.java
new file mode 100644 (file)
index 0000000..672dd86
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.persistence.dialect;
+
+import com.google.common.base.Predicate;
+import com.google.common.collect.Iterators;
+import org.apache.commons.lang.StringUtils;
+import org.sonar.api.utils.SonarException;
+
+import java.util.NoSuchElementException;
+
+public final class DialectUtils {
+
+  private DialectUtils() {
+  }
+
+  private static final Dialect[] DIALECTS = new Dialect[]{new Derby(), new MySql(), new Oracle(), new PostgreSql(), new MsSql()};
+
+  public static Dialect find(final String dialectId, final String jdbcConnectionUrl) {
+    Dialect match = StringUtils.isNotBlank(dialectId) ? findById(dialectId) : findByJdbcUrl(jdbcConnectionUrl);
+    if (match == null) {
+      throw new SonarException("Unable to determine database dialect to use within sonar with dialect " + dialectId + " jdbc url " + jdbcConnectionUrl);
+    }
+    return match;
+  }
+
+  private static Dialect findByJdbcUrl(final String jdbcConnectionUrl) {
+    Dialect match = findDialect(new Predicate<Dialect>() {
+      public boolean apply(Dialect dialect) {
+        return dialect.matchesJdbcURL(StringUtils.trimToEmpty(jdbcConnectionUrl));
+      }
+    });
+    return match;
+  }
+
+  private static Dialect findById(final String dialectId) {
+    return findDialect(new Predicate<Dialect>() {
+      public boolean apply(Dialect dialect) {
+        return dialect.getId().equals(dialectId);
+      }
+    });
+  }
+
+  private static Dialect findDialect(Predicate<Dialect> predicate) {
+    try {
+      return Iterators.find(Iterators.forArray(DIALECTS), predicate);
+    } catch (NoSuchElementException ex) {
+      return null;
+    }
+  }
+}
diff --git a/sonar-core/src/main/java/org/sonar/persistence/dialect/MsSql.java b/sonar-core/src/main/java/org/sonar/persistence/dialect/MsSql.java
new file mode 100644 (file)
index 0000000..6a09b80
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.persistence.dialect;
+
+import org.apache.commons.lang.StringUtils;
+import org.hibernate.HibernateException;
+import org.hibernate.dialect.SQLServerDialect;
+import org.sonar.api.database.DatabaseProperties;
+
+import java.sql.Types;
+
+public class MsSql implements Dialect {
+
+  public static final String ID = "mssql";
+
+  public String getId() {
+    return ID;
+  }
+
+  public String getActiveRecordDialectCode() {
+    return "sqlserver";
+  }
+
+  public String getActiveRecordJdbcAdapter() {
+    return "jdbc";
+  }
+
+  public Class<? extends org.hibernate.dialect.Dialect> getHibernateDialectClass() {
+    return MsSqlDialect.class;
+  }
+
+  public boolean matchesJdbcURL(String jdbcConnectionURL) {
+    return StringUtils.startsWithIgnoreCase(jdbcConnectionURL, "jdbc:microsoft:sqlserver:")
+      || StringUtils.startsWithIgnoreCase(jdbcConnectionURL, "jdbc:jtds:sqlserver:");
+  }
+
+  public static class MsSqlDialect extends SQLServerDialect {
+    public MsSqlDialect() {
+      super();
+      registerColumnType(Types.DOUBLE, "decimal");
+      registerColumnType(Types.VARCHAR, 255, "nvarchar($l)");
+      registerColumnType(Types.VARCHAR, DatabaseProperties.MAX_TEXT_SIZE, "nvarchar(max)");
+      registerColumnType(Types.CHAR, "nchar(1)");
+      registerColumnType(Types.CLOB, "nvarchar(max)");
+    }
+
+    public String getTypeName(int code, int length, int precision, int scale) throws HibernateException {
+      if (code != 2005) {
+        return super.getTypeName(code, length, precision, scale);
+      } else {
+        return "ntext";
+      }
+    }
+  }
+
+  public String getDefaultDriverClassName() {
+    return "net.sourceforge.jtds.jdbc.Driver";
+  }
+
+  public String getConnectionInitStatement(String schema) {
+    return null;
+  }
+}
+
diff --git a/sonar-core/src/main/java/org/sonar/persistence/dialect/MySql.java b/sonar-core/src/main/java/org/sonar/persistence/dialect/MySql.java
new file mode 100644 (file)
index 0000000..a35b7f2
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.persistence.dialect;
+
+import org.apache.commons.lang.StringUtils;
+import org.hibernate.dialect.MySQLDialect;
+import org.sonar.api.database.DatabaseProperties;
+
+import java.sql.Types;
+
+/**
+ * @since 1.12
+ */
+public class MySql implements Dialect {
+
+  public static final String ID = "mysql";
+
+  public String getId() {
+    return ID;
+  }
+
+  public String getActiveRecordDialectCode() {
+    return "mysql";
+  }
+
+  public String getActiveRecordJdbcAdapter() {
+    return "jdbc";
+  }
+
+  public Class<? extends org.hibernate.dialect.Dialect> getHibernateDialectClass() {
+    return MySqlWithDecimalDialect.class;
+  }
+
+  public boolean matchesJdbcURL(String jdbcConnectionURL) {
+    return StringUtils.startsWithIgnoreCase(jdbcConnectionURL, "jdbc:mysql:");
+  }
+
+  public static class MySqlWithDecimalDialect extends MySQLDialect {
+    public MySqlWithDecimalDialect() {
+      super();
+      registerColumnType(Types.DOUBLE, "decimal precision");
+      registerColumnType(Types.VARCHAR, DatabaseProperties.MAX_TEXT_SIZE, "mediumtext");
+      registerColumnType(Types.CLOB, "mediumtext");
+      registerColumnType(Types.BLOB, "blob");
+    }
+  }
+
+  public String getDefaultDriverClassName() {
+    return "com.mysql.jdbc.Driver";
+  }
+
+  public String getConnectionInitStatement(String schema) {
+    return null;
+  }
+}
diff --git a/sonar-core/src/main/java/org/sonar/persistence/dialect/Oracle.java b/sonar-core/src/main/java/org/sonar/persistence/dialect/Oracle.java
new file mode 100644 (file)
index 0000000..ae1f5ee
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.persistence.dialect;
+
+import org.apache.commons.lang.StringUtils;
+import org.hibernate.dialect.Oracle10gDialect;
+import org.sonar.api.database.DatabaseProperties;
+
+import java.sql.Types;
+
+/**
+ * @since 1.12
+ */
+public class Oracle implements Dialect {
+
+  public static final String ID = "oracle";
+
+  public String getId() {
+    return ID;
+  }
+
+  public String getActiveRecordDialectCode() {
+    return "oracle";
+  }
+
+  public String getActiveRecordJdbcAdapter() {
+    return "oracle_enhanced";
+  }
+
+  public Class<? extends org.hibernate.dialect.Dialect> getHibernateDialectClass() {
+    return Oracle10gWithDecimalDialect.class;
+  }
+
+  public boolean matchesJdbcURL(String jdbcConnectionURL) {
+    return StringUtils.startsWithIgnoreCase(jdbcConnectionURL, "jdbc:oracle:");
+  }
+
+  public static class Oracle10gWithDecimalDialect extends Oracle10gDialect {
+    public Oracle10gWithDecimalDialect() {
+      super();
+      registerColumnType(Types.DOUBLE, "number($p,$s)");
+      registerColumnType(Types.VARCHAR, DatabaseProperties.MAX_TEXT_SIZE, "clob");
+      registerColumnType(Types.VARBINARY, "blob");
+    }
+
+    @Override
+    public Class getNativeIdentifierGeneratorClass() {
+      return OracleSequenceGenerator.class;
+    }
+  }
+
+  public String getDefaultDriverClassName() {
+    return "oracle.jdbc.OracleDriver";
+  }
+
+  public String getConnectionInitStatement(String schema) {
+    if (StringUtils.isNotBlank(schema)) {
+      return "ALTER SESSION SET CURRENT_SCHEMA = \"" + schema + "\"";
+    }
+    return null;
+  }
+}
diff --git a/sonar-core/src/main/java/org/sonar/persistence/dialect/OracleSequenceGenerator.java b/sonar-core/src/main/java/org/sonar/persistence/dialect/OracleSequenceGenerator.java
new file mode 100644 (file)
index 0000000..f1a30f3
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.persistence.dialect;
+
+import org.apache.commons.lang.StringUtils;
+import org.hibernate.MappingException;
+import org.hibernate.dialect.Dialect;
+import org.hibernate.id.PersistentIdentifierGenerator;
+import org.hibernate.id.SequenceGenerator;
+import org.hibernate.type.Type;
+
+import java.util.Properties;
+
+/**
+ * @since 1.10
+ */
+public class OracleSequenceGenerator extends SequenceGenerator {
+
+  public static final String SEQUENCE_NAME_SUFFIX = "_SEQ";
+
+  @Override
+  public void configure(Type type, Properties params, Dialect dialect)
+    throws MappingException {
+
+    String tableName = params.getProperty(PersistentIdentifierGenerator.TABLE);
+
+    if (tableName != null) {
+      StringBuilder sequenceNameBuilder = new StringBuilder();
+
+      sequenceNameBuilder.append(tableName);
+      sequenceNameBuilder.append(SEQUENCE_NAME_SUFFIX);
+
+      params.setProperty(SEQUENCE, StringUtils.upperCase(sequenceNameBuilder.toString()));
+    }
+
+    super.configure(type, params, dialect);
+  }
+
+}
diff --git a/sonar-core/src/main/java/org/sonar/persistence/dialect/PostgreSQLSequenceGenerator.java b/sonar-core/src/main/java/org/sonar/persistence/dialect/PostgreSQLSequenceGenerator.java
new file mode 100644 (file)
index 0000000..1c22d30
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.persistence.dialect;
+
+import org.hibernate.MappingException;
+import org.hibernate.dialect.Dialect;
+import org.hibernate.id.PersistentIdentifierGenerator;
+import org.hibernate.id.SequenceGenerator;
+import org.hibernate.type.Type;
+
+import java.util.Properties;
+
+/**
+ * if the underlying database is PostgreSQL, the sequence
+ * naming convention is different and includes the primary key
+ * column name
+ *
+ * @since 1.10
+ */
+public class PostgreSQLSequenceGenerator extends SequenceGenerator {
+
+  public static final String SEQUENCE_NAME_SEPARATOR = "_";
+  public static final String SEQUENCE_NAME_SUFFIX = "seq";
+
+  @Override
+  public void configure(Type type, Properties params, Dialect dialect)
+    throws MappingException {
+
+    String tableName = params.getProperty(PersistentIdentifierGenerator.TABLE);
+    String columnName = params.getProperty(PersistentIdentifierGenerator.PK);
+
+    if (tableName != null && columnName != null) {
+      StringBuilder sequenceNameBuilder = new StringBuilder();
+
+      sequenceNameBuilder.append(tableName);
+      sequenceNameBuilder.append(SEQUENCE_NAME_SEPARATOR);
+      sequenceNameBuilder.append(columnName);
+      sequenceNameBuilder.append(SEQUENCE_NAME_SEPARATOR);
+      sequenceNameBuilder.append(SEQUENCE_NAME_SUFFIX);
+
+      params.setProperty(SEQUENCE, sequenceNameBuilder.toString());
+    }
+
+    super.configure(type, params, dialect);
+  }
+
+}
diff --git a/sonar-core/src/main/java/org/sonar/persistence/dialect/PostgreSql.java b/sonar-core/src/main/java/org/sonar/persistence/dialect/PostgreSql.java
new file mode 100644 (file)
index 0000000..67c494f
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.persistence.dialect;
+
+import org.apache.commons.lang.StringUtils;
+import org.hibernate.dialect.PostgreSQLDialect;
+
+import java.sql.Types;
+
+/**
+ * @since 1.12
+ */
+public class PostgreSql implements Dialect {
+
+  public static final String ID = "postgresql";
+
+  public String getId() {
+    return ID;
+  }
+
+  public String getActiveRecordDialectCode() {
+    return "postgre";
+  }
+
+  public String getActiveRecordJdbcAdapter() {
+    return "jdbc";
+  }
+
+  public Class<? extends org.hibernate.dialect.Dialect> getHibernateDialectClass() {
+    return PostgreSQLWithDecimalDialect.class;
+  }
+
+  public boolean matchesJdbcURL(String jdbcConnectionURL) {
+    return StringUtils.startsWithIgnoreCase(jdbcConnectionURL, "jdbc:postgresql:");
+  }
+
+  public static class PostgreSQLWithDecimalDialect extends PostgreSQLDialect {
+    public PostgreSQLWithDecimalDialect() {
+      super();
+      registerColumnType(Types.DOUBLE, "numeric($p,$s)");
+    }
+
+    @Override
+    public Class getNativeIdentifierGeneratorClass() {
+      return PostgreSQLSequenceGenerator.class;
+    }
+  }
+
+  public String getDefaultDriverClassName() {
+    return "org.postgresql.Driver";
+  }
+
+  public String getConnectionInitStatement(String schema) {
+    if (StringUtils.isNotBlank(schema)) {
+      return "SET SEARCH_PATH TO " + schema;
+    }
+    return null;
+  }
+}
diff --git a/sonar-core/src/test/java/org/sonar/jpa/dialect/DerbyTest.java b/sonar-core/src/test/java/org/sonar/jpa/dialect/DerbyTest.java
deleted file mode 100644 (file)
index 7da394f..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
- */
-package org.sonar.jpa.dialect;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-import org.junit.Test;
-
-public class DerbyTest {
-  @Test
-  public void matchesJdbcURL() {
-    assertThat(new Derby().matchesJdbcURL("jdbc:derby:foo"), is(true));
-    assertThat(new Derby().matchesJdbcURL("jdbc:hsql:foo"), is(false));
-  }
-}
diff --git a/sonar-core/src/test/java/org/sonar/jpa/dialect/DialectRepositoryTest.java b/sonar-core/src/test/java/org/sonar/jpa/dialect/DialectRepositoryTest.java
deleted file mode 100644 (file)
index 1ee33f8..0000000
+++ /dev/null
@@ -1,83 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
- */
-package org.sonar.jpa.dialect;
-
-import org.junit.Test;
-import org.sonar.api.database.DatabaseProperties;
-import org.sonar.api.utils.SonarException;
-
-import static org.junit.Assert.assertEquals;
-
-public class DialectRepositoryTest {
-
-  @Test
-  public void testFindById() {
-    Dialect d = DialectRepository.find(DatabaseProperties.DIALECT_MYSQL, null);
-    assertEquals(MySql.class, d.getClass());
-  }
-
-  @Test
-  public void testFindByJdbcUrl() {
-    Dialect d = DialectRepository.find(null, "jdbc:mysql:foo:bar");
-    assertEquals(MySql.class, d.getClass());
-  }
-
-  @Test
-  public void testFindClassName() {
-    Dialect d = DialectRepository.find(TestDialect.class.getName(), null);
-    assertEquals(TestDialect.class, d.getClass());
-  }
-
-  @Test(expected = SonarException.class)
-  public void testFindNoMatch() {
-    DialectRepository.find("foo", "bar");
-  }
-
-  public static class TestDialect implements Dialect {
-    public boolean matchesJdbcURL(String jdbcConnectionURL) {
-      return false;
-    }
-
-    public String getDefaultDriverClassName() {
-      return null;
-    }
-
-    public String getConnectionInitStatement(String schema) {
-      return null;
-    }
-
-    public String getId() {
-      return "testDialect";
-    }
-
-    public Class<? extends org.hibernate.dialect.Dialect> getHibernateDialectClass() {
-      return null;
-    }
-
-    public String getActiveRecordDialectCode() {
-      return "test";
-    }
-
-    public String getActiveRecordJdbcAdapter() {
-      return "jdbc";
-    }
-  }
-
-}
diff --git a/sonar-core/src/test/java/org/sonar/jpa/dialect/MsSqlTest.java b/sonar-core/src/test/java/org/sonar/jpa/dialect/MsSqlTest.java
deleted file mode 100644 (file)
index 5554eba..0000000
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
- */
-package org.sonar.jpa.dialect;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-import org.junit.Test;
-
-public class MsSqlTest {
-
-  @Test
-  public void matchesJdbcURL() {
-    assertThat(new MsSql().matchesJdbcURL("jdbc:jtds:sqlserver://localhost;databaseName=SONAR;SelectMethod=Cursor"), is(true));
-    assertThat(new MsSql().matchesJdbcURL("jdbc:microsoft:sqlserver://localhost:1433;databasename=sonar"), is(true));
-
-    assertThat(new MsSql().matchesJdbcURL("jdbc:hsql:foo"), is(false));
-    assertThat(new MsSql().matchesJdbcURL("jdbc:mysql:foo"), is(false));
-  }
-
-}
diff --git a/sonar-core/src/test/java/org/sonar/jpa/dialect/MySqlTest.java b/sonar-core/src/test/java/org/sonar/jpa/dialect/MySqlTest.java
deleted file mode 100644 (file)
index a071768..0000000
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
- */
-package org.sonar.jpa.dialect;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-import org.junit.Test;
-
-public class MySqlTest {
-
-  @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(new MySql().matchesJdbcURL("jdbc:hsql:foo"), is(false));
-    assertThat(new MySql().matchesJdbcURL("jdbc:oracle:foo"), is(false));
-  }
-
-}
diff --git a/sonar-core/src/test/java/org/sonar/jpa/dialect/OracleSequenceGeneratorTest.java b/sonar-core/src/test/java/org/sonar/jpa/dialect/OracleSequenceGeneratorTest.java
deleted file mode 100644 (file)
index 05d963f..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
- */
-package org.sonar.jpa.dialect;
-
-import org.hibernate.id.PersistentIdentifierGenerator;
-import org.junit.Test;
-
-import java.util.Properties;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-
-public class OracleSequenceGeneratorTest {
-
-  @Test
-  public void sequenceNameShouldFollowRailsConventions() {
-    Properties props = new Properties();
-    props.setProperty(PersistentIdentifierGenerator.TABLE, "my_table");
-    props.setProperty(PersistentIdentifierGenerator.PK, "id");
-
-    OracleSequenceGenerator generator = new OracleSequenceGenerator();
-    generator.configure(null, props, new Oracle.Oracle10gWithDecimalDialect());
-    assertThat(generator.getSequenceName(), is("MY_TABLE_SEQ"));
-  }
-
-}
diff --git a/sonar-core/src/test/java/org/sonar/jpa/dialect/OracleTest.java b/sonar-core/src/test/java/org/sonar/jpa/dialect/OracleTest.java
deleted file mode 100644 (file)
index b7c549d..0000000
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
- */
-package org.sonar.jpa.dialect;
-
-import org.hamcrest.core.Is;
-import org.junit.Test;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertThat;
-
-public class OracleTest {
-  @Test
-  public void matchesJdbcURL() {
-    assertThat(new Oracle().matchesJdbcURL("jdbc:oracle:thin:@localhost/XE"), is(true));
-    assertThat(new Oracle().matchesJdbcURL("jdbc:hsql:foo"), is(false));
-  }
-
-  /**
-   * Avoid conflicts with other schemas
-   */
-  @Test
-  public void shouldChangeOracleSchema() {
-    String initStatement = new Oracle().getConnectionInitStatement("my_schema");
-
-    assertThat(initStatement, Is.is("ALTER SESSION SET CURRENT_SCHEMA = \"my_schema\""));
-  }
-
-  @Test
-  public void shouldNotChangeOracleSchemaByDefault() {
-    assertNull(new Oracle().getConnectionInitStatement(null));
-  }
-
-
-}
diff --git a/sonar-core/src/test/java/org/sonar/jpa/dialect/PostgreSQLSequenceGeneratorTest.java b/sonar-core/src/test/java/org/sonar/jpa/dialect/PostgreSQLSequenceGeneratorTest.java
deleted file mode 100644 (file)
index b7ed6ce..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
- */
-package org.sonar.jpa.dialect;
-
-import org.hibernate.id.PersistentIdentifierGenerator;
-import org.junit.Test;
-
-import java.util.Properties;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-
-public class PostgreSQLSequenceGeneratorTest {
-
-  @Test
-  public void sequenceNameShouldFollowRailsConventions() {
-    Properties props = new Properties();
-    props.setProperty(PersistentIdentifierGenerator.TABLE, "my_table");
-    props.setProperty(PersistentIdentifierGenerator.PK, "id");
-
-    PostgreSQLSequenceGenerator generator = new PostgreSQLSequenceGenerator();
-    generator.configure(null, props, new PostgreSql.PostgreSQLWithDecimalDialect());
-    assertThat(generator.getSequenceName(), is("my_table_id_seq"));
-  }
-
-}
diff --git a/sonar-core/src/test/java/org/sonar/jpa/dialect/PostgreSqlTest.java b/sonar-core/src/test/java/org/sonar/jpa/dialect/PostgreSqlTest.java
deleted file mode 100644 (file)
index 2116a7e..0000000
+++ /dev/null
@@ -1,51 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
- */
-package org.sonar.jpa.dialect;
-
-import org.hamcrest.core.Is;
-import org.junit.Test;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertThat;
-
-public class PostgreSqlTest {
-  @Test
-  public void matchesJdbcURL() {
-    assertThat(new PostgreSql().matchesJdbcURL("jdbc:postgresql://localhost/sonar"), is(true));
-    assertThat(new PostgreSql().matchesJdbcURL("jdbc:hsql:foo"), is(false));
-  }
-
-  /**
-   * Avoid conflicts with other schemas
-   */
-  @Test
-  public void shouldChangePostgreSearchPath() {
-    String initStatement = new PostgreSql().getConnectionInitStatement("my_schema");
-
-    assertThat(initStatement, Is.is("SET SEARCH_PATH TO my_schema"));
-  }
-
-  @Test
-  public void shouldNotChangePostgreSearchPathByDefault() {
-    assertNull(new PostgreSql().getConnectionInitStatement(null));
-  }
-
-}
index ea30a991ee183ee2d0ebfef08e5b0c56791c9477..2f841413d8dc1e2f5876edc6ae93b9f72735b911 100644 (file)
@@ -28,7 +28,7 @@ import org.dbunit.ext.mysql.MySqlDataTypeFactory;
 import org.dbunit.ext.oracle.Oracle10DataTypeFactory;
 import org.dbunit.ext.postgresql.PostgresqlDataTypeFactory;
 import org.dbunit.operation.DatabaseOperation;
-import org.sonar.jpa.dialect.*;
+import org.sonar.persistence.dialect.*;
 
 import java.util.Arrays;
 import java.util.List;
index 212a749a3db26291fd042b756586dae7474da611..a1a6cdf1ae38e4186c3961f627252353184d30ce 100644 (file)
@@ -23,8 +23,8 @@ import org.apache.commons.dbcp.BasicDataSource;
 import org.hamcrest.core.Is;
 import org.junit.Test;
 import org.sonar.api.config.Settings;
-import org.sonar.jpa.dialect.Oracle;
-import org.sonar.jpa.dialect.PostgreSql;
+import org.sonar.persistence.dialect.Oracle;
+import org.sonar.persistence.dialect.PostgreSql;
 
 import java.sql.SQLException;
 import java.util.Properties;
index f499b6a83d9c5ae484f72f2507086561a41bc3d6..a705e503c913c0ec99ecedfb702ed9a0c3f4f42b 100644 (file)
@@ -22,9 +22,9 @@ package org.sonar.persistence;
 import org.apache.commons.dbcp.BasicDataSource;
 import org.apache.commons.dbcp.BasicDataSourceFactory;
 import org.hibernate.cfg.Environment;
-import org.sonar.jpa.dialect.Derby;
-import org.sonar.jpa.dialect.Dialect;
 import org.sonar.jpa.session.CustomHibernateConnectionProvider;
+import org.sonar.persistence.dialect.Derby;
+import org.sonar.persistence.dialect.Dialect;
 
 import javax.sql.DataSource;
 import java.sql.*;
@@ -95,7 +95,7 @@ public class InMemoryDatabase implements Database {
       DatabaseMetaData meta = connection.getMetaData();
       Statement statement = connection.createStatement();
 
-      ResultSet res = meta.getTables(null, null, null, new String[] { "TABLE" });
+      ResultSet res = meta.getTables(null, null, null, new String[]{"TABLE"});
       while (res.next()) {
         String tableName = res.getString("TABLE_NAME");
         statement.executeUpdate("TRUNCATE TABLE " + tableName);
diff --git a/sonar-core/src/test/java/org/sonar/persistence/dialect/DerbyTest.java b/sonar-core/src/test/java/org/sonar/persistence/dialect/DerbyTest.java
new file mode 100644 (file)
index 0000000..130e68f
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.persistence.dialect;
+
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public class DerbyTest {
+  @Test
+  public void matchesJdbcURL() {
+    assertThat(new Derby().matchesJdbcURL("jdbc:derby:foo"), is(true));
+    assertThat(new Derby().matchesJdbcURL("jdbc:hsql:foo"), is(false));
+  }
+}
diff --git a/sonar-core/src/test/java/org/sonar/persistence/dialect/DialectUtilsTest.java b/sonar-core/src/test/java/org/sonar/persistence/dialect/DialectUtilsTest.java
new file mode 100644 (file)
index 0000000..b0a4786
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.persistence.dialect;
+
+import org.hamcrest.core.Is;
+import org.junit.Test;
+import org.sonar.api.database.DatabaseProperties;
+import org.sonar.api.utils.SonarException;
+
+import static org.junit.Assert.assertThat;
+
+public class DialectUtilsTest {
+
+  @Test
+  public void testFindById() {
+    Dialect d = DialectUtils.find(DatabaseProperties.DIALECT_MYSQL, null);
+    assertThat(d, Is.is(MySql.class));
+  }
+
+  @Test
+  public void testFindByJdbcUrl() {
+    Dialect d = DialectUtils.find(null, "jdbc:mysql:foo:bar");
+    assertThat(d, Is.is(MySql.class));
+  }
+
+  @Test(expected = SonarException.class)
+  public void testFindNoMatch() {
+    DialectUtils.find("foo", "bar");
+  }
+}
diff --git a/sonar-core/src/test/java/org/sonar/persistence/dialect/MsSqlTest.java b/sonar-core/src/test/java/org/sonar/persistence/dialect/MsSqlTest.java
new file mode 100644 (file)
index 0000000..589d0cd
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.persistence.dialect;
+
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public class MsSqlTest {
+
+  @Test
+  public void matchesJdbcURL() {
+    assertThat(new MsSql().matchesJdbcURL("jdbc:jtds:sqlserver://localhost;databaseName=SONAR;SelectMethod=Cursor"), is(true));
+    assertThat(new MsSql().matchesJdbcURL("jdbc:microsoft:sqlserver://localhost:1433;databasename=sonar"), is(true));
+
+    assertThat(new MsSql().matchesJdbcURL("jdbc:hsql:foo"), is(false));
+    assertThat(new MsSql().matchesJdbcURL("jdbc:mysql:foo"), is(false));
+  }
+
+}
diff --git a/sonar-core/src/test/java/org/sonar/persistence/dialect/MySqlTest.java b/sonar-core/src/test/java/org/sonar/persistence/dialect/MySqlTest.java
new file mode 100644 (file)
index 0000000..d326839
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.persistence.dialect;
+
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public class MySqlTest {
+
+  @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(new MySql().matchesJdbcURL("jdbc:hsql:foo"), is(false));
+    assertThat(new MySql().matchesJdbcURL("jdbc:oracle:foo"), is(false));
+  }
+
+}
diff --git a/sonar-core/src/test/java/org/sonar/persistence/dialect/OracleSequenceGeneratorTest.java b/sonar-core/src/test/java/org/sonar/persistence/dialect/OracleSequenceGeneratorTest.java
new file mode 100644 (file)
index 0000000..96a144a
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.persistence.dialect;
+
+import org.hibernate.id.PersistentIdentifierGenerator;
+import org.junit.Test;
+
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public class OracleSequenceGeneratorTest {
+
+  @Test
+  public void sequenceNameShouldFollowRailsConventions() {
+    Properties props = new Properties();
+    props.setProperty(PersistentIdentifierGenerator.TABLE, "my_table");
+    props.setProperty(PersistentIdentifierGenerator.PK, "id");
+
+    OracleSequenceGenerator generator = new OracleSequenceGenerator();
+    generator.configure(null, props, new Oracle.Oracle10gWithDecimalDialect());
+    assertThat(generator.getSequenceName(), is("MY_TABLE_SEQ"));
+  }
+
+}
diff --git a/sonar-core/src/test/java/org/sonar/persistence/dialect/OracleTest.java b/sonar-core/src/test/java/org/sonar/persistence/dialect/OracleTest.java
new file mode 100644 (file)
index 0000000..43ccd7f
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.persistence.dialect;
+
+import org.hamcrest.core.Is;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+
+public class OracleTest {
+  @Test
+  public void matchesJdbcURL() {
+    assertThat(new Oracle().matchesJdbcURL("jdbc:oracle:thin:@localhost/XE"), is(true));
+    assertThat(new Oracle().matchesJdbcURL("jdbc:hsql:foo"), is(false));
+  }
+
+  /**
+   * Avoid conflicts with other schemas
+   */
+  @Test
+  public void shouldChangeOracleSchema() {
+    String initStatement = new Oracle().getConnectionInitStatement("my_schema");
+
+    assertThat(initStatement, Is.is("ALTER SESSION SET CURRENT_SCHEMA = \"my_schema\""));
+  }
+
+  @Test
+  public void shouldNotChangeOracleSchemaByDefault() {
+    assertNull(new Oracle().getConnectionInitStatement(null));
+  }
+
+
+}
diff --git a/sonar-core/src/test/java/org/sonar/persistence/dialect/PostgreSQLSequenceGeneratorTest.java b/sonar-core/src/test/java/org/sonar/persistence/dialect/PostgreSQLSequenceGeneratorTest.java
new file mode 100644 (file)
index 0000000..8dc84f4
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.persistence.dialect;
+
+import org.hibernate.id.PersistentIdentifierGenerator;
+import org.junit.Test;
+
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public class PostgreSQLSequenceGeneratorTest {
+
+  @Test
+  public void sequenceNameShouldFollowRailsConventions() {
+    Properties props = new Properties();
+    props.setProperty(PersistentIdentifierGenerator.TABLE, "my_table");
+    props.setProperty(PersistentIdentifierGenerator.PK, "id");
+
+    PostgreSQLSequenceGenerator generator = new PostgreSQLSequenceGenerator();
+    generator.configure(null, props, new PostgreSql.PostgreSQLWithDecimalDialect());
+    assertThat(generator.getSequenceName(), is("my_table_id_seq"));
+  }
+
+}
diff --git a/sonar-core/src/test/java/org/sonar/persistence/dialect/PostgreSqlTest.java b/sonar-core/src/test/java/org/sonar/persistence/dialect/PostgreSqlTest.java
new file mode 100644 (file)
index 0000000..84647a9
--- /dev/null
@@ -0,0 +1,51 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2008-2011 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * Sonar is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
+ */
+package org.sonar.persistence.dialect;
+
+import org.hamcrest.core.Is;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+
+public class PostgreSqlTest {
+  @Test
+  public void matchesJdbcURL() {
+    assertThat(new PostgreSql().matchesJdbcURL("jdbc:postgresql://localhost/sonar"), is(true));
+    assertThat(new PostgreSql().matchesJdbcURL("jdbc:hsql:foo"), is(false));
+  }
+
+  /**
+   * Avoid conflicts with other schemas
+   */
+  @Test
+  public void shouldChangePostgreSearchPath() {
+    String initStatement = new PostgreSql().getConnectionInitStatement("my_schema");
+
+    assertThat(initStatement, Is.is("SET SEARCH_PATH TO my_schema"));
+  }
+
+  @Test
+  public void shouldNotChangePostgreSearchPathByDefault() {
+    assertNull(new PostgreSql().getConnectionInitStatement(null));
+  }
+
+}
index d0ef238566f4490bc0c7c6351ce4a43a37cc4280..b4f4ac838dd9b6d824c63ba4d96ad78fecc14153 100644 (file)
@@ -20,9 +20,9 @@
 package org.sonar.server.platform;
 
 import org.junit.Test;
-import org.sonar.jpa.dialect.Dialect;
-import org.sonar.jpa.dialect.MySql;
 import org.sonar.jpa.session.DatabaseConnector;
+import org.sonar.persistence.dialect.Dialect;
+import org.sonar.persistence.dialect.MySql;
 import org.sonar.test.TestUtils;
 
 import java.io.File;