New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove blank lines in SQL #1126
Comments
Hi @zhaoxi1988 , contributing welcome!! :D |
Oh. I accept it. By the way. |
@zhaoxi1988 Is the problem solved? |
@George5814 Still not solved...I busy on mybatis UnitTest... |
@George5814 单元测试在IntelliJ上, 只安装JDK8有点尴尬... |
I'm wondering why this feature request gets many votes. |
This is to delete the extra whitespace produced by the dynamic tags, not to beautify the SQL. |
@George5814 |
@harawata Because the Database Server logging SQL is not convenient. |
@harawata I was an DBA. I see the problem like MySQL truncate the SQL because of the many blank lines. |
@zhaoxi1988 , |
It is not. |
@zhaoxi1988 , Thank you for the comment!
Okay. As I commented on the PR, the best we can do is to reduce the number of line breaks, probably. |
Hi, is this problem solved? |
Nope. The language driver can be pretty simple. public class CustomXmlLanguageDriver extends XMLLanguageDriver {
@Override
public SqlSource createSqlSource(Configuration configuration, XNode script, Class<?> parameterType) {
CustomXmlScriptBuilder builder = new CustomXmlScriptBuilder(configuration, script, parameterType);
return builder.parseScriptNode();
}
}
And set <settings>
<setting name="defaultScriptingLanguage"
value="xxx.yyy.CustomXmlLanguageDriver" />
</settings> |
It might be cleaner to do the substitution in the language driver. import java.util.regex.Pattern;
import org.apache.ibatis.builder.xml.XMLMapperEntityResolver;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.parsing.PropertyParser;
import org.apache.ibatis.parsing.XNode;
import org.apache.ibatis.parsing.XPathParser;
import org.apache.ibatis.scripting.defaults.RawSqlSource;
import org.apache.ibatis.scripting.xmltags.DynamicSqlSource;
import org.apache.ibatis.scripting.xmltags.TextSqlNode;
import org.apache.ibatis.scripting.xmltags.XMLLanguageDriver;
import org.apache.ibatis.session.Configuration;
public class CompactXMLLanguageDriver extends XMLLanguageDriver {
private static final Pattern pattern = Pattern.compile("[\\s]+");
@Override
public SqlSource createSqlSource(Configuration configuration, XNode script, Class<?> parameterType) {
script.getNode().setTextContent(pattern.matcher(script.getNode().getTextContent()).replaceAll(" "));
return super.createSqlSource(configuration, script, parameterType);
}
@Override
public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) {
if (script.startsWith("<script>")) {
XPathParser parser = new XPathParser(script, false, configuration.getVariables(), new XMLMapperEntityResolver());
return createSqlSource(configuration, parser.evalNode("/script"), parameterType);
} else {
script = PropertyParser.parse(pattern.matcher(script).replaceAll(" "), configuration.getVariables());
TextSqlNode textSqlNode = new TextSqlNode(script);
if (textSqlNode.isDynamic()) {
return new DynamicSqlSource(configuration, textSqlNode);
} else {
return new RawSqlSource(configuration, script, parameterType);
}
}
}
} You don't have to modify |
Fixed via #1901 |
Question:
MyBatis build SQL include many blank lines from XML.
The blank lines is unnecessary.
https://issues.apache.org/jira/browse/IBATIS-143
class XMLScriptBuilder
Let XNode replace '\n' or "\r\n" to ' ' .
I want the feature can be configurable.
The text was updated successfully, but these errors were encountered: