/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* JBoss, Home of Professional Open Source
* Copyright 2010-2011 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
package org.hibernate.ogm.type.descriptor;
import java.sql.SQLException;
import java.util.Arrays;
import org.hibernate.engine.jdbc.LobCreator;
import org.hibernate.engine.jdbc.NonContextualLobCreator;
import org.hibernate.ogm.datastore.spi.Tuple;
import org.hibernate.ogm.util.impl.Log;
import org.hibernate.ogm.util.impl.LoggerFactory;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
/**
* @author Emmanuel Bernard
*/
public abstract class BasicGridBinder<X> implements GridValueBinder<X>{
private static final Log log = LoggerFactory.make();
private final JavaTypeDescriptor<X> javaDescriptor;
private final GridTypeDescriptor gridDescriptor;
private static final WrapperOptions DEFAULT_OPTIONS = new WrapperOptions() {
@Override
public boolean useStreamForLobBinding() {
return false;
}
@Override
public LobCreator getLobCreator() {
return NonContextualLobCreator.INSTANCE;
}
@Override
public SqlTypeDescriptor remapSqlTypeDescriptor(SqlTypeDescriptor sqlTypeDescriptor) {
//OGM dialect don't remap types yet
return sqlTypeDescriptor;
}
};
public BasicGridBinder(JavaTypeDescriptor<X> javaDescriptor, GridTypeDescriptor gridDescriptor) {
this.javaDescriptor = javaDescriptor;
this.gridDescriptor = gridDescriptor;
}
@Override
public void bind(Tuple resultset, X value, String[] names) {
if ( value == null ) {
for ( String name : names ) {
log.tracef( "binding [null] to parameter [%1$s]", name );
resultset.put( name, null );
}
}
else {
if ( log.isTraceEnabled() ) {
log.tracef( "binding [%1$s] to parameter(s) %2$s", javaDescriptor.extractLoggableRepresentation( value ), Arrays.toString( names ) );
}
doBind( resultset, value, names, DEFAULT_OPTIONS );
}
}
/**
* Perform the binding. Safe to assume that value is not null.
*
* @param st The prepared statement
* @param value The value to bind (not null).
* @param index The index at which to bind
* @param options The binding options
*
* @throws SQLException Indicates a problem binding to the prepared statement.
*/
protected abstract void doBind(Tuple resultset, X value, String[] names, WrapperOptions options);
}