/** * Copyright (c) 2011-2020, hubin (jobob@qq.com). *
* 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 com.baomidou.mybatisplus.toolkit; import com.baomidou.mybatisplus.enums.SqlLike; import com.baomidou.mybatisplus.exceptions.MybatisPlusException; import com.baomidou.mybatisplus.plugins.parser.ISqlParser; import com.baomidou.mybatisplus.plugins.parser.SqlInfo; import com.baomidou.mybatisplus.plugins.pagination.Pagination; /** *
* SqlUtils工具类 *
* * @author Caratacus * @Date 2016-11-13 */ public class SqlUtils { private final static SqlFormatter sqlFormatter = new SqlFormatter(); public final static String SQL_BASE_COUNT = "SELECT COUNT(1) FROM ( %s ) TOTAL"; public static ISqlParser COUNT_SQL_PARSER = null; private static Class* 获取CountOptimize *
* * @param sqlParser Count SQL 解析类 * @param originalSql 需要计算Count SQL * @return SqlInfo */ public static SqlInfo getCountOptimize(ISqlParser sqlParser, String originalSql) { // COUNT SQL 解析器 if (null == COUNT_SQL_PARSER) { if (null != sqlParser) { // 用户自定义 COUNT SQL 解析 COUNT_SQL_PARSER = sqlParser; } else { // 默认 JsqlParser 优化 COUNT try { // TODO: 2017/11/20 这里我改动了下. COUNT_SQL_PARSER = DEFAULT_CLASS.newInstance(); } catch (Exception e) { throw new MybatisPlusException(e); } } } return COUNT_SQL_PARSER.optimizeSql(null, originalSql); } /** * 查询SQL拼接Order By * * @param originalSql 需要拼接的SQL * @param page page对象 * @param orderBy 是否需要拼接Order By * @return */ public static String concatOrderBy(String originalSql, Pagination page, boolean orderBy) { if (orderBy && StringUtils.isNotEmpty(page.getOrderByField()) && page.isOpenSort()) { StringBuilder buildSql = new StringBuilder(originalSql); buildSql.append(" ORDER BY ").append(page.getOrderByField()); buildSql.append(page.isAsc() ? " ASC " : " DESC "); return buildSql.toString(); } return originalSql; } /** * 格式sql * * @param boundSql * @param format * @return */ public static String sqlFormat(String boundSql, boolean format) { if (format) { return sqlFormatter.format(boundSql); } else { return boundSql.replaceAll("[\\s]+", " "); } } /** ** 用%连接like *
* * @param str 原字符串 * @return */ public static String concatLike(String str, SqlLike type) { StringBuilder builder = new StringBuilder(str.length() + 3); switch (type) { case LEFT: builder.append("%").append(str); break; case RIGHT: builder.append(str).append("%"); break; case CUSTOM: builder.append(str); break; default: builder.append("%").append(str).append("%"); } return builder.toString(); } }