Skip to content

Commit

Permalink
Fixed #223 - Persistence module needs to support 'bit' datatype
Browse files Browse the repository at this point in the history
  • Loading branch information
delchev committed Feb 7, 2018
1 parent 564c1f0 commit bc8b167
Show file tree
Hide file tree
Showing 20 changed files with 647 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ public static void executeSingleStatement(Connection connection, String sql, boo
callback.updateDone(preparedStatement.getUpdateCount());
}
} catch (Exception e) {
logger.error(sql);
logger.error(e.getMessage(), e);
callback.error(e);
} finally {
try {
Expand Down
7 changes: 7 additions & 0 deletions modules/database/database-persistence/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@
<version>1.4.193</version>
<scope>test</scope>
</dependency>

<!-- <dependency>
<groupId>com.sybase.jdbc4</groupId>
<artifactId>jconn4</artifactId>
<version>7.07.138.26368</version>
<scope>test</scope>
</dependency> -->

</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.Date;
Expand Down Expand Up @@ -124,6 +126,7 @@ protected void setValuesFromPojo(PersistenceTableModel tableModel, Object pojo,
}
setValue(preparedStatement, i++, dataType, valueObject);
} catch (PersistenceException e) {
logger.error(e.getMessage(), e);
throw new PersistenceException(format("Database type [{0}] not supported (Class: [{1}])", dataType, pojo.getClass()));
}
}
Expand Down Expand Up @@ -239,6 +242,8 @@ protected void setValue(PreparedStatement preparedStatement, int i, String dataT
preparedStatement.setString(i, (String) value);
} else if ((value instanceof Character) || char.class.getCanonicalName().equals(value.getClass().getCanonicalName())) {
preparedStatement.setString(i, new String(new char[] { (char) value }));
} else {
throw new PersistenceException(format("Database type [{0}] cannot be set as [{1}]", dataType, value.getClass().getName()));
}
} else if (DataTypeUtils.isDate(dataType)) {
preparedStatement.setDate(i, (Date) value);
Expand All @@ -253,18 +258,40 @@ protected void setValue(PreparedStatement preparedStatement, int i, String dataT
} else if (DataTypeUtils.isSmallint(dataType)) {
preparedStatement.setShort(i, (Short) value);
} else if (DataTypeUtils.isBigint(dataType)) {
preparedStatement.setLong(i, (Long) value);
if (value instanceof Long) {
preparedStatement.setLong(i, (Long) value);
} else if (value instanceof BigInteger) {
preparedStatement.setLong(i, ((BigInteger) value).longValueExact());
} else {
throw new PersistenceException(format("Database type [{0}] cannot be set as [{1}]", dataType, value.getClass().getName()));
}
} else if (DataTypeUtils.isReal(dataType)) {
preparedStatement.setFloat(i, (Float) value);
} else if (DataTypeUtils.isDouble(dataType)) {
preparedStatement.setDouble(i, (Double) value);
} else if (DataTypeUtils.isBoolean(dataType)) {
preparedStatement.setBoolean(i, (Boolean) value);
} else if (DataTypeUtils.isDecimal(dataType)) {
preparedStatement.setDouble(i, (Double) value);
if (value instanceof Double) {
preparedStatement.setDouble(i, (Double) value);
} else if (value instanceof BigDecimal) {
preparedStatement.setBigDecimal(i, ((BigDecimal) value));
} else {
throw new PersistenceException(format("Database type [{0}] cannot be set as [{1}]", dataType, value.getClass().getName()));
}
} else if (DataTypeUtils.isBlob(dataType)) {
byte[] bytes = (byte[]) value;
preparedStatement.setBinaryStream(i, new ByteArrayInputStream(bytes), bytes.length);
} else if (DataTypeUtils.isBit(dataType)) {
if ((value instanceof Boolean) || Boolean.TYPE.isInstance(value)) {
preparedStatement.setBoolean(i, (Boolean) value);
} else if (value instanceof Byte || Byte.TYPE.isInstance(value)) {
preparedStatement.setBoolean(i, ((Byte) value == 1));
} else if (value instanceof Integer || Integer.TYPE.isInstance(value)) {
preparedStatement.setBoolean(i, ((Integer) value == 1));
} else {
throw new PersistenceException(format("Database type [{0}] cannot be set as [{1}]", dataType, value.getClass().getName()));
}
}

else {
Expand Down Expand Up @@ -336,40 +363,91 @@ protected void setValueToPojo(Object pojo, Object value, PersistenceTableColumnM
throw new IllegalStateException("The annotation @Enumerated is misused, the value is unknown.");
}
}
if (field.getType().equals(byte.class) || field.getType().equals(Byte.class)) {
if (value instanceof Integer) {
value = ((Integer) value).byteValue();
}
}
if (field.getType().equals(int.class) || field.getType().equals(Integer.class)) {
if (value instanceof Long) {
value = ((Long) value).intValue();
}
}
value = byteAdaptation(value, field);
value = intAdaptation(value, field);
value = blobAdaptation(value);
value = charAdaptation(value, field);
value = booleanAdaptation(value, field);
value = bigIntegerAdaptation(value, field);
value = shortAdaptation(value, field);

if (getEntityManagerInterceptor() != null) {
value = getEntityManagerInterceptor().onSetValueAfterQuery(pojo, field, value);
}
if (value instanceof Blob) {
value = IOUtils.toByteArray(((Blob) value).getBinaryStream());
}
if (field.getType().equals(char.class) || field.getType().equals(Character.class)) {
if ((value instanceof String) && (((String) value).length() <= 1)) {
value = new Character(((String) value).charAt(0));
} else {
throw new IllegalStateException("Trying to set a multi-character string to a single character field.");
}
}
if (field.getType().equals(boolean.class) || field.getType().equals(Boolean.class)) {
if (value instanceof Short) {
value = Boolean.valueOf(((Short) value) != 0);
}
}

field.set(pojo, value);
} finally {
resetAccessible(field, oldAccessible);
}
}

