码迷,mamicode.com
首页 > 数据库 > 详细

sql 语句对应关系显示 _set

时间:2016-11-28 14:42:13      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:imp   system   entity   []   new   str   string   ash   style   

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.HashSet;

import

java.util.Set;

 

public

class GetSqlQueryTest {

 

public static void main(String[] args) {

Set<SqlElementsWrapper> ss = getOutputElementEntityMap(

"select {a.*},{b.*},a.runId from TableA a,TableB b where a.runId=b.runId");

for (SqlElementsWrapper obj : ss) {

 

System.out.println(obj.toString());

}

}

 

private static Set<SqlElementsWrapper> getOutputElementEntityMap(String sql) {

Set<SqlElementsWrapper> set = getOutputAlias(sql);

for (SqlElementsWrapper wrapper : set) {

 

getTableName(sql, wrapper);

}

return set;

}

 

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(",");

for (String obj : arr) {

String[] objAlias = obj.split(" ");

String alias = objAlias[1];

if (wrapper.alias.equals(alias)) {

String tableName = objAlias[0];

wrapper.table = tableName;

 

}

 

}

 

}

 

private static Set<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(",");

 

Set<SqlElementsWrapper> set = new HashSet<SqlElementsWrapper>();

 

for (String output : arr) {

SqlElementsWrapper a = new SqlElementsWrapper();

a.oriOutput = output;

a.setAlias();

set.add(a);

}

return set;

}

 

}

sql 语句对应关系显示 _set

标签:imp   system   entity   []   new   str   string   ash   style   

原文地址:http://www.cnblogs.com/mabel/p/6109221.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!