标签:line map ble parse nts rgs element shm string
import
java.util.HashMap;
import
java.util.Map;
import
java.util.Map.Entry;
public
class SqlStringParseDemo {
publicstaticvoid main(String[] args) {
String
sql = "Select {a.*},{b.*},{c.*} from tableA a,tableB b,tableC c";
// key: a, value: tableA a
Map<String, String>
m = new HashMap();
relevantAliasTable(
m, sql);
printAliasTable(
m);
}
privatestaticvoid printAliasTable(Map<String, String> m) {
for (Entry<String, String> obj : m.entrySet()) {
System.
out.println(obj.getKey() + " " + obj.getValue());
}
}
privatestaticvoid relevantAliasTable(Map<String, String> m, String sql) {
String[]
elementSelect = getElement(sql);
String[]
tableAliasFrom = getTableAlias(sql);
putElementsToMap(
m, elementSelect);
putTableAliasToMap(
m, tableAliasFrom);
}
privatestaticvoid putTableAliasToMap(Map<String, String> m, String[] tableAliasFrom) {
for(String obj:tableAliasFrom){
String[]
arr=obj.split(" ");
m.put("{"+arr[1]+".*" +"}",obj);
}
}
privatestaticvoid putElementsToMap(Map<String, String> m, String[] elementSelect) {
// for (String obj : elementSelect) {
//
// m.put(obj, "");
// }
//
// //System.out.println(m);
}
privatestatic String[] getTableAlias(String sql) {
intbeginIndexOfTableAlias = sql.indexOf("from") + "from".length() + 1;
intendIndexOfTableAlias = sql.length();
String
tableAlias = sql.substring(beginIndexOfTableAlias, endIndexOfTableAlias).trim();
String[]
arr = tableAlias.split(",");
returnarr;
}
privatestatic String[] getElement(String sql) {
intbeginIndexOfElement = "Select".length() + 1;
intendIndexOfElement = sql.indexOf("from");
String
tableAlias = sql.substring(beginIndexOfElement, endIndexOfElement).trim();
String[]
arr1 = tableAlias.split(",");
returnarr1;
}
}
标签:line map ble parse nts rgs element shm string
原文地址:http://www.cnblogs.com/mabel/p/6062255.html