org.hibernate.type.descriptor.WrapperOptions Java Examples

The following examples show how to use org.hibernate.type.descriptor.WrapperOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BasicExtractor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public J extract(CallableStatement statement, int index, WrapperOptions options) throws SQLException {
	final J value = doExtract( statement, index, options );
	final boolean traceEnabled = log.isTraceEnabled();
	if ( value == null || statement.wasNull() ) {
		if ( traceEnabled ) {
			log.tracef(
					"extracted procedure output  parameter ([%s] : [%s]) - [null]",
					index,
					JdbcTypeNameMapper.getTypeName( getSqlDescriptor().getSqlType() )
			);
		}
		return null;
	}
	else {
		if ( traceEnabled ) {
			log.tracef(
					"extracted procedure output  parameter ([%s] : [%s]) - [%s]",
					index,
					JdbcTypeNameMapper.getTypeName( getSqlDescriptor().getSqlType() ),
					getJavaDescriptor().extractLoggableRepresentation( value )
			);
		}
		return value;
	}
}
 
Example #2
Source File: LongTypeDescriptor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public <X> Long wrap(X value, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( Long.class.isInstance( value ) ) {
		return (Long) value;
	}
	if ( Number.class.isInstance( value ) ) {
		return ( (Number) value ).longValue();
	}
	else if ( String.class.isInstance( value ) ) {
		return Long.valueOf( ( (String) value ) );
	}
	throw unknownWrap( value.getClass() );
}
 
Example #3
Source File: PrimitiveCharacterArrayTypeDescriptor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings({ "unchecked" })
public <X> X unwrap(char[] value, Class<X> type, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( char[].class.isAssignableFrom( type ) ) {
		return (X) value;
	}
	if ( String.class.isAssignableFrom( type ) ) {
		return (X) new String( value );
	}
	if ( Clob.class.isAssignableFrom( type ) ) {
		return (X) options.getLobCreator().createClob( new String( value ) );
	}
	if ( Reader.class.isAssignableFrom( type ) ) {
		return (X) new StringReader( new String( value ) );
	}
	if ( CharacterStream.class.isAssignableFrom( type ) ) {
		return (X) new CharacterStreamImpl( new String( value ) );
	}
	throw unknownUnwrap( type );
}
 
Example #4
Source File: BooleanTypeDescriptor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public <X> Boolean wrap(X value, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( Boolean.class.isInstance( value ) ) {
		return (Boolean) value;
	}
	if ( Number.class.isInstance( value ) ) {
		final int intValue = ( (Number) value ).intValue();
		return intValue == 0 ? FALSE : TRUE;
	}
	if ( Character.class.isInstance( value ) ) {
		return isTrue( (Character) value ) ? TRUE : FALSE;
	}
	if ( String.class.isInstance( value ) ) {
		return isTrue((String) value) ? TRUE : FALSE;
	}
	throw unknownWrap( value.getClass() );
}
 
Example #5
Source File: ShortTypeDescriptor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public <X> Short wrap(X value, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( Short.class.isInstance( value ) ) {
		return (Short) value;
	}
	if ( Number.class.isInstance( value ) ) {
		return ( (Number) value ).shortValue();
	}
	if ( String.class.isInstance( value ) ) {
		return Short.valueOf( ( (String) value ) );
	}
	throw unknownWrap( value.getClass() );
}
 
Example #6
Source File: CharacterArrayTypeDescriptor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings({ "unchecked" })
@Override
public <X> X unwrap(Character[] value, Class<X> type, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( Character[].class.isAssignableFrom( type ) ) {
		return (X) value;
	}
	if ( String.class.isAssignableFrom( type ) ) {
		return (X) new String( unwrapChars( value ) );
	}
	if ( Clob.class.isAssignableFrom( type ) ) {
		return (X) options.getLobCreator().createClob( new String( unwrapChars( value ) ) );
	}
	if ( Reader.class.isAssignableFrom( type ) ) {
		return (X) new StringReader( new String( unwrapChars( value ) ) );
	}
	if ( CharacterStream.class.isAssignableFrom( type ) ) {
		return (X) new CharacterStreamImpl( new String( unwrapChars( value ) ) );
	}
	throw unknownUnwrap( type );
}
 
