Sql对应关系显示_list
package
com20161125;
public
class SqlElementsWrapper {
/*
* oriOutput: {a.*}, {b.*}, a.runId
* alias: a, b
* table: TableA, TabelB
*/
String oriOutput;
String alias;
String table;
public void setAlias() {
if (oriOutput.indexOf("{") > -1) {
this.alias = oriOutput.replace("{", "").replace(".*}", "");
return;
}
this.alias = oriOutput.substring(0, oriOutput.indexOf("."));
}
@Override
public String toString() {
return this.oriOutput + " " + this.alias + " " + this.table;
}
}
package com20161125;
import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set;
public class GetSqlQueryTest4 {
public static void main(String[] args) {
List<SqlElementsWrapper> ss = getOutputElementEntityMap("select {b.*},{a.*},a.runId from TableA a Inner Join TableB b on a.runId=b.runId");
for (SqlElementsWrapper obj : ss) {
System.out.println(obj.toString());
}
}
private static List<SqlElementsWrapper> getOutputElementEntityMap(String sql) { List<SqlElementsWrapper> list = getOutputAlias(sql); for (SqlElementsWrapper wrapper : list) {
getTableName(sql, wrapper); } return list; }
private static void getTableName(String sql, SqlElementsWrapper wrapper) { int beginIndex = sql.indexOf("from") + "from".length(); int endIndex = sql.indexOf("where") == -1 ? sql.length() : sql.indexOf("where");
String popObjStr = sql.substring(beginIndex, endIndex).trim(); String[] arr = popObjStr.split(" ");
List<String> list = putElementsToList_between_from_where(arr); for (int i = 0; i < list.size(); i++) {
if (wrapper.alias.equals(list.get(i))) { String tableName = list.get(i-1); wrapper.table = tableName;
}
}
}
private static List<String> putElementsToList_between_from_where(String[] arr) { List<String> list=new ArrayList<String>(); for(int i=0; i<arr.length;i++){ if(!arr[i].equals("")){ list.add(arr[i]); } } return list; }
private static List<SqlElementsWrapper> getOutputAlias(String sql) { int beginIndex = sql.indexOf("select") + "select".length(); int endIndex = sql.indexOf("from");
String tempObjStr = sql.substring(beginIndex, endIndex).trim(); String[] arr = tempObjStr.split(",");
List<SqlElementsWrapper> list = new ArrayList<SqlElementsWrapper>();
for (String output : arr) { SqlElementsWrapper a = new SqlElementsWrapper(); a.oriOutput = output; a.setAlias(); list.add(a); }
// for(SqlElementsWrapper obj:set){ // System.out.println(obj); // } return list; }
}