Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

mysql limit ? 或者 limit ?,?不进行替换,防止参数数量不匹配 #1903

Merged
merged 2 commits into from
Sep 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/com/alibaba/druid/sql/PagerUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ private static boolean limitMySqlQueryBlock(MySqlSelectQueryBlock queryBlock, St
if (rowCount <= count && offset <= 0) {
return false;
}
} else if (check && limit.getRowCount() instanceof SQLVariantRefExpr) {
return false;
}

limit.setRowCount(new SQLIntegerExpr(count));
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}

}