package com.test.objectStudy;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Table {
public static void main(String[] args) {
String textString="select *from emp where job =c";
String teString="select *from (select *from (select *from emp where job =d) emp where job =b)where job =c";
String pattern2="(\\(select\\s*)(.+)(\\))";
Pattern rPattern=Pattern.compile(pattern2);
Matcher matcher=rPattern.matcher(teString);
if(matcher.find()) {
PareParsql(teString);
}
getColumTable("");
}
//单表无子查询解析
public static void singleParsql(String sql) {
String pattern="(select)(.+)(from)(.+)(where)(.+)";
Pattern rPattern=Pattern.compile(pattern);
Matcher matcher=rPattern.matcher(sql);
if(matcher.find()) {
String condition=matcher.group(6);
String table=matcher.group(2);
System.out.println("table"+ table+" ,condition"+condition);
}
}
//有子查询解析
public static void PareParsql(String sql) {
String resut="";
String pattern="(\\(select\\s*)(.+)(\\))";
Pattern rPattern=Pattern.compile(pattern);
Matcher matcher=rPattern.matcher(sql);
if(matcher.find()) {
String temp=matcher.group(0);
resut=temp.substring(1,temp.length()-1);
}
Pattern rPattern2=Pattern.compile(pattern);
Matcher matcher2=rPattern2.matcher(resut);
if(matcher2.find()) {
PareParsql(resut);
}
System.out.println("temp:"+resut);
}
}
}