From 3b55d06b89bf64873e685c3d78fce5affbba3d17 Mon Sep 17 00:00:00 2001 From: zheng_wenlong Date: Tue, 16 Apr 2019 11:20:38 +0900 Subject: Add warehouse server source code. [Patch Set 2] Add ReadMe.md Change-Id: I6ade52d2490f5ca4ba107c1a27ed6d5b39048725 Signed-off-by: zheng_wenlong --- .../interceptor/PaginationInterceptor.java | 74 ++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 appmarket-persistence/src/main/java/app/market/persistence/interceptor/PaginationInterceptor.java (limited to 'appmarket-persistence/src/main/java/app/market/persistence/interceptor/PaginationInterceptor.java') diff --git a/appmarket-persistence/src/main/java/app/market/persistence/interceptor/PaginationInterceptor.java b/appmarket-persistence/src/main/java/app/market/persistence/interceptor/PaginationInterceptor.java new file mode 100644 index 0000000..0e84e4d --- /dev/null +++ b/appmarket-persistence/src/main/java/app/market/persistence/interceptor/PaginationInterceptor.java @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2019 TOYOTA MOTOR CORPORATION + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package app.market.persistence.interceptor; + +import java.sql.Connection; +import java.util.Properties; +import java.util.regex.Pattern; + +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.reflect.FieldUtils; +import org.apache.ibatis.executor.statement.StatementHandler; +import org.apache.ibatis.mapping.BoundSql; +import org.apache.ibatis.plugin.Interceptor; +import org.apache.ibatis.plugin.Intercepts; +import org.apache.ibatis.plugin.Invocation; +import org.apache.ibatis.plugin.Plugin; +import org.apache.ibatis.plugin.Signature; +import org.apache.ibatis.session.RowBounds; + +@Intercepts(@Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class })) +public class PaginationInterceptor implements Interceptor { + + private final static String SQL_SELECT_REGEX = "(?is)^\\s*SELECT.*$"; + + private final static String SQL_COUNT_REGEX = "(?is)^\\s*SELECT\\s+COUNT\\s*\\(\\s*(?:\\*|\\w+)\\s*\\).*$"; + + @Override + public Object intercept(Invocation inv) throws Throwable { + StatementHandler target = (StatementHandler) inv.getTarget(); + BoundSql boundSql = target.getBoundSql(); + String sql = boundSql.getSql(); + if ( StringUtils.isBlank( sql ) ) { + return inv.proceed(); + } + if ( sql.matches( SQL_SELECT_REGEX ) && !Pattern.matches( SQL_COUNT_REGEX, sql ) ) { + Object obj = FieldUtils.readField( target, "delegate", true ); + RowBounds rowBounds = (RowBounds) FieldUtils.readField( obj, "rowBounds", true ); + if ( rowBounds != null && rowBounds != RowBounds.DEFAULT ) { + FieldUtils.writeField( boundSql, "sql", newSql( sql, rowBounds ), true ); + FieldUtils.writeField( rowBounds, "offset", RowBounds.NO_ROW_OFFSET, true ); + FieldUtils.writeField( rowBounds, "limit", RowBounds.NO_ROW_LIMIT, true ); + } + } + return inv.proceed(); + } + + public String newSql(String oldSql, RowBounds rowBounds) { + String end = " limit " + rowBounds.getOffset() + "," + rowBounds.getLimit(); + return oldSql + end; + } + + @Override + public Object plugin(Object target) { + return Plugin.wrap( target, this ); + } + + @Override + public void setProperties(Properties arg0) { + } + +} \ No newline at end of file -- cgit 1.2.3-korg