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();
}
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());
}
}
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;
}
Aggregations