Skip to content

Commit

Permalink
HBX-2390: Create a JBoss Tools adaptation layer in Hibernate Tools
Browse files Browse the repository at this point in the history
- Create interface 'org.hibernate.tool.orm.jbt.wrp.ColumnWrapper'
  - Change class 'org.hibernate.tool.orm.jbt.wrp.ColumnWrapper' into 'org.hibernate.tool.orm.jbt.wrp.DelegatingColumnWrapperImpl' that extends Column and implemnts ColumnWrapper
  - Modify method 'org.hibernate.tool.orm.jbt.wrp.DelegatingTableWrapperImpl# getPrimaryKey()' to wrap the resulting PrimaryKey value
  - Modify method 'org.hibernate.tool.orm.jbt.wrp.DelegatingTableWrapperImpl#getColumnIterator()' to wrap the resulting columns
  - Modify 'next()' method in return value of 'org.hibernate.tool.orm.jbt.wrp.DelegatingTableWrapperImpl#getForeignKeyIterator()' and wrap the resulting ForeignKey instance
  - Modify interface method 'org.hibernate.tool.orm.jbt.wrp.TypeWrapperFactory.TypeWrapper#getAssociatedEntityName()'
  - Handle 'getType' case in 'org.hibernate.tool.orm.jbt.wrp.ValueWrapperFactory.ValueWrapperInvocationHandler#invoke(...)'
  - Modify method 'org.hibernate.tool.orm.jbt.wrp.WrapperFactory#createColumnWrapper(String)'
  - Modify test cases in class 'org.hibernate.tool.orm.jbt.wrp.ColumnWrapperTest'
  - Modify test case 'org.hibernate.tool.orm.jbt.wrp.TypeWrapperTest#testGetAssociatedEntityName()'
  - Modify test case 'org.hibernate.tool.orm.jbt.wrp.WrapperFactoryTest#testCreateColumnWrapper()'

Signed-off-by: Koen Aers <[email protected]>
  • Loading branch information
koentsje committed Jul 3, 2023
1 parent d5c5a26 commit f33c4b2
Show file tree
Hide file tree
Showing 13 changed files with 306 additions and 100 deletions.
89 changes: 17 additions & 72 deletions jbt/src/main/java/org/hibernate/tool/orm/jbt/wrp/ColumnWrapper.java
Original file line number Diff line number Diff line change
@@ -1,81 +1,26 @@
package org.hibernate.tool.orm.jbt.wrp;

import java.util.HashMap;
import java.util.Map;

import org.hibernate.boot.Metadata;
import org.hibernate.boot.internal.MetadataImpl;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.dialect.spi.DialectFactory;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Value;
import org.hibernate.tool.orm.jbt.util.MetadataHelper;
import org.hibernate.type.spi.TypeConfiguration;

