📄 escapeprocessor.java
字号:
// Make sure there isn't any garbage after the time
i = skipWhitespace(str, i);
i = skipQuote(str, i);
i = skipWhitespace(str, i);
if (i<str.length())
{
throw new SQLException("Malformed date");
}
return ("'" + year + month + day + " "
+ hour + ":" + minute + ":" + second + "." + fraction + "'");
} // getTimestamp()
public String expandEscape(String escapeSequence)
throws SQLException
{
String str = new String(escapeSequence);
String result = null;
// XXX Is it always okay to trim leading and trailing blanks?
str = str.trim();
if (str.startsWith("fn "))
{
str = str.substring(3);
result = expandCommonFunction(str);
if (result == null)
{
result = expandDBSpecificFunction(str);
}
}
else if (str.startsWith("call ")
|| (str.startsWith("?=")
&& str.substring(2).trim().startsWith("call ")))
{
throw new SQLException("Not implemented yet");
}
else if (str.startsWith("d "))
{
result = getDate(str);
}
else if (str.startsWith("t "))
{
result = getTime(str);
}
else if (str.startsWith("ts "))
{
result = getTimestamp(str);
}
else if (str.startsWith("oj "))
{
throw new SQLException("Not implemented yet");
}
else
{
throw new SQLException("Unrecognized escape sequence-\n" +
escapeSequence);
}
return result;
} // expandEscape()
/**
* Expand functions that are common to both SQLServer and Sybase
*
*/
public String expandCommonFunction(String str)
{
String result = null;
if (str.equalsIgnoreCase("user()"))
{
result = " user_name() ";
}
else if (str.equalsIgnoreCase("now()"))
{
result = " getdate() ";
}
return result;
} // expandCommonFunction()
public String nativeString()
throws SQLException
{
return nativeString(input, '\\');
} // nativeString()
private String nativeString(String sql, char escapeCharacter)
throws SQLException
{
String result = "";
String escape = "";
int i;
// Simple finite state machine. Bonehead, but it works.
final int normal = 0;
final int inString = 1;
final int inStringWithBackquote = 2;
final int inEscape = 3;
final int inEscapeInString = 4;
final int inEscapeInStringWithBackquote = 5;
int state = normal;
char ch;
int escapeStartedAt = -1;
i = 0;
while(i<sql.length())
{
ch = sql.charAt(i);
switch(state)
{
case normal:
{
if (ch == '{')
{
escapeStartedAt = i;
state = inEscape;
escape = "";
}
else
{
result = result + ch;
if (ch == '\'') state = inString;
}
break;
}
case inString:
case inStringWithBackquote:
{
if ((i+1)<sql.length()
&& ch == escapeCharacter
&& (sql.charAt(i+1)=='_'
|| sql.charAt(i+1)=='%'))
{
i++;
ch = sql.charAt(i);
result = result + '\\' + ch;
}
else
{
result = result + ch;
if (state == inStringWithBackquote)
{
state = inString;
}
else
{
if (ch == '\\') state = inStringWithBackquote;
if (ch == '\'') state = normal;
}
}
break;
}
case inEscape:
{
if (ch == '}')
{
// At this point there are a couple of things to
// consider. First, if the escape is of the form
// "{escape 'c'} but it is not at the end of the SQL
// we consider that a malformed SQL string. If it
// is the "{escape 'c'}" clause and it is at the end
// of the string then we have to go through and
// reparse this whole thing again, this time with an
// escape character. Any other escape is handled in
// the expandEscape method()
if (escape.startsWith("escape "))
{
char c;
// make sure it is the last thing in the sql
if (i+1!=sql.length())
{
throw new SQLException("Malformed statement. " +
"escape clause must be at " +
"the end of the query");
}
// parse the sql again, this time without the
// ending string but with the escape character
// set
c = findEscapeCharacter(sql.substring(escapeStartedAt));
result = nativeString(sql.substring(0, escapeStartedAt),
c);
state = normal;
}
else
{
state = normal;
result = result + expandEscape(escape);
escapeStartedAt = -1;
}
}
else
{
escape = escape + ch;
if (ch == '\'')
{
state = inEscapeInString;
}
}
break;
}
case inEscapeInString:
case inEscapeInStringWithBackquote:
{
escape = escape + ch;
if (state == inEscapeInStringWithBackquote)
{
state = inEscapeInString;
}
else
{
if (ch == '\\') state = inEscapeInStringWithBackquote;
if (ch == '\'') state = inEscape;
}
break;
}
default:
{
throw new SQLException("Internal error. Unknown state in FSM");
}
}
i++;
}
if (state!=normal && state!=inString)
{
throw new SQLException("Syntax error in SQL escape syntax");
}
return result;
} // nativeString()
static char findEscapeCharacter(String original_str)
throws SQLException
{
String str = new String(original_str);
str = str.trim();
if (str.charAt(0)!='{' || str.charAt(str.length()-1)!='}'
|| str.length()<12)
{
throw new SQLException("Internal Error");
}
str = str.substring(1, str.length()-1);
str = str.trim();
if (! str.startsWith("escape"))
{
throw new SQLException("Internal Error");
}
str = str.substring(6);
str = str.trim();
if (str.length()!=3 || str.charAt(0)!='\'' || str.charAt(2)!='\'')
{
throw new SQLException("Malformed escape clause- |" +
original_str + "|");
}
return str.charAt(1);
} // findEscapeCharacter()
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -