improve sql parser performance

This commit is contained in:
wenshao
2013-01-14 20:45:13 +08:00
parent cb45e49a0c
commit 7aa2038498
6 changed files with 71 additions and 21 deletions
@@ -209,13 +209,24 @@ public class MySqlLexer extends Lexer {
{
boolean hasSpecial = false;
int startIndex = pos + 1;
int endIndex = text.indexOf('\'', startIndex);
int endIndex = -1; //text.indexOf('\'', startIndex);
for (int i = startIndex; i < text.length(); ++i) {
final char ch = text.charAt(i);
if (ch == '\\') {
hasSpecial = true;
}
if (ch == '\'') {
endIndex = i;
break;
}
}
if (endIndex == -1) {
throw new ParserException("unclosed str");
}
String stringVal = subString(startIndex, endIndex - startIndex);
hasSpecial = stringVal.indexOf('\\') != -1;
// hasSpecial = stringVal.indexOf('\\') != -1;
if (!hasSpecial) {
this.stringVal = stringVal;
@@ -79,7 +79,7 @@ public class Lexer {
return text.charAt(index);
}
public String addSymbol() {
public final String addSymbol() {
return subString(mark, bufPos);
}
@@ -137,9 +137,30 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
}
private String handleName(String ident) {
ident = ident.replaceAll("\"", "");
ident = ident.replaceAll("`", "");
ident = ident.replaceAll(" ", "");
boolean flag0 = false;
boolean flag1 = false;
boolean flag2 = false;
for (int i = 0; i < ident.length(); ++i) {
final char ch = ident.charAt(i);
if (ch == '\"') {
flag0 = true;
} else if (ch == '`') {
flag1 = true;
} else if (ch == ' ') {
flag2 = true;
}
}
if (flag0) {
ident = ident.replaceAll("\"", "");
}
if (flag1) {
ident = ident.replaceAll("`", "");
}
if (flag2) {
ident = ident.replaceAll(" ", "");
}
ident = aliasWrap(ident);
@@ -20,6 +20,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.alibaba.druid.util.StringUtils;
public class TableStat {
int selectCount = 0;
@@ -170,7 +172,7 @@ public class TableStat {
}
public int hashCode() {
return this.name.toLowerCase().hashCode();
return StringUtils.lowerHashCode(name);
}
public boolean equals(Object o) {
@@ -401,8 +403,8 @@ public class TableStat {
}
public int hashCode() {
int tableHashCode = table != null ? table.toLowerCase().hashCode() : 0;
int nameHashCode = name != null ? name.toLowerCase().hashCode() : 0;
int tableHashCode = table != null ? StringUtils.lowerHashCode(table) : 0;
int nameHashCode = name != null ? StringUtils.lowerHashCode(name) : 0;
return tableHashCode + nameHashCode;
}
@@ -91,4 +91,18 @@ public class StringUtils {
return false;
}
public static int lowerHashCode(String text) {
// return text.toLowerCase().hashCode();
int h = 0;
for (int i = 0; i < text.length(); ++i) {
char ch = text.charAt(i);
if (ch >= 'A' && ch <= 'Z') {
ch = (char) (ch + 32);
}
h = 31 * h + ch;
}
return h;
}
}