public class ColumnWrapper extends Column {

private static final int DEFAULT_LENGTH = 255;
private static final int DEFAULT_PRECISION = 19;
private static final int DEFAULT_SCALE = 2;

public ColumnWrapper(String name) {
super(name);
}

public String getSqlType(Configuration configuration) {
Metadata metadata = MetadataHelper.getMetadata(configuration);
TypeConfiguration tc = ((MetadataImpl)metadata).getTypeConfiguration();
return super.getSqlType(tc, buildDialect(configuration), metadata);
}

public Long getLength() {
Long length = super.getLength();
return length == null ? Integer.MIN_VALUE : length.longValue();
}

public int getDefaultLength() {
return DEFAULT_LENGTH;
}

public Integer getPrecision() {
Integer precision = super.getPrecision();
return precision == null ? Integer.MIN_VALUE : precision.intValue();
}

public int getDefaultPrecision() {
return DEFAULT_PRECISION;
}

public Integer getScale() {
Integer scale = super.getScale();
return scale == null ? Integer.MIN_VALUE : scale.intValue();
}

public int getDefaultScale() {
return DEFAULT_SCALE;
}

@Override
public Value getValue() {
Value val = super.getValue();
if (val != null) {
val = ValueWrapperFactory.createValueWrapper(val);
}
return val;
}

private Dialect buildDialect(Configuration configuration) {
Map<String, Object> dialectPropertyMap = new HashMap<String, Object>();
dialectPropertyMap.put(
AvailableSettings.DIALECT,
configuration.getProperty(AvailableSettings.DIALECT));
DialectFactory df = configuration
.getStandardServiceRegistryBuilder()
.build()
.getService(DialectFactory.class);
return df.buildDialect(dialectPropertyMap, null);
}
public interface ColumnWrapper extends Wrapper {

@Override Column getWrappedObject();
String getName();
Integer getSqlTypeCode();
String getSqlType();
Long getLength();
int getDefaultLength();
Integer getPrecision();
int getDefaultPrecision();
Integer getScale();
int getDefaultScale();
boolean isNullable();
Value getValue();
boolean isUnique();
String getSqlType(Configuration configuration);
void setSqlType(String sqlType);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package org.hibernate.tool.orm.jbt.wrp;

import java.util.HashMap;
import java.util.Map;

import org.hibernate.boot.Metadata;
import org.hibernate.boot.internal.MetadataImpl;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.dialect.spi.DialectFactory;
import org.hibernate.mapping.Column;
import org.hibernate.mapping.Value;
import org.hibernate.tool.orm.jbt.util.MetadataHelper;
import org.hibernate.type.spi.TypeConfiguration;

public class DelegatingColumnWrapperImpl extends Column implements ColumnWrapper {

private static final int DEFAULT_LENGTH = 255;
private static final int DEFAULT_PRECISION = 19;
private static final int DEFAULT_SCALE = 2;

private Column delegate = null;

public DelegatingColumnWrapperImpl(Column c) {
delegate = c;
}

@Override
public Column getWrappedObject() {
return delegate;
}

@Override
public String getSqlType(Configuration configuration) {
Metadata metadata = MetadataHelper.getMetadata(configuration);
TypeConfiguration tc = ((MetadataImpl)metadata).getTypeConfiguration();
return delegate.getSqlType(tc, buildDialect(configuration), metadata);
}

@Override
public Long getLength() {
Long length = delegate.getLength();
return length == null ? Integer.MIN_VALUE : length.longValue();
}

@Override
public int getDefaultLength() {
return DEFAULT_LENGTH;
}

@Override
public Integer getPrecision() {
Integer precision = delegate.getPrecision();
return precision == null ? Integer.MIN_VALUE : precision.intValue();
}

@Override
public int getDefaultPrecision() {
return DEFAULT_PRECISION;
}

@Override
public Integer getScale() {
Integer scale = delegate.getScale();
return scale == null ? Integer.MIN_VALUE : scale.intValue();
}

@Override
public int getDefaultScale() {
return DEFAULT_SCALE;
}

@Override
public Value getValue() {
Value val = delegate.getValue();
if (val != null) {
val = ValueWrapperFactory.createValueWrapper(val);
}
return val;
}

@Override
public String getName() {
return delegate.getName();
}

@Override
public Integer getSqlTypeCode() {
return delegate.getSqlTypeCode();
}

@Override
public String getSqlType() {
return delegate.getSqlType();
}

@Override
public boolean isNullable() {
return delegate.isNullable();
}

@Override
public boolean isUnique() {
return delegate.isUnique();
}

@Override
public void setSqlType(String sqlType) {
delegate.setSqlType(sqlType);
}

@Override
public boolean equals(Object o) {
if (!(o instanceof DelegatingColumnWrapperImpl)) {
return false;
} else {
return ((DelegatingColumnWrapperImpl)o).getWrappedObject().equals(delegate);
}
}


private Dialect buildDialect(Configuration configuration) {
Map<String, Object> dialectPropertyMap = new HashMap<String, Object>();
dialectPropertyMap.put(
AvailableSettings.DIALECT,
configuration.getProperty(AvailableSettings.DIALECT));
DialectFactory df = configuration
.getStandardServiceRegistryBuilder()
.build()
.getService(DialectFactory.class);
return df.buildDialect(dialectPropertyMap, null);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.hibernate.tool.orm.jbt.wrp;

import java.util.Iterator;
import java.util.List;

import org.hibernate.mapping.Column;
import org.hibernate.mapping.ForeignKey;
import org.hibernate.mapping.Table;

public class DelegatingForeignKeyWrapperImpl extends ForeignKey implements ForeignKeyWrapper {

private ForeignKey delegate = null;

public DelegatingForeignKeyWrapperImpl(ForeignKey fk) {
delegate = fk;
}

@Override
public ForeignKey getWrappedObject() {
return delegate;
}

@Override
public Iterator<Column> columnIterator() {
return delegate.getColumns().iterator();
}

@Override
public Table getReferencedTable() {
Table result = delegate.getReferencedTable();
return result == null ? null : new DelegatingTableWrapperImpl(result);
}

@Override
public boolean isReferenceToPrimaryKey() {
return delegate.isReferenceToPrimaryKey();
}

@Override
public List<Column> getReferencedColumns() {
return delegate.getReferencedColumns();
}

@Override
public boolean containsColumn(Column column) {
return delegate.containsColumn(column);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.hibernate.tool.orm.jbt.wrp;

import java.util.Iterator;

import org.hibernate.mapping.Column;
import org.hibernate.mapping.PrimaryKey;

public class DelegatingPrimaryKeyWrapperImpl extends PrimaryKey implements PrimaryKeyWrapper {

private PrimaryKey delegate = null;

public DelegatingPrimaryKeyWrapperImpl(PrimaryKey pk) {
super(pk.getTable());
delegate = pk;
}

@Override
public PrimaryKey getWrappedObject() {
return delegate;
}

@Override
public Iterator<Column> columnIterator() {
return delegate.getColumns().iterator();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,23 @@ public String getSchema() {

@Override
public PrimaryKey getPrimaryKey() {
return delegate.getPrimaryKey();
PrimaryKey result = delegate.getPrimaryKey();
return result == null ? null : new DelegatingPrimaryKeyWrapperImpl(result);
}

@Override
public Iterator<Column> getColumnIterator() {
return delegate.getColumnIterator();
final Iterator<Column> iterator = delegate.getColumnIterator();
return new Iterator<Column>() {
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public Column next() {
return new DelegatingColumnWrapperImpl(iterator.next());
}
};
}

@Override
Expand All @@ -67,7 +78,7 @@ public boolean hasNext() {
}
@Override
public ForeignKey next() {
return (ForeignKey)ForeignKeyWrapperFactory.createForeinKeyWrapper(iterator.next());
return new DelegatingForeignKeyWrapperImpl(iterator.next());
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.hibernate.tool.orm.jbt.wrp;

import java.util.Iterator;
import java.util.List;

import org.hibernate.mapping.Column;
import org.hibernate.mapping.ForeignKey;
import org.hibernate.mapping.Table;

public interface ForeignKeyWrapper extends Wrapper {

@Override ForeignKey getWrappedObject();
Table getReferencedTable();
Iterator<Column> columnIterator();
boolean isReferenceToPrimaryKey();
List<Column> getReferencedColumns();
boolean containsColumn(Column column);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.hibernate.tool.orm.jbt.wrp;

import java.util.Iterator;
import java.util.List;

import org.hibernate.mapping.Column;
import org.hibernate.mapping.PrimaryKey;
import org.hibernate.mapping.Table;

public interface PrimaryKeyWrapper extends Wrapper {

@Override PrimaryKey getWrappedObject();
void addColumn(Column column);
int getColumnSpan();
List<Column> getColumns();
Column getColumn(int i);
Table getTable();
boolean containsColumn(Column column);
Iterator<Column> columnIterator();
String getName();

}
Loading

0 comments on commit f33c4b2

Please sign in to comment.