Skip to content

Commit

Permalink
Add specific mybatis string type handler
Browse files Browse the repository at this point in the history
  • Loading branch information
tijsrademakers committed Jun 14, 2024
1 parent af0d12f commit 6a2936a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
import org.flowable.common.engine.impl.cfg.standalone.StandaloneMybatisTransactionContextFactory;
import org.flowable.common.engine.impl.db.CommonDbSchemaManager;
import org.flowable.common.engine.impl.db.DbSqlSessionFactory;
import org.flowable.common.engine.impl.db.FlowableStringTypeHandler;
import org.flowable.common.engine.impl.db.LogSqlExecutionTimePlugin;
import org.flowable.common.engine.impl.db.MybatisTypeAliasConfigurator;
import org.flowable.common.engine.impl.db.MybatisTypeHandlerConfigurator;
Expand Down Expand Up @@ -948,8 +949,8 @@ public void initMybatisTypeHandlers(Configuration configuration) {
// thus the same handler is used for both types.
handlerRegistry.register(String.class, JdbcType.VARCHAR, new StringTypeHandler());
handlerRegistry.register(Object.class, JdbcType.VARCHAR, new StringTypeHandler());
handlerRegistry.register(String.class, JdbcType.NVARCHAR, new StringTypeHandler()); // Notice: no 'N' prefix here
handlerRegistry.register(Object.class, JdbcType.NVARCHAR, new StringTypeHandler()); // Notice: no 'N' prefix here
handlerRegistry.register(String.class, JdbcType.NVARCHAR, new FlowableStringTypeHandler()); // Notice: no 'N' prefix here
handlerRegistry.register(Object.class, JdbcType.NVARCHAR, new FlowableStringTypeHandler()); // Notice: no 'N' prefix here
}

handlerRegistry.register(Object.class, JdbcType.LONGVARCHAR, new StringTypeHandler());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.flowable.common.engine.impl.db;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeException;

public class FlowableStringTypeHandler extends BaseTypeHandler<String> {

@Override
public void setParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
if (parameter == null) {
if (jdbcType == null) {
throw new TypeException("JDBC requires that the JdbcType must be specified for all nullable parameters.");
}
try {
ps.setNull(i, Types.NULL);
} catch (SQLException e) {
throw new TypeException("Error setting null for parameter #" + i + " with JdbcType " + jdbcType + " . "
+ "Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. "
+ "Cause: " + e, e);
}
} else {
try {
setNonNullParameter(ps, i, parameter, jdbcType);
} catch (Exception e) {
throw new TypeException("Error setting non null for parameter #" + i + " with JdbcType " + jdbcType + " . "
+ "Try setting a different JdbcType for this parameter or a different configuration property. " + "Cause: "
+ e, e);
}
}
}

@Override
public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType)
throws SQLException {
ps.setString(i, parameter);
}

@Override
public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
return rs.getString(columnName);
}

@Override
public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return rs.getString(columnIndex);
}

@Override
public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return cs.getString(columnIndex);
}
}

0 comments on commit 6a2936a

Please sign in to comment.