Skip to content

Commit

Permalink
Adjustment for mybatis/mybatis-3#2760
Browse files Browse the repository at this point in the history
  • Loading branch information
harawata committed Jan 28, 2025
1 parent 287fce2 commit 56c3013
Showing 1 changed file with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2022 the original author or authors.
* Copyright 2015-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,11 +19,15 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.builder.ParameterMappingTokenHandler;
import org.apache.ibatis.builder.SqlSourceBuilder;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.ParameterMapping;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.parsing.GenericTokenParser;
import org.apache.ibatis.session.Configuration;

import freemarker.template.Template;
Expand Down Expand Up @@ -110,9 +114,18 @@ public BoundSql getBoundSql(Object parameterObject) {
}

// Pass retrieved SQL into MyBatis engine, it will substitute prepared-statements parameters
SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration);
Class<?> parameterType1 = parameterObject == null ? Object.class : parameterObject.getClass();
SqlSource sqlSource = sqlSourceParser.parse(sql, parameterType1, new HashMap<String, Object>());
SqlSource sqlSource = parse(configuration, sql, parameterObject, new HashMap<String, Object>());
return sqlSource.getBoundSql(parameterObject);
}

private static SqlSource parse(Configuration configuration, String originalSql, Object parameterObject,
Map<String, Object> additionalParameters) {
Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass();
List<ParameterMapping> parameterMappings = new ArrayList<>();
ParameterMappingTokenHandler handler = new ParameterMappingTokenHandler(parameterMappings, configuration,
parameterObject, parameterType, additionalParameters, true);
GenericTokenParser parser = new GenericTokenParser("#{", "}", handler);
return SqlSourceBuilder.buildSqlSource(configuration, parser.parse(originalSql), parameterMappings);
}

}

0 comments on commit 56c3013

Please sign in to comment.