Search in sources :

Example 6 with ObjectNameNormalizer

use of org.hibernate.boot.model.naming.ObjectNameNormalizer in project hibernate-orm by hibernate.

the class IncrementGenerator method configure.

@Override
public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException {
    returnClass = type.getReturnedClass();
    final JdbcEnvironment jdbcEnvironment = serviceRegistry.getService(JdbcEnvironment.class);
    final ObjectNameNormalizer normalizer = (ObjectNameNormalizer) params.get(PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER);
    String column = params.getProperty("column");
    if (column == null) {
        column = params.getProperty(PersistentIdentifierGenerator.PK);
    }
    column = normalizer.normalizeIdentifierQuoting(column).render(jdbcEnvironment.getDialect());
    String tableList = params.getProperty("tables");
    if (tableList == null) {
        tableList = params.getProperty(PersistentIdentifierGenerator.TABLES);
    }
    String[] tables = StringHelper.split(", ", tableList);
    final String schema = normalizer.toDatabaseIdentifierText(params.getProperty(PersistentIdentifierGenerator.SCHEMA));
    final String catalog = normalizer.toDatabaseIdentifierText(params.getProperty(PersistentIdentifierGenerator.CATALOG));
    StringBuilder buf = new StringBuilder();
    for (int i = 0; i < tables.length; i++) {
        final String tableName = normalizer.toDatabaseIdentifierText(tables[i]);
        if (tables.length > 1) {
            buf.append("select max(").append(column).append(") as mx from ");
        }
        buf.append(Table.qualify(catalog, schema, tableName));
        if (i < tables.length - 1) {
            buf.append(" union ");
        }
    }
    if (tables.length > 1) {
        buf.insert(0, "( ").append(" ) ids_");
        column = "ids_.mx";
    }
    sql = "select max(" + column + ") from " + buf.toString();
}
Also used : ObjectNameNormalizer(org.hibernate.boot.model.naming.ObjectNameNormalizer) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)

Example 7 with ObjectNameNormalizer

use of org.hibernate.boot.model.naming.ObjectNameNormalizer in project hibernate-orm by hibernate.

the class Ejb3JoinColumn method addColumnBinding.

@Override
protected void addColumnBinding(SimpleValue value) {
    if (StringHelper.isEmpty(mappedBy)) {
        // was the column explicitly quoted in the mapping/annotation
        // TODO: in metamodel, we need to better split global quoting and explicit quoting w/ respect to logical names
        boolean isLogicalColumnQuoted = StringHelper.isQuoted(getLogicalColumnName());
        final ObjectNameNormalizer nameNormalizer = getBuildingContext().getObjectNameNormalizer();
        final String logicalColumnName = nameNormalizer.normalizeIdentifierQuotingAsString(getLogicalColumnName());
        final String referencedColumn = nameNormalizer.normalizeIdentifierQuotingAsString(getReferencedColumn());
        final String unquotedLogColName = StringHelper.unquote(logicalColumnName);
        final String unquotedRefColumn = StringHelper.unquote(referencedColumn);
        String logicalCollectionColumnName = StringHelper.isNotEmpty(unquotedLogColName) ? unquotedLogColName : getPropertyName() + '_' + unquotedRefColumn;
        logicalCollectionColumnName = getBuildingContext().getMetadataCollector().getDatabase().getJdbcEnvironment().getIdentifierHelper().toIdentifier(logicalCollectionColumnName, isLogicalColumnQuoted).render();
        getBuildingContext().getMetadataCollector().addColumnNameBinding(value.getTable(), logicalCollectionColumnName, getMappingColumn());
    }
}
Also used : ObjectNameNormalizer(org.hibernate.boot.model.naming.ObjectNameNormalizer)

Example 8 with ObjectNameNormalizer

use of org.hibernate.boot.model.naming.ObjectNameNormalizer in project hibernate-orm by hibernate.

the class Ejb3Column method buildColumnFromAnnotation.