private Object shortAdaptation(Object value, Field field) {
if (field.getType().equals(short.class) || field.getType().equals(Short.class)) {
if (value instanceof Long) {
value = ((Long) value).shortValue();
} else if (value instanceof Integer) {
value = ((Integer) value).shortValue();
} else if (value instanceof Byte) {
value = ((Byte) value).shortValue();
}
}
return value;
}

private Object bigIntegerAdaptation(Object value, Field field) {
if (field.getType().equals(BigInteger.class)) {
if (value instanceof Long) {
value = BigInteger.valueOf(((Long) value));
}
}
return value;
}

private Object booleanAdaptation(Object value, Field field) {
if (field.getType().equals(boolean.class) || field.getType().equals(Boolean.class)) {
if (value instanceof Short) {
value = Boolean.valueOf(((Short) value) != 0);
}
}
return value;
}

private Object charAdaptation(Object value, Field field) {
if (field.getType().equals(char.class) || field.getType().equals(Character.class)) {
if ((value instanceof String) && (((String) value).length() <= 1)) {
value = new Character(((String) value).charAt(0));
} else {
throw new IllegalStateException("Trying to set a multi-character string to a single character field.");
}
}
return value;
}

private Object blobAdaptation(Object value) throws IOException, SQLException {
if (value instanceof Blob) {
value = IOUtils.toByteArray(((Blob) value).getBinaryStream());
}
return value;
}

private Object intAdaptation(Object value, Field field) {
if (field.getType().equals(int.class) || field.getType().equals(Integer.class)) {
if (value instanceof Long) {
value = ((Long) value).intValue();
}
}
return value;
}

private Object byteAdaptation(Object value, Field field) {
if (field.getType().equals(byte.class) || field.getType().equals(Byte.class)) {
if (value instanceof Integer) {
value = ((Integer) value).byteValue();
}
}
return value;
}

