package net.kaleidos.hibernate;

import grails.util.Holders;
import net.kaleidos.hibernate.usertype.ArrayType;
import net.kaleidos.hibernate.usertype.HstoreMapType;
import net.kaleidos.hibernate.usertype.JsonMapType;
import net.kaleidos.hibernate.usertype.JsonbMapType;
import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.PostgreSQL81Dialect;
import org.hibernate.id.PersistentIdentifierGenerator;
import org.hibernate.id.SequenceGenerator;
import org.hibernate.type.Type;

import java.sql.Types;
import java.util.Properties;

public class PostgresqlExtensionsDialect extends PostgreSQL81Dialect {

    /**
     * Register postgresql types
     */
    public PostgresqlExtensionsDialect() {
        super();
        registerColumnType(Types.ARRAY, "array");
        registerColumnType(ArrayType.LONG_ARRAY, "int8[]");
        registerColumnType(ArrayType.INTEGER_ARRAY, "int[]");
        registerColumnType(ArrayType.ENUM_INTEGER_ARRAY, "int[]");
        registerColumnType(ArrayType.STRING_ARRAY, "varchar[]");
        registerColumnType(ArrayType.DOUBLE_ARRAY, "float8[]");
        registerColumnType(ArrayType.FLOAT_ARRAY, "float[]");
        registerColumnType(HstoreMapType.SQLTYPE, "hstore");
        registerColumnType(JsonMapType.SQLTYPE, "json");
        registerColumnType(JsonbMapType.SQLTYPE, "jsonb");
    }

    /**
     * Get the native identifier generator class.
     *
     * @return TableNameSequenceGenerator.
     */
    @Override
    public Class<?> getNativeIdentifierGeneratorClass() {
        return TableNameSequenceGenerator.class;
    }

    /**
     * Creates a sequence per table instead of the default behavior of one sequence.
     * From <a href='http://www.hibernate.org/296.html'>http://www.hibernate.org/296.html</a>
     */
    public static class TableNameSequenceGenerator extends SequenceGenerator {

        /**
         * {@inheritDoc}
         * If the parameters do not contain a {@link SequenceGenerator#SEQUENCE} name, we
         * assign one based on the table name.
         */
        @Override
        public void configure(final Type type, final Properties params, final Dialect dialect) {

            Boolean sequencePerTable = (Boolean) Holders.getFlatConfig().get("dataSource.postgresql.extensions.sequence_per_table");

            if ((sequencePerTable == null) || sequencePerTable) {
                if (params.getProperty(SEQUENCE) == null || params.getProperty(SEQUENCE).length() == 0) {
                    String tableName = params.getProperty(PersistentIdentifierGenerator.TABLE);
                    String schemaName = params.getProperty("schemaName");
                    if (schemaName != null) {
                        params.setProperty(PersistentIdentifierGenerator.SCHEMA, schemaName);
                    }
                    if (tableName != null) {
                        params.setProperty(SEQUENCE, "seq_" + tableName);
                    }
                }
            }
            super.configure(type, params, dialect);
        }
    }
}

@@ -2193,18 +2193,8 @@
 le =
- (Boolean)
  Hol
@@ -2205,12 +2205,8 @@
 .get
-Flat
 Conf
@@ -2213,16 +2213,24 @@
 ig().get
+Property
 (%22dataSo
@@ -2279,58 +2279,49 @@
 ble%22
-);%0A%0A            if ((sequencePerTable == null) %7C%7C 
+, Boolean.class, true);%0A%0A            if (
 sequ

Fix #84. Get 'sequence_per_table' value correctly