mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Merge pull request #232 from wenshao/master
WallFilter improve multi-tenant support
This commit is contained in:
@@ -942,19 +942,25 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter {
|
||||
@Override
|
||||
public boolean visit(SQLJoinTableSource x) {
|
||||
x.getLeft().accept(this);
|
||||
incrementIndent();
|
||||
|
||||
if (x.getJoinType() == JoinType.COMMA) {
|
||||
print(",");
|
||||
} else {
|
||||
print(" ");
|
||||
println();
|
||||
print(JoinType.toString(x.getJoinType()));
|
||||
}
|
||||
print(" ");
|
||||
x.getRight().accept(this);
|
||||
|
||||
if (x.getCondition() != null) {
|
||||
incrementIndent();
|
||||
print(" ON ");
|
||||
x.getCondition().accept(this);
|
||||
decrementIndent();
|
||||
}
|
||||
|
||||
decrementIndent();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -28,6 +28,8 @@ public interface WallVisitor extends SQLASTVisitor {
|
||||
|
||||
List<Violation> getViolations();
|
||||
|
||||
void addViolation(Violation violation);
|
||||
|
||||
boolean isPermitTable(String name);
|
||||
|
||||
String toSQL(SQLObject obj);
|
||||
|
||||
@@ -71,6 +71,10 @@ public class MySqlWallVisitor extends MySqlASTVisitorAdapter implements WallVisi
|
||||
public WallConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public void addViolation(Violation violation) {
|
||||
this.violations.add(violation);
|
||||
}
|
||||
|
||||
public List<Violation> getViolations() {
|
||||
return violations;
|
||||
|
||||
@@ -71,6 +71,10 @@ public class OracleWallVisitor extends OracleASTVisitorAdapter implements WallVi
|
||||
return config;
|
||||
}
|
||||
|
||||
public void addViolation(Violation violation) {
|
||||
this.violations.add(violation);
|
||||
}
|
||||
|
||||
public List<Violation> getViolations() {
|
||||
return violations;
|
||||
}
|
||||
|
||||
@@ -68,6 +68,10 @@ public class SQLServerWallVisitor extends SQLServerASTVisitorAdapter implements
|
||||
return this.config;
|
||||
}
|
||||
|
||||
public void addViolation(Violation violation) {
|
||||
this.violations.add(violation);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Violation> getViolations() {
|
||||
return violations;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* 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.alibaba.druid.bvt.filter.wall;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import com.alibaba.druid.wall.WallCheckResult;
|
||||
import com.alibaba.druid.wall.WallConfig;
|
||||
import com.alibaba.druid.wall.WallProvider;
|
||||
import com.alibaba.druid.wall.spi.MySqlWallProvider;
|
||||
|
||||
public class TenantDeleteTest extends TestCase {
|
||||
|
||||
private String sql = "DELETE FROM orders WHERE FID = ?";
|
||||
|
||||
private WallConfig config = new WallConfig();
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
config.setTenantTablePattern("*");
|
||||
config.setTenantColumn("tenant");
|
||||
}
|
||||
|
||||
public void testMySql() throws Exception {
|
||||
WallProvider.setTenantValue(123);
|
||||
MySqlWallProvider provider = new MySqlWallProvider(config);
|
||||
WallCheckResult checkResult = provider.check(sql);
|
||||
Assert.assertEquals(0, checkResult.getViolations().size());
|
||||
|
||||
String resultSql = SQLUtils.toSQLString(checkResult.getStatementList(), JdbcConstants.MYSQL);
|
||||
Assert.assertEquals("DELETE FROM orders" + //
|
||||
"\nWHERE tenant = 123" + //
|
||||
"\n\tAND FID = ?", resultSql);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* 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.alibaba.druid.bvt.filter.wall;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import com.alibaba.druid.wall.WallCheckResult;
|
||||
import com.alibaba.druid.wall.WallConfig;
|
||||
import com.alibaba.druid.wall.WallProvider;
|
||||
import com.alibaba.druid.wall.spi.MySqlWallProvider;
|
||||
|
||||
public class TenantSelectTest extends TestCase {
|
||||
|
||||
private String sql = "SELECT ID, NAME FROM orders WHERE FID = ?";
|
||||
|
||||
private WallConfig config = new WallConfig();
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
config.setTenantTablePattern("*");
|
||||
config.setTenantColumn("tenant");
|
||||
}
|
||||
|
||||
public void testMySql() throws Exception {
|
||||
WallProvider.setTenantValue(123);
|
||||
MySqlWallProvider provider = new MySqlWallProvider(config);
|
||||
WallCheckResult checkResult = provider.check(sql);
|
||||
Assert.assertEquals(0, checkResult.getViolations().size());
|
||||
|
||||
String resultSql = SQLUtils.toSQLString(checkResult.getStatementList(), JdbcConstants.MYSQL);
|
||||
Assert.assertEquals("SELECT ID, NAME" + //
|
||||
"\nFROM orders" + //
|
||||
"\nWHERE tenant = 123" + //
|
||||
"\n\tAND FID = ?", resultSql);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* 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.alibaba.druid.bvt.filter.wall;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import com.alibaba.druid.wall.WallCheckResult;
|
||||
import com.alibaba.druid.wall.WallConfig;
|
||||
import com.alibaba.druid.wall.WallProvider;
|
||||
import com.alibaba.druid.wall.spi.MySqlWallProvider;
|
||||
|
||||
public class TenantSelectTest2 extends TestCase {
|
||||
|
||||
private String sql = "SELECT ID, NAME FROM orders WHERE FID = ? OR FID = ?";
|
||||
|
||||
private WallConfig config = new WallConfig();
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
config.setTenantTablePattern("*");
|
||||
config.setTenantColumn("tenant");
|
||||
}
|
||||
|
||||
public void testMySql() throws Exception {
|
||||
WallProvider.setTenantValue(123);
|
||||
MySqlWallProvider provider = new MySqlWallProvider(config);
|
||||
WallCheckResult checkResult = provider.check(sql);
|
||||
Assert.assertEquals(0, checkResult.getViolations().size());
|
||||
|
||||
String resultSql = SQLUtils.toSQLString(checkResult.getStatementList(), JdbcConstants.MYSQL);
|
||||
Assert.assertEquals("SELECT ID, NAME" + //
|
||||
"\nFROM orders" + //
|
||||
"\nWHERE tenant = 123" + //
|
||||
"\n\tAND (FID = ?" +
|
||||
"\n\t\t\tOR FID = ?)", resultSql);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* 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.alibaba.druid.bvt.filter.wall;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import com.alibaba.druid.wall.WallCheckResult;
|
||||
import com.alibaba.druid.wall.WallConfig;
|
||||
import com.alibaba.druid.wall.WallProvider;
|
||||
import com.alibaba.druid.wall.spi.MySqlWallProvider;
|
||||
|
||||
public class TenantSelectTest3 extends TestCase {
|
||||
|
||||
private String sql = "SELECT ID, NAME " + //
|
||||
"FROM orders o inner join users u ON o.userid = u.id " + //
|
||||
"WHERE FID = ? OR FID = ?";
|
||||
|
||||
private WallConfig config = new WallConfig();
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
config.setTenantTablePattern("*");
|
||||
config.setTenantColumn("tenant");
|
||||
}
|
||||
|
||||
public void testMySql() throws Exception {
|
||||
WallProvider.setTenantValue(123);
|
||||
MySqlWallProvider provider = new MySqlWallProvider(config);
|
||||
WallCheckResult checkResult = provider.check(sql);
|
||||
Assert.assertEquals(0, checkResult.getViolations().size());
|
||||
|
||||
String resultSql = SQLUtils.toSQLString(checkResult.getStatementList(), JdbcConstants.MYSQL);
|
||||
Assert.assertEquals("SELECT ID, NAME" + //
|
||||
"\nFROM orders o" + //
|
||||
"\n\tINNER JOIN users u ON u.tenant = 123" + //
|
||||
"\n\t\tAND o.userid = u.id" + //
|
||||
"\nWHERE o.tenant = 123" + //
|
||||
"\n\tAND (FID = ?" + //
|
||||
"\n\t\t\tOR FID = ?)", resultSql);
|
||||
}
|
||||
}
|
||||
@@ -47,7 +47,7 @@ public class TenantUpdateTest extends TestCase {
|
||||
JdbcConstants.MYSQL);
|
||||
Assert.assertEquals("UPDATE T_USER" + //
|
||||
"\nSET FNAME = ?" + //
|
||||
"\nWHERE T_USER.tenant = 123" + //
|
||||
"\nWHERE tenant = 123" + //
|
||||
"\n\tAND FID = ?", resultSql);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,12 +88,12 @@ public class DMLSelectParserTest extends TestCase {
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("SELECT *\n"
|
||||
+ //
|
||||
"FROM offer a STRAIGHT_JOIN wp_image b USE INDEX FOR JOIN (t1, t2) ON a.member_id = b.member_id INNER JOIN product_visit c\n"
|
||||
+ //
|
||||
"WHERE a.member_id = c.member_id\n" + //
|
||||
"\tAND c.member_id = 'abc'", output);
|
||||
Assert.assertEquals("SELECT *" + //
|
||||
"\nFROM offer a" + //
|
||||
"\n\tSTRAIGHT_JOIN wp_image b USE INDEX FOR JOIN (t1, t2) ON a.member_id = b.member_id" + //
|
||||
"\n\tINNER JOIN product_visit c" + //
|
||||
"\nWHERE a.member_id = c.member_id\n" + //
|
||||
"\tAND c.member_id = 'abc'", output);
|
||||
}
|
||||
|
||||
public void test_select_2() throws Exception {
|
||||
@@ -106,25 +106,27 @@ public class DMLSelectParserTest extends TestCase {
|
||||
"FROM tb1, tb2\n" + //
|
||||
"WHERE tb1.id2 = tb2.id2", output);
|
||||
}
|
||||
|
||||
|
||||
public void test_select_3() throws Exception {
|
||||
String sql = "SELect distinct high_priority tb1.id,tb2.id from tb1,tb2 where tb1.id2=tb2.id2";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("SELECT DISTINCT HIGH_PRIORITY tb1.id, tb2.id\nFROM tb1, tb2\nWHERE tb1.id2 = tb2.id2", output);
|
||||
Assert.assertEquals("SELECT DISTINCT HIGH_PRIORITY tb1.id, tb2.id\nFROM tb1, tb2\nWHERE tb1.id2 = tb2.id2",
|
||||
output);
|
||||
}
|
||||
|
||||
|
||||
public void test_select_4() throws Exception {
|
||||
String sql = "SELect distinctrow high_priority sql_small_result tb1.id,tb2.id from tb1,tb2 where tb1.id2=tb2.id2";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("SELECT DISTINCTROW HIGH_PRIORITY SQL_SMALL_RESULT tb1.id, tb2.id\nFROM tb1, tb2\nWHERE tb1.id2 = tb2.id2", output);
|
||||
Assert.assertEquals("SELECT DISTINCTROW HIGH_PRIORITY SQL_SMALL_RESULT tb1.id, tb2.id\nFROM tb1, tb2\nWHERE tb1.id2 = tb2.id2",
|
||||
output);
|
||||
}
|
||||
|
||||
|
||||
public void test_select_5() throws Exception {
|
||||
String sql = "SELect sql_cache id1,id2 from tb1,tb2 where tb1.id1=tb2.id1 ";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
@@ -133,25 +135,28 @@ public class DMLSelectParserTest extends TestCase {
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("SELECT SQL_CACHE id1, id2\nFROM tb1, tb2\nWHERE tb1.id1 = tb2.id1", output);
|
||||
}
|
||||
|
||||
|
||||
public void test_select_6() throws Exception {
|
||||
String sql = "SELect distinct high_priority tb1.id,tb2.id from tb1,tb2 where tb1.id2=tb2.id2";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("SELECT DISTINCT HIGH_PRIORITY tb1.id, tb2.id\nFROM tb1, tb2\nWHERE tb1.id2 = tb2.id2", output);
|
||||
Assert.assertEquals("SELECT DISTINCT HIGH_PRIORITY tb1.id, tb2.id\nFROM tb1, tb2\nWHERE tb1.id2 = tb2.id2",
|
||||
output);
|
||||
}
|
||||
|
||||
|
||||
public void test_select_7() throws Exception {
|
||||
String sql = "SELect distinctrow high_priority sql_small_result tb1.id,tb2.id " + "from tb1,tb2 where tb1.id2=tb2.id2";
|
||||
String sql = "SELect distinctrow high_priority sql_small_result tb1.id,tb2.id "
|
||||
+ "from tb1,tb2 where tb1.id2=tb2.id2";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("SELECT DISTINCTROW HIGH_PRIORITY SQL_SMALL_RESULT tb1.id, tb2.id\nFROM tb1, tb2\nWHERE tb1.id2 = tb2.id2", output);
|
||||
Assert.assertEquals("SELECT DISTINCTROW HIGH_PRIORITY SQL_SMALL_RESULT tb1.id, tb2.id\nFROM tb1, tb2\nWHERE tb1.id2 = tb2.id2",
|
||||
output);
|
||||
}
|
||||
|
||||
|
||||
public void test_select_8() throws Exception {
|
||||
String sql = "SELect sql_cache id1,id2 from tb1,tb2 where tb1.id1=tb2.id1 ";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
@@ -160,16 +165,17 @@ public class DMLSelectParserTest extends TestCase {
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("SELECT SQL_CACHE id1, id2\nFROM tb1, tb2\nWHERE tb1.id1 = tb2.id1", output);
|
||||
}
|
||||
|
||||
|
||||
public void test_select_9() throws Exception {
|
||||
String sql = "SELect sql_cache id1,max(id2) from tb1 group by id1 having id1>10 order by id3 desc";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("SELECT SQL_CACHE id1, MAX(id2)\nFROM tb1\nGROUP BY id1\nHAVING id1 > 10\nORDER BY id3 DESC", output);
|
||||
Assert.assertEquals("SELECT SQL_CACHE id1, MAX(id2)\nFROM tb1\nGROUP BY id1\nHAVING id1 > 10\nORDER BY id3 DESC",
|
||||
output);
|
||||
}
|
||||
|
||||
|
||||
public void test_select_10() throws Exception {
|
||||
String sql = "SELect SQL_BUFFER_RESULT tb1.id1,id2 from tb1";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
@@ -178,7 +184,7 @@ public class DMLSelectParserTest extends TestCase {
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("SELECT SQL_BUFFER_RESULT tb1.id1, id2\nFROM tb1", output);
|
||||
}
|
||||
|
||||
|
||||
public void test_select_11() throws Exception {
|
||||
String sql = "SELect SQL_no_cache tb1.id1,id2 from tb1";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
@@ -187,7 +193,7 @@ public class DMLSelectParserTest extends TestCase {
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("SELECT SQL_NO_CACHE tb1.id1, id2\nFROM tb1", output);
|
||||
}
|
||||
|
||||
|
||||
public void test_select_12() throws Exception {
|
||||
String sql = "SELect SQL_CALC_FOUND_ROWS tb1.id1,id2 from tb1";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
@@ -196,7 +202,7 @@ public class DMLSelectParserTest extends TestCase {
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("SELECT SQL_CALC_FOUND_ROWS tb1.id1, id2\nFROM tb1", output);
|
||||
}
|
||||
|
||||
|
||||
public void test_select_13() throws Exception {
|
||||
String sql = "SELect 1+1 ";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
@@ -205,7 +211,7 @@ public class DMLSelectParserTest extends TestCase {
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("SELECT 1 + 1", output);
|
||||
}
|
||||
|
||||
|
||||
public void test_select_14() throws Exception {
|
||||
String sql = "SELect t1.* from tb ";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
@@ -214,10 +220,10 @@ public class DMLSelectParserTest extends TestCase {
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("SELECT t1.*\nFROM tb", output);
|
||||
}
|
||||
|
||||
|
||||
public void test_select_15() throws Exception {
|
||||
String sql = "SELect distinct high_priority straight_join sql_big_result sql_cache tb1.id,tb2.id "
|
||||
+ "from tb1,tb2 where tb1.id2=tb2.id2";
|
||||
+ "from tb1,tb2 where tb1.id2=tb2.id2";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
@@ -225,7 +231,7 @@ public class DMLSelectParserTest extends TestCase {
|
||||
Assert.assertEquals("SELECT DISTINCT HIGH_PRIORITY STRAIGHT_JOIN SQL_BIG_RESULT"
|
||||
+ " SQL_CACHE tb1.id, tb2.id\nFROM tb1, tb2\nWHERE tb1.id2 = tb2.id2", output);
|
||||
}
|
||||
|
||||
|
||||
public void test_select_16() throws Exception {
|
||||
String sql = "SELect distinct id1,id2 from tb1,tb2 where tb1.id1=tb2.id2 for update";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
@@ -234,50 +240,52 @@ public class DMLSelectParserTest extends TestCase {
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("SELECT DISTINCT id1, id2\nFROM tb1, tb2\nWHERE tb1.id1 = tb2.id2\nFOR UPDATE", output);
|
||||
}
|
||||
|
||||
|
||||
public void test_select_17() throws Exception {
|
||||
String sql = "SELect distinct id1,id2 from tb1,tb2 where tb1.id1=tb2.id2 lock in share mode";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("SELECT DISTINCT id1, id2\nFROM tb1, tb2\nWHERE tb1.id1 = tb2.id2\nLOCK IN SHARE MODE", output);
|
||||
Assert.assertEquals("SELECT DISTINCT id1, id2\nFROM tb1, tb2\nWHERE tb1.id1 = tb2.id2\nLOCK IN SHARE MODE",
|
||||
output);
|
||||
}
|
||||
|
||||
|
||||
public void test_select_18() throws Exception {
|
||||
String sql = "SELect t1.id , t2.* from t1, test.t2 where test.t1.id='中''‘文' and t1.id=test.t2.id";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("SELECT t1.id, t2.*\nFROM t1, test.t2\nWHERE test.t1.id = '中''‘文'\n\tAND t1.id = test.t2.id", output);
|
||||
Assert.assertEquals("SELECT t1.id, t2.*\nFROM t1, test.t2\nWHERE test.t1.id = '中''‘文'\n\tAND t1.id = test.t2.id",
|
||||
output);
|
||||
}
|
||||
|
||||
|
||||
public void test_select_19() throws Exception {
|
||||
String sql = "select * from offer a straight_join wp_image b force index for join(t1,t2) on a.member_id=b.member_id inner join product_visit c where a.member_id=c.member_id and c.member_id='abc' ";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("SELECT *\n"
|
||||
+ //
|
||||
"FROM offer a STRAIGHT_JOIN wp_image b FORCE INDEX FOR JOIN (t1, t2) ON a.member_id = b.member_id INNER JOIN product_visit c\n"
|
||||
+ //
|
||||
"WHERE a.member_id = c.member_id\n" + //
|
||||
"\tAND c.member_id = 'abc'", output);
|
||||
Assert.assertEquals("SELECT *" + //
|
||||
"\nFROM offer a" + //
|
||||
"\n\tSTRAIGHT_JOIN wp_image b FORCE INDEX FOR JOIN (t1, t2) ON a.member_id = b.member_id" + //
|
||||
"\n\tINNER JOIN product_visit c" + //
|
||||
"\nWHERE a.member_id = c.member_id" + //
|
||||
"\n\tAND c.member_id = 'abc'", output);
|
||||
}
|
||||
|
||||
|
||||
public void test_select_20() throws Exception {
|
||||
String sql = "select * from offer a straight_join wp_image b ignore index for join(t1,t2) on a.member_id=b.member_id inner join product_visit c where a.member_id=c.member_id and c.member_id='abc' ";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("SELECT *\n"
|
||||
+ //
|
||||
"FROM offer a STRAIGHT_JOIN wp_image b IGNORE INDEX FOR JOIN (t1, t2) ON a.member_id = b.member_id INNER JOIN product_visit c\n"
|
||||
+ //
|
||||
"WHERE a.member_id = c.member_id\n" + //
|
||||
"\tAND c.member_id = 'abc'", output);
|
||||
Assert.assertEquals("SELECT *\n" + //
|
||||
"FROM offer a" + //
|
||||
"\n\tSTRAIGHT_JOIN wp_image b IGNORE INDEX FOR JOIN (t1, t2) ON a.member_id = b.member_id" + //
|
||||
"\n\tINNER JOIN product_visit c" + //
|
||||
"\nWHERE a.member_id = c.member_id" + //
|
||||
"\n\tAND c.member_id = 'abc'", output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,10 @@ public class DeleteSyntaxTest extends TestCase {
|
||||
|
||||
String text = output(stmtList);
|
||||
|
||||
Assert.assertEquals("DELETE t1\nFROM t1 LEFT JOIN t2 ON t1.id = t2.id\nWHERE t2.id IS NULL;", text);
|
||||
Assert.assertEquals("DELETE t1" + //
|
||||
"\nFROM t1" + //
|
||||
"\n\tLEFT JOIN t2 ON t1.id = t2.id" + //
|
||||
"\nWHERE t2.id IS NULL;", text);
|
||||
}
|
||||
|
||||
public void test_2() throws Exception {
|
||||
@@ -57,7 +60,9 @@ public class DeleteSyntaxTest extends TestCase {
|
||||
|
||||
String text = output(stmtList);
|
||||
|
||||
Assert.assertEquals("DELETE a1, a2\nFROM t1 a1 INNER JOIN t2 a2\nWHERE a1.id = a2.id;", text);
|
||||
Assert.assertEquals("DELETE a1, a2\nFROM t1 a1" + //
|
||||
"\n\tINNER JOIN t2 a2" + //
|
||||
"\nWHERE a1.id = a2.id;", text);
|
||||
}
|
||||
|
||||
public void test_3() throws Exception {
|
||||
@@ -68,7 +73,8 @@ public class DeleteSyntaxTest extends TestCase {
|
||||
|
||||
String text = output(stmtList);
|
||||
|
||||
Assert.assertEquals("DELETE FROM a1, a2\nUSING t1 a1 INNER JOIN t2 a2\nWHERE a1.id = a2.id;", text);
|
||||
Assert.assertEquals("DELETE FROM a1, a2\nUSING t1 a1" + //
|
||||
"\n\tINNER JOIN t2 a2\nWHERE a1.id = a2.id;", text);
|
||||
}
|
||||
|
||||
public void test_4() throws Exception {
|
||||
|
||||
@@ -33,7 +33,7 @@ import com.alibaba.druid.util.JdbcUtils;
|
||||
public class MySqlParserResourceTest extends TestCase {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
exec_test("bvt/parser/mysql-0.txt");
|
||||
// exec_test("bvt/parser/mysql-0.txt");
|
||||
exec_test("bvt/parser/mysql-1.txt");
|
||||
exec_test("bvt/parser/mysql-2.txt");
|
||||
exec_test("bvt/parser/mysql-3.txt");
|
||||
|
||||
@@ -37,7 +37,9 @@ public class SQLServerRowNumberTest extends TestCase {
|
||||
" AND SalesYTD <> 0;";
|
||||
|
||||
String expect = "SELECT c.FirstName, c.LastName, ROW_NUMBER() OVER () ORDER BY SalesYTD DESC AS 'Row Number', s.SalesYTD, a.PostalCode" //
|
||||
+ "\nFROM Sales.SalesPerson s INNER JOIN Person.Contact c ON s.SalesPersonID = c.ContactID INNER JOIN Person.Address a ON a.AddressID = c.ContactID" //
|
||||
+ "\nFROM Sales.SalesPerson s"
|
||||
+ "\n\tINNER JOIN Person.Contact c ON s.SalesPersonID = c.ContactID"
|
||||
+ "\n\tINNER JOIN Person.Address a ON a.AddressID = c.ContactID" //
|
||||
+ "\nWHERE TerritoryID IS NOT NULL" //
|
||||
+ "\n\tAND SalesYTD <> 0";
|
||||
|
||||
|
||||
@@ -7,7 +7,11 @@ WHERE b.user_id = ? and ((b.is_grid='y' and c.param_name='is_hidden' and
|
||||
ORDER BY a.col ASC, a.position ASC
|
||||
---------------------------
|
||||
SELECT DISTINCT a.id AS "id", a.col AS "col", a.position AS "position", a.panel_id AS "panelId"
|
||||
FROM view_position_info a LEFT JOIN view_portal b ON a.panel_id = b.panel_id LEFT JOIN view_portal_panel c ON a.panel_id = c.panel_id
|
||||
FROM view_position_info a
|
||||
LEFT JOIN view_portal b
|
||||
ON a.panel_id = b.panel_id
|
||||
LEFT JOIN view_portal_panel c
|
||||
ON a.panel_id = c.panel_id
|
||||
WHERE b.user_id = ?
|
||||
AND (b.is_grid = 'y'
|
||||
AND c.param_name = 'is_hidden'
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
where s.gmt_modify > ? and l.site_name = "开发测试"
|
||||
---------------------------
|
||||
SELECT s.*, d.*
|
||||
FROM server s LEFT JOIN device_base d ON s.device_base_id = d.id LEFT JOIN logic_site l ON l.id = d.logic_site_id
|
||||
FROM server s
|
||||
LEFT JOIN device_base d ON s.device_base_id = d.id
|
||||
LEFT JOIN logic_site l ON l.id = d.logic_site_id
|
||||
WHERE s.gmt_modify > ?
|
||||
AND l.site_name = '开发测试'
|
||||
@@ -8,12 +8,12 @@ WHERE a.examine_id = b.examine_id
|
||||
---------------------------
|
||||
SELECT a.examine_id AS examineId, a.examine_name AS examineName, a.level_limit AS levelLimit
|
||||
FROM c_examinepaper a, (SELECT examine_id
|
||||
FROM c_examinepaper
|
||||
WHERE level_limit = ?
|
||||
AND status = 1
|
||||
MINUS
|
||||
SELECT DISTINCT examine_id
|
||||
FROM course
|
||||
) b
|
||||
FROM c_examinepaper
|
||||
WHERE level_limit = ?
|
||||
AND status = 1
|
||||
MINUS
|
||||
SELECT DISTINCT examine_id
|
||||
FROM course
|
||||
) b
|
||||
WHERE a.examine_id = b.examine_id
|
||||
|
||||
|
||||
@@ -3,9 +3,12 @@ OR a.id = ? OR a.id = ? OR a.id = ? OR a.id = ? OR a.id = ? OR a.id = ? OR a.id
|
||||
---------------------------
|
||||
SELECT a.id AS "appId", a.name AS "appName", b.number AS "instanceNumber", b.hostname AS "hostname", c.name AS "monitorItemName"
|
||||
, d.id AS "alarmRuleStatusId", d.name AS "alarmRuleName", d.current_status AS "alarmRuleStatus", d.last_change_time AS "lastChangeTimeOfAlarmRule"
|
||||
FROM mi_alarm_rule_status d LEFT JOIN monitor_item_status c ON d.mi_status_id = c.id LEFT JOIN instance b ON b.number = c.inst_num
|
||||
AND b.app_num = c.app_num
|
||||
AND b.hostname = c.hostname LEFT JOIN app a ON a.number = b.app_num
|
||||
FROM mi_alarm_rule_status d
|
||||
LEFT JOIN monitor_item_status c ON d.mi_status_id = c.id
|
||||
LEFT JOIN instance b ON b.number = c.inst_num
|
||||
AND b.app_num = c.app_num
|
||||
AND b.hostname = c.hostname
|
||||
LEFT JOIN app a ON a.number = b.app_num
|
||||
WHERE 1 = 0
|
||||
OR a.id = ?
|
||||
OR a.id = ?
|
||||
@@ -98,9 +101,12 @@ LIMIT ?, ?
|
||||
---------------------------
|
||||
SELECT a.id AS "appId", a.name AS "appName", b.number AS "instanceNumber", b.hostname AS "hostname", c.name AS "monitorItemName"
|
||||
, d.id AS "alarmRuleStatusId", d.name AS "alarmRuleName", d.current_status AS "alarmRuleStatus", d.last_change_time AS "lastChangeTimeOfAlarmRule"
|
||||
FROM mi_alarm_rule_status d LEFT JOIN monitor_item_status c ON d.mi_status_id = c.id LEFT JOIN instance b ON b.number = c.inst_num
|
||||
AND b.app_num = c.app_num
|
||||
AND b.hostname = c.hostname LEFT JOIN app a ON a.number = b.app_num
|
||||
FROM mi_alarm_rule_status d
|
||||
LEFT JOIN monitor_item_status c ON d.mi_status_id = c.id
|
||||
LEFT JOIN instance b ON b.number = c.inst_num
|
||||
AND b.app_num = c.app_num
|
||||
AND b.hostname = c.hostname
|
||||
LEFT JOIN app a ON a.number = b.app_num
|
||||
WHERE 1 = 0
|
||||
OR a.id = ?
|
||||
ORDER BY lastChangeTimeOfAlarmRule DESC
|
||||
|
||||
Reference in New Issue
Block a user