InspectLobsterSubtree.java 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import java.io.*;
  2. import java.sql.*;
  3. public class InspectLobsterSubtree {
  4. static final String URL = "jdbc:mysql://cq-cdb-8fjmemkb.sql.tencentcdb.com:27220/fs_tenant_cs1?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8";
  5. public static void main(String[] args) throws Exception {
  6. Class.forName("com.mysql.cj.jdbc.Driver");
  7. new File("output").mkdirs();
  8. try (Connection c = DriverManager.getConnection(URL, "root", "Ylrz_1q2w3e4r5t6y");
  9. PrintWriter out = new PrintWriter(new OutputStreamWriter(
  10. new FileOutputStream("output/inspect-lobster-subtree.txt"), "UTF-8"))) {
  11. dumpRecursive(c, out, "sys_menu", 29900L);
  12. dumpRecursive(c, out, "company_menu", 29900L);
  13. }
  14. }
  15. static void dumpRecursive(Connection c, PrintWriter out, String table, long root) throws Exception {
  16. out.println("\n=== " + table + " recursive from " + root + " ===");
  17. Set<Long> ids = new java.util.LinkedHashSet<>();
  18. collect(c, table, root, ids);
  19. String in = ids.toString().replace('[', ' ').replace(']', ' ').trim().replace(" ", "");
  20. try (Statement st = c.createStatement();
  21. ResultSet rs = st.executeQuery(
  22. "SELECT menu_id, menu_name, parent_id, order_num, menu_type, visible, path, component "
  23. + "FROM " + table + " WHERE menu_id IN (" + in + ") ORDER BY parent_id, order_num, menu_id")) {
  24. int n = 0;
  25. while (rs.next()) {
  26. n++;
  27. out.printf("[%d] %s parent=%d type=%s visible=%s order=%d path=%s%n",
  28. rs.getLong(1), rs.getString(2), rs.getLong(3), rs.getString(5),
  29. rs.getString(6), rs.getInt(4), rs.getString(7));
  30. }
  31. out.println("count=" + n);
  32. }
  33. }
  34. static void collect(Connection c, String table, long id, Set<Long> ids) throws SQLException {
  35. if (!ids.add(id)) return;
  36. try (PreparedStatement ps = c.prepareStatement("SELECT menu_id FROM " + table + " WHERE parent_id=?")) {
  37. ps.setLong(1, id);
  38. try (ResultSet rs = ps.executeQuery()) {
  39. while (rs.next()) collect(c, table, rs.getLong(1), ids);
  40. }
  41. }
  42. }
  43. }