oracle倒排索引sql,SQL语句反向分析工具?

写了一个StringParser类,比较烂,不过可以用。。。输入是Toad--Formatter Plus格式化过的SQL,输出是排序后的表名.列名 列表

import java.io.*;

import java.util.*;

public class StringParser {

public static void main(String[] args) {

try {

BufferedReader br = new BufferedReader(new InputStreamReader(

new FileInputStream("c:/1.sql&quot

9f7588d3b12cd5d674b5f81c0b8fc6cb.gif));

String data = null;

HashMap colHolder;

HashMap tabHolder;

Set keyCol;

Set keyTab;

String curKey;

HashMap fullColHolder = new HashMap();

HashMap fullTabHolder = new HashMap();

Set keyFinal;

HashMap finalRes = new HashMap();

while ((data = br.readLine()) != null) {

if (!data.toString().startsWith("/*&quot

9f7588d3b12cd5d674b5f81c0b8fc6cb.gif) { //ignore the first line generated by Toad formmater plus!

colHolder = getADotBHashMap(data);

keyCol = colHolder.keySet();

Iterator itCol = keyCol.iterator();

tabHolder = getTableHashMap(data);

keyTab = tabHolder.keySet();

Iterator itTab = keyTab.iterator();

while (itCol.hasNext()) {

curKey = (String) (itCol.next());

String curColumn = (String) colHolder.get(curKey);

fullColHolder.put(curKey, curColumn);

}

while (itTab.hasNext()) {

curKey = (String) (itTab.next());

String curTab = (String) tabHolder.get(curKey);

fullTabHolder.put(curKey, curTab);

}

}

// get the current line for loop processing

}

System.out.println("111111:"+fullTabHolder.size());

System.out.println("222222:"+fullColHolder.size());

keyCol = fullColHolder.keySet();

Iterator itCol = keyCol.iterator();

while (itCol.hasNext()) {

curKey = (String) (itCol.next());

String curColumn = ((String) fullColHolder.get(curKey)).trim();

keyTab = fullTabHolder.keySet();

Iterator itTab = keyTab.iterator();

while (itTab.hasNext()) {

String curTabKey = (String) (itTab.next());

String curTable = ((String) fullTabHolder.get(curTabKey)).trim();

if (curTable.substring(0, curTable.indexOf(".&quot

9f7588d3b12cd5d674b5f81c0b8fc6cb.gif)

.equalsIgnoreCase(

curColumn.substring(0, curColumn

.indexOf(".&quot

9f7588d3b12cd5d674b5f81c0b8fc6cb.gif))) {

String curVal = curTable.substring(curTable.indexOf(".&quot

9f7588d3b12cd5d674b5f81c0b8fc6cb.gif + 1,

curTable.length())

+ "."

+ curColumn.substring(curColumn.indexOf(".&quot

9f7588d3b12cd5d674b5f81c0b8fc6cb.gif+1,

curColumn.length());

finalRes.put(curVal, curVal);

}

}

}

TreeMap finalResSorted  = new TreeMap(finalRes);

keyFinal = finalResSorted.keySet();

Iterator itFinal = keyFinal.iterator();

while(itFinal.hasNext()){

curKey = (String)(itFinal.next());

System.out.println(finalResSorted.get(curKey));

}

} catch (FileNotFoundException e) {

System.out.println("File Not Found&quot

9f7588d3b12cd5d674b5f81c0b8fc6cb.gif;

} catch (IOException e1) {

e1.printStackTrace();

}

}

// read current line, from start to end, find A.B, put A.B into output array

// resume the rest of the current line, until finishes

public static String getADotB(String curLine) {

int posStart = 0;

int posEnd = 0;

for (int i = 0; i <= curLine.length(); i++) {

// first find the Dot

if (curLine.indexOf(".&quot

9f7588d3b12cd5d674b5f81c0b8fc6cb.gif >= 0) {

posStart = getPortion(curLine, curLine.indexOf(".&quot

9f7588d3b12cd5d674b5f81c0b8fc6cb.gif, 1);

posEnd = getPortion(curLine, curLine.indexOf(".&quot

9f7588d3b12cd5d674b5f81c0b8fc6cb.gif, 0);

}

return curLine.substring(posStart, posEnd);

}

return "null";

}

public static HashMap getADotBHashMap(String curLine) {

int posStart = 0;

int posEnd = 0;

HashMap hashADB = new HashMap();

for (int i = 0; i <= curLine.length(); i++) {

// first find the Dot

if (curLine.indexOf(".") >= 0) {

posStart = getPortion(curLine, curLine.indexOf("."), 1);

posEnd = getPortion(curLine, curLine.indexOf("."), 0);

String curColumn = curLine.substring(posStart, posEnd);

hashADB.put(curLine + posEnd, curColumn);

}

String subLine = curLine.substring(posEnd, curLine.length());

if (subLine.indexOf(".") >= 0) {

posStart = getPortion(subLine, subLine.indexOf("."), 1);

posEnd = getPortion(subLine, subLine.indexOf("."), 0);

hashADB.put(subLine + posEnd, subLine.substring(posStart,

posEnd));

}

}

return hashADB;

}

public static HashMap getTableHashMap(String curLine) {

int posStart = 0;

int posEnd = 0;

String tableAlias;

String tableName;

HashMap hashTableName = new HashMap();

for (int i = 0; i <= curLine.length(); i++) {

if (curLine.indexOf("FROM") >= 0) {

posStart = StringParser.getPortion(curLine, curLine

.indexOf("FROM") + 3, 0);

posEnd = StringParser.getPortion(curLine, posStart, 0);

tableName = curLine.substring(posStart + 1, posEnd);

posStart = StringParser.getPortion(curLine, curLine

.indexOf("FROM") + 5, 0);

posEnd = StringParser.getPortion(curLine, posStart, 0);

tableAlias = curLine.substring(posStart, posEnd);

hashTableName.put(curLine + posEnd, tableAlias + "."+ tableName);

String subLine = curLine.substring(posEnd, curLine.length());

if (subLine.indexOf(",") >= 0) {

posStart = StringParser.getPortion(subLine, 0, 0);

posEnd = StringParser.getPortion(subLine, posStart, 0);

tableName = subLine.substring(posStart + 1, posEnd);

posStart = StringParser.getPortion(subLine, subLine

.indexOf(" "), 0);

posEnd = StringParser.getPortion(subLine, posStart, 0);

tableAlias = subLine.substring(posStart, posEnd);

hashTableName.put(curLine + posEnd, tableAlias + "."

+ tableName);

}

}

}

return hashTableName;

}

// Find the adjacent space, direction = 0 -- right, 1 --left

public static int getPortion(String curLine, int indOfDot, int direction) {

int posResult = 0;

if (direction == 0) {

for (int j = indOfDot + 1; j < curLine.length(); j++) {

if (foundToken(curLine.substring(j, j + 1))) {

// find the delimiter

posResult = j;

return posResult;

}

if (j == curLine.length() - 1) {

posResult = j;

return posResult + 1;

}

}

} else {

for (int i = indOfDot; i > 0; i--) {

if (foundToken(curLine.substring(i - 1, i))) {

// find the delimiter

return i;

}

}

}

return 991214;

}

// function for determine that char is part of A-Z a-Z 1-9

public static boolean foundToken(String curChar) {

String testCond = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXZY0123456789_";

if (testCond.indexOf(curChar) >= 0)

return false;

return true;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值