Skip to content

Commit

Permalink
#21 Exception when hibernate returns oracle varchar2(n char)
Browse files Browse the repository at this point in the history
  • Loading branch information
nvoxland committed Nov 14, 2013
1 parent 40e3360 commit 20f7a2c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import liquibase.structure.core.DataType;
import liquibase.structure.core.Schema;
import liquibase.structure.core.Table;
import liquibase.util.StringUtils;
import org.hibernate.cfg.Configuration;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.Mapping;
Expand All @@ -20,7 +21,7 @@

public class TableSnapshotGenerator extends HibernateSnapshotGenerator {

private final static Pattern pattern = Pattern.compile("([^\\(]*)\\s*\\(?\\s*(\\d*)?\\s*,?\\s*(\\d*)?\\s*\\)?");
private final static Pattern pattern = Pattern.compile("([^\\(]*)\\s*\\(?\\s*(\\d*)?\\s*,?\\s*(\\d*)?\\s*([^\\(]*?)\\)?");

public TableSnapshotGenerator() {
super(Table.class, new Class[]{Schema.class});
Expand Down Expand Up @@ -52,21 +53,13 @@ protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot
// TODO autoincrement

String hibernateType = hibernateColumn.getSqlType(dialect, mapping);
LOG.info("Found column " + column.getName() + " " + hibernateType);
Matcher matcher = pattern.matcher(hibernateColumn.getSqlType(dialect, mapping));
if (!matcher.matches())
throw new DatabaseException("Unable to find column data type for column " + column.getName());
DataType dataType = new DataType(matcher.group(1));
if (matcher.group(3).isEmpty()) {
if (!matcher.group(2).isEmpty())
dataType.setColumnSize(Integer.parseInt(matcher.group(2)));
} else {
dataType.setColumnSize(Integer.parseInt(matcher.group(2)));
dataType.setDecimalDigits(Integer.parseInt(matcher.group(3)));
DataType dataType = toDataType(hibernateType, hibernateColumn.getSqlTypeCode());
if (dataType == null) {
throw new DatabaseException("Unable to find column data type for column " + hibernateColumn.getName());
}

dataType.setDataTypeId(hibernateColumn.getSqlTypeCode());
column.setType(dataType);
LOG.info("Found column " + column.getName() + " " + column.getType().toString());

column.setRemarks(hibernateColumn.getComment());
column.setDefaultValue(hibernateColumn.getDefaultValue());
Expand All @@ -88,6 +81,31 @@ protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot
return table;
}

protected DataType toDataType(String hibernateType, int sqlTypeCode) throws DatabaseException {
Matcher matcher = pattern.matcher(hibernateType);
if (!matcher.matches()) {
return null;
}
DataType dataType = new DataType(matcher.group(1));
if (matcher.group(3).isEmpty()) {
if (!matcher.group(2).isEmpty())
dataType.setColumnSize(Integer.parseInt(matcher.group(2)));
} else {
dataType.setColumnSize(Integer.parseInt(matcher.group(2)));
dataType.setDecimalDigits(Integer.parseInt(matcher.group(3)));
}

String extra = StringUtils.trimToNull(matcher.group(4));
if (extra != null) {
if (extra.equalsIgnoreCase("char")) {
dataType.setColumnSizeUnit(DataType.ColumnSizeUnit.CHAR);
}
}

dataType.setDataTypeId(sqlTypeCode);
return dataType;
}

@Override
protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException {
if (!snapshot.getSnapshotControl().shouldInclude(Table.class)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package liquibase.ext.hibernate.snapshot;

import junit.framework.Assert;
import liquibase.exception.DatabaseException;
import liquibase.structure.core.DataType;
import org.junit.Test;

import java.sql.Types;

import static junit.framework.Assert.assertEquals;
import static junit.framework.TestCase.assertNull;

public class TableSnapshotGeneratorTest {

@Test
public void toDataType() throws DatabaseException {
TableSnapshotGenerator tableSnapshotGenerator = new TableSnapshotGenerator();
DataType varchar = tableSnapshotGenerator.toDataType("varchar(255)", Types.VARCHAR);
assertEquals("varchar", varchar.getTypeName());
assertEquals(255, varchar.getColumnSize().intValue());
assertEquals(Types.VARCHAR, varchar.getDataTypeId().intValue());
assertNull(varchar.getColumnSizeUnit());

DataType intType = tableSnapshotGenerator.toDataType("integer", Types.INTEGER);
assertEquals("integer", intType.getTypeName());

DataType varcharChar = tableSnapshotGenerator.toDataType("varchar2(30 char)", Types.INTEGER);
assertEquals("varchar2", varcharChar.getTypeName());
assertEquals(30, varcharChar.getColumnSize().intValue());
assertEquals(DataType.ColumnSizeUnit.CHAR, varcharChar.getColumnSizeUnit());

}
}

0 comments on commit 20f7a2c

Please sign in to comment.