From: Simon Brandhof Date: Fri, 16 Dec 2011 18:39:40 +0000 (+0100) Subject: Move org.sonar.jpa.dialect to org.sonar.persistence.dialect and refactor DialectRepos... X-Git-Tag: 2.13~86 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=64de319d4bf2003788ed944efa8be014e01b0b48;p=sonarqube.git Move org.sonar.jpa.dialect to org.sonar.persistence.dialect and refactor DialectRepository --- 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 index cebb9e0a2b1..00000000000 --- a/sonar-core/src/main/java/org/sonar/jpa/dialect/Derby.java +++ /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 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 index a33df55a692..00000000000 --- a/sonar-core/src/main/java/org/sonar/jpa/dialect/Dialect.java +++ /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 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 index 80394a8834a..00000000000 --- a/sonar-core/src/main/java/org/sonar/jpa/dialect/DialectRepository.java +++ /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 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() { - 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() { - 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 dialects, Predicate predicate) { - try { - return Iterators.find(dialects.iterator(), predicate); - } catch (NoSuchElementException ex) { - return null; - } - } - - private static Dialect getDialectByClassname(String dialectId) { - try { - Class dialectClass = (Class) 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 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 index 8cafb79a8c3..00000000000 --- a/sonar-core/src/main/java/org/sonar/jpa/dialect/MsSql.java +++ /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 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 index 4287617cdaa..00000000000 --- a/sonar-core/src/main/java/org/sonar/jpa/dialect/MySql.java +++ /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 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 index eb92ca54b4a..00000000000 --- a/sonar-core/src/main/java/org/sonar/jpa/dialect/Oracle.java +++ /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 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 index 5450f27e6f4..00000000000 --- a/sonar-core/src/main/java/org/sonar/jpa/dialect/OracleSequenceGenerator.java +++ /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 index a04390c2b3e..00000000000 --- a/sonar-core/src/main/java/org/sonar/jpa/dialect/PostgreSQLSequenceGenerator.java +++ /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 index 495dfef2459..00000000000 --- a/sonar-core/src/main/java/org/sonar/jpa/dialect/PostgreSql.java +++ /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 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/main/java/org/sonar/jpa/session/AbstractDatabaseConnector.java b/sonar-core/src/main/java/org/sonar/jpa/session/AbstractDatabaseConnector.java index 6dbcfd8daa1..e0a5d739d68 100644 --- a/sonar-core/src/main/java/org/sonar/jpa/session/AbstractDatabaseConnector.java +++ b/sonar-core/src/main/java/org/sonar/jpa/session/AbstractDatabaseConnector.java @@ -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; diff --git a/sonar-core/src/main/java/org/sonar/jpa/session/DatabaseConnector.java b/sonar-core/src/main/java/org/sonar/jpa/session/DatabaseConnector.java index 72abcf6755a..2f793c3e75d 100644 --- a/sonar-core/src/main/java/org/sonar/jpa/session/DatabaseConnector.java +++ b/sonar-core/src/main/java/org/sonar/jpa/session/DatabaseConnector.java @@ -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(); - + } diff --git a/sonar-core/src/main/java/org/sonar/persistence/Database.java b/sonar-core/src/main/java/org/sonar/persistence/Database.java index 9fd31c7b652..95b9b10b739 100644 --- a/sonar-core/src/main/java/org/sonar/persistence/Database.java +++ b/sonar-core/src/main/java/org/sonar/persistence/Database.java @@ -19,17 +19,17 @@ */ 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(); /** diff --git a/sonar-core/src/main/java/org/sonar/persistence/DefaultDatabase.java b/sonar-core/src/main/java/org/sonar/persistence/DefaultDatabase.java index 9f7c6af21c3..f18e404485e 100644 --- a/sonar-core/src/main/java/org/sonar/persistence/DefaultDatabase.java +++ b/sonar-core/src/main/java/org/sonar/persistence/DefaultDatabase.java @@ -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 index 00000000000..0377f2a0277 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/persistence/dialect/Derby.java @@ -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 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 index 00000000000..8ba4d9b5e55 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/persistence/dialect/Dialect.java @@ -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 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 index 00000000000..672dd86a7fc --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/persistence/dialect/DialectUtils.java @@ -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() { + public boolean apply(Dialect dialect) { + return dialect.matchesJdbcURL(StringUtils.trimToEmpty(jdbcConnectionUrl)); + } + }); + return match; + } + + private static Dialect findById(final String dialectId) { + return findDialect(new Predicate() { + public boolean apply(Dialect dialect) { + return dialect.getId().equals(dialectId); + } + }); + } + + private static Dialect findDialect(Predicate 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 index 00000000000..6a09b80092d --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/persistence/dialect/MsSql.java @@ -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 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 index 00000000000..a35b7f2540c --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/persistence/dialect/MySql.java @@ -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 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 index 00000000000..ae1f5ee69ef --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/persistence/dialect/Oracle.java @@ -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 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 index 00000000000..f1a30f368c3 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/persistence/dialect/OracleSequenceGenerator.java @@ -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 index 00000000000..1c22d307bf8 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/persistence/dialect/PostgreSQLSequenceGenerator.java @@ -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 index 00000000000..67c494fa625 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/persistence/dialect/PostgreSql.java @@ -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 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 index 7da394f4fce..00000000000 --- a/sonar-core/src/test/java/org/sonar/jpa/dialect/DerbyTest.java +++ /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 index 1ee33f89cfb..00000000000 --- a/sonar-core/src/test/java/org/sonar/jpa/dialect/DialectRepositoryTest.java +++ /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 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 index 5554eba67da..00000000000 --- a/sonar-core/src/test/java/org/sonar/jpa/dialect/MsSqlTest.java +++ /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 index a0717686535..00000000000 --- a/sonar-core/src/test/java/org/sonar/jpa/dialect/MySqlTest.java +++ /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 index 05d963f9323..00000000000 --- a/sonar-core/src/test/java/org/sonar/jpa/dialect/OracleSequenceGeneratorTest.java +++ /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 index b7c549d78e5..00000000000 --- a/sonar-core/src/test/java/org/sonar/jpa/dialect/OracleTest.java +++ /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 index b7ed6cee924..00000000000 --- a/sonar-core/src/test/java/org/sonar/jpa/dialect/PostgreSQLSequenceGeneratorTest.java +++ /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 index 2116a7e1ab0..00000000000 --- a/sonar-core/src/test/java/org/sonar/jpa/dialect/PostgreSqlTest.java +++ /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)); - } - -} diff --git a/sonar-core/src/test/java/org/sonar/persistence/DatabaseCommands.java b/sonar-core/src/test/java/org/sonar/persistence/DatabaseCommands.java index ea30a991ee1..2f841413d8d 100644 --- a/sonar-core/src/test/java/org/sonar/persistence/DatabaseCommands.java +++ b/sonar-core/src/test/java/org/sonar/persistence/DatabaseCommands.java @@ -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; diff --git a/sonar-core/src/test/java/org/sonar/persistence/DefaultDatabaseTest.java b/sonar-core/src/test/java/org/sonar/persistence/DefaultDatabaseTest.java index 212a749a3db..a1a6cdf1ae3 100644 --- a/sonar-core/src/test/java/org/sonar/persistence/DefaultDatabaseTest.java +++ b/sonar-core/src/test/java/org/sonar/persistence/DefaultDatabaseTest.java @@ -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; diff --git a/sonar-core/src/test/java/org/sonar/persistence/InMemoryDatabase.java b/sonar-core/src/test/java/org/sonar/persistence/InMemoryDatabase.java index f499b6a83d9..a705e503c91 100644 --- a/sonar-core/src/test/java/org/sonar/persistence/InMemoryDatabase.java +++ b/sonar-core/src/test/java/org/sonar/persistence/InMemoryDatabase.java @@ -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 index 00000000000..130e68f3886 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/persistence/dialect/DerbyTest.java @@ -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 index 00000000000..b0a47867c87 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/persistence/dialect/DialectUtilsTest.java @@ -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 index 00000000000..589d0cd3875 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/persistence/dialect/MsSqlTest.java @@ -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 index 00000000000..d3268392ae4 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/persistence/dialect/MySqlTest.java @@ -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 index 00000000000..96a144a33e4 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/persistence/dialect/OracleSequenceGeneratorTest.java @@ -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 index 00000000000..43ccd7f24a1 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/persistence/dialect/OracleTest.java @@ -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 index 00000000000..8dc84f4336c --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/persistence/dialect/PostgreSQLSequenceGeneratorTest.java @@ -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 index 00000000000..84647a986ff --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/persistence/dialect/PostgreSqlTest.java @@ -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)); + } + +} diff --git a/sonar-server/src/test/java/org/sonar/server/platform/ServerFileSystemTest.java b/sonar-server/src/test/java/org/sonar/server/platform/ServerFileSystemTest.java index d0ef238566f..b4f4ac838dd 100644 --- a/sonar-server/src/test/java/org/sonar/server/platform/ServerFileSystemTest.java +++ b/sonar-server/src/test/java/org/sonar/server/platform/ServerFileSystemTest.java @@ -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;