-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25f7af7
commit b1af0bc
Showing
6 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/org/apache/ibatis/scripting/xmltags/DynamicSqlSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.apache.ibatis.scripting.xmltags; | ||
|
||
import org.apache.ibatis.builder.SqlSourceBuilder; | ||
import org.apache.ibatis.mapping.BoundSql; | ||
import org.apache.ibatis.mapping.SqlSource; | ||
import org.apache.ibatis.session.Configuration; | ||
|
||
/** | ||
* @author furious 2024/4/16 | ||
*/ | ||
public class DynamicSqlSource implements SqlSource { | ||
|
||
private final Configuration configuration; | ||
private final SqlNode rootSqlNode; | ||
|
||
public DynamicSqlSource(Configuration configuration, SqlNode rootSqlNode) { | ||
this.configuration = configuration; | ||
this.rootSqlNode = rootSqlNode; | ||
} | ||
|
||
@Override | ||
public BoundSql getBoundSql(Object parameterObject) { | ||
DynamicContext context = new DynamicContext(parameterObject); | ||
rootSqlNode.apply(context); | ||
SqlSourceBuilder sqlSourceParser = new SqlSourceBuilder(configuration); | ||
Class<?> parameterType = parameterObject == null ? Object.class : parameterObject.getClass(); | ||
SqlSource sqlSource = sqlSourceParser.parse(context.getSql(), parameterType); | ||
return sqlSource.getBoundSql(parameterObject); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/org/apache/ibatis/scripting/xmltags/IfSqlNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.apache.ibatis.scripting.xmltags; | ||
|
||
import ognl.Ognl; | ||
|
||
/** | ||
* @author furious 2024/4/16 | ||
*/ | ||
public class IfSqlNode implements SqlNode { | ||
|
||
private final String test; | ||
private final SqlNode contents; | ||
|
||
public IfSqlNode(String test, SqlNode contents) { | ||
this.test = test; | ||
this.contents = contents; | ||
} | ||
|
||
@Override | ||
public boolean apply(DynamicContext context) { | ||
try { | ||
Object value = Ognl.getValue(Ognl.parseExpression(test), context.getParameterObject()); | ||
if (value instanceof Boolean && (Boolean) value) { | ||
contents.apply(context); | ||
} | ||
return true; | ||
} catch (Exception e) { | ||
return false; | ||
} | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
src/main/java/org/apache/ibatis/scripting/xmltags/TrimSqlNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package org.apache.ibatis.scripting.xmltags; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
|
||
/** | ||
* @author furious 2024/4/16 | ||
*/ | ||
public class TrimSqlNode implements SqlNode { | ||
|
||
private final SqlNode contents; | ||
private final String prefix; | ||
private final List<String> prefixesToOverride; | ||
|
||
public TrimSqlNode(SqlNode contents, String prefix, String prefixesToOverride) { | ||
this.contents = contents; | ||
this.prefix = prefix; | ||
this.prefixesToOverride = parseOverrides(prefixesToOverride); | ||
} | ||
|
||
@Override | ||
public boolean apply(DynamicContext context) { | ||
FilteredDynamicContext filteredDynamicContext = new FilteredDynamicContext(context); | ||
boolean result = contents.apply(filteredDynamicContext); | ||
filteredDynamicContext.applyAll(); | ||
return result; | ||
} | ||
|
||
private static List<String> parseOverrides(String overrides) { | ||
if (StringUtils.isEmpty(overrides)) { | ||
return Collections.emptyList(); | ||
} | ||
return Arrays.stream(overrides.split("\\|")).collect(Collectors.toList()); | ||
} | ||
|
||
private class FilteredDynamicContext extends DynamicContext { | ||
private final DynamicContext delegate; | ||
private StringBuilder sqlBuffer = new StringBuilder(); | ||
|
||
public FilteredDynamicContext(DynamicContext delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
public void applyAll() { | ||
sqlBuffer = new StringBuilder(sqlBuffer.toString().trim()); | ||
String trimmedUppercaseSql = sqlBuffer.toString().toUpperCase(); | ||
if (trimmedUppercaseSql.length() > 0) { | ||
applyPrefix(sqlBuffer, trimmedUppercaseSql); | ||
} | ||
delegate.appendSql(sqlBuffer.toString()); | ||
} | ||
|
||
@Override | ||
public void appendSql(String sql) { | ||
sqlBuffer.append(sql); | ||
} | ||
|
||
@Override | ||
public String getSql() { | ||
return delegate.getSql(); | ||
} | ||
|
||
@Override | ||
public Object getParameterObject() { | ||
return delegate.getParameterObject(); | ||
} | ||
|
||
private void applyPrefix(StringBuilder sql, String trimmedUppercaseSql) { | ||
if (Objects.nonNull(prefixesToOverride)) { | ||
for (String remove : prefixesToOverride) { | ||
if (trimmedUppercaseSql.startsWith(remove)) { | ||
sql.delete(0, remove.trim().length()); | ||
break; | ||
} | ||
} | ||
} | ||
if (prefix != null) { | ||
sql.insert(0, " "); | ||
sql.insert(0, prefix); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters