+++ /dev/null
-/*
- * 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;
- }
- }
-
-}
+++ /dev/null
-/*
- * 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);
-}
+++ /dev/null
-/*
- * 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());
- }
-}
+++ /dev/null
-/*
- * 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;
- }
-}
-
+++ /dev/null
-/*
- * 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;
- }
-}
+++ /dev/null
-/*
- * 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;
- }
-}
+++ /dev/null
-/*
- * 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);
- }
-
-}
+++ /dev/null
-/*
- * 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);
- }
-
-}
+++ /dev/null
-/*
- * 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;
- }
-}
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;
*/
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;
EntityManager createEntityManager();
int getDatabaseVersion();
-
+
}
*/
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();
/**
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;
}
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.");
}
--- /dev/null
+/*
+ * 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;
+ }
+ }
+
+}
--- /dev/null
+/*
+ * 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);
+}
--- /dev/null
+/*
+ * 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;
+ }
+ }
+}
--- /dev/null
+/*
+ * 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;
+ }
+}
+
--- /dev/null
+/*
+ * 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;
+ }
+}
--- /dev/null
+/*
+ * 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;
+ }
+}
--- /dev/null
+/*
+ * 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);
+ }
+
+}
--- /dev/null
+/*
+ * 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);
+ }
+
+}
--- /dev/null
+/*
+ * 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;
+ }
+}
+++ /dev/null
-/*
- * 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));
- }
-}
+++ /dev/null
-/*
- * 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";
- }
- }
-
-}
+++ /dev/null
-/*
- * 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));
- }
-
-}
+++ /dev/null
-/*
- * 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));
- }
-
-}
+++ /dev/null
-/*
- * 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"));
- }
-
-}
+++ /dev/null
-/*
- * 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));
- }
-
-
-}
+++ /dev/null
-/*
- * 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"));
- }
-
-}
+++ /dev/null
-/*
- * 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));
- }
-
-}
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;
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;
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.*;
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);
--- /dev/null
+/*
+ * 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));
+ }
+}
--- /dev/null
+/*
+ * 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");
+ }
+}
--- /dev/null
+/*
+ * 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));
+ }
+
+}
--- /dev/null
+/*
+ * 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));
+ }
+
+}
--- /dev/null
+/*
+ * 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"));
+ }
+
+}
--- /dev/null
+/*
+ * 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));
+ }
+
+
+}
--- /dev/null
+/*
+ * 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"));
+ }
+
+}
--- /dev/null
+/*
+ * 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));
+ }
+
+}
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;