FixLobsterMenuGarbledNames.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import java.sql.*;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. /** Fix lobster menu names corrupted as literal u5de5... unicode escapes. */
  5. public class FixLobsterMenuGarbledNames {
  6. private static final String MASTER_HOST = "cq-cdb-8fjmemkb.sql.tencentcdb.com";
  7. private static final int MASTER_PORT = 27220;
  8. private static final String MASTER_USER = "root";
  9. private static final String MASTER_PASSWORD = "Ylrz_1q2w3e4r5t6y";
  10. private static final String MASTER_DB = "ylrz_saas";
  11. private static final String TEMPLATE = "\u5de5\u4f5c\u6d41\u6a21\u677f";
  12. private static final String EXECUTION = "\u5de5\u4f5c\u6d41\u6267\u884c";
  13. private static final String PROFILE = "\u5ba2\u6237\u753b\u50cf";
  14. public static void main(String[] args) throws Exception {
  15. try (Connection master = open(MASTER_DB)) {
  16. fixConn(master, "tenant_company_menu", true);
  17. fixConn(master, "sys_menu", true);
  18. for (Long tenantId : loadActiveTenantIds(master)) {
  19. TenantDb t = loadTenant(master, tenantId);
  20. if (t == null) continue;
  21. System.out.println("[TENANT " + tenantId + "] " + t.database);
  22. try (Connection c = openDb(t)) {
  23. int n = fixConn(c, "company_menu", false);
  24. n += fixConn(c, "sys_menu", false);
  25. System.out.println(" fixed_rows=" + n);
  26. verify(c);
  27. }
  28. }
  29. }
  30. }
  31. private static int fixConn(Connection conn, String table, boolean master) throws SQLException {
  32. int total = 0;
  33. total += fixByMenuId(conn, table, 32481, TEMPLATE);
  34. total += fixByMenuId(conn, table, 29903, TEMPLATE);
  35. total += fixByMenuId(conn, table, 29949, EXECUTION);
  36. total += fixByMenuId(conn, table, 29950, PROFILE);
  37. total += fixGarbledPattern(conn, table, "u5de5u4f5cu6d41u6a21u677f", TEMPLATE);
  38. total += fixGarbledPattern(conn, table, "u5de5u4f5cu6d41u6267u884c", EXECUTION);
  39. total += fixGarbledPattern(conn, table, "u5ba2u6237u753bu50cf", PROFILE);
  40. total += fixGarbledPattern(conn, table, "\\u5de5\\u4f5c\\u6d41\\u6a21\\u677f", TEMPLATE);
  41. total += fixGarbledPattern(conn, table, "\\u5de5\\u4f5c\\u6d41\\u6267\\u884c", EXECUTION);
  42. total += fixLikeGarbled(conn, table, TEMPLATE, EXECUTION, PROFILE);
  43. return total;
  44. }
  45. private static int fixByMenuId(Connection conn, String table, long menuId, String name) throws SQLException {
  46. String sql = "UPDATE " + table + " SET menu_name = ? WHERE menu_id = ?";
  47. try (PreparedStatement ps = conn.prepareStatement(sql)) {
  48. ps.setString(1, name);
  49. ps.setLong(2, menuId);
  50. return ps.executeUpdate();
  51. }
  52. }
  53. private static int fixGarbledPattern(Connection conn, String table, String bad, String good) throws SQLException {
  54. String sql = "UPDATE " + table + " SET menu_name = ? WHERE menu_name = ?";
  55. try (PreparedStatement ps = conn.prepareStatement(sql)) {
  56. ps.setString(1, good);
  57. ps.setString(2, bad);
  58. return ps.executeUpdate();
  59. }
  60. }
  61. private static int fixLikeGarbled(Connection conn, String table, String template, String execution, String profile)
  62. throws SQLException {
  63. int n = 0;
  64. n += fixLike(conn, table, "u5de5u4f5cu6d41u6a21%", template);
  65. n += fixLike(conn, table, "u5de5u4f5cu6d41u6267%", execution);
  66. n += fixLike(conn, table, "u5ba2u6237u753b%", profile);
  67. n += fixLike(conn, table, "\\\\u5de5\\\\u4f5c\\\\u6d41\\\\u6a21%", template);
  68. n += fixLike(conn, table, "\\\\u5de5\\\\u4f5c\\\\u6d41\\\\u6267%", execution);
  69. return n;
  70. }
  71. private static int fixLike(Connection conn, String table, String like, String good) throws SQLException {
  72. String sql = "UPDATE " + table + " SET menu_name = ? WHERE menu_name LIKE ?";
  73. try (PreparedStatement ps = conn.prepareStatement(sql)) {
  74. ps.setString(1, good);
  75. ps.setString(2, like);
  76. return ps.executeUpdate();
  77. }
  78. }
  79. private static void verify(Connection conn) throws SQLException {
  80. String sql = "SELECT menu_id, menu_name FROM company_menu "
  81. + "WHERE parent_id = 29900 AND menu_id IN (32481, 29949, 29950) ORDER BY menu_id";
  82. try (Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(sql)) {
  83. while (rs.next()) {
  84. System.out.println(" menu " + rs.getLong(1) + " = " + rs.getString(2));
  85. }
  86. }
  87. String bad = "SELECT COUNT(*) FROM company_menu WHERE menu_name LIKE 'u5de5%' OR menu_name LIKE '\\\\u5de5%'";
  88. try (Statement st = conn.createStatement(); ResultSet rs = st.executeQuery(bad)) {
  89. rs.next();
  90. System.out.println(" garbled_remaining=" + rs.getInt(1));
  91. }
  92. }
  93. private static List<Long> loadActiveTenantIds(Connection master) throws SQLException {
  94. List<Long> ids = new ArrayList<>();
  95. try (Statement st = master.createStatement();
  96. ResultSet rs = st.executeQuery("SELECT id FROM tenant_info WHERE status = 1 ORDER BY id")) {
  97. while (rs.next()) ids.add(rs.getLong(1));
  98. }
  99. return ids;
  100. }
  101. private static TenantDb loadTenant(Connection master, long tenantId) throws SQLException {
  102. try (PreparedStatement ps = master.prepareStatement(
  103. "SELECT db_url, db_account, db_pwd FROM tenant_info WHERE id = ? AND status = 1 LIMIT 1")) {
  104. ps.setLong(1, tenantId);
  105. try (ResultSet rs = ps.executeQuery()) {
  106. if (!rs.next()) return null;
  107. TenantDb t = new TenantDb();
  108. String url = rs.getString("db_url");
  109. String body = url.substring("jdbc:mysql://".length());
  110. int slash = body.indexOf('/');
  111. String hostPort = body.substring(0, slash);
  112. String db = body.substring(slash + 1).replaceAll("\\?.*", "");
  113. int colon = hostPort.indexOf(':');
  114. t.host = colon >= 0 ? hostPort.substring(0, colon) : hostPort;
  115. t.port = colon >= 0 ? Integer.parseInt(hostPort.substring(colon + 1)) : 3306;
  116. t.database = db;
  117. t.user = rs.getString("db_account");
  118. t.password = rs.getString("db_pwd");
  119. return t;
  120. }
  121. }
  122. }
  123. private static Connection open(String db) throws SQLException {
  124. return DriverManager.getConnection(
  125. "jdbc:mysql://" + MASTER_HOST + ":" + MASTER_PORT + "/" + db
  126. + "?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8",
  127. MASTER_USER, MASTER_PASSWORD);
  128. }
  129. private static Connection openDb(TenantDb t) throws SQLException {
  130. return DriverManager.getConnection(
  131. "jdbc:mysql://" + t.host + ":" + t.port + "/" + t.database
  132. + "?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8",
  133. t.user != null ? t.user : MASTER_USER,
  134. t.password != null ? t.password : MASTER_PASSWORD);
  135. }
  136. private static final class TenantDb {
  137. String host;
  138. int port;
  139. String database;
  140. String user;
  141. String password;
  142. }
  143. }