Parse table names in sql

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SqlTabName {

    public static void main(String[] args) throws Exception {
       
        String s = "select * from aaa";
       
        getStrings01();
        //SELECT 列名称(*所有列) FROM 表名称
        //SELECT 列名称 FROM 表名称 where 条件
//        System.out.println( matchSql( "select * from aaa ") );
// System.out.println( matchSql( "select id,name,password from bbb where id = 1 ") );
// //INSERT INTO table name VALUES (value 1, value 2,....)
// //INSERT INTO table_name (column1, column2,...) VALUES (value1, value2,....)
// System.out.println( matchSql("insert into ccc valuse(1,'neo' ,'password')") );
// System.out.println( matchSql("insert into ddd (id,name,password) valuses(1,'neo','password')") );
// // UPDATE table name SET column name = new value WHERE column name = some value
// System.out.println( matchSql("update eee set name = 'neo' where id = 1 ") );
// //DELETE FROM table name WHERE column name = value
// System.out.println( matchSql("delete from fff where id = 1 ") );

//        String sql = "delete from fff where id = 1 ";
//        String changedSql = changeDelete(sql);
//        System.out.println(changedSql);
    }
     private static void getStrings() throws Exception {
         String txt = "";
         String ss = "E:\\aa\\one.txt";
            File file = new File(ss);
            InputStreamReader read = new InputStreamReader(
                    new FileInputStream(file),"UTF-8");//考虑到编码格式
                    BufferedReader bufferedReader = new BufferedReader(read);
                    String lineTxt = null;
                    while((lineTxt = bufferedReader.readLine()) != null){
                        txt += lineTxt + "\n\t";
//                        System.out.println(lineTxt);
                    }
                    read.close();
//                    System.out.println(txt);
//            String str = "select * from aaa a left from bbb b on a.a = b.a ";
//            String sm = "select * from (select * from s.aaas)";
//            String pp = "\\*\\s+from\\s+[\\w]*\\.?[\\w]*\\.?\\[?(\\b\\w+)\\]?[\\r\\n\\s]*";
//            String diy = "\\s+(join|from)\\s+([(\\w+\\.)]?\\w+)[\\s^]*";
//                    System.out.println(txt);
            Pattern ps = Pattern.compile("(?ms)('(?:''|[^'])*')|--.*?$|/\\*.*?\\*/"); 
            String presult = ps.matcher(txt).replaceAll("$1");
//                    System.out.println(presult);
                    String diy = "\\s+(join|from)\\s+((\\w+\\.)\\w+)[\\s^]*";
//            String diy = "\\s+(join|from)\\s+(\\w+\\.?\\w*)[\\s^]*";
            Pattern p = Pattern.compile(diy);
            Matcher m = p.matcher(presult.toLowerCase());
            Set<String> strs = new HashSet<String>();
            while (m.find()) {
                strs.add(m.group(2));           
            }
            for (String s : strs){
                System.out.println(s.toUpperCase());
            }       
        }
     
    /**
     * @param sql lowcase
     * @return
     */
    public static String matchSql(String sql){
        Matcher matcher = null;
        //SELECT 列名称 FROM 表名称
        //SELECT * FROM 表名称
        if( sql.startsWith("select") ){
            matcher = Pattern.compile("select\\s.+from\\s(.+)where\\s(.*)").matcher(sql);
            if(matcher.find()){
                return matcher.group(1);
            }
        }
        //INSERT INTO 表名称 VALUES (值1, 值2,....)
        //INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....)
        if( sql.startsWith("insert") ){
            matcher = Pattern.compile("insert\\sinto\\s(.+)\\(.*\\)\\s.*").matcher(sql);
            if(matcher.find()){
                return matcher.group(1);
            }
        }
        //UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值
        if( sql.startsWith("update") ){
            matcher = Pattern.compile("update\\s(.+)set\\s.*").matcher(sql);
            if(matcher.find()){
                return matcher.group(1);
            }
        }
        //DELETE FROM 表名称 WHERE 列名称 = 值
        if( sql.startsWith("delete") ){
            matcher = Pattern.compile("delete\\sfrom\\s(.+)where\\s(.*)").matcher(sql);
            if(matcher.find()){
                return matcher.group(1);
            }
        }
        return null;
    }
   
    private static void getStrings01() throws Exception {
         String txt = "";
         String ss = "E:\\aa\\one.txt";
            File file = new File(ss);
            InputStreamReader read = new InputStreamReader(
                   new FileInputStream(file),"UTF-8");//考虑到编码格式
                   BufferedReader bufferedReader = new BufferedReader(read);
                   String lineTxt = null;
                   while((lineTxt = bufferedReader.readLine()) != null){
                       txt += lineTxt + "\n\t";
                   }
                   read.close();
           Pattern ps = Pattern.compile("(?ms)('(?:''|[^'])*')|--.*?$|/\\*.*?\\*/"); 
           String presult = ps.matcher(txt).replaceAll("$1");
// String diy = "\\s+(join|from)\\s+((\\w+\\.)\\w+)[\\s^]*"; //Get table with schema
            String diy = " \\s+(join|from)\\s+(\\w+\\.?\\w*)[\\s^]*"; // Get table with schema and table without schema
            Pattern p = Pattern .compile(diy);
            Matcher m = p.matcher(presult.toLowerCase());
            Set<String> strs = new HashSet<String>();
            while (m.find()) {
                strs.add(m.group (2));           
            }
            for (String s : strs){
                System.out.println(s.toUpperCase());
            }       
        }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326222649&siteId=291194637