Example #7
Source File: CharacterArrayTypeDescriptor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public <X> Character[] wrap(X value, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( Character[].class.isInstance( value ) ) {
		return (Character[]) value;
	}
	if ( String.class.isInstance( value ) ) {
		return wrapChars( ( (String) value ).toCharArray() );
	}
	if ( Clob.class.isInstance( value ) ) {
		return wrapChars( DataHelper.extractString( ( (Clob) value ) ).toCharArray() );
	}
	if ( Reader.class.isInstance( value ) ) {
		return wrapChars( DataHelper.extractString( (Reader) value ).toCharArray() );
	}
	throw unknownWrap( value.getClass() );
}
 
Example #8
Source File: JdbcDateTypeDescriptor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public <X> Date wrap(X value, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( java.sql.Date.class.isInstance( value ) ) {
		return (Date) value;
	}

	if ( Long.class.isInstance( value ) ) {
		return new java.sql.Date( ( (Long) value ).longValue() );
	}

	if ( Calendar.class.isInstance( value ) ) {
		return new java.sql.Date( ( (Calendar) value ).getTimeInMillis() );
	}

	if ( Date.class.isInstance( value ) ) {
		return new java.sql.Date( ( (Date) value ).getTime() );
	}

	throw unknownWrap( value.getClass() );
}
 
Example #9
Source File: PrimitiveCharacterArrayTypeDescriptor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public <X> char[] wrap(X value, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( char[].class.isInstance( value ) ) {
		return (char[]) value;
	}
	if ( String.class.isInstance( value ) ) {
		return ( (String) value ).toCharArray();
	}
	if ( Clob.class.isInstance( value ) ) {
		return DataHelper.extractString( ( (Clob) value ) ).toCharArray();
	}
	if ( Reader.class.isInstance( value ) ) {
		return DataHelper.extractString( ( (Reader) value ) ).toCharArray();
	}
	throw unknownWrap( value.getClass() );
}
 
Example #10
Source File: CalendarDateTypeDescriptor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings({ "unchecked" })
public <X> X unwrap(Calendar value, Class<X> type, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( Calendar.class.isAssignableFrom( type ) ) {
		return (X) value;
	}
	if ( java.sql.Date.class.isAssignableFrom( type ) ) {
		return (X) new java.sql.Date( value.getTimeInMillis() );
	}
	if ( java.sql.Time.class.isAssignableFrom( type ) ) {
		return (X) new java.sql.Time( value.getTimeInMillis() );
	}
	if ( java.sql.Timestamp.class.isAssignableFrom( type ) ) {
		return (X) new java.sql.Timestamp( value.getTimeInMillis() );
	}
	if ( Date.class.isAssignableFrom( type ) ) {
		return (X) new  Date( value.getTimeInMillis() );
	}
	throw unknownUnwrap( type );
}
 
Example #11
Source File: UUIDTypeDescriptor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings({ "unchecked" })
public <X> X unwrap(UUID value, Class<X> type, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( UUID.class.isAssignableFrom( type ) ) {
		return (X) PassThroughTransformer.INSTANCE.transform( value );
	}
	if ( String.class.isAssignableFrom( type ) ) {
		return (X) ToStringTransformer.INSTANCE.transform( value );
	}
	if ( byte[].class.isAssignableFrom( type ) ) {
		return (X) ToBytesTransformer.INSTANCE.transform( value );
	}
	throw unknownUnwrap( type );
}
 
