This commit is contained in:
wenshao
2013-02-27 10:58:42 +08:00
19 changed files with 1026 additions and 118 deletions
@@ -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 TenantSelectTest4 extends TestCase {
private String sql = "SELECT a.*,b.name " + //
"FROM vote_info a left join vote_item b on a.item_id=b.id " + //
"where 1=1 limit 1,10";
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 a.*, b.name" + //
"\nFROM vote_info a" + //
"\n\tLEFT JOIN vote_item b ON b.tenant = 123" + //
"\n\t\tAND a.item_id = b.id" + //
"\nWHERE a.tenant = 123" + //
"\n\tAND 1 = 1" + //
"\nLIMIT 1, 10", resultSql);
}
}
@@ -0,0 +1,54 @@
package com.alibaba.druid.bvt.sql;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.PagerUtils;
import com.alibaba.druid.util.JdbcConstants;
public class PagerUtilsTest_Count_MySql_0 extends TestCase {
public void test_mysql_0() throws Exception {
String sql = "select * from t";
String result = PagerUtils.count(sql, JdbcConstants.MYSQL);
Assert.assertEquals("SELECT COUNT(*)\n" + //
"FROM t", result);
}
public void test_mysql_1() throws Exception {
String sql = "select id, name from t";
String result = PagerUtils.count(sql, JdbcConstants.MYSQL);
Assert.assertEquals("SELECT COUNT(*)\n" + //
"FROM t", result);
}
public void test_mysql_2() throws Exception {
String sql = "select id, name from t order by id";
String result = PagerUtils.count(sql, JdbcConstants.MYSQL);
Assert.assertEquals("SELECT COUNT(*)\n" + //
"FROM t", result);
}
public void test_mysql_group_0() throws Exception {
String sql = "select type, count(*) from t group by type";
String result = PagerUtils.count(sql, JdbcConstants.MYSQL);
Assert.assertEquals("SELECT COUNT(*)" + //
"\nFROM (SELECT type, COUNT(*)" + //
"\n\tFROM t" + //
"\n\tGROUP BY type" + //
"\n\t)", result);
}
public void test_mysql_union_0() throws Exception {
String sql = "select id, name from t1 union select id, name from t2 order by id";
String result = PagerUtils.count(sql, JdbcConstants.MYSQL);
Assert.assertEquals("SELECT COUNT(*)\n" + //
"FROM (SELECT id, name" + //
"\n\tFROM t1" + //
"\n\tUNION" + //
"\n\tSELECT id, name" + //
"\n\tFROM t2" + //
"\n\t)", result);
}
}
@@ -0,0 +1,54 @@
package com.alibaba.druid.bvt.sql;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.PagerUtils;
import com.alibaba.druid.util.JdbcConstants;
public class PagerUtilsTest_Count_Oracle_0 extends TestCase {
public void test_oracle_0() throws Exception {
String sql = "select * from t";
String result = PagerUtils.count(sql, JdbcConstants.ORACLE);
Assert.assertEquals("SELECT COUNT(*)\n" + //
"FROM t", result);
}
public void test_oracle_1() throws Exception {
String sql = "select id, name from t";
String result = PagerUtils.count(sql, JdbcConstants.ORACLE);
Assert.assertEquals("SELECT COUNT(*)\n" + //
"FROM t", result);
}
public void test_oracle_2() throws Exception {
String sql = "select id, name from t order by id";
String result = PagerUtils.count(sql, JdbcConstants.ORACLE);
Assert.assertEquals("SELECT COUNT(*)\n" + //
"FROM t", result);
}
public void test_oracle_group_0() throws Exception {
String sql = "select type, count(*) from t group by type";
String result = PagerUtils.count(sql, JdbcConstants.ORACLE);
Assert.assertEquals("SELECT COUNT(*)" + //
"\nFROM (SELECT type, COUNT(*)" + //
"\n\tFROM t" + //
"\n\tGROUP BY type" + //
"\n\t)", result);
}
public void test_oracle_union_0() throws Exception {
String sql = "select id, name from t1 union select id, name from t2 order by id";
String result = PagerUtils.count(sql, JdbcConstants.ORACLE);
Assert.assertEquals("SELECT COUNT(*)\n" + //
"FROM (SELECT id, name" + //
"\n\tFROM t1" + //
"\n\tUNION" + //
"\n\tSELECT id, name" + //
"\n\tFROM t2" + //
"\n\t)", result);
}
}
@@ -0,0 +1,54 @@
package com.alibaba.druid.bvt.sql;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.PagerUtils;
import com.alibaba.druid.util.JdbcConstants;
public class PagerUtilsTest_Count_PG_0 extends TestCase {
public void test_pg_0() throws Exception {
String sql = "select * from t";
String result = PagerUtils.count(sql, JdbcConstants.POSTGRESQL);
Assert.assertEquals("SELECT COUNT(*)\n" + //
"FROM t", result);
}
public void test_pg_1() throws Exception {
String sql = "select id, name from t";
String result = PagerUtils.count(sql, JdbcConstants.POSTGRESQL);
Assert.assertEquals("SELECT COUNT(*)\n" + //
"FROM t", result);
}
public void test_pg_2() throws Exception {
String sql = "select id, name from t order by id";
String result = PagerUtils.count(sql, JdbcConstants.POSTGRESQL);
Assert.assertEquals("SELECT COUNT(*)\n" + //
"FROM t", result);
}
public void test_pg_group_0() throws Exception {
String sql = "select type, count(*) from t group by type";
String result = PagerUtils.count(sql, JdbcConstants.POSTGRESQL);
Assert.assertEquals("SELECT COUNT(*)" + //
"\nFROM (SELECT type, COUNT(*)" + //
"\n\tFROM t" + //
"\n\tGROUP BY type" + //
"\n\t)", result);
}
public void test_pg_union_0() throws Exception {
String sql = "select id, name from t1 union select id, name from t2 order by id";
String result = PagerUtils.count(sql, JdbcConstants.POSTGRESQL);
Assert.assertEquals("SELECT COUNT(*)\n" + //
"FROM (SELECT id, name" + //
"\n\tFROM t1" + //
"\n\tUNION" + //
"\n\tSELECT id, name" + //
"\n\tFROM t2" + //
"\n\t)", result);
}
}
@@ -0,0 +1,54 @@
package com.alibaba.druid.bvt.sql;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.PagerUtils;
import com.alibaba.druid.util.JdbcConstants;
public class PagerUtilsTest_Count_SQLServer_0 extends TestCase {
public void test_sqlserver_0() throws Exception {
String sql = "select * from t";
String result = PagerUtils.count(sql, JdbcConstants.SQL_SERVER);
Assert.assertEquals("SELECT COUNT(*)\n" + //
"FROM t", result);
}
public void test_sqlserver_1() throws Exception {
String sql = "select id, name from t";
String result = PagerUtils.count(sql, JdbcConstants.SQL_SERVER);
Assert.assertEquals("SELECT COUNT(*)\n" + //
"FROM t", result);
}
public void test_sqlserver_2() throws Exception {
String sql = "select id, name from t order by id";
String result = PagerUtils.count(sql, JdbcConstants.SQL_SERVER);
Assert.assertEquals("SELECT COUNT(*)\n" + //
"FROM t", result);
}
public void test_sqlserver_group_0() throws Exception {
String sql = "select type, count(*) from t group by type";
String result = PagerUtils.count(sql, JdbcConstants.SQL_SERVER);
Assert.assertEquals("SELECT COUNT(*)" + //
"\nFROM (SELECT type, COUNT(*)" + //
"\n\tFROM t" + //
"\n\tGROUP BY type" + //
"\n\t)", result);
}
public void test_sqlserver_union_0() throws Exception {
String sql = "select id, name from t1 union select id, name from t2 order by id";
String result = PagerUtils.count(sql, JdbcConstants.SQL_SERVER);
Assert.assertEquals("SELECT COUNT(*)\n" + //
"FROM (SELECT id, name" + //
"\n\tFROM t1" + //
"\n\tUNION" + //
"\n\tSELECT id, name" + //
"\n\tFROM t2" + //
"\n\t)", result);
}
}
@@ -0,0 +1,44 @@
package com.alibaba.druid.bvt.sql;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.PagerUtils;
import com.alibaba.druid.util.JdbcConstants;
public class PagerUtilsTest_Limit_mysql_0 extends TestCase {
public void test_mysql_0() throws Exception {
String sql = "select * from t";
String result = PagerUtils.limit(sql, JdbcConstants.MYSQL, 0, 10);
Assert.assertEquals("SELECT *" + //
"\nFROM t" + //
"\nLIMIT 10", result);
}
public void test_mysql_1() throws Exception {
String sql = "select * from t";
String result = PagerUtils.limit(sql, JdbcConstants.MYSQL, 10, 10);
Assert.assertEquals("SELECT *" + //
"\nFROM t" + //
"\nLIMIT 10, 10", result);
}
public void test_mysql_2() throws Exception {
String sql = "select * from t";
String result = PagerUtils.limit(sql, JdbcConstants.MYSQL, 20, 10);
Assert.assertEquals("SELECT *" + //
"\nFROM t" + //
"\nLIMIT 20, 10", result);
}
public void test_mysql_3() throws Exception {
String sql = "select id, name, salary from t order by id, name";
String result = PagerUtils.limit(sql, JdbcConstants.MYSQL, 20, 10);
Assert.assertEquals("SELECT id, name, salary" + //
"\nFROM t" + //
"\nORDER BY id, name" + //
"\nLIMIT 20, 10", result);
}
}
@@ -0,0 +1,70 @@
package com.alibaba.druid.bvt.sql;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.PagerUtils;
import com.alibaba.druid.util.JdbcConstants;
public class PagerUtilsTest_Limit_oracle_0 extends TestCase {
public void test_oracle_oderby_0() throws Exception {
String sql = "select * from t order by id";
String result = PagerUtils.limit(sql, JdbcConstants.ORACLE, 0, 10);
Assert.assertEquals("SELECT _X.*, ROWNUM AS _RN" + //
"\nFROM (SELECT *" + //
"\n\tFROM t" + //
"\n\tORDER BY id" + //
"\n\t) _X" + //
"\nWHERE ROWNUM <= 10", result);
}
public void test_oracle_0() throws Exception {
String sql = "select * from t";
String result = PagerUtils.limit(sql, JdbcConstants.ORACLE, 0, 10);
Assert.assertEquals("SELECT *" + //
"\nFROM t" + //
"\nWHERE ROWNUM <= 10", result);
}
public void test_oracle_1() throws Exception {
String sql = "select * from t";
String result = PagerUtils.limit(sql, JdbcConstants.ORACLE, 10, 10);
Assert.assertEquals("SELECT *" + //
"\nFROM (SELECT _X.*, ROWNUM AS _RN" + //
"\n\tFROM (SELECT *" + //
"\n\t\tFROM t" + //
"\n\t\t) _X" + //
"\n\tWHERE ROWNUM <= 20" + //
"\n\t) _XX" + //
"\nWHERE _RN > 10", result);
}
public void test_oracle_2() throws Exception {
String sql = "select * from t";
String result = PagerUtils.limit(sql, JdbcConstants.ORACLE, 20, 10);
Assert.assertEquals("SELECT *" + //
"\nFROM (SELECT _X.*, ROWNUM AS _RN" + //
"\n\tFROM (SELECT *" + //
"\n\t\tFROM t" + //
"\n\t\t) _X" + //
"\n\tWHERE ROWNUM <= 30" + //
"\n\t) _XX" + //
"\nWHERE _RN > 20", result);
}
public void test_oracle_3() throws Exception {
String sql = "select id, name, salary from t order by id, name";
String result = PagerUtils.limit(sql, JdbcConstants.ORACLE, 20, 10);
Assert.assertEquals("SELECT *" + //
"\nFROM (SELECT _X.*, ROWNUM AS _RN" + //
"\n\tFROM (SELECT id, name, salary" + //
"\n\t\tFROM t" + //
"\n\t\tORDER BY id, name" + //
"\n\t\t) _X" + //
"\n\tWHERE ROWNUM <= 30" + //
"\n\t) _XX" + //
"\nWHERE _RN > 20", result);
}
}
@@ -0,0 +1,19 @@
package com.alibaba.druid.bvt.sql.eval;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.visitor.SQLEvalVisitorUtils;
import com.alibaba.druid.util.JdbcConstants;
public class EvalLikeTest extends TestCase {
public void test_like() throws Exception {
Assert.assertEquals(true, SQLEvalVisitorUtils.evalExpr(JdbcConstants.MYSQL, "'a' LIKE '[a-d]'"));
}
public void test_not_like() throws Exception {
Assert.assertEquals(false, SQLEvalVisitorUtils.evalExpr(JdbcConstants.MYSQL, "'a' NOT LIKE '[a-d]'"));
}
}
@@ -0,0 +1,58 @@
package com.alibaba.druid.bvt.sql.eval;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.visitor.SQLEvalVisitorUtils;
public class LikeTest extends TestCase {
public void test_any_0() throws Exception {
Assert.assertTrue(SQLEvalVisitorUtils.like("abcdefg", "%"));
}
public void test_any_1() throws Exception {
Assert.assertTrue(SQLEvalVisitorUtils.like("abcdefg", "%%"));
}
public void test_any_2() throws Exception {
Assert.assertTrue(SQLEvalVisitorUtils.like("abcdefg", "%b%"));
}
public void test_any_3() throws Exception {
Assert.assertTrue(SQLEvalVisitorUtils.like("abcdefg", "a%"));
}
public void test_any_4() throws Exception {
Assert.assertTrue(SQLEvalVisitorUtils.like("abcdefg", "%g"));
}
public void test_single_0() throws Exception {
Assert.assertTrue(SQLEvalVisitorUtils.like("a", "_"));
}
public void test_single_1() throws Exception {
Assert.assertTrue(SQLEvalVisitorUtils.like("ab", "__"));
}
public void test_single_2() throws Exception {
Assert.assertTrue(SQLEvalVisitorUtils.like("abcd", "a__d"));
}
public void test_single_3() throws Exception {
Assert.assertTrue(SQLEvalVisitorUtils.like("abcdf", "a__d_"));
}
public void test_range_0() throws Exception {
Assert.assertTrue(SQLEvalVisitorUtils.like("abcdf", "abcd[a-f]"));
}
public void test_range_1() throws Exception {
Assert.assertFalse(SQLEvalVisitorUtils.like("abcdf", "abcd[a-e]"));
}
public void test_range_2() throws Exception {
Assert.assertTrue(SQLEvalVisitorUtils.like("ab", "a[abf]"));
}
}
@@ -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.sql.sqlserver;
import junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser;
import com.alibaba.druid.sql.test.TestUtils;
public class SQLServerSelectTest12 extends TestCase {
public void test_simple() throws Exception {
String sql = "SELECT Row, Name " + //
"FROM(" + //
"SELECT ROW_NUMBER() OVER (ORDER BY ProductID) AS Row, Name " + //
"FROM Product " + //
") AS ProductsWithRowNumbers " + //
"WHERE Row >= 6 AND Row <= 10";
String expect = "SELECT Row, Name" + //
"\nFROM (SELECT ROW_NUMBER() OVER (ORDER BY ProductID) AS Row, Name" + //
"\n\tFROM Product" + //
"\n\t) ProductsWithRowNumbers" + //
"\nWHERE Row >= 6" + //
"\n\tAND Row <= 10";
SQLServerStatementParser parser = new SQLServerStatementParser(sql);
SQLStatement stmt = parser.parseStatementList().get(0);
String text = TestUtils.outputSqlServer(stmt);
Assert.assertEquals(expect, text);
System.out.println(text);
}
}