A solution forced upon me at work

I believe that new partners in the workplace must have experienced some things in the group statistics, simple but tedious. Some time ago, I was given a task about which tables were used by users in various systems within the statistical group. When I first got this task, I completed it according to the previous thinking mode, directly searching the user name globally in the editing tool (because the tables of our company are all user names). Table name), I was ok with this on my first system because there were fewer users involved and fewer tables. So it is quickly completed step by step, but with the back of the system more and more, involving more users and tables, if I still do so, boring and easy to make mistakes. The main reason is laziness, which is a waste of time, so I want to think about an easier and more convenient way to get these things done. Getting help was out of the question, and all I wanted to get used to was writing code. So I looked into the feasibility of it.

  1. Use of theMybatisFramework, so SQL is stored in XML files. Storage address uniform.
  2. There’s a naming convention for company tables, basicallyUser name T_ User name _ Table name. Consider regular expression parsing

So basically the idea is clear

  1. The IO stream reads all the XML files that hold the SQL
  2. The regular expression pulls out the matching information
  3. withMap<String,List<String>>Stores each table based on the key for the user name

The first step is to write the code to read all the files in the folder:

 public static String getFileName(String path) throws IOException {
        File file = new File(path);
        File[] tempList = file.listFiles();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < tempList.length; i++) {
            ifString str2 =getFileName(tempList[I].getPath()); (tempList[I].isdirectory ()){String str2 =getFileName(tempList[I].getPath()); stringBuilder.append(str2); }else {
                String str = readFile(path+"/"+tempList[i].getName()); stringBuilder.append(str); }}return stringBuilder.toString();
    }

Copy the code

When I first wrote this code, I got stuck with the problem of having folders under folders, because maybe folder A has folder B, folder B has files C, and so on. And then it occurred to me that this is very similar to the combinatorial patterns that we’ve seen before, describing the relationship between parts and the whole. The idea is to make the use of single objects and composite objects consistent.

The second step here is about regular expression enhancement

public static String readFile(String fileName) throws IOException { BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));  String str; StringBuilder stringBuilder = new StringBuilder();while((str = bufferedReader.readLine()) ! = null){ stringBuilder.append(str.toUpperCase() +"\n");
      }
      bufferedReader.close();
      String str2 = stringBuilder.toString().toUpperCase().replaceAll("\t"."");
      String pattern = "T_[A-Z][A-Z][A-Z]_[A-Z_]+";
      Pattern r = Pattern.compile(pattern);
      Matcher matcher = r.matcher(str2);
      StringBuilder resultString = new StringBuilder();
      while (matcher.find()){
          String result= matcher.group(0);
          resultString.append(result+"\n");
      }
      return resultString.toString();
  }

Copy the code

The third step is to recalculate and categorize all the numbers presented

public static void printDate(String string){

      String [] ArrStr= string.split("\n");
      Set<String> set =new HashSet();
      Map<String,List<String>> map = new HashMap<>();
      for (int i = 0; i < ArrStr.length; i++) {
          set.add(ArrStr[i]);
      }
      for (String s:set) {if(! s.isEmpty()){ String qianzhui=s.substring(s.indexOf("_")+1,s.indexOf("_") + 4); String tableName= qianzhui+"."+s;
              if(! map.containsKey(qianzhui)){ List<String> list = new ArrayList<>(); list.add(tableName); map.put(qianzhui,list); }else{ List<String> resultList = map.get(qianzhui); resultList.add(tableName); map.put(qianzhui,resultList); }}}for (String str:map.keySet()){
          System.out.println(str);
          List myList = map.get(str);
          myList.forEach((resultStr)->{
              System.out.println(resultStr);
          });
          System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- --"); }}Copy the code

Although it took some time to write the code, it took 2 hours to write the code, and it could take 1 day to count all the systems, and the human statistics could be wrong. So it’s useful to spend that time coding. The most important thing is to exercise the thinking of solving problems. In the future, when encountering problems, we should think about what the optimal solution is rather than directly coming up with a problem, which may be thankless in the end.