use of io.vertigo.lang.DataStream in project vertigo by KleeGroup.
the class SqlVendorMapping method setValueOnStatement.
/**
* {@inheritDoc}
*/
@Override
public <O> void setValueOnStatement(final java.sql.PreparedStatement statement, final int index, final Class<O> dataType, final O value) throws SQLException {
if (value == null) {
final int typeSQL = getSqlType(dataType);
statement.setNull(index, typeSQL);
} else {
if (Integer.class.isAssignableFrom(dataType)) {
statement.setInt(index, (Integer) value);
} else if (Long.class.isAssignableFrom(dataType)) {
statement.setLong(index, (Long) value);
} else if (Boolean.class.isAssignableFrom(dataType)) {
if (booleanAsBit) {
final int intValue = Boolean.TRUE.equals(value) ? 1 : 0;
statement.setInt(index, intValue);
} else {
// Boolean as Boolean
statement.setBoolean(index, Boolean.TRUE.equals(value));
}
} else if (Double.class.isAssignableFrom(dataType)) {
statement.setDouble(index, (Double) value);
} else if (BigDecimal.class.isAssignableFrom(dataType)) {
statement.setBigDecimal(index, (BigDecimal) value);
} else if (String.class.isAssignableFrom(dataType)) {
statement.setString(index, (String) value);
} else if (LocalDate.class.isAssignableFrom(dataType)) {
final LocalDate localDate = (LocalDate) value;
statement.setDate(index, java.sql.Date.valueOf(localDate));
} else if (Date.class.isAssignableFrom(dataType)) {
final Date date = (Date) value;
final Timestamp ts = new Timestamp(date.getTime());
statement.setTimestamp(index, ts);
} else if (Instant.class.isAssignableFrom(dataType)) {
final Instant instant = (Instant) value;
final Timestamp ts = Timestamp.from(instant);
statement.setTimestamp(index, ts);
} else if (DataStream.class.isAssignableFrom(dataType)) {
try {
final DataStream dataStream = (DataStream) value;
// Notice : setBinaryStream() without length is NOT implemented by all the database drivers.
statement.setBinaryStream(index, dataStream.createInputStream(), (int) dataStream.getLength());
} catch (final IOException e) {
throw new SQLException("writing error", e);
}
} else {
throw new IllegalArgumentException(TYPE_UNSUPPORTED + dataType);
}
}
}
use of io.vertigo.lang.DataStream in project vertigo by KleeGroup.
the class DataStreamType method nullSafeSet.
/**
* {@inheritDoc}
*/
@Override
public void nullSafeSet(final PreparedStatement statement, final Object value, final int index, final SharedSessionContractImplementor session) throws SQLException {
if (value == null) {
statement.setNull(index, sqlTypes()[0]);
} else {
// Cf io.vertigo.dynamo.impl.database.vendor.core.SQLMappingImpl
try {
final DataStream dataStream = (DataStream) value;
// attention le setBinaryStream avec une longueur de fichier en long N'EST PAS implémentée dans de nombreux drivers !!
statement.setBinaryStream(index, dataStream.createInputStream(), (int) dataStream.getLength());
} catch (final IOException e) {
throw new SQLException("Erreur d'ecriture du flux", e);
}
}
}
Aggregations