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,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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user