Merge pull request #1903 from GodzillaHua/master

mysql limit ? 或者 limit ?,?不进行替换,防止参数数量不匹配
This commit is contained in:
高铁
2017-09-04 15:49:45 +08:00
committed by GitHub
2 changed files with 58 additions and 0 deletions
@@ -381,6 +381,8 @@ public class PagerUtils {
if (rowCount <= count && offset <= 0) {
return false;
}
} else if (check && limit.getRowCount() instanceof SQLVariantRefExpr) {
return false;
}
limit.setRowCount(new SQLIntegerExpr(count));
@@ -0,0 +1,56 @@
package com.alibaba.druid.bvt.sql;
import com.alibaba.druid.sql.PagerUtils;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLSelect;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.parser.ParserException;
import com.alibaba.druid.util.JdbcConstants;
import junit.framework.TestCase;
import org.junit.Assert;
import java.util.List;
public class PagerUtilsTest_Limit_mysql_question_placeholder extends TestCase {
public void testQuestionLimitPlaceholder1(){
String sql = "select * from test_table limit ?";
testQuestionLimitPlaceholderInternal(sql);
}
public void testQuestionLimitPlaceholder2(){
String sql = "select * from test_table limit ?, ?";
testQuestionLimitPlaceholderInternal(sql);
}
private void testQuestionLimitPlaceholderInternal(String sql){
List<SQLStatement> statements;
try{
statements = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
}catch(ParserException e){
Assert.fail(e.getMessage());
return;
}
if (statements == null || statements.size() == 0){
Assert.fail("no sql found!");
return;
}
if (statements.size() != 1) {
Assert.fail("sql not support count : " + sql);
return;
}
SQLSelectStatement statement = (SQLSelectStatement)statements.get(0);
if (!(statement instanceof SQLSelectStatement)) {
Assert.fail("sql not support count : " + sql);
return;
}
SQLSelect select = statement.getSelect();
PagerUtils.limit(select, JdbcConstants.MYSQL, 0, 200, true);
SQLUtils.FormatOption options = new SQLUtils.FormatOption();
options.setPrettyFormat(false);
options.setUppCase(false);
assertEquals(sql, SQLUtils.toSQLString(select, JdbcConstants.MYSQL, options));
}
}