Example #12
Source File: YearMonthTypeDescriptor.java    From hibernate-types with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked"})
@Override
public <X> X unwrap(YearMonth value, Class<X> type, WrapperOptions options) {
    if (value == null) {
        return null;
    }
    if (String.class.isAssignableFrom(type)) {
        return (X) toString(value);
    }
    if (Number.class.isAssignableFrom(type)) {
        Integer numericValue = (value.getYear() * 100) + value.getMonth().getValue();
        return (X) (numericValue);
    }
    if (Timestamp.class.isAssignableFrom(type)) {
        return (X) java.sql.Timestamp.valueOf(value.atDay(1).atStartOfDay());
    }
    if (Date.class.isAssignableFrom(type)) {
        return (X) java.sql.Date.valueOf(value.atDay(1));
    }
    throw unknownUnwrap(type);
}
 
Example #13
Source File: JdbcTimestampTypeDescriptor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public <X> Date wrap(X value, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( Timestamp.class.isInstance( value ) ) {
		return (Timestamp) value;
	}

	if ( Long.class.isInstance( value ) ) {
		return new Timestamp( (Long) value );
	}

	if ( Calendar.class.isInstance( value ) ) {
		return new Timestamp( ( (Calendar) value ).getTimeInMillis() );
	}

	if ( Date.class.isInstance( value ) ) {
		return new Timestamp( ( (Date) value ).getTime() );
	}

	throw unknownWrap( value.getClass() );
}
 
Example #14
Source File: AbstractArrayTypeDescriptor.java    From JuniperBot with GNU General Public License v3.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <X> T wrap(
        X value,
        WrapperOptions options
) {
    if (value instanceof Array) {
        Array array = (Array) value;
        try {
            return ArrayUtil.unwrapArray(
                    (Object[]) array.getArray(),
                    arrayObjectClass
            );
        } catch (SQLException e) {
            throw new IllegalArgumentException(e);
        }
    }
    return (T) value;
}
 
Example #15
Source File: DurationJavaDescriptor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public <X> Duration wrap(X value, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}

	if ( Duration.class.isInstance( value ) ) {
		return (Duration) value;
	}

	if ( Long.class.isInstance( value ) ) {
		return Duration.ofNanos( (Long) value );
	}

	if ( String.class.isInstance( value ) ) {
		return Duration.parse( (String) value );
	}

	throw unknownWrap( value.getClass() );
}
 
Example #16
Source File: BlobTypeDescriptor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public <X> Blob wrap(X value, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}

	// Support multiple return types from
	// org.hibernate.type.descriptor.sql.BlobTypeDescriptor
	if ( Blob.class.isAssignableFrom( value.getClass() ) ) {
		return options.getLobCreator().wrap( (Blob) value );
	}
	else if ( byte[].class.isAssignableFrom( value.getClass() ) ) {
		return options.getLobCreator().createBlob( ( byte[] ) value);
	}
	else if ( InputStream.class.isAssignableFrom( value.getClass() ) ) {
		InputStream inputStream = ( InputStream ) value;
		try {
			return options.getLobCreator().createBlob( inputStream, inputStream.available() );
		}
		catch ( IOException e ) {
			throw unknownWrap( value.getClass() );
		}
	}

	throw unknownWrap( value.getClass() );
}
 
Example #17
Source File: PrimitiveByteArrayTypeDescriptor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings({ "unchecked" })
public <X> X unwrap(byte[] value, Class<X> type, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( byte[].class.isAssignableFrom( type ) ) {
		return (X) value;
	}
	if ( InputStream.class.isAssignableFrom( type ) ) {
		return (X) new ByteArrayInputStream( value );
	}
	if ( BinaryStream.class.isAssignableFrom( type ) ) {
		return (X) new BinaryStreamImpl( value );
	}
	if ( Blob.class.isAssignableFrom( type ) ) {
		return (X) options.getLobCreator().createBlob( value );
	}

	throw unknownUnwrap( type );
}
 
Example #18
Source File: ByteArrayTypeDescriptor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings({ "unchecked" })
@Override
public <X> X unwrap(Byte[] value, Class<X> type, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( Byte[].class.isAssignableFrom( type ) ) {
		return (X) value;
	}
	if ( byte[].class.isAssignableFrom( type ) ) {
		return (X) unwrapBytes( value );
	}
	if ( InputStream.class.isAssignableFrom( type ) ) {
		return (X) new ByteArrayInputStream( unwrapBytes( value ) );
	}
	if ( BinaryStream.class.isAssignableFrom( type ) ) {
		return (X) new BinaryStreamImpl( unwrapBytes( value ) );
	}
	if ( Blob.class.isAssignableFrom( type ) ) {
		return (X) options.getLobCreator().createBlob( unwrapBytes( value ) );
	}

	throw unknownUnwrap( type );
}
 
Example #19
Source File: YearMonthTypeDescriptor.java    From hibernate-types with Apache License 2.0 6 votes vote down vote up
@Override
public <X> YearMonth wrap(X value, WrapperOptions options) {
    if (value == null) {
        return null;
    }
    if (value instanceof String) {
        return fromString((String) value);
    }
    if (value instanceof Number) {
        int numericValue = ((Number) (value)).intValue();
        int year = numericValue / 100;
        int month = numericValue % 100;
        return YearMonth.of(year, month);
    }
    if (value instanceof Date) {
        Date date = (Date) value;
        return YearMonth.from(Instant.ofEpochMilli(date.getTime())
                .atZone(ZoneId.systemDefault())
                .toLocalDate());
    }
    throw unknownWrap(value.getClass());
}
 
Example #20
Source File: ArraySqlTypeDescriptor.java    From hibernate-types with Apache License 2.0 5 votes vote down vote up
@Override
public <X> ValueExtractor<X> getExtractor(final JavaTypeDescriptor<X> javaTypeDescriptor) {
    return new BasicExtractor<X>(javaTypeDescriptor, this) {
        @Override
        protected X doExtract(ResultSet rs, String name, WrapperOptions options) throws SQLException {
            return javaTypeDescriptor.wrap(rs.getArray(name), options);
        }
    };
}
 
Example #21
Source File: ListArrayTypeDescriptor.java    From hibernate-types with Apache License 2.0 5 votes vote down vote up
@Override
public Object unwrap(Object value, Class type, WrapperOptions options) {
    if (value instanceof Object[]) {
        return value;
    } else if (value instanceof List) {
        return super.unwrap(((List) value).toArray(), type, options);
    } else {
        throw new UnsupportedOperationException("The provided " + value + " is not a Object[] or List!");
    }
}
 
Example #22
Source File: LocalDateJavaDescriptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <X> X unwrap(LocalDate value, Class<X> type, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}

	if ( LocalDate.class.isAssignableFrom( type ) ) {
		return (X) value;
	}

	if ( java.sql.Date.class.isAssignableFrom( type ) ) {
		return (X) java.sql.Date.valueOf( value );
	}

	final LocalDateTime localDateTime = value.atStartOfDay();

	if ( Timestamp.class.isAssignableFrom( type ) ) {
		return (X) Timestamp.valueOf( localDateTime );
	}

	final ZonedDateTime zonedDateTime = localDateTime.atZone( ZoneId.systemDefault() );

	if ( Calendar.class.isAssignableFrom( type ) ) {
		return (X) GregorianCalendar.from( zonedDateTime );
	}

	final Instant instant = zonedDateTime.toInstant();

	if ( Date.class.equals( type ) ) {
		return (X) Date.from( instant );
	}

	if ( Long.class.isAssignableFrom( type ) ) {
		return (X) Long.valueOf( instant.toEpochMilli() );
	}

	throw unknownUnwrap( type );
}
 
Example #23
Source File: YearTypeDescriptor.java    From hibernate-types with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked"})
@Override
public <X> X unwrap(Year value, Class<X> type, WrapperOptions options) {
    if (value == null) {
        return null;
    }
    if (String.class.isAssignableFrom(type)) {
        return (X) toString(value);
    }
    if (Number.class.isAssignableFrom(type)) {
        Short numericValue = (short) value.getValue();
        return (X) (numericValue);
    }
    throw unknownUnwrap(type);
}
 
Example #24
Source File: JsonTypeDescriptor.java    From JuniperBot with GNU General Public License v3.0 5 votes vote down vote up
@SuppressWarnings({"unchecked"})
@Override
public <X> X unwrap(Object value, Class<X> type, WrapperOptions options) {
    if (value == null) {
        return null;
    }
    if (String.class.isAssignableFrom(type)) {
        return (X) toString(value);
    }
    if (Object.class.isAssignableFrom(type)) {
        return (X) JacksonUtil.toJsonNode(toString(value));
    }
    throw unknownUnwrap(type);
}
 
Example #25
Source File: Iso8601MonthMonthTypeDescriptor.java    From hibernate-types with Apache License 2.0 5 votes vote down vote up
@Override
public <X> Month wrap(X value, WrapperOptions options) {
    if (value == null) {
        return null;
    }
    if (value instanceof Number) {
        int numericValue = ((Number) (value)).intValue();
        return Month.of(numericValue);
    }
    throw unknownWrap(value.getClass());
}
 
Example #26
Source File: ClassTypeDescriptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public <X> Class wrap(X value, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( Class.class.isInstance( value ) ) {
		return (Class) value;
	}
	if ( String.class.isInstance( value ) ) {
		return fromString( (String)value );
	}
	throw unknownWrap( value.getClass() );
}
 
Example #27
Source File: EnumJavaTypeDescriptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <X> X unwrap(T value, Class<X> type, WrapperOptions options) {
	if ( String.class.equals( type ) ) {
		return (X) toName( value );
	}
	else if ( Integer.class.isInstance( type ) ) {
		return (X) toOrdinal( value );
	}

	return (X) value;
}
 
Example #28
Source File: BasicBinder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public final void bind(CallableStatement st, J value, String name, WrapperOptions options) throws SQLException {
	final boolean traceEnabled = log.isTraceEnabled();
	if ( value == null ) {
		if ( traceEnabled ) {
			log.trace(
					String.format(
							NULL_BIND_MSG_TEMPLATE,
							name,
							JdbcTypeNameMapper.getTypeName( getSqlDescriptor().getSqlType() )
					)
			);
		}
		st.setNull( name, sqlDescriptor.getSqlType() );
	}
	else {
		if ( traceEnabled ) {
			log.trace(
					String.format(
							BIND_MSG_TEMPLATE,
							name,
							JdbcTypeNameMapper.getTypeName( sqlDescriptor.getSqlType() ),
							getJavaDescriptor().extractLoggableRepresentation( value )
					)
			);
		}
		doBind( st, value, name, options );
	}
}
 
Example #29
Source File: LocaleTypeDescriptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked" })
public <X> X unwrap(Locale value, Class<X> type, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( String.class.isAssignableFrom( type ) ) {
		return (X) value.toString();
	}
	throw unknownUnwrap( type );
}
 
Example #30
Source File: UUIDTypeDescriptor.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public <X> UUID wrap(X value, WrapperOptions options) {
	if ( value == null ) {
		return null;
	}
	if ( UUID.class.isInstance( value ) ) {
		return PassThroughTransformer.INSTANCE.parse( value );
	}
	if ( String.class.isInstance( value ) ) {
		return ToStringTransformer.INSTANCE.parse( value );
	}
	if ( byte[].class.isInstance( value ) ) {
		return ToBytesTransformer.INSTANCE.parse( value );
	}
	throw unknownWrap( value.getClass() );
}