com.scudata.dm.query.PerfectSubquery Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of esproc Show documentation
Show all versions of esproc Show documentation
SPL(Structured Process Language) A programming language specially for structured data computing.
package com.scudata.dm.query;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.scudata.common.MD5;
import com.scudata.common.RQException;
import com.scudata.common.UUID;
public class PerfectSubquery //ֻ???from?Ӿ??е??Ӳ?ѯ???Ż?
{
public static boolean optimizeSubquery(Token[] tokens, List sqlTokenList, boolean isSubquery, List canOptimizeList)
{
if(sqlTokenList == null || canOptimizeList == null)
{
throw new RQException("");
}
boolean hasOptimized = false;
int unionPos = Tokenizer.scanKeyWords(new String[]{"UNION", "EXCEPT", "INTERSECT", "MINUS"}, tokens, 0, tokens.length);
if(unionPos >= 0)
{
while(unionPos >= 0)
{
Token[] leftTokens = Arrays.copyOfRange(tokens, 0, unionPos);
Token[] keyTokens = null;
if(tokens[unionPos].isKeyWord("UNION"))
{
if(unionPos + 1 < tokens.length && tokens[unionPos + 1].isKeyWord("ALL"))
{
keyTokens = new Token[2];
keyTokens[0] = tokens[unionPos];
keyTokens[1] = tokens[unionPos + 1];
}
else
{
keyTokens = new Token[1];
keyTokens[0] = tokens[unionPos];
}
}
else
{
keyTokens = new Token[1];
keyTokens[0] = tokens[unionPos];
}
Token[] rightTokens = null;
if(keyTokens.length == 1)
{
rightTokens = Arrays.copyOfRange(tokens, unionPos + 1, tokens.length);
}
else
{
rightTokens = Arrays.copyOfRange(tokens, unionPos + 2, tokens.length);
}
while(leftTokens[0].getType() == Tokenizer.LPAREN)//???????????
{
int parenPos = Tokenizer.scanParen(leftTokens, 0, leftTokens.length);
if(parenPos == leftTokens.length - 1)
{
leftTokens = Arrays.copyOfRange(leftTokens, 1, parenPos);
}
else
{
break;
}
}
Token[] bakTokens = SimpleSQL.copyTokens(leftTokens);//???????Ż?
List tokenList = new ArrayList();
boolean leftOptimize = optimizeSubquery(leftTokens, tokenList, isSubquery, new ArrayList());
if(!leftOptimize)
{
leftTokens = bakTokens;
}
else
{
leftTokens = new Token[tokenList.size()];
tokenList.toArray(leftTokens);
}
Token tk = new Token(Tokenizer.LPAREN, "(", -1, "(");
tk.addSpace();
sqlTokenList.add(tk);
sqlTokenList.addAll(Arrays.asList(leftTokens));
tk = new Token(Tokenizer.RPAREN, ")", -1, ")");
tk.addSpace();
sqlTokenList.add(tk);
sqlTokenList.addAll(Arrays.asList(keyTokens));
hasOptimized = hasOptimized || leftOptimize;
tokens = rightTokens;
unionPos = Tokenizer.scanKeyWords(new String[]{"UNION", "EXCEPT", "INTERSECT", "MINUS"}, tokens, 0, tokens.length);
if(unionPos < 0)
{
while(tokens[0].getType() == Tokenizer.LPAREN)//???????????
{
int parenPos = Tokenizer.scanParen(tokens, 0, tokens.length);
if(parenPos == tokens.length - 1)
{
tokens = Arrays.copyOfRange(tokens, 1, parenPos);
}
else
{
break;
}
}
bakTokens = SimpleSQL.copyTokens(tokens);//???????Ż?
tokenList = new ArrayList();
boolean rightOptimize = optimizeSubquery(tokens, tokenList, isSubquery, new ArrayList());
if(!rightOptimize)
{
tokens = bakTokens;
}
else
{
tokens = new Token[tokenList.size()];
tokenList.toArray(tokens);
}
tk = new Token(Tokenizer.LPAREN, "(", -1, "(");
tk.addSpace();
sqlTokenList.add(tk);
sqlTokenList.addAll(Arrays.asList(tokens));
tk = new Token(Tokenizer.RPAREN, ")", -1, ")");
tk.addSpace();
sqlTokenList.add(tk);
hasOptimized = hasOptimized || rightOptimize;
}
}
}
else
{
List> tokensListList = new ArrayList>();
hasOptimized = getSubquery(tokens, tokensListList, isSubquery, canOptimizeList);
List usedAliasList = new ArrayList();
if(tokensListList.size() > 1 && canOptimizeList.size() > 1)
{
for(List tokensList : tokensListList)
{
Token[] fromTokens = tokensList.get(2);
String tableAlias = null;
if(fromTokens.length >= 2)
{
if(fromTokens[fromTokens.length - 1].getType() == Tokenizer.IDENT
&& fromTokens[fromTokens.length - 2].getType() == Tokenizer.RPAREN)
{
tableAlias = fromTokens[fromTokens.length - 1].getString();
}
else if(fromTokens[fromTokens.length - 1].getType() == Tokenizer.IDENT
&& fromTokens[fromTokens.length - 2].getSpaces().length() >= 1
&& fromTokens[fromTokens.length - 2].getType() == Tokenizer.IDENT)
{
tableAlias = fromTokens[fromTokens.length - 1].getString();
}
}
if(tableAlias != null && tableAlias.isEmpty())
{
usedAliasList.add(tableAlias);
}
}
}
while(tokensListList.size() > 1 && canOptimizeList.size() > 1)
{
Boolean subCanOptimize = canOptimizeList.get(0);
Boolean canOptimize = canOptimizeList.get(1);
canOptimizeList.remove(subCanOptimize);
List subTokensList = tokensListList.get(0);
List tokensList = tokensListList.get(1);
tokensListList.remove(subTokensList);
tokensListList.remove(tokensList);
Token[] selectTokens = tokensList.get(0);
Token[] parallelTokens = tokensList.get(1);
Token[] fromTokens = tokensList.get(2);
Token[] whereTokens = tokensList.get(3);
Token[] subSelectTokens = subTokensList.get(0);
Token[] subParallelTokens = subTokensList.get(1);
Token[] subFromTokens = subTokensList.get(2);
Token[] subWhereTokens = subTokensList.get(3);
if(subCanOptimize && canOptimize)
{
String tableAlias = null;
String tableName = "";
int pos = 0;
while(pos < fromTokens.length)
{
tableName = tableName + fromTokens[pos].getOriginString();
tableName = tableName + fromTokens[pos].getSpaces();
pos++;
}
tableName = tableName.trim();
if (fromTokens.length - 2 >= 0 && fromTokens[fromTokens.length - 1].getType() == Tokenizer.IDENT)
{
int splitPos = tableName.lastIndexOf(" ");
if(splitPos != -1)
{
tableAlias = tableName.substring(splitPos + 1);
if(tableAlias.equals(fromTokens[fromTokens.length - 1].getOriginString()))
{
tableName = tableName.substring(0, splitPos).trim();
}
else
{
tableAlias = null;
}
}
}
if(tableAlias == null)
{
Token tailToken = fromTokens[fromTokens.length - 1];
if(tailToken.getSpaces().isEmpty())
{
tailToken.addSpace();
}
tableAlias = getNewAlias();
List tmpTokenList = new ArrayList();
tmpTokenList.addAll(Arrays.asList(fromTokens));
Token aliasToken = new Token(Tokenizer.IDENT, tableAlias, -1, tableAlias);
aliasToken.addSpace();
tmpTokenList.add(aliasToken);
fromTokens = new Token[tmpTokenList.size()];
tmpTokenList.toArray(fromTokens);
}
String subTableAlias = null;
String subTableName = "";
pos = 0;
while(pos < subFromTokens.length)
{
subTableName = subTableName + subFromTokens[pos].getOriginString();
subTableName = subTableName + subFromTokens[pos].getSpaces();
pos++;
}
subTableName = subTableName.trim();
if (subFromTokens.length - 2 >= 0 && subFromTokens[subFromTokens.length - 1].getType() == Tokenizer.IDENT)
{
int splitPos = subTableName.lastIndexOf(" ");
if(splitPos != -1)
{
subTableAlias = subTableName.substring(splitPos + 1);
if(subTableAlias.equals(subFromTokens[subFromTokens.length - 1].getOriginString()))
{
subTableName = subTableName.substring(0, splitPos).trim();
}
else
{
subTableAlias = null;
}
}
}
if(subTableAlias == null)
{
Token tailToken = subFromTokens[subFromTokens.length - 1];
if(tailToken.getSpaces().isEmpty())
{
tailToken.addSpace();
}
subTableAlias = getNewAlias();
List tmpTokenList = new ArrayList();
tmpTokenList.addAll(Arrays.asList(subFromTokens));
Token aliasToken = new Token(Tokenizer.IDENT, subTableAlias, -1, subTableAlias);
aliasToken.addSpace();
tmpTokenList.add(aliasToken);
subFromTokens = new Token[tmpTokenList.size()];
tmpTokenList.toArray(subFromTokens);
}
selectTokens = regulateFieldTokens(selectTokens, tableAlias);
whereTokens = regulateFieldTokens(whereTokens, tableAlias);
subSelectTokens = regulateFieldTokens(subSelectTokens, subTableAlias);
subWhereTokens = regulateFieldTokens(subWhereTokens, subTableAlias);
Map subAliasColumnMap = new LinkedHashMap();
if(subSelectTokens.length > 1)
{
int start = 1;
int end = -1;
do
{
end = Tokenizer.scanComma(subSelectTokens, start, subSelectTokens.length);
if(end == -1)
{
end = subSelectTokens.length;
}
Token[] columnTokens = Arrays.copyOfRange(subSelectTokens, start, end);
if(columnTokens.length < 2 || columnTokens[columnTokens.length - 1].getType() != Tokenizer.IDENT)
{
if(columnTokens.length == 0)
{
throw new RQException("?Ӳ?ѯ???б???ʽ????Ϊ??");
}
else if(columnTokens.length == 1 && (columnTokens[0].getType() == Tokenizer.IDENT || columnTokens[0].getString().equals("*")))
{
Token aliasToken = columnTokens[columnTokens.length - 1];
subAliasColumnMap.put(aliasToken.getString(), columnTokens);
}
else
{
throw new RQException("?Ӳ?ѯ???б???ʽ????Ҫ????ȷ?ı???");
}
}
else
{
if(!columnTokens[columnTokens.length - 2].isKeyWord("AS")
&& columnTokens[columnTokens.length - 2].getType() != Tokenizer.IDENT
&& columnTokens[columnTokens.length - 2].getType() != Tokenizer.NUMBER
&& columnTokens[columnTokens.length - 2].getType() != Tokenizer.RPAREN
&& columnTokens[columnTokens.length - 2].getType() != Tokenizer.STRING)
{
throw new RQException("?Ӳ?ѯ???б???ʽ?ĸ?ʽ???ڴ???");
}
Token aliasToken = columnTokens[columnTokens.length - 1];
columnTokens = Arrays.copyOfRange(columnTokens, 0, columnTokens.length - 1);
if(columnTokens[columnTokens.length - 1].isKeyWord("AS"))
{
columnTokens = Arrays.copyOfRange(columnTokens, 0, columnTokens.length - 1);
}
subAliasColumnMap.put(aliasToken.getString(), columnTokens);
}
start = end + 1;
}
while(start < subSelectTokens.length);
}
fromTokens = subFromTokens;
if(selectTokens.length == 2 && selectTokens[1].getString().equals("*"))
{
List tokenList = new ArrayList();
tokenList.add(selectTokens[0]);
for(Map.Entry subAliasColumnEntry : subAliasColumnMap.entrySet())
{
if(tokenList.size() > 1)
{
Token tk = new Token(Tokenizer.COMMA, ",", -1, ",");
tk.addSpace();
tokenList.add(tk);
}
Token[] columnTokens = subAliasColumnEntry.getValue();
tokenList.addAll(Arrays.asList(columnTokens));
String alias = subAliasColumnEntry.getKey();
if(!alias.equals("*"))
{
Token tk = new Token(Tokenizer.IDENT, alias, -1, alias);
tk.addSpace();
tokenList.add(tk);
}
}
selectTokens = new Token[tokenList.size()];
tokenList.toArray(selectTokens);
}
else
{
Map aliasColumnMap = new LinkedHashMap();
int start = 1;
int end = -1;
int index = 1;
do
{
end = Tokenizer.scanComma(selectTokens, start, selectTokens.length);
if(end == -1)
{
end = selectTokens.length;
}
Token[] columnTokens = Arrays.copyOfRange(selectTokens, start, end);
if(columnTokens.length >= 2)
{
if(columnTokens[columnTokens.length - 1].getType() != Tokenizer.IDENT)
{
Token tk = new Token(Tokenizer.IDENT, "_"+index, -1, "_"+index++);
tk.addSpace();
aliasColumnMap.put(tk, columnTokens);
}
else if(!columnTokens[columnTokens.length - 2].isKeyWord("AS")
&& columnTokens[columnTokens.length - 2].getType() != Tokenizer.IDENT
&& columnTokens[columnTokens.length - 2].getType() != Tokenizer.NUMBER
&& columnTokens[columnTokens.length - 2].getType() != Tokenizer.RPAREN
&& columnTokens[columnTokens.length - 2].getType() != Tokenizer.STRING)
{
Token tk = new Token(Tokenizer.IDENT, "_"+index, -1, "_"+index++);
tk.addSpace();
aliasColumnMap.put(tk, columnTokens);
}
else
{
Token aliasToken = columnTokens[columnTokens.length - 1];
columnTokens = Arrays.copyOfRange(columnTokens, 0, columnTokens.length - 1);
if(columnTokens[columnTokens.length - 1].isKeyWord("AS"))
{
columnTokens = Arrays.copyOfRange(columnTokens, 0, columnTokens.length - 1);
}
aliasColumnMap.put(aliasToken, columnTokens);
}
}
else
{
Token tk = new Token(Tokenizer.IDENT, "_"+index, -1, "_"+index++);
tk.addSpace();
aliasColumnMap.put(tk, columnTokens);
}
start = end + 1;
}
while(start < selectTokens.length);
Map newAliasColumnMap = new LinkedHashMap();
for(Map.Entry aliasColumnEntry : aliasColumnMap.entrySet())
{
Token aliasToken = aliasColumnEntry.getKey();
Token[] columnTokens = aliasColumnEntry.getValue();
List columnTokenList = new ArrayList();
for(int i = 0; i < columnTokens.length; i++)
{
Token columnToken = columnTokens[i];
if(columnToken.getType() == Tokenizer.IDENT)
{
if(i < columnTokens.length - 2
&& columnTokens[i + 1].getType() == Tokenizer.DOT
&& columnTokens[i + 2].getType() == Tokenizer.IDENT
&& columnToken.getString().equalsIgnoreCase(tableAlias)) // T.F
{
String subColumnAlias = columnTokens[i + 2].getString();
for(String subAlias : subAliasColumnMap.keySet())
{
if(subAlias.equalsIgnoreCase(subColumnAlias))
{
subColumnAlias = subAlias;
break;
}
}
Token[] subColumnTokens = subAliasColumnMap.get(subColumnAlias);
if(subColumnTokens == null)
{
columnTokenList.add(columnTokens[i + 2]);
}
else
{
if(subColumnTokens.length == 1)
{
columnTokenList.add(subColumnTokens[0]);
}
else if(subColumnTokens.length == 3
&& subColumnTokens[0].getString().equalsIgnoreCase(subTableAlias)
&& subColumnTokens[1].getType() == Tokenizer.DOT
&& subColumnTokens[2].getType() == Tokenizer.IDENT)
{
columnTokenList.addAll(Arrays.asList(subColumnTokens));
}
else if(subColumnTokens[0].getType() == Tokenizer.LPAREN
&& Tokenizer.scanParen(subColumnTokens, 0, subColumnTokens.length) == subColumnTokens.length - 1)
{
columnTokenList.addAll(Arrays.asList(subColumnTokens));
}
else
{
Token tk = new Token(Tokenizer.LPAREN, "(", -1, "(");
tk.addSpace();
columnTokenList.add(tk);
columnTokenList.addAll(Arrays.asList(subColumnTokens));
tk = new Token(Tokenizer.RPAREN, ")", -1, ")");
tk.addSpace();
columnTokenList.add(tk);
}
}
i = i + 2;
}
else if(i < columnTokens.length - 2
&& columnTokens[i + 1].getType() == Tokenizer.LPAREN) //fun()//?Ǿۺ???ͨ????????ʽ
{
for(int j = i; j <= i + 1; j++)
{
columnTokenList.add(columnTokens[j]);
}
i = i + 1;
}
else //F
{
String subColumnAlias = columnToken.getString();
for(String subAlias : subAliasColumnMap.keySet())
{
if(subAlias.equalsIgnoreCase(subColumnAlias))
{
subColumnAlias = subAlias;
break;
}
}
Token[] subColumnTokens = subAliasColumnMap.get(subColumnAlias);
if(subColumnTokens == null)
{
columnTokenList.add(columnToken);
}
else
{
if(subColumnTokens.length == 1)
{
columnTokenList.add(subColumnTokens[0]);
}
else if(subColumnTokens.length == 3
&& subColumnTokens[0].getString().equalsIgnoreCase(subTableAlias)
&& subColumnTokens[1].getType() == Tokenizer.DOT
&& subColumnTokens[2].getType() == Tokenizer.IDENT)
{
columnTokenList.addAll(Arrays.asList(subColumnTokens));
}
else if(subColumnTokens[0].getType() == Tokenizer.LPAREN
&& Tokenizer.scanParen(subColumnTokens, 0, subColumnTokens.length) == subColumnTokens.length - 1)
{
columnTokenList.addAll(Arrays.asList(subColumnTokens));
}
else
{
Token tk = new Token(Tokenizer.LPAREN, "(", -1, "(");
tk.addSpace();
columnTokenList.add(tk);
columnTokenList.addAll(Arrays.asList(subColumnTokens));
tk = new Token(Tokenizer.RPAREN, ")", -1, ")");
tk.addSpace();
columnTokenList.add(tk);
}
}
}
}
else
{
columnTokenList.add(columnToken);
}
}
columnTokens = new Token[columnTokenList.size()];
columnTokenList.toArray(columnTokens);
newAliasColumnMap.put(aliasToken, columnTokens);
}
aliasColumnMap = newAliasColumnMap;
List tokenList = new ArrayList();
tokenList.add(selectTokens[0]);
for(Map.Entry aliasColumnEntry : aliasColumnMap.entrySet())
{
if(tokenList.size() > 1)
{
Token tk = new Token(Tokenizer.COMMA, ",", -1, ",");
tk.addSpace();
tokenList.add(tk);
}
Token aliasToken = aliasColumnEntry.getKey();
Token[] columnTokens = aliasColumnEntry.getValue();
tokenList.addAll(Arrays.asList(columnTokens));
tokenList.add(aliasToken);
}
selectTokens = new Token[tokenList.size()];
tokenList.toArray(selectTokens);
}
if(whereTokens != null)
{
List whereTokenList = new ArrayList();
for(int i = 0; i < whereTokens.length; i++)
{
Token whereToken = whereTokens[i];
if(whereToken.getType() == Tokenizer.IDENT)
{
if(i < whereTokens.length - 2
&& whereTokens[i + 1].getType() == Tokenizer.DOT
&& whereTokens[i + 2].getType() == Tokenizer.IDENT
&& whereToken.getString().equalsIgnoreCase(tableAlias)) // T.F
{
String subColumnAlias = whereTokens[i + 2].getString();
for(String subAlias : subAliasColumnMap.keySet())
{
if(subAlias.equalsIgnoreCase(subColumnAlias))
{
subColumnAlias = subAlias;
break;
}
}
Token[] subColumnTokens = subAliasColumnMap.get(subColumnAlias);
if(subColumnTokens == null)
{
whereTokenList.add(whereToken);
}
else
{
if(subColumnTokens.length == 1)
{
whereTokenList.add(subColumnTokens[0]);
}
else if(subColumnTokens.length == 3
&& subColumnTokens[0].getString().equalsIgnoreCase(subTableAlias)
&& subColumnTokens[1].getType() == Tokenizer.DOT
&& subColumnTokens[2].getType() == Tokenizer.IDENT)
{
whereTokenList.addAll(Arrays.asList(subColumnTokens));
}
else if(subColumnTokens[0].getType() == Tokenizer.LPAREN
&& Tokenizer.scanParen(subColumnTokens, 0, subColumnTokens.length) == subColumnTokens.length - 1)
{
whereTokenList.addAll(Arrays.asList(subColumnTokens));
}
else
{
Token tk = new Token(Tokenizer.LPAREN, "(", -1, "(");
tk.addSpace();
whereTokenList.add(tk);
whereTokenList.addAll(Arrays.asList(subColumnTokens));
tk = new Token(Tokenizer.RPAREN, ")", -1, ")");
tk.addSpace();
whereTokenList.add(tk);
}
}
i = i + 2;
}
else if(i < whereTokens.length - 2
&& whereTokens[i + 1].getType() == Tokenizer.LPAREN) //fun()//?Ǿۺ???ͨ????????ʽ
{
for(int j = i; j <= i + 1; j++)
{
whereTokenList.add(whereTokens[j]);
}
i = i + 1;
}
else //F
{
String subColumnAlias = whereToken.getString();
for(String subAlias : subAliasColumnMap.keySet())
{
if(subAlias.equalsIgnoreCase(subColumnAlias))
{
subColumnAlias = subAlias;
break;
}
}
Token[] subColumnTokens = subAliasColumnMap.get(subColumnAlias);
if(subColumnTokens == null)
{
whereTokenList.add(whereToken);
}
else
{
if(subColumnTokens.length == 1)
{
whereTokenList.add(subColumnTokens[0]);
}
else if(subColumnTokens.length == 3
&& subColumnTokens[0].getString().equalsIgnoreCase(subTableAlias)
&& subColumnTokens[1].getType() == Tokenizer.DOT
&& subColumnTokens[2].getType() == Tokenizer.IDENT)
{
whereTokenList.addAll(Arrays.asList(subColumnTokens));
}
else if(subColumnTokens[0].getType() == Tokenizer.LPAREN
&& Tokenizer.scanParen(subColumnTokens, 0, subColumnTokens.length) == subColumnTokens.length - 1)
{
whereTokenList.addAll(Arrays.asList(subColumnTokens));
}
else
{
Token tk = new Token(Tokenizer.LPAREN, "(", -1, "(");
tk.addSpace();
whereTokenList.add(tk);
whereTokenList.addAll(Arrays.asList(subColumnTokens));
tk = new Token(Tokenizer.RPAREN, ")", -1, ")");
tk.addSpace();
whereTokenList.add(tk);
}
}
}
}
else
{
whereTokenList.add(whereToken);
}
}
whereTokens = new Token[whereTokenList.size()];
whereTokenList.toArray(whereTokens);
if(subWhereTokens != null)
{
List tokenList = new ArrayList();
Token tk = new Token(Tokenizer.KEYWORD, "WHERE", -1, "WHERE");
tk.addSpace();
tokenList.add(tk);
tk = new Token(Tokenizer.LPAREN, "(", -1, "(");
tk.addSpace();
tokenList.add(tk);
tokenList.addAll(Arrays.asList(Arrays.copyOfRange(subWhereTokens, 1, subWhereTokens.length)));
tk = new Token(Tokenizer.RPAREN, ")", -1, ")");
tk.addSpace();
tokenList.add(tk);
tk = new Token(Tokenizer.KEYWORD, "AND", -1, "AND");
tk.addSpace();
tokenList.add(tk);
tk = new Token(Tokenizer.LPAREN, "(", -1, "(");
tk.addSpace();
tokenList.add(tk);
tokenList.addAll(Arrays.asList(Arrays.copyOfRange(whereTokens, 1, whereTokens.length)));
tk = new Token(Tokenizer.RPAREN, ")", -1, ")");
tk.addSpace();
tokenList.add(tk);
whereTokens = new Token[tokenList.size()];
tokenList.toArray(whereTokens);
}
}
else
{
if(subWhereTokens != null)
{
whereTokens = subWhereTokens;
}
}
if(subParallelTokens != null)
{
parallelTokens = subParallelTokens;
}
List newTokensList = new ArrayList();
newTokensList.add(selectTokens);
newTokensList.add(parallelTokens);
newTokensList.add(fromTokens);
newTokensList.add(whereTokens);
if(tokensListList.isEmpty() && !isSubquery)
{
Token[] orderTokens = tokensList.get(4);
if(orderTokens != null)
{
List orderTokenList = new ArrayList();
for(int i = 0; i < orderTokens.length; i++)
{
Token orderToken = orderTokens[i];
if(orderToken.getType() == Tokenizer.IDENT)
{
if(i < orderTokens.length - 2
&& orderTokens[i + 1].getType() == Tokenizer.DOT
&& orderTokens[i + 2].getType() == Tokenizer.IDENT
&& orderToken.getString().equalsIgnoreCase(tableAlias)) // T.F
{
String subColumnAlias = orderTokens[i + 2].getString();
for(String subAlias : subAliasColumnMap.keySet())
{
if(subAlias.equalsIgnoreCase(subColumnAlias))
{
subColumnAlias = subAlias;
break;
}
}
Token[] subColumnTokens = subAliasColumnMap.get(subColumnAlias);
if(subColumnTokens == null)
{
orderTokenList.add(orderToken);
}
else
{
if(subColumnTokens.length == 1)
{
orderTokenList.add(subColumnTokens[0]);
}
else if(subColumnTokens.length == 3
&& subColumnTokens[0].getString().equalsIgnoreCase(subTableAlias)
&& subColumnTokens[1].getType() == Tokenizer.DOT
&& subColumnTokens[2].getType() == Tokenizer.IDENT)
{
orderTokenList.addAll(Arrays.asList(subColumnTokens));
}
else if(subColumnTokens[0].getType() == Tokenizer.LPAREN
&& Tokenizer.scanParen(subColumnTokens, 0, subColumnTokens.length) == subColumnTokens.length - 1)
{
orderTokenList.addAll(Arrays.asList(subColumnTokens));
}
else
{
Token tk = new Token(Tokenizer.LPAREN, "(", -1, "(");
tk.addSpace();
orderTokenList.add(tk);
orderTokenList.addAll(Arrays.asList(subColumnTokens));
tk = new Token(Tokenizer.RPAREN, ")", -1, ")");
tk.addSpace();
orderTokenList.add(tk);
}
}
i = i + 2;
}
else if(i < orderTokens.length - 2
&& orderTokens[i + 1].getType() == Tokenizer.LPAREN) //fun()//?Ǿۺ???ͨ????????ʽ
{
for(int j = i; j <= i + 1; j++)
{
orderTokenList.add(orderTokens[j]);
}
i = i + 1;
}
else //F
{
String subColumnAlias = orderToken.getString();
for(String subAlias : subAliasColumnMap.keySet())
{
if(subAlias.equalsIgnoreCase(subColumnAlias))
{
subColumnAlias = subAlias;
break;
}
}
Token[] subColumnTokens = subAliasColumnMap.get(subColumnAlias);
if(subColumnTokens == null)
{
orderTokenList.add(orderToken);
}
else
{
if(subColumnTokens.length == 1)
{
orderTokenList.add(subColumnTokens[0]);
}
else if(subColumnTokens.length == 3
&& subColumnTokens[0].getString().equalsIgnoreCase(subTableAlias)
&& subColumnTokens[1].getType() == Tokenizer.DOT
&& subColumnTokens[2].getType() == Tokenizer.IDENT)
{
orderTokenList.addAll(Arrays.asList(subColumnTokens));
}
else if(subColumnTokens[0].getType() == Tokenizer.LPAREN
&& Tokenizer.scanParen(subColumnTokens, 0, subColumnTokens.length) == subColumnTokens.length - 1)
{
orderTokenList.addAll(Arrays.asList(subColumnTokens));
}
else
{
Token tk = new Token(Tokenizer.LPAREN, "(", -1, "(");
tk.addSpace();
orderTokenList.add(tk);
orderTokenList.addAll(Arrays.asList(subColumnTokens));
tk = new Token(Tokenizer.RPAREN, ")", -1, ")");
tk.addSpace();
orderTokenList.add(tk);
}
}
}
}
else
{
orderTokenList.add(orderToken);
}
}
orderTokens = new Token[orderTokenList.size()];
orderTokenList.toArray(orderTokens);
}
newTokensList.add(orderTokens);
Token[] limitTokens = tokensList.get(5);
newTokensList.add(limitTokens);
}
tokensListList.add(0, newTokensList);
}
else if(subCanOptimize) //?ݲ??Ż????Ժ??滻??????Ż?
{
if(fromTokens.length > 3 && fromTokens[0].isKeyWord("FROM") && fromTokens[1].getType() == Tokenizer.LPAREN)
{
List fromTokenList = new ArrayList();
fromTokenList.add(fromTokens[0]);
fromTokenList.add(fromTokens[1]);
int end = Tokenizer.scanParen(fromTokens, 1, fromTokens.length);
fromTokenList.addAll(Arrays.asList(subSelectTokens));
if(subParallelTokens != null)
{
fromTokenList.addAll(Arrays.asList(subParallelTokens));
}
fromTokenList.addAll(Arrays.asList(subFromTokens));
if(subWhereTokens != null)
{
fromTokenList.addAll(Arrays.asList(subWhereTokens));
}
for(int i = end; i < fromTokens.length; i++)
{
fromTokenList.add(fromTokens[i]);
}
fromTokens = new Token[fromTokenList.size()];
fromTokenList.toArray(fromTokens);
}
else
{
throw new RQException("");
}
List newTokensList = new ArrayList();
newTokensList.add(selectTokens);
newTokensList.add(parallelTokens);
newTokensList.add(fromTokens);
newTokensList.add(whereTokens);
if(tokensListList.isEmpty() && !isSubquery)
{
Token[] orderTokens = tokensList.get(4);
newTokensList.add(orderTokens);
Token[] limitTokens = tokensList.get(5);
newTokensList.add(limitTokens);
}
tokensListList.add(0, newTokensList);
}
else
{
if(fromTokens.length > 3 && fromTokens[0].isKeyWord("FROM") && fromTokens[1].getType() == Tokenizer.LPAREN)
{
List fromTokenList = new ArrayList();
fromTokenList.add(fromTokens[0]);
fromTokenList.add(fromTokens[1]);
int end = Tokenizer.scanParen(fromTokens, 1, fromTokens.length);
fromTokenList.addAll(Arrays.asList(subSelectTokens));
fromTokenList.addAll(Arrays.asList(subFromTokens));
for(int i = end; i < fromTokens.length; i++)
{
fromTokenList.add(fromTokens[i]);
}
fromTokens = new Token[fromTokenList.size()];
fromTokenList.toArray(fromTokens);
}
else
{
throw new RQException("");
}
List newTokensList = new ArrayList();
newTokensList.add(selectTokens);
newTokensList.add(parallelTokens);
newTokensList.add(fromTokens);
newTokensList.add(whereTokens);
if(tokensListList.isEmpty() && !isSubquery)
{
Token[] orderTokens = tokensList.get(4);
newTokensList.add(orderTokens);
Token[] limitTokens = tokensList.get(5);
newTokensList.add(limitTokens);
}
tokensListList.add(0, newTokensList);
}
hasOptimized = hasOptimized || canOptimize;
}
List tokensList = tokensListList.get(0);
Token[] selectTokens = tokensList.get(0);
Token[] parallelTokens = tokensList.get(1);
Token[] fromTokens = tokensList.get(2);
Token[] whereTokens = tokensList.get(3);
Token[] orderTokens = null;
Token[] limitTokens = null;
if(tokensList.size() > 4)
{
orderTokens = tokensList.get(4);
limitTokens = tokensList.get(5);
}
sqlTokenList.add(selectTokens[0]);
if(parallelTokens != null)
{
sqlTokenList.addAll(Arrays.asList(parallelTokens));
}
sqlTokenList.addAll(Arrays.asList(Arrays.copyOfRange(selectTokens, 1, selectTokens.length)));
sqlTokenList.addAll(Arrays.asList(fromTokens));
if(whereTokens != null)
{
Token whereToken = whereTokens[0];
whereTokens = Arrays.copyOfRange(whereTokens, 1, whereTokens.length);
whereTokens = new PerfectWhere(whereTokens, new ArrayList
© 2015 - 2024 Weber Informatics LLC | Privacy Policy