mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
sql desensitize support.
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright 1999-2017 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.benckmark.sql;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor;
|
||||
import com.alibaba.druid.sql.test.TestUtils;
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import com.alibaba.druid.util.Utils;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.util.List;
|
||||
|
||||
public class MySqlPerfMain_schemaStat {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
System.out.println(System.getProperty("java.vm.name") + " " + System.getProperty("java.runtime.version"));
|
||||
List<String> arguments = ManagementFactory.getRuntimeMXBean().getInputArguments();
|
||||
System.out.println(arguments);
|
||||
|
||||
String sql = "select * from t ";
|
||||
for (int i = 0; i < 300; ++i) {
|
||||
if (i == 0) {
|
||||
sql += " WHERE ";
|
||||
} else {
|
||||
sql += " OR ";
|
||||
}
|
||||
sql += (" id = " + i);
|
||||
}
|
||||
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
perfMySql(sql);
|
||||
}
|
||||
}
|
||||
|
||||
static long perfMySql(String sql) {
|
||||
long startYGC = TestUtils.getYoungGC();
|
||||
long startYGCTime = TestUtils.getYoungGCTime();
|
||||
long startFGC = TestUtils.getFullGC();
|
||||
|
||||
long startMillis = System.currentTimeMillis();
|
||||
for (int i = 0; i < 100 * 1; ++i) {
|
||||
execMySql(sql);
|
||||
}
|
||||
long millis = System.currentTimeMillis() - startMillis;
|
||||
|
||||
long ygc = TestUtils.getYoungGC() - startYGC;
|
||||
long ygct = TestUtils.getYoungGCTime() - startYGCTime;
|
||||
long fgc = TestUtils.getFullGC() - startFGC;
|
||||
|
||||
System.out.println("MySql\t" + millis + ", ygc " + ygc + ", ygct " + ygct + ", fgc " + fgc);
|
||||
return millis;
|
||||
}
|
||||
|
||||
static String execMySql(String sql) {
|
||||
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
|
||||
SchemaStatVisitor statVisitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.MYSQL);
|
||||
|
||||
for (SQLStatement stmt : statementList) {
|
||||
stmt.accept(statVisitor);
|
||||
}
|
||||
// for (SQLStatement statement : statementList) {
|
||||
// statement.accept(visitor);
|
||||
// visitor.println();
|
||||
// }
|
||||
return "";
|
||||
}
|
||||
}
|
||||
@@ -37,7 +37,7 @@ public class MySqlWallTest87 extends TestCase {
|
||||
Assert.assertTrue(provider.checkValid(//
|
||||
"CREATE VIEW test.v AS SELECT * FROM t;"));
|
||||
|
||||
Assert.assertEquals(1, provider.getTableStats().size());
|
||||
Assert.assertEquals(2, provider.getTableStats().size());
|
||||
Assert.assertTrue(provider.getTableStats().containsKey("t"));
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.alibaba.druid.bvt.sql.oracle;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Created by wenshao on 19/06/2017.
|
||||
*/
|
||||
public class DesensitizationTest extends TestCase {
|
||||
public void test_for_desensitization() throws Exception {
|
||||
String sql = "select id, name from mytable where id = 3";
|
||||
|
||||
SQLUtils.FormatOption option = new SQLUtils.FormatOption();
|
||||
option.setDesensitize(true);
|
||||
option.setParameterized(true);
|
||||
|
||||
String desens_Sql = SQLUtils.format(sql, JdbcConstants.ORACLE, option);
|
||||
System.out.println(sql);
|
||||
System.out.println("-------------------");
|
||||
System.out.println(desens_Sql);
|
||||
|
||||
assertEquals("SELECT id, name\n" +
|
||||
"FROM T_771DB81F525E1E75\n" +
|
||||
"WHERE id = ?", desens_Sql);
|
||||
}
|
||||
|
||||
public void test_for_desensitization_1() throws Exception {
|
||||
String sql = "select id, name from \"mytable\" where id = 3";
|
||||
|
||||
SQLUtils.FormatOption option = new SQLUtils.FormatOption();
|
||||
option.setDesensitize(true);
|
||||
option.setParameterized(true);
|
||||
|
||||
String desens_Sql = SQLUtils.format(sql, JdbcConstants.ORACLE, option);
|
||||
System.out.println(sql);
|
||||
System.out.println("-------------------");
|
||||
System.out.println(desens_Sql);
|
||||
|
||||
assertEquals("SELECT id, name\n" +
|
||||
"FROM T_771DB81F525E1E75\n" +
|
||||
"WHERE id = ?", desens_Sql);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.alibaba.druid.bvt.sql.oracle;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Created by wenshao on 19/06/2017.
|
||||
*/
|
||||
public class DesensitizationTest_createTable extends TestCase {
|
||||
public void test_for_desensitization() throws Exception {
|
||||
String sql = "CREATE TABLE customers\n" +
|
||||
"( customer_id number(10) NOT NULL,\n" +
|
||||
" customer_name varchar2(50) NOT NULL,\n" +
|
||||
" city varchar2(50)\n" +
|
||||
");";
|
||||
|
||||
SQLUtils.FormatOption option = new SQLUtils.FormatOption();
|
||||
option.setDesensitize(true);
|
||||
option.setParameterized(true);
|
||||
|
||||
String desens_Sql = SQLUtils.format(sql, JdbcConstants.ORACLE, option);
|
||||
System.out.println(sql);
|
||||
System.out.println("-------------------");
|
||||
System.out.println(desens_Sql);
|
||||
|
||||
assertEquals("CREATE TABLE T_69BFC132C09344DE (\n" +
|
||||
"\tcustomer_id number(10) NOT NULL,\n" +
|
||||
"\tcustomer_name varchar2(50) NOT NULL,\n" +
|
||||
"\tcity varchar2(50)\n" +
|
||||
");", desens_Sql);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.alibaba.druid.bvt.sql.oracle;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by wenshao on 19/06/2017.
|
||||
*/
|
||||
public class DesensitizationTest_createView extends TestCase {
|
||||
public void test_for_desensitization() throws Exception {
|
||||
String sql = "CREATE VIEW sup_orders AS\n" +
|
||||
" SELECT suppliers.supplier_id, orders.quantity, orders.price\n" +
|
||||
" FROM suppliers\n" +
|
||||
" INNER JOIN orders\n" +
|
||||
" ON suppliers.supplier_id = orders.supplier_id\n" +
|
||||
" WHERE suppliers.supplier_name = 'Microsoft';";
|
||||
|
||||
SQLUtils.FormatOption option = new SQLUtils.FormatOption();
|
||||
option.setDesensitize(true);
|
||||
option.setParameterized(true);
|
||||
|
||||
String desens_Sql = SQLUtils.format(sql, JdbcConstants.ORACLE, option);
|
||||
System.out.println(sql);
|
||||
System.out.println("-------------------");
|
||||
System.out.println(desens_Sql);
|
||||
|
||||
assertEquals("CREATE VIEW T_14856F0E5B66E911\n" +
|
||||
"AS\n" +
|
||||
"SELECT suppliers.supplier_id, orders.quantity, orders.price\n" +
|
||||
"FROM T_126BF2439561E30E\n" +
|
||||
"\tINNER JOIN T_C5B90BBF325E96EC ON suppliers.supplier_id = orders.supplier_id \n" +
|
||||
"WHERE suppliers.supplier_name = ?;", desens_Sql);
|
||||
|
||||
Map<String, String> tableMapping = new HashMap<String, String>();
|
||||
tableMapping.put(SQLUtils.desensitizeTable("sup_orders"), "sup_orders");
|
||||
tableMapping.put(SQLUtils.desensitizeTable("suppliers"), "suppliers");
|
||||
|
||||
String restore_sql = SQLUtils.refactor(desens_Sql, JdbcConstants.ORACLE, tableMapping);
|
||||
System.out.println("-------------------");
|
||||
System.out.println(restore_sql);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.alibaba.druid.bvt.sql.oracle;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Created by wenshao on 19/06/2017.
|
||||
*/
|
||||
public class DesensitizationTest_delete extends TestCase {
|
||||
public void test_for_desensitization() throws Exception {
|
||||
String sql = "DELETE FROM customers\n" +
|
||||
"WHERE last_name = 'Smith';";
|
||||
|
||||
SQLUtils.FormatOption option = new SQLUtils.FormatOption();
|
||||
option.setDesensitize(true);
|
||||
option.setParameterized(true);
|
||||
|
||||
String desens_Sql = SQLUtils.format(sql, JdbcConstants.ORACLE, option);
|
||||
System.out.println(sql);
|
||||
System.out.println("-------------------");
|
||||
System.out.println(desens_Sql);
|
||||
|
||||
assertEquals("DELETE FROM T_69BFC132C09344DE\n" +
|
||||
"WHERE last_name = ?;", desens_Sql);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.alibaba.druid.bvt.sql.oracle;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Created by wenshao on 19/06/2017.
|
||||
*/
|
||||
public class DesensitizationTest_insert extends TestCase {
|
||||
public void test_for_desensitization() throws Exception {
|
||||
String sql = "INSERT INTO suppliers\n" +
|
||||
"(supplier_id, supplier_name)\n" +
|
||||
"SELECT account_no, name\n" +
|
||||
"FROM customers\n" +
|
||||
"WHERE customer_id > 5000;";
|
||||
|
||||
SQLUtils.FormatOption option = new SQLUtils.FormatOption();
|
||||
option.setDesensitize(true);
|
||||
option.setParameterized(true);
|
||||
|
||||
String desens_Sql = SQLUtils.format(sql, JdbcConstants.ORACLE, option);
|
||||
System.out.println(sql);
|
||||
System.out.println("-------------------");
|
||||
System.out.println(desens_Sql);
|
||||
|
||||
assertEquals("INSERT INTO T_126BF2439561E30E (supplier_id, supplier_name)\n" +
|
||||
"SELECT account_no, name\n" +
|
||||
"FROM T_69BFC132C09344DE\n" +
|
||||
"WHERE customer_id > ?;", desens_Sql);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.alibaba.druid.bvt.sql.oracle;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Created by wenshao on 19/06/2017.
|
||||
*/
|
||||
public class DesensitizationTest_update extends TestCase {
|
||||
public void test_for_desensitization() throws Exception {
|
||||
String sql = "UPDATE customers\n" +
|
||||
"SET last_name = 'Anderson'\n" +
|
||||
"WHERE customer_id = 5000;";
|
||||
|
||||
SQLUtils.FormatOption option = new SQLUtils.FormatOption();
|
||||
option.setDesensitize(true);
|
||||
option.setParameterized(true);
|
||||
|
||||
String desens_Sql = SQLUtils.format(sql, JdbcConstants.ORACLE, option);
|
||||
System.out.println(sql);
|
||||
System.out.println("-------------------");
|
||||
System.out.println(desens_Sql);
|
||||
|
||||
assertEquals("UPDATE T_69BFC132C09344DE\n" +
|
||||
"SET last_name = ?\n" +
|
||||
"WHERE customer_id = ?;", desens_Sql);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.alibaba.druid.bvt.utils;
|
||||
|
||||
import com.alibaba.druid.util.Utils;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* Created by wenshao on 19/06/2017.
|
||||
*/
|
||||
public class Fnv64_lower_test extends TestCase {
|
||||
public void test_fnv_lower() throws Exception {
|
||||
assertEquals(Utils.fnv_64_lower("Id"), Utils.fnv_64_lower("ID"));
|
||||
assertEquals(Utils.fnv_64_lower("id"), Utils.fnv_64_lower("ID"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user