Merge pull request #858 from wenshao/master

SQLUtils support addCondition & addSelectItem
This commit is contained in:
高铁
2015-06-07 18:42:35 +08:00
15 changed files with 450 additions and 7 deletions
@@ -23,6 +23,13 @@ import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr;
import com.alibaba.druid.sql.ast.expr.SQLBinaryOperator;
import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement;
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
import com.alibaba.druid.sql.ast.statement.SQLSelectQuery;
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
import com.alibaba.druid.sql.dialect.db2.visitor.DB2OutputVisitor;
import com.alibaba.druid.sql.dialect.db2.visitor.DB2SchemaStatVisitor;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor;
@@ -241,7 +248,7 @@ public class SQLUtils {
if (JdbcUtils.DB2.equals(dbType)) {
return new DB2OutputVisitor(out);
}
if (JdbcUtils.ODPS.equals(dbType)) {
return new OdpsOutputVisitor(out);
}
@@ -340,7 +347,7 @@ public class SQLUtils {
public static String translateOracleToMySql(String sql) {
List<SQLStatement> stmtList = toStatementList(sql, JdbcConstants.ORACLE);
StringBuilder out = new StringBuilder();
OracleToMySqlOutputVisitor visitor = new OracleToMySqlOutputVisitor(out, false);
for (int i = 0; i < stmtList.size(); ++i) {
@@ -349,6 +356,147 @@ public class SQLUtils {
String mysqlSql = out.toString();
return mysqlSql;
}
public static String addCondition(String sql, String condition, String dbType) {
String result = addCondition(sql, condition, SQLBinaryOperator.BooleanAnd, false, dbType);
return result;
}
public static String addCondition(String sql, String condition, SQLBinaryOperator op, boolean left, String dbType) {
if (sql == null) {
throw new IllegalArgumentException("sql is null");
}
if (condition == null) {
return sql;
}
if (op == null) {
op = SQLBinaryOperator.BooleanAnd;
}
if (op != SQLBinaryOperator.BooleanAnd //
&& op != SQLBinaryOperator.BooleanOr) {
throw new IllegalArgumentException("add condition not support : " + op);
}
List<SQLStatement> stmtList = parseStatements(sql, dbType);
if (stmtList.size() == 0) {
throw new IllegalArgumentException("not support empty-statement :" + sql);
}
if (stmtList.size() > 1) {
throw new IllegalArgumentException("not support multi-statement :" + sql);
}
SQLStatement stmt = stmtList.get(0);
SQLExpr conditionExpr = toSQLExpr(condition, dbType);
addCondition(stmt, op, conditionExpr, left);
return toSQLString(stmt, dbType);
}
public static void addCondition(SQLStatement stmt, SQLBinaryOperator op, SQLExpr condition, boolean left) {
if (stmt instanceof SQLSelectStatement) {
SQLSelectQuery query = ((SQLSelectStatement) stmt).getSelect().getQuery();
if (query instanceof SQLSelectQueryBlock) {
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) query;
SQLExpr newCondition = buildNewCondition(op, condition, left, queryBlock.getWhere());
queryBlock.setWhere(newCondition);
} else {
throw new IllegalArgumentException("add condition not support " + stmt.getClass().getName());
}
return;
}
if (stmt instanceof SQLDeleteStatement) {
SQLDeleteStatement delete = (SQLDeleteStatement) stmt;
SQLExpr newCondition = buildNewCondition(op, condition, left, delete.getWhere());
delete.setWhere(newCondition);
return;
}
if (stmt instanceof SQLUpdateStatement) {
SQLUpdateStatement update = (SQLUpdateStatement) stmt;
SQLExpr newCondition = buildNewCondition(op, condition, left, update.getWhere());
update.setWhere(newCondition);
return;
}
throw new IllegalArgumentException("add condition not support " + stmt.getClass().getName());
}
protected static SQLExpr buildNewCondition(SQLBinaryOperator op, SQLExpr condition, boolean left, SQLExpr where) {
if (where == null) {
return condition;
}
SQLBinaryOpExpr newCondition;
if (left) {
newCondition = new SQLBinaryOpExpr(condition, op, where);
} else {
newCondition = new SQLBinaryOpExpr(where, op, condition);
}
return newCondition;
}
public static String addSelectItem(String selectSql, String expr, String alias, String dbType) {
return addSelectItem(selectSql, expr, alias, false, dbType);
}
public static String addSelectItem(String selectSql, String expr, String alias, boolean first, String dbType) {
List<SQLStatement> stmtList = parseStatements(selectSql, dbType);
if (stmtList.size() == 0) {
throw new IllegalArgumentException("not support empty-statement :" + selectSql);
}
if (stmtList.size() > 1) {
throw new IllegalArgumentException("not support multi-statement :" + selectSql);
}
SQLStatement stmt = stmtList.get(0);
SQLExpr columnExpr = toSQLExpr(expr, dbType);
addSelectItem(stmt, columnExpr, alias, first);
return toSQLString(stmt, dbType);
}
public static void addSelectItem(SQLStatement stmt, SQLExpr expr, String alias, boolean first) {
if (expr == null) {
return;
}
if (stmt instanceof SQLSelectStatement) {
SQLSelectQuery query = ((SQLSelectStatement) stmt).getSelect().getQuery();
if (query instanceof SQLSelectQueryBlock) {
SQLSelectQueryBlock queryBlock = (SQLSelectQueryBlock) query;
addSelectItem(queryBlock, expr, alias, first);
} else {
throw new IllegalArgumentException("add condition not support " + stmt.getClass().getName());
}
return;
}
throw new IllegalArgumentException("add selectItem not support " + stmt.getClass().getName());
}
public static void addSelectItem(SQLSelectQueryBlock queryBlock, SQLExpr expr, String alias, boolean first) {
SQLSelectItem selectItem = new SQLSelectItem(expr, alias);
queryBlock.getSelectList().add(selectItem);
selectItem.setParent(selectItem);
}
}
@@ -2540,7 +2540,7 @@ public class MySqlStatementParser extends SQLStatementParser {
}
}
if (lexer.token() == Token.COMMA) {
if (parenFlag && lexer.token() == Token.COMMA) {
lexer.nextToken();
continue;
}
@@ -296,9 +296,11 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
if (x.getWhere() != null) {
println();
incrementIndent();
print("WHERE ");
x.getWhere().setParent(x);
x.getWhere().accept(this);
decrementIndent();
}
if (x.getReturning() != null) {
@@ -770,8 +772,10 @@ public class OracleOutputVisitor extends SQLASTOutputVisitor implements OracleAS
if (x.getWhere() != null) {
println();
print("WHERE ");
incrementIndent();
x.getWhere().setParent(x);
x.getWhere().accept(this);
decrementIndent();
}
if (x.getReturning().size() > 0) {
@@ -308,8 +308,10 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
if (x.getWhere() != null) {
println();
print("WHERE ");
incrementIndent();
x.getWhere().setParent(x);
x.getWhere().accept(this);
decrementIndent();
}
if (x.isReturning()) {
@@ -426,8 +428,10 @@ public class PGOutputVisitor extends SQLASTOutputVisitor implements PGASTVisitor
if (x.getWhere() != null) {
println();
print("WHERE ");
incrementIndent();
x.getWhere().setParent(x);
x.getWhere().accept(this);
decrementIndent();
}
if (x.getReturning().size() > 0) {
@@ -245,8 +245,10 @@ public class SQLServerOutputVisitor extends SQLASTOutputVisitor implements SQLSe
if (x.getWhere() != null) {
println();
print("WHERE ");
incrementIndent();
x.getWhere().setParent(x);
x.getWhere().accept(this);
decrementIndent();
}
return false;
@@ -722,9 +722,16 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
}
x.getExpr().accept(this);
if ((x.getAlias() != null) && (x.getAlias().length() > 0)) {
String alias = x.getAlias();
if (alias != null && alias.length() > 0) {
print(" AS ");
print(x.getAlias());
if (alias.indexOf(' ') == -1 || alias.charAt(0) == '"' || alias.charAt(0) == '\'') {
print(alias);
} else {
print('"');
print(alias);
print('"');
}
}
return false;
}
@@ -948,9 +955,12 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
x.getTableName().accept(this);
if (x.getWhere() != null) {
print(" WHERE ");
println();
print("WHERE ");
incrementIndent();
x.getWhere().setParent(x);
x.getWhere().accept(this);
decrementIndent();
}
return false;
@@ -1023,9 +1033,11 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
if (x.getWhere() != null) {
println();
incrementIndent();
print("WHERE ");
x.getWhere().setParent(x);
x.getWhere().accept(this);
decrementIndent();
}
return false;
@@ -0,0 +1,26 @@
package com.alibaba.druid.bvt.sql.mysql;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
public class SQLUtilsAddColumnTest extends TestCase {
public void test_select() throws Exception {
Assert.assertEquals("SELECT id, name" //
+ "\nFROM t", SQLUtils.addSelectItem("select id from t", "name", null, null));
}
public void test_select_1() throws Exception {
Assert.assertEquals("SELECT id, name AS XX" //
+ "\nFROM t", SQLUtils.addSelectItem("select id from t", "name", "XX", null));
}
public void test_select_2() throws Exception {
Assert.assertEquals("SELECT id, name AS \"XX W\"" //
+ "\nFROM t", SQLUtils.addSelectItem("select id from t", "name", "XX W", null));
}
}
@@ -0,0 +1,48 @@
package com.alibaba.druid.bvt.sql.mysql;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
public class SQLUtilsAddConditionTest extends TestCase {
public void test_select() throws Exception {
Assert.assertEquals("SELECT *" //
+ "\nFROM t" //
+ "\nWHERE id = 0", SQLUtils.addCondition("select * from t", "id = 0", null));
}
public void test_select_1() throws Exception {
Assert.assertEquals("SELECT *" //
+ "\nFROM t" //
+ "\nWHERE id = 0" //
+ "\n\tAND name = 'aaa'", SQLUtils.addCondition("select * from t where id = 0", "name = 'aaa'", null));
}
public void test_delete() throws Exception {
Assert.assertEquals("DELETE FROM t" //
+ "\nWHERE id = 0", SQLUtils.addCondition("delete from t", "id = 0", null));
}
public void test_delete_1() throws Exception {
Assert.assertEquals("DELETE FROM t" //
+ "\nWHERE id = 0" //
+ "\n\tAND name = 'aaa'", SQLUtils.addCondition("delete from t where id = 0", "name = 'aaa'", null));
}
public void test_update() throws Exception {
Assert.assertEquals("UPDATE t"//
+ "\nSET f1 = ?" //
+ "\nWHERE id = 0", SQLUtils.addCondition("update t set f1 = ?", "id = 0", null));
}
public void test_update_1() throws Exception {
Assert.assertEquals("UPDATE t"//
+ "\nSET f1 = ?" //
+ "\nWHERE id = 0"
+ "\n\tAND name = 'bb'", SQLUtils.addCondition("update t set f1 = ? where id = 0", "name = 'bb'", null));
}
}
@@ -0,0 +1,48 @@
package com.alibaba.druid.bvt.sql.mysql;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
public class SQLUtilsAddConditionTest_mysql extends TestCase {
public void test_select() throws Exception {
Assert.assertEquals("SELECT *" //
+ "\nFROM t" //
+ "\nWHERE id = 0", SQLUtils.addCondition("select * from t", "id = 0", "mysql"));
}
public void test_select_1() throws Exception {
Assert.assertEquals("SELECT *" //
+ "\nFROM t" //
+ "\nWHERE id = 0" //
+ "\n\tAND name = 'aaa'", SQLUtils.addCondition("select * from t where id = 0", "name = 'aaa'", "mysql"));
}
public void test_delete() throws Exception {
Assert.assertEquals("DELETE FROM t" //
+ "\nWHERE id = 0", SQLUtils.addCondition("delete from t", "id = 0", "mysql"));
}
public void test_delete_1() throws Exception {
Assert.assertEquals("DELETE FROM t" //
+ "\nWHERE id = 0" //
+ "\n\tAND name = 'aaa'", SQLUtils.addCondition("delete from t where id = 0", "name = 'aaa'", "mysql"));
}
public void test_update() throws Exception {
Assert.assertEquals("UPDATE t"//
+ "\nSET f1 = ?" //
+ "\nWHERE id = 0", SQLUtils.addCondition("update t set f1 = ?", "id = 0", "mysql"));
}
public void test_update_1() throws Exception {
Assert.assertEquals("UPDATE t"//
+ "\nSET f1 = ?" //
+ "\nWHERE id = 0"
+ "\n\tAND name = 'bb'", SQLUtils.addCondition("update t set f1 = ? where id = 0", "name = 'bb'", "mysql"));
}
}
@@ -0,0 +1,48 @@
package com.alibaba.druid.bvt.sql.mysql;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
public class SQLUtilsAddConditionTest_oracle extends TestCase {
public void test_select() throws Exception {
Assert.assertEquals("SELECT *" //
+ "\nFROM t" //
+ "\nWHERE id = 0", SQLUtils.addCondition("select * from t", "id = 0", "oracle"));
}
public void test_select_1() throws Exception {
Assert.assertEquals("SELECT *" //
+ "\nFROM t" //
+ "\nWHERE id = 0" //
+ "\n\tAND name = 'aaa'", SQLUtils.addCondition("select * from t where id = 0", "name = 'aaa'", "oracle"));
}
public void test_delete() throws Exception {
Assert.assertEquals("DELETE FROM t" //
+ "\nWHERE id = 0", SQLUtils.addCondition("delete from t", "id = 0", "oracle"));
}
public void test_delete_1() throws Exception {
Assert.assertEquals("DELETE FROM t" //
+ "\nWHERE id = 0" //
+ "\n\tAND name = 'aaa'", SQLUtils.addCondition("delete from t where id = 0", "name = 'aaa'", "oracle"));
}
public void test_update() throws Exception {
Assert.assertEquals("UPDATE t"//
+ "\nSET f1 = ?" //
+ "\nWHERE id = 0", SQLUtils.addCondition("update t set f1 = ?", "id = 0", "oracle"));
}
public void test_update_1() throws Exception {
Assert.assertEquals("UPDATE t"//
+ "\nSET f1 = ?" //
+ "\nWHERE id = 0"
+ "\n\tAND name = 'bb'", SQLUtils.addCondition("update t set f1 = ? where id = 0", "name = 'bb'", "oracle"));
}
}
@@ -0,0 +1,48 @@
package com.alibaba.druid.bvt.sql.mysql;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
public class SQLUtilsAddConditionTest_pg extends TestCase {
public void test_select() throws Exception {
Assert.assertEquals("SELECT *" //
+ "\nFROM t" //
+ "\nWHERE id = 0", SQLUtils.addCondition("select * from t", "id = 0", "postgresql"));
}
public void test_select_1() throws Exception {
Assert.assertEquals("SELECT *" //
+ "\nFROM t" //
+ "\nWHERE id = 0" //
+ "\n\tAND name = 'aaa'", SQLUtils.addCondition("select * from t where id = 0", "name = 'aaa'", "postgresql"));
}
public void test_delete() throws Exception {
Assert.assertEquals("DELETE FROM t" //
+ "\nWHERE id = 0", SQLUtils.addCondition("delete from t", "id = 0", "postgresql"));
}
public void test_delete_1() throws Exception {
Assert.assertEquals("DELETE FROM t" //
+ "\nWHERE id = 0" //
+ "\n\tAND name = 'aaa'", SQLUtils.addCondition("delete from t where id = 0", "name = 'aaa'", "postgresql"));
}
public void test_update() throws Exception {
Assert.assertEquals("UPDATE t"//
+ "\nSET f1 = ?" //
+ "\nWHERE id = 0", SQLUtils.addCondition("update t set f1 = ?", "id = 0", "postgresql"));
}
public void test_update_1() throws Exception {
Assert.assertEquals("UPDATE t"//
+ "\nSET f1 = ?" //
+ "\nWHERE id = 0"
+ "\n\tAND name = 'bb'", SQLUtils.addCondition("update t set f1 = ? where id = 0", "name = 'bb'", "postgresql"));
}
}
@@ -0,0 +1,48 @@
package com.alibaba.druid.bvt.sql.mysql;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
public class SQLUtilsAddConditionTest_sqlserver extends TestCase {
public void test_select() throws Exception {
Assert.assertEquals("SELECT *" //
+ "\nFROM t" //
+ "\nWHERE id = 0", SQLUtils.addCondition("select * from t", "id = 0", "sqlserver"));
}
public void test_select_1() throws Exception {
Assert.assertEquals("SELECT *" //
+ "\nFROM t" //
+ "\nWHERE id = 0" //
+ "\n\tAND name = 'aaa'", SQLUtils.addCondition("select * from t where id = 0", "name = 'aaa'", "sqlserver"));
}
public void test_delete() throws Exception {
Assert.assertEquals("DELETE FROM t" //
+ "\nWHERE id = 0", SQLUtils.addCondition("delete from t", "id = 0", "sqlserver"));
}
public void test_delete_1() throws Exception {
Assert.assertEquals("DELETE FROM t" //
+ "\nWHERE id = 0" //
+ "\n\tAND name = 'aaa'", SQLUtils.addCondition("delete from t where id = 0", "name = 'aaa'", "sqlserver"));
}
public void test_update() throws Exception {
Assert.assertEquals("UPDATE t"//
+ "\nSET f1 = ?" //
+ "\nWHERE id = 0", SQLUtils.addCondition("update t set f1 = ?", "id = 0", "sqlserver"));
}
public void test_update_1() throws Exception {
Assert.assertEquals("UPDATE t"//
+ "\nSET f1 = ?" //
+ "\nWHERE id = 0"
+ "\n\tAND name = 'bb'", SQLUtils.addCondition("update t set f1 = ? where id = 0", "name = 'bb'", "sqlserver"));
}
}
@@ -49,7 +49,7 @@ public class SQLServerUpdateTest extends TestCase {
"\n\t\tGROUP BY a.FId" +
"\n\t\t) b" +
"\nWHERE a.FId = b.FId" +
"\nAND a.FId = ?";
"\n\tAND a.FId = ?";
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
SQLStatement stmt = parser.parseStatementList().get(0);
@@ -51,7 +51,9 @@ public class DruidStatServiceTest extends TestCase {
private DruidDataSource dataSource;
@Override
protected void setUp() throws Exception {
DruidStatService.getInstance().setResetEnable(true);
// DruidStatService is singleton, reset all for other testcase.
DruidStatService.getInstance().service("/reset-all.json");
// clear web app.
@@ -127,6 +129,10 @@ public class DruidStatServiceTest extends TestCase {
}
public void test_statService_getDataSourceList() throws Exception {
{
DruidStatService.getInstance().service("/reset-all.json");
}
String sql = "select 1";
Connection conn = dataSource.getConnection();
@@ -155,6 +155,7 @@ public class DruidStatServiceTest2 extends TestCase {
}
public void test_statService_getDataSourceList() throws Exception {
DruidStatService.getInstance().service("/reset-all.json");
String result = DruidStatService.getInstance().service("/datasource.json");
Map<String, Object> resultMap = (Map<String, Object>) JSONUtils.parse(result);
List<Map<String, Object>> dataSourceList = (List<Map<String, Object>>) resultMap.get("Content");