public static Ejb3Column[] buildColumnFromAnnotation(javax.persistence.Column[] anns, org.hibernate.annotations.Formula formulaAnn, Nullability nullability, PropertyHolder propertyHolder, PropertyData inferredData, String suffixForDefaultColumnName, Map<String, Join> secondaryTables, MetadataBuildingContext context) {
    Ejb3Column[] columns;
    if (formulaAnn != null) {
        Ejb3Column formulaColumn = new Ejb3Column();
        formulaColumn.setFormula(formulaAnn.value());
        formulaColumn.setImplicit(false);
        formulaColumn.setBuildingContext(context);
        formulaColumn.setPropertyHolder(propertyHolder);
        formulaColumn.bind();
        columns = new Ejb3Column[] { formulaColumn };
    } else {
        javax.persistence.Column[] actualCols = anns;
        javax.persistence.Column[] overriddenCols = propertyHolder.getOverriddenColumn(StringHelper.qualify(propertyHolder.getPath(), inferredData.getPropertyName()));
        if (overriddenCols != null) {
            //check for overridden first
            if (anns != null && overriddenCols.length != anns.length) {
                throw new AnnotationException("AttributeOverride.column() should override all columns for now");
            }
            actualCols = overriddenCols.length == 0 ? null : overriddenCols;
            LOG.debugf("Column(s) overridden for property %s", inferredData.getPropertyName());
        }
        if (actualCols == null) {
            columns = buildImplicitColumn(inferredData, suffixForDefaultColumnName, secondaryTables, propertyHolder, nullability, context);
        } else {
            final int length = actualCols.length;
            columns = new Ejb3Column[length];
            for (int index = 0; index < length; index++) {
                final ObjectNameNormalizer normalizer = context.getObjectNameNormalizer();
                final Database database = context.getMetadataCollector().getDatabase();
                final ImplicitNamingStrategy implicitNamingStrategy = context.getBuildingOptions().getImplicitNamingStrategy();
                final PhysicalNamingStrategy physicalNamingStrategy = context.getBuildingOptions().getPhysicalNamingStrategy();
                javax.persistence.Column col = actualCols[index];
                final String sqlType;
                if (col.columnDefinition().equals("")) {
                    sqlType = null;
                } else {
                    sqlType = normalizer.applyGlobalQuoting(col.columnDefinition());
                }
                final String tableName;
                if (StringHelper.isEmpty(col.table())) {
                    tableName = "";
                } else {
                    tableName = database.getJdbcEnvironment().getIdentifierHelper().toIdentifier(col.table()).render();
                //						final Identifier logicalName = database.getJdbcEnvironment()
                //								.getIdentifierHelper()
                //								.toIdentifier( col.table() );
                //						final Identifier physicalName = physicalNamingStrategy.toPhysicalTableName( logicalName );
                //						tableName = physicalName.render( database.getDialect() );
                }
                final String columnName;
                if ("".equals(col.name())) {
                    columnName = null;
                } else {
                    // NOTE : this is the logical column name, not the physical!
                    columnName = database.getJdbcEnvironment().getIdentifierHelper().toIdentifier(col.name()).render();
                }
                Ejb3Column column = new Ejb3Column();
                if (length == 1) {
                    applyColumnDefault(column, inferredData);
                }
                column.setImplicit(false);
                column.setSqlType(sqlType);
                column.setLength(col.length());
                column.setPrecision(col.precision());
                column.setScale(col.scale());
                if (StringHelper.isEmpty(columnName) && !StringHelper.isEmpty(suffixForDefaultColumnName)) {
                    column.setLogicalColumnName(inferredData.getPropertyName() + suffixForDefaultColumnName);
                } else {
                    column.setLogicalColumnName(columnName);
                }
                column.setPropertyName(BinderHelper.getRelativePath(propertyHolder, inferredData.getPropertyName()));
                column.setNullable(col.nullable());
                //TODO force to not null if available? This is a (bad) user choice.
                column.setUnique(col.unique());
                column.setInsertable(col.insertable());
                column.setUpdatable(col.updatable());
                column.setExplicitTableName(tableName);
                column.setPropertyHolder(propertyHolder);
                column.setJoins(secondaryTables);
                column.setBuildingContext(context);
                column.extractDataFromPropertyData(inferredData);
                column.bind();
                columns[index] = column;
            }
        }
    }
    return columns;
}
Also used : ImplicitNamingStrategy(org.hibernate.boot.model.naming.ImplicitNamingStrategy) ObjectNameNormalizer(org.hibernate.boot.model.naming.ObjectNameNormalizer) PhysicalNamingStrategy(org.hibernate.boot.model.naming.PhysicalNamingStrategy) Column(org.hibernate.mapping.Column) Database(org.hibernate.boot.model.relational.Database) AnnotationException(org.hibernate.AnnotationException)

Aggregations

ObjectNameNormalizer (org.hibernate.boot.model.naming.ObjectNameNormalizer)8 ImplicitNamingStrategy (org.hibernate.boot.model.naming.ImplicitNamingStrategy)3 Database (org.hibernate.boot.model.relational.Database)3 Identifier (org.hibernate.boot.model.naming.Identifier)2 ImplicitBasicColumnNameSource (org.hibernate.boot.model.naming.ImplicitBasicColumnNameSource)2 PhysicalNamingStrategy (org.hibernate.boot.model.naming.PhysicalNamingStrategy)2 Column (org.hibernate.mapping.Column)2 Properties (java.util.Properties)1 JoinColumn (javax.persistence.JoinColumn)1 PrimaryKeyJoinColumn (javax.persistence.PrimaryKeyJoinColumn)1 AnnotationException (org.hibernate.AnnotationException)1 Metadata (org.hibernate.boot.Metadata)1 MetadataSources (org.hibernate.boot.MetadataSources)1 AttributePath (org.hibernate.boot.model.source.spi.AttributePath)1 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)1 MetadataBuildingContext (org.hibernate.boot.spi.MetadataBuildingContext)1 JdbcEnvironment (org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)1 SequenceStyleGenerator (org.hibernate.id.enhanced.SequenceStyleGenerator)1 MetadataBuildingContextTestingImpl (org.hibernate.testing.boot.MetadataBuildingContextTestingImpl)1 Before (org.junit.Before)1