org.simpleflatmapper.jdbc.impl.MapperQueryBinder Maven / Gradle / Ivy
package org.simpleflatmapper.jdbc.impl;
import org.simpleflatmapper.jdbc.QueryBinder;
import org.simpleflatmapper.util.ErrorHelper;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class MapperQueryBinder implements QueryBinder {
private final MapperQueryPreparer queryPreparer;
private final Connection connection;
public MapperQueryBinder(MapperQueryPreparer queryPreparer, Connection connection) {
this.queryPreparer = queryPreparer;
this.connection = connection;
}
@Override
public PreparedStatement bind(T value) throws SQLException {
PreparedStatement preparedStatement = queryPreparer.prepareStatement(connection);
try {
queryPreparer.mapper().mapTo(value, preparedStatement, null);
return preparedStatement;
} catch(Exception t) {
try {
preparedStatement.close();
} catch(SQLException e) {
// IGNORE
}
ErrorHelper.rethrow(t);
return null;
}
}
@Override
public void bindTo(T value, PreparedStatement ps) throws SQLException {
try {
queryPreparer.mapper().mapTo(value, ps, null);
} catch (Exception e) {
ErrorHelper.rethrow(e);
}
}
}