I wish there was a whitespace removal feature for use in the cloud service.

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

I wish there was a whitespace removal feature for use in the cloud service.

HeeGu Lee
Many services are being provided through the cloud these days.
My system was also decided to service through AWS.
These cloud services cost even one packet.

So it would be nice if mybatis also had the option to automatically reduce the whitespace in the XML file to a minimum.

I tried to do this as a plugin.
I read several blogs through Google Ring.
However, since reflect is used every time, it is judged whether the performance of the computer will be slightly slower.
So it would be nice to be able to set the compression of the query using the whitespaceStripper function, which is called when the log is taken, even when running the actual log.

Please review.
Thank you.

@Intercepts ({@ Signature (type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})})
public class WhitespaceStripperPlugin implements Interceptor {

    private static final ObjectFactory DEFAULT_OBJECT_FACTORY = new DefaultObjectFactory ();
    private static final ObjectWrapperFactory DEFAULT_OBJECT_WRAPPER_FACTORY = new DefaultObjectWrapperFactory ();
    private static final ReflectorFactory DEFAULT_REFLECTOR_FACTORY = new DefaultReflectorFactory ();

    @Override
    public Object intercept (Invocation invocation) throws Throwable {

        if (invocation.getTarget () instanceof StatementHandler) {
            StatementHandler statementHandler = (StatementHandler) invocation.getTarget ();
            MetaObject metaStatementHandler = MetaObject.forObject (statementHandler, DEFAULT_OBJECT_FACTORY, DEFAULT_OBJECT_WRAPPER_FACTORY, DEFAULT_REFLECTOR_FACTORY);
            String originalSql = (String) metaStatementHandler.getValue ("delegate.boundSql.sql");
            metaStatementHandler.setValue ("delegate.boundSql.sql", removeBreakingWhitespace (originalSql));
        }

        return invocation.proceed ();
    }

    protected String removeBreakingWhitespace (String original) { // copy of BaseJdbcLogger
        StringTokenizer whitespaceStripper = new StringTokenizer (original);
        StringBuilder builder = new StringBuilder ();
        while (whitespaceStripper.hasMoreTokens ()) {
            builder.append (whitespaceStripper.nextToken ());
            builder.append ("");
        }
        return builder.toString ();
    }

    @Override
    public Object plugin (Object target) {
        return Plugin.wrap (target, this);
    }

    @Override
    public void setProperties (Properties properties) {
        // no op
    }
}

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/4c47bd4e-cc7a-4ca3-86ed-ade406aade2b%40googlegroups.com.