improved sql parser.

This commit is contained in:
wenshao
2017-10-16 01:53:58 +08:00
parent 7fe9f468ac
commit c23bdc0907
4 changed files with 60 additions and 3 deletions
@@ -40,7 +40,16 @@ public class SQLServerExprParser extends SQLExprParser {
public final static long[] AGGREGATE_FUNCTIONS_CODES;
static {
String[] strings = { "AVG", "COUNT", "MAX", "MIN", "ROW_NUMBER", "STDDEV", "SUM" };
String[] strings = {
"AVG",
"COUNT",
"FIRST_VALUE",
"MAX",
"MIN",
"ROW_NUMBER",
"STDDEV",
"SUM"
};
AGGREGATE_FUNCTIONS_CODES = FnvHash.fnv1a_64_lower(strings, true);
AGGREGATE_FUNCTIONS = new String[AGGREGATE_FUNCTIONS_CODES.length];
for (String str : strings) {
@@ -34,10 +34,24 @@ public class ParameterizedOutputVisitorUtils {
};
public static String parameterize(String sql, String dbType) {
return parameterize(sql, dbType, null);
return parameterize(sql, dbType, null, null);
}
public static String parameterize(String sql, String dbType, SQLSelectListCache selectListCache) {
public static String parameterize(String sql
, String dbType
, SQLSelectListCache selectListCache) {
return parameterize(sql, dbType, selectListCache, null);
}
public static String parameterize(String sql
, String dbType
, List<Object> outParameters) {
return parameterize(sql, dbType, null, outParameters);
}
public static String parameterize(String sql
, String dbType
, SQLSelectListCache selectListCache, List<Object> outParameters) {
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, dbType, features);
if (selectListCache != null) {
@@ -51,6 +65,9 @@ public class ParameterizedOutputVisitorUtils {
StringBuilder out = new StringBuilder(sql.length());
ParameterizedVisitor visitor = createParameterizedOutputVisitor(out, dbType);
if (outParameters != null) {
visitor.setOutputParameters(outParameters);
}
for (int i = 0; i < statementList.size(); i++) {
if (i > 0) {
@@ -15,6 +15,8 @@
*/
package com.alibaba.druid.sql.visitor;
import java.util.List;
public interface ParameterizedVisitor extends PrintableVisitor {
int getReplaceCount();
@@ -23,4 +25,5 @@ public interface ParameterizedVisitor extends PrintableVisitor {
String getDbType();
void setOutputParameters(List<Object> parameters);
}
@@ -0,0 +1,28 @@
package com.alibaba.druid.bvt.sql.mysql.param;
import com.alibaba.druid.sql.visitor.ParameterizedOutputVisitorUtils;
import com.alibaba.druid.util.JdbcConstants;
import junit.framework.TestCase;
import java.util.ArrayList;
import java.util.List;
public class SQL_Parser_Parameterize_Test extends TestCase {
public void test_parameterized() throws Exception {
final String dbType = JdbcConstants.MYSQL;
List<Object> outParameters = new ArrayList<Object>();
String sql = "select * from t where id = 101 and age = 102 or name = 'wenshao'";
String psql = ParameterizedOutputVisitorUtils.parameterize(sql, dbType, outParameters);
assertEquals("SELECT *\n" +
"FROM t\n" +
"WHERE id = ?\n" +
"\tAND age = ?\n" +
"\tOR name = ?", psql);
assertEquals(3, outParameters.size());
assertEquals(101, outParameters.get(0));
assertEquals(102, outParameters.get(1));
assertEquals("wenshao", outParameters.get(2));
}
}