/**
* Gets the value from pojo.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ public int delete(Connection connection, PersistenceTableModel tableModel, Class
setValue(preparedStatement, 1, id);
return preparedStatement.executeUpdate();
} catch (Exception e) {
logger.error(sql);
logger.error(e.getMessage(), e);
throw new PersistenceException(sql, e);
} finally {
closePreparedStatement(preparedStatement);
Expand Down Expand Up @@ -143,6 +145,8 @@ public int deleteAll(Connection connection, PersistenceTableModel tableModel, Cl
preparedStatement = openPreparedStatement(connection, sql);
return preparedStatement.executeUpdate();
} catch (Exception e) {
logger.error(sql);
logger.error(e.getMessage(), e);
throw new PersistenceException(sql, e);
} finally {
closePreparedStatement(preparedStatement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public int execute(Connection connection, String sql, List<Object> values) {
}
return preparedStatement.executeUpdate();
} catch (Exception e) {
logger.error(sql);
logger.error(e.getMessage(), e);
throw new PersistenceException(e);
} finally {
closePreparedStatement(preparedStatement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ public Object insert(Connection connection, PersistenceTableModel tableModel, T
}
}
} catch (Exception e) {
logger.error(sql);
logger.error(e.getMessage(), e);
throw new PersistenceException(sql, e);
} finally {
closePreparedStatement(preparedStatement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ public int update(Connection connection, PersistenceTableModel tableModel, T poj
setValue(preparedStatement, tableModel.getColumns().size(), id);
return preparedStatement.executeUpdate();
} catch (Exception e) {
logger.error(sql);
logger.error(e.getMessage(), e);
throw new PersistenceException(sql, e);
} finally {
closePreparedStatement(preparedStatement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public int create(Connection connection, PersistenceTableModel tableModel) throw
preparedStatement = openPreparedStatement(connection, sql);
result = preparedStatement.executeUpdate();
} catch (Exception e) {
logger.error(sql);
logger.error(e.getMessage(), e);
throw new PersistenceException(sql, e);
} finally {
closePreparedStatement(preparedStatement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public int drop(Connection connection, PersistenceTableModel tableModel) throws
preparedStatement = openPreparedStatement(connection, sql);
result = preparedStatement.executeUpdate();
} catch (Exception e) {
logger.error(sql);
logger.error(e.getMessage(), e);
throw new PersistenceException(sql, e);
} finally {
closePreparedStatement(preparedStatement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected String generateScript(Connection connection, PersistenceTableModel tab
columnModel.isNullable(), columnModel.isUnique(), columnModel.isIdentity());
break;
case CHAR:
createTableBuilder.columnVarchar(columnModel.getName(), columnModel.getLength(), columnModel.isPrimaryKey(),
createTableBuilder.columnChar(columnModel.getName(), columnModel.getLength(), columnModel.isPrimaryKey(),
columnModel.isNullable(), columnModel.isUnique(), columnModel.isIdentity());
break;
case DATE:
Expand All @@ -78,7 +78,7 @@ protected String generateScript(Connection connection, PersistenceTableModel tab
columnModel.isUnique(), columnModel.isIdentity());
break;
case TINYINT:
createTableBuilder.columnInteger(columnModel.getName(), columnModel.isPrimaryKey(), columnModel.isNullable(),
createTableBuilder.columnTinyint(columnModel.getName(), columnModel.isPrimaryKey(), columnModel.isNullable(),
columnModel.isUnique());
break;
case BIGINT:
Expand Down Expand Up @@ -108,6 +108,9 @@ protected String generateScript(Connection connection, PersistenceTableModel tab
createTableBuilder.columnDecimal(columnModel.getName(), columnModel.getPrecision(), columnModel.getScale(),
columnModel.isPrimaryKey(), columnModel.isNullable(), columnModel.isUnique(), columnModel.isIdentity());
break;
case BIT:
createTableBuilder.columnBit(columnModel.getName(), columnModel.isNullable());
break;
}
}

Expand Down Expand Up @@ -137,6 +140,8 @@ public int create(Connection connection, PersistenceTableModel tableModel) throw
preparedStatement = openPreparedStatement(connection, sql);
result = preparedStatement.executeUpdate();
} catch (Exception e) {
logger.error(sql);
logger.error(e.getMessage(), e);
throw new PersistenceException(sql, e);
} finally {
closePreparedStatement(preparedStatement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public int drop(Connection connection, PersistenceTableModel tableModel) throws
preparedStatement = openPreparedStatement(connection, sql);
result = preparedStatement.executeUpdate();
} catch (Exception e) {
logger.error(sql);
logger.error(e.getMessage(), e);
throw new PersistenceException(sql, e);
} finally {
closePreparedStatement(preparedStatement);
Expand Down
Loading

0 comments on commit bc8b167

Please sign in to comment.