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.
Hi @zhaoxi1988 , contributing welcome!! :D
Oh. I accept it.
By the way.
Why do not mybatis's package name 'org.apache.mybatis' ?
@zhaoxi1988 Is the problem solved?
@George5814 Still not solved...I busy on mybatis UnitTest...
@George5814
My idea:
1.add org.apache.ibatis.session.Configuration
2.push configuration down , let SqlSourceBuilder trim '\r|\n' or replace('\r|\n', ' ')
单元测试在IntelliJ上, 只安装JDK8有点尴尬...
I'm wondering why this feature request gets many votes.
Is there any actual harm caused by the extra blank lines?
This is to delete the extra whitespace produced by the dynamic tags, not to beautify the SQL.
Improve readability.
@George5814
Recent versions of MyBatis remove blank lines when printing the log, so where do you see the SQLs with blank lines?
@harawata Because the Database Server logging SQL is not convenient.
Then the next requirement is that tracing SQL instance in MyBatis , e.g.
@harawata I was an DBA. I see the problem like MySQL truncate the SQL because of the many blank lines.
@zhaoxi1988 ,
Do you mean that the length of a generated SQL exceeds max_allowed_packet ?
@harawata
Do you mean that the length of a generated SQL exceeds max_allowed_packet
It is not.
It is MySQL's query-log or InnoDB log when had a dead lock.
@zhaoxi1988 ,
Thank you for the comment!
It is MySQL's query-log or InnoDB log when had a dead lock.
Okay. As I commented on the PR, the best we can do is to reduce the number of line breaks, probably.
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.
Hi, is this problem solved?
Nope.
If it's very important, writing a custom language driver and script builder should work.
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();
}
}
CustomXmlScriptBuilder is almost identical to org.apache.ibatis.scripting.xmltags.XMLScriptBuilder.
Just remove whitespaces in parseDynamicTags() method as @zhaoxi1988 did in #1153 .
And set defaultScriptLanguage in the config.
<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 XMLScriptBuilder anymore.
Fixed via #1901
We added a new option shrinkWhitespacesInSql
Please test 3.5.5.-SNAPSHOT.
Most helpful comment
Hi @zhaoxi1988 , contributing welcome!! :D