Search in sources :

Example 1 with DataStream

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);
        }
    }
}
Also used : DataStream(io.vertigo.lang.DataStream) SQLException(java.sql.SQLException) Instant(java.time.Instant) IOException(java.io.IOException) LocalDate(java.time.LocalDate) Timestamp(java.sql.Timestamp) Date(java.util.Date) LocalDate(java.time.LocalDate)

Example 2 with DataStream

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);
        }
    }
}
Also used : DataStream(io.vertigo.lang.DataStream) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Aggregations

DataStream (io.vertigo.lang.DataStream)2 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 Timestamp (java.sql.Timestamp)1 Instant (java.time.Instant)1 LocalDate (java.time.LocalDate)1 Date (java.util.Date)1