123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- /**
- * Copyright (c) 2011-2020, hubin (jobob@qq.com).
- * <p>
- * 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
- * <p>
- * http://www.apache.org/licenses/LICENSE-2.0
- * <p>
- * 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;
- /**
- * <p>
- * SqlUtils工具类
- * </p>
- *
- * @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<ISqlParser> DEFAULT_CLASS = null;
- static {
- try {
- DEFAULT_CLASS = (Class<ISqlParser>) Class.forName("com.baomidou.mybatisplus.plugins.pagination.optimize.JsqlParserCountOptimize");
- } catch (ClassNotFoundException e) {
- //skip
- }
- }
- /**
- * <p>
- * 获取CountOptimize
- * </p>
- *
- * @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]+", " ");
- }
- }
- /**
- * <p>
- * 用%连接like
- * </p>
- *
- * @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();
- }
- }
|