package com.palacesun.masking.tools;

import com.palacesun.masking.vo.Columninfo;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

public class MysqlJdbc {
   public static void main(String[] args) {
      try {
         Class.forName("com.palacesun.engine.spy.MaskingDriver");
      } catch (Exception e) {
         e.printStackTrace();
      }

      try {
         Connection connect = DriverManager.getConnection("jdbc:mysql:masking://192.168.2.29:3306/ry?zeroDateTimeBehavior=convertToNull&useSSL=false", "ry", "123456");
         Columninfo field = new Columninfo();
         field.setColumnname("ry.testx.name");
         field.setEncryptiontype("SM4");
         field.setSecretkey("54D7C5CDED265552F3DC474DD43939BC");
         EngineUtil.encryptionMap.put("ry_testx_name", field);
         field = new Columninfo();
         field.setColumnname("ry.sys_dept.dept_name");
         field.setEncryptiontype("SM4");
         field.setSecretkey("9523C620396BCE6FC8C0F7E0C9CB8C47");
         EngineUtil.encryptionMap.put("ry_sys_dept_dept_name", field);
         executeSqlStatement(connect);
      } catch (Exception e) {
         e.printStackTrace();
      }

   }

   public static void showDatabase(Connection connection) throws SQLException {
      String sql = "show databases";
      PreparedStatement statement = connection.prepareStatement(sql);
      statement.executeQuery();
   }

   public static void executeSqlStatement(Connection connection) throws SQLException {
      String sql = "SELECT u.user_id, u.dept_id, u.login_name, u.user_name, u.user_type, u.email, u.avatar, u.phonenumber, u.password, u.sex, u.salt, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark, d.dept_name, d.leader FROM sys_user u LEFT JOIN sys_dept d ON u.dept_id = d.dept_id WHERE u.del_flag = '0' order by create_time desc";
      PreparedStatement statement = connection.prepareStatement(sql);
      statement.executeQuery(sql);
   }

   public static void executeInsertStatement(Connection connection) throws SQLException {
      String sql = "insert into testx(id,name,birth) values('1','test',sysdate())";
      Statement statement = connection.prepareStatement(sql);
      statement.execute(sql);
   }

   public static void executeUpdateStatement(Connection connection) throws SQLException {
      String sql = "update testx set name='123123' where id='1'";
      Statement statement = connection.prepareStatement(sql);
      statement.execute(sql);
   }

   public static void executeDeleteStatement(Connection connection) throws SQLException {
      String sql = "delete from testx where id='xx' or name='xxx'";
      Statement statement = connection.prepareStatement(sql);
      statement.execute(sql);
   }

   public static void executeInsert(Connection connection) throws SQLException {
      String sql = "insert into testx(id,name,birth) values('1','test',sysdate())";
      PreparedStatement statement = connection.prepareStatement(sql);
      statement.execute();
   }

   public static void executeUpdate(Connection connection) throws SQLException {
      String sql = "update testx set name='123123' where id='1'";
      PreparedStatement statement = connection.prepareStatement(sql);
      statement.execute();
   }

   public static void executeDelete(Connection connection) throws SQLException {
      String sql = "delete from testx where id='xx'";
      PreparedStatement statement = connection.prepareStatement(sql);
      statement.execute();
   }

   public static void executeInsertParams(Connection connection) throws SQLException {
      String sql = "insert into testx(id,name,birth) values(?,?,?)";
      PreparedStatement statement = connection.prepareStatement(sql);
      statement.setString(1, "1");
      statement.setString(2, "xxxxx");
      statement.setDate(3, new Date(1, 1, 1));
      statement.execute();
   }

   public static void executeUpdateParams(Connection connection) throws SQLException {
      String sql = "update testx set name=?,birth=? where id=? or name='234'";
      PreparedStatement statement = connection.prepareStatement(sql);
      statement.setString(1, "1");
      statement.setString(2, "ssss");
      statement.setDate(3, new Date(33, 1, 1));
      statement.execute();
   }

   public static void executeDeleteParams(Connection connection) throws SQLException {
      String sql = "delete from testx where id=? or name=?";
      PreparedStatement statement = connection.prepareStatement(sql);
      statement.setString(1, "11");
      statement.setString(2, "ssss");
      statement.execute();
   }
}
