Guest User

Untitled

a guest
May 3rd, 2017
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.82 KB | None | 0 0
  1.  
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.HashMap;
  9. import java.util.Map;
  10.  
  11. /**
  12.  * Created by piyush.mukati on 24/04/17.
  13.  */
  14. public class BadgerProcessDataChutiyapaCleaner
  15. {
  16.  
  17.     public static final String getpids = "select  entity.id as id, entity.processData_id as pid,entity.relayerTopic as topic,entity.type as type, CASE WHEN entity.schemaVersion like \"%.%\" THEN 'minor' ELSE 'major' END as version  from entity join process_data on entity.processData_id=process_data.id where entity_type='DART' and process_data.active=1\n";
  18.  
  19.     public static void main(String[] args) throws SQLException
  20.     {
  21.         Map<String, Integer> map = new HashMap<String, Integer>();
  22.         Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/b1_badger_local", "root", "");
  23.         ResultSet rs = conn.createStatement().executeQuery(getpids);
  24.         int removedCnt = 0;
  25.         while (rs.next()) {
  26.  
  27.             if (map.containsKey(getKey(rs))) {
  28.                 System.out.println("key " + getKey(rs));
  29.                 updateDb(conn, rs.getInt("pid"), rs.getInt("id"), map.get(getKey(rs)));
  30.                 updateOpDb(conn, rs.getInt("pid"), rs.getInt("id"), map.get(getKey(rs)));
  31.                 removedCnt++;
  32.             }
  33.             else {
  34.                 map.put(getKey(rs), rs.getInt("id"));
  35.             }
  36.         }
  37.         System.out.println("total active" + map.size());
  38.         System.out.println("total removed" + removedCnt);
  39.  
  40.         rs.close();
  41.     }
  42.  
  43.     private static void updateDb(Connection conn, Integer oldPid, Integer oldEid, Integer newEid) throws SQLException
  44.     {
  45.         String q = "";
  46.         // adding new dependency
  47.         ResultSet rs = conn.createStatement().executeQuery(String.format("select process_id from process_dependent where parent_id='%s'", oldEid));
  48.         while (rs.next()) {
  49.             try {
  50.                 q = String.format("update process_dependent set  parent_id ='%s' where parent_id='%s' and process_id='%s'", oldEid, newEid, rs.getInt("process_id"));
  51.                 conn.createStatement().executeUpdate(q);
  52.                 System.out.println("done: " + q);
  53.             }
  54.             catch (com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException e) {
  55.                 System.out.println("failed: " + q);
  56.             }
  57.         }
  58.         q = String.format("update process_data set active=0 where id=%s", oldPid);
  59.         conn.createStatement().executeUpdate(q);
  60.         System.out.println("done: " + q);
  61.  
  62.  
  63.     }
  64.  
  65.     private static void updateOpDb(Connection conn, Integer oldPid, Integer oldEid, Integer newEid) throws SQLException
  66.     {
  67.         String q = "";
  68.         // adding new dependency
  69.         ResultSet rs = conn.createStatement().executeQuery(String.format("select process_id from process_optional_dependent where optional_parent_id='%s'", oldEid));
  70.         while (rs.next()) {
  71.             try {
  72.                 q = String.format("update process_optional_dependent set  optional_parent_id ='%s' where optional_parent_id='%s' and process_id='%s'", oldEid, newEid, rs.getInt("process_id"));
  73.                 conn.createStatement().executeUpdate(q);
  74.                 System.out.println("done: " + q);
  75.             }
  76.             catch (Exception e) {
  77.                 System.out.println("failed: " + q);
  78.             }
  79.         }
  80.         q = String.format("update process_data set active=0 where id=%s", oldPid);
  81.         conn.createStatement().executeUpdate(q);
  82.         System.out.println("done: " + q);
  83.  
  84.  
  85.     }
  86.  
  87.     private static String getKey(ResultSet rs) throws SQLException
  88.     {
  89.         return rs.getString("type") + rs.getString("version") + rs.getString("topic");
  90.     }
  91. }
Add Comment
Please, Sign In to add comment