Caratacus 8 лет назад
Родитель
Сommit
e835c6ed0a

+ 40 - 0
mybatis-plus/src/main/java/com/baomidou/mybatisplus/toolkit/DruidUtils.java

@@ -0,0 +1,40 @@
+/**
+ * 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.alibaba.druid.sql.PagerUtils;
+
+/**
+ * <p>
+ * DruidUtils工具类
+ * </p>
+ *
+ * @author Caratacus
+ * @Date 2016-11-30
+ */
+public class DruidUtils {
+	/**
+	 * 通过Druid方式获取count语句
+	 * 
+	 * @param originalSql
+	 * @param dialectType
+	 * @return
+	 */
+	public static String count(String originalSql, String dialectType) {
+		return PagerUtils.count(originalSql, dialectType);
+	}
+
+}

+ 101 - 0
mybatis-plus/src/main/java/com/baomidou/mybatisplus/toolkit/JsqlParserUtils.java

@@ -0,0 +1,101 @@
+/**
+ * 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.plugins.entity.CountOptimize;
+import net.sf.jsqlparser.expression.Expression;
+import net.sf.jsqlparser.expression.Function;
+import net.sf.jsqlparser.expression.LongValue;
+import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
+import net.sf.jsqlparser.parser.CCJSqlParserUtil;
+import net.sf.jsqlparser.statement.select.Distinct;
+import net.sf.jsqlparser.statement.select.OrderByElement;
+import net.sf.jsqlparser.statement.select.PlainSelect;
+import net.sf.jsqlparser.statement.select.Select;
+import net.sf.jsqlparser.statement.select.SelectExpressionItem;
+import net.sf.jsqlparser.statement.select.SelectItem;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * <p>
+ * JsqlParserUtils工具类
+ * </p>
+ *
+ * @author Caratacus
+ * @Date 2016-11-30
+ */
+public class JsqlParserUtils {
+	private static List<SelectItem> countSelectItem = null;
+
+	/**
+	 * jsqlparser方式获取select的count语句
+	 *
+	 * @param originalSql
+	 *            selectSQL
+	 * @return
+	 */
+	public static CountOptimize jsqlparserCount(CountOptimize countOptimize, String originalSql) {
+		String sqlCount;
+		try {
+			Select selectStatement = (Select) CCJSqlParserUtil.parse(originalSql);
+			PlainSelect plainSelect = (PlainSelect) selectStatement.getSelectBody();
+			Distinct distinct = plainSelect.getDistinct();
+			List<Expression> groupBy = plainSelect.getGroupByColumnReferences();
+			// 包含 distinct、groupBy不优化
+			if (distinct != null || CollectionUtil.isNotEmpty(groupBy)) {
+				sqlCount = String.format(SqlUtils.SQL_BASE_COUNT, originalSql);
+			}
+			// 优化Order by
+			List<OrderByElement> orderBy = plainSelect.getOrderByElements();
+			if (CollectionUtil.isNotEmpty(orderBy)) {
+				plainSelect.setOrderByElements(null);
+				countOptimize.setOrderBy(false);
+			}
+			List<SelectItem> selectCount = countSelectItem();
+			plainSelect.setSelectItems(selectCount);
+			sqlCount = selectStatement.toString();
+		} catch (Exception e) {
+			sqlCount = String.format(SqlUtils.SQL_BASE_COUNT, originalSql);
+		}
+		countOptimize.setCountSQL(sqlCount);
+		return countOptimize;
+	}
+
+	/**
+	 * 获取jsqlparser中count的SelectItem
+	 *
+	 * @return
+	 */
+	private static List<SelectItem> countSelectItem() {
+		if (CollectionUtil.isNotEmpty(countSelectItem)) {
+			return countSelectItem;
+		}
+		Function function = new Function();
+		function.setName("COUNT");
+		List<Expression> expressions = new ArrayList<Expression>();
+		LongValue longValue = new LongValue(1);
+		ExpressionList expressionList = new ExpressionList();
+		expressions.add(longValue);
+		expressionList.setExpressions(expressions);
+		function.setParameters(expressionList);
+		countSelectItem = new ArrayList<SelectItem>();
+		SelectExpressionItem selectExpressionItem = new SelectExpressionItem(function);
+		countSelectItem.add(selectExpressionItem);
+		return countSelectItem;
+	}
+}

+ 6 - 75
mybatis-plus/src/main/java/com/baomidou/mybatisplus/toolkit/SqlUtils.java

@@ -15,25 +15,10 @@
  */
 package com.baomidou.mybatisplus.toolkit;
 
-import com.alibaba.druid.sql.PagerUtils;
 import com.baomidou.mybatisplus.plugins.SQLFormatter;
 import com.baomidou.mybatisplus.plugins.entity.CountOptimize;
 import com.baomidou.mybatisplus.plugins.entity.Optimize;
 import com.baomidou.mybatisplus.plugins.pagination.Pagination;
-import net.sf.jsqlparser.expression.Expression;
-import net.sf.jsqlparser.expression.Function;
-import net.sf.jsqlparser.expression.LongValue;
-import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
-import net.sf.jsqlparser.parser.CCJSqlParserUtil;
-import net.sf.jsqlparser.statement.select.Distinct;
-import net.sf.jsqlparser.statement.select.OrderByElement;
-import net.sf.jsqlparser.statement.select.PlainSelect;
-import net.sf.jsqlparser.statement.select.Select;
-import net.sf.jsqlparser.statement.select.SelectExpressionItem;
-import net.sf.jsqlparser.statement.select.SelectItem;
-
-import java.util.ArrayList;
-import java.util.List;
 
 /**
  * <p>
@@ -45,8 +30,7 @@ import java.util.List;
  */
 public class SqlUtils {
 	private final static SQLFormatter sqlFormatter = new SQLFormatter();
-	private static final String SQL_BASE_COUNT = "SELECT COUNT(1) FROM ( %s )";
-	private static List<SelectItem> countSelectItem = null;
+	public static final String SQL_BASE_COUNT = "SELECT COUNT(1) FROM ( %s )";
 
 	/**
 	 * 获取CountOptimize
@@ -79,11 +63,14 @@ public class SqlUtils {
 				 * 
 				 * @see com.alibaba.druid.util.JdbcConstants
 				 */
-				String aliCountSql = PagerUtils.count(originalSql, dialectType);
+				String aliCountSql = DruidUtils.count(originalSql, dialectType);
 				countOptimize.setCountSQL(aliCountSql);
 				break;
 			case JSQLPARSER:
-				jsqlparserCount(countOptimize, originalSql);
+				/**
+				 * 调用JsqlParser方式
+				 */
+				JsqlParserUtils.jsqlparserCount(countOptimize, originalSql);
 				break;
 			default:
 				StringBuffer countSql = new StringBuffer("SELECT COUNT(1) AS TOTAL ");
@@ -151,60 +138,4 @@ public class SqlUtils {
 		}
 	}
 
-	/**
-	 * jsqlparser方式获取select的count语句
-	 *
-	 * @param originalSql
-	 *            selectSQL
-	 * @return
-	 */
-	public static CountOptimize jsqlparserCount(CountOptimize countOptimize, String originalSql) {
-		String sqlCount;
-		try {
-			Select selectStatement = (Select) CCJSqlParserUtil.parse(originalSql);
-			PlainSelect plainSelect = (PlainSelect) selectStatement.getSelectBody();
-			Distinct distinct = plainSelect.getDistinct();
-			List<Expression> groupBy = plainSelect.getGroupByColumnReferences();
-			// 包含 distinct、groupBy不优化
-			if (distinct != null || CollectionUtil.isNotEmpty(groupBy)) {
-				sqlCount = String.format(SQL_BASE_COUNT, originalSql);
-			}
-			// 优化Order by
-			List<OrderByElement> orderBy = plainSelect.getOrderByElements();
-			if (CollectionUtil.isNotEmpty(orderBy)) {
-				plainSelect.setOrderByElements(null);
-				countOptimize.setOrderBy(false);
-			}
-			List<SelectItem> selectCount = countSelectItem();
-			plainSelect.setSelectItems(selectCount);
-			sqlCount = selectStatement.toString();
-		} catch (Exception e) {
-			sqlCount = String.format(SQL_BASE_COUNT, originalSql);
-		}
-		countOptimize.setCountSQL(sqlCount);
-		return countOptimize;
-	}
-
-	/**
-	 * 获取jsqlparser中count的SelectItem
-	 *
-	 * @return
-	 */
-	private static List<SelectItem> countSelectItem() {
-		if (CollectionUtil.isNotEmpty(countSelectItem)) {
-			return countSelectItem;
-		}
-		Function function = new Function();
-		function.setName("COUNT");
-		List<Expression> expressions = new ArrayList<Expression>();
-		LongValue longValue = new LongValue(1);
-		ExpressionList expressionList = new ExpressionList();
-		expressions.add(longValue);
-		expressionList.setExpressions(expressions);
-		function.setParameters(expressionList);
-		countSelectItem = new ArrayList<SelectItem>();
-		SelectExpressionItem selectExpressionItem = new SelectExpressionItem(function);
-		countSelectItem.add(selectExpressionItem);
-		return countSelectItem;
-	}
 }

+ 0 - 20
mybatis-plus/src/test/java/com/baomidou/mybatisplus/test/SqlUtilsTest.java

@@ -1,10 +1,7 @@
 package com.baomidou.mybatisplus.test;
 
-import org.junit.Assert;
 import org.junit.Test;
 
-import com.baomidou.mybatisplus.plugins.entity.CountOptimize;
-import com.baomidou.mybatisplus.toolkit.SqlUtils;
 
 
 /**
@@ -21,10 +18,6 @@ public class SqlUtilsTest {
 	 */
 	@Test
 	public void test1() {
-		String sql = "select * from test orDer    \r\n   by name";
-		CountOptimize countOptimize = SqlUtils.getCountOptimize(sql, true);
-		Assert.assertEquals("{\"orderBy\":\"false\",\"countSQL\":\"SELECT COUNT(1) AS TOTAL from test \"}",
-				countOptimize.toString());
 
 	}
 
@@ -33,10 +26,6 @@ public class SqlUtilsTest {
 	 */
 	@Test
 	public void test2() {
-		String sql = "select distinct name from test orDer       by name";
-		CountOptimize countOptimize = SqlUtils.getCountOptimize(sql, true);
-		Assert.assertEquals("{\"orderBy\":\"true\",\"countSQL\":\"SELECT COUNT(1) AS TOTAL FROM (select distinct name from test orDer       by name) A\"}",
-				countOptimize.toString());
 
 	}
 
@@ -45,11 +34,6 @@ public class SqlUtilsTest {
 	 */
 	@Test
 	public void test3() {
-		String sql = "select DATE_FORMAT('2016-05-01 18:31:33','%Y-%m-%d')";
-		CountOptimize countOptimize = SqlUtils.getCountOptimize(sql, true);
-		Assert.assertEquals(
-				"{\"orderBy\":\"true\",\"countSQL\":\"SELECT COUNT(1) AS TOTAL FROM (select DATE_FORMAT('2016-05-01 18:31:33','%Y-%m-%d')) A\"}",
-				countOptimize.toString());
 
 	}
 
@@ -58,10 +42,6 @@ public class SqlUtilsTest {
 	 */
 	@Test
 	public void test4() {
-		String sql = "select DATE_FORMAT('2016-05-01 18:31:33','%Y-%m-%d') from test";
-		CountOptimize countOptimize = SqlUtils.getCountOptimize(sql, true);
-		Assert.assertEquals("{\"orderBy\":\"true\",\"countSQL\":\"SELECT COUNT(1) AS TOTAL from test\"}",
-				countOptimize.toString());
 
 	}