/* * Hibernate, Relational Persistence for Idiomatic Java * * License: GNU Lesser General Public License (LGPL), version 2.1 or later * See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html */ package org.hibernate.orm.type.descriptor.sql.spi; import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Types; import org.hibernate.orm.type.descriptor.java.spi.BasicJavaTypeDescriptor; import org.hibernate.orm.type.descriptor.java.spi.JavaTypeDescriptor; import org.hibernate.orm.type.descriptor.spi.JdbcValueBinder; import org.hibernate.orm.type.descriptor.spi.JdbcValueExtractor; import org.hibernate.orm.type.descriptor.spi.WrapperOptions; import org.hibernate.orm.type.descriptor.sql.internal.JdbcLiteralFormatterCharacterData; import org.hibernate.orm.type.spi.JdbcLiteralFormatter; import org.hibernate.orm.type.spi.TypeConfiguration; /** * Descriptor for {@link Types#NVARCHAR NVARCHAR} handling. * * @author Steve Ebersole */ public class NVarcharSqlDescriptor implements SqlTypeDescriptor { public static final NVarcharSqlDescriptor INSTANCE = new NVarcharSqlDescriptor(); public NVarcharSqlDescriptor() { } @Override public int getSqlType() { return Types.NVARCHAR; } @Override public boolean canBeRemapped() { return true; } @Override @SuppressWarnings("unchecked") public <T> JdbcLiteralFormatter<T> getJdbcLiteralFormatter(JavaTypeDescriptor<T> javaTypeDescriptor) { return new JdbcLiteralFormatterCharacterData( javaTypeDescriptor, true ); } @Override public BasicJavaTypeDescriptor getJdbcRecommendedJavaTypeMapping(TypeConfiguration typeConfiguration) { return (BasicJavaTypeDescriptor) typeConfiguration.getJavaTypeDescriptorRegistry().getDescriptor( String.class ); } @Override public <X> JdbcValueBinder<X> getBinder(final JavaTypeDescriptor<X> javaTypeDescriptor) { return new StandardJdbcValueBinder<X>( javaTypeDescriptor, this ) { @Override protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException { st.setNString( index, javaTypeDescriptor.unwrap( value, String.class, options ) ); } @Override protected void doBind(CallableStatement st, X value, String name, WrapperOptions options) throws SQLException { st.setNString( name, javaTypeDescriptor.unwrap( value, String.class, options ) ); } }; } @Override public <X> JdbcValueExtractor<X> getExtractor(final JavaTypeDescriptor<X> javaTypeDescriptor) { return new StandardJdbcValueExtractor<X>( javaTypeDescriptor, this ) { @Override protected X doExtract(ResultSet rs, String name, WrapperOptions options) throws SQLException { return javaTypeDescriptor.wrap( rs.getNString( name ), options ); } @Override protected X doExtract(CallableStatement statement, int index, WrapperOptions options) throws SQLException { return javaTypeDescriptor.wrap( statement.getNString( index ), options ); } @Override protected X doExtract(CallableStatement statement, String name, WrapperOptions options) throws SQLException { return javaTypeDescriptor.wrap( statement.getNString( name ), options ); } }; } }