📄 escapeprocessor.java
字号:
//
// Copyright 1998 CDS Networks, Inc., Medford Oregon
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. All advertising materials mentioning features or use of this software
// must display the following acknowledgement:
// This product includes software developed by CDS Networks, Inc.
// 4. The name of CDS Networks, Inc. may not be used to endorse or promote
// products derived from this software without specific prior
// written permission.
//
// THIS SOFTWARE IS PROVIDED BY CDS NETWORKS, INC. ``AS IS'' AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL CDS NETWORKS, INC. BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
//
package net.sourceforge.jtds.jdbc;
import java.sql.*;
abstract public class EscapeProcessor
{
public static final String cvsVersion = "$Id: EscapeProcessor.java,v 1.4 2002/10/22 09:50:10 alin_sinpalean Exp $";
String input;
private static final String ESCAPE_PREFIX_DATE = "d ";
private static final String ESCAPE_PREFIX_TIME = "t ";
private static final String ESCAPE_PREFIX_TIMESTAMP = "ts ";
private static final String ESCAPE_PREFIX_OUTER_JOIN = "oj ";
private static final String ESCAPE_PREFIX_FUNCTION = "fn ";
private static final String ESCAPE_PREFIX_CALL = "call ";
private static final String ESCAPE_PREFIX_ESCAPE_CHAR = "escape ";
public EscapeProcessor(String sql)
{
input = sql;
}
abstract public String expandDBSpecificFunction(String escapeSequence) throws SQLException;
/**
* Is the string made up only of digits?
* <p>
* <b>Note:</b> Leading/trailing spaces or signs are not considered digits.
*
* @return true if the string has only digits, false otherwise.
*/
private static boolean validDigits(String str)
{
for( int i=0; i<str.length(); i++ )
if( !Character.isDigit(str.charAt(i)) )
return false;
return true;
}
/**
* Given a string and an index into that string return the index
* of the next non-whitespace character.
*
* @return index of next non-whitespace character.
*/
private static int skipWhitespace(String str, int i)
{
while( i<str.length() && Character.isWhitespace(str.charAt(i)) )
i++;
return i;
}
/**
* Given a string and an index into that string, advanvance the index
* iff it is on a quote character.
*
* @return the next position in the string iff the current character is a
* quote
*/
private static int skipQuote(String str, int i)
{
// skip over the leading quote if it exists
if( i<str.length() && (str.charAt(i)=='\'' || str.charAt(i)=='"') )
// XXX Note- The spec appears to prohibit the quote character,
// but many drivers allow it. We should probably control this
// with a flag.
i++;
return i;
}
/**
* Convert a JDBC SQL escape date sequence into a datestring recognized by SQLServer.
*/
private static String getDate(String str) throws SQLException
{
int i;
// skip over the "d "
i = 2;
// skip any additional spaces
i = skipWhitespace(str, i);
i = skipQuote(str, i);
// i is now up to the point where the date had better start.
if( str.length()-i<10 || str.charAt(i+4)!='-' || str.charAt(i+7)!='-' )
throw new SQLException("Malformed date");
String year = str.substring(i, i+4);
String month = str.substring(i+5, i+5+2);
String day = str.substring(i+5+3, i+5+3+2);
// Make sure the year, month, and day are numeric
if( !validDigits(year) || !validDigits(month) || !validDigits(day) )
throw new SQLException("Malformed date");
// Make sure there isn't any garbage after the date
i = i+10;
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 + "'";
}
/**
* Convert a JDBC SQL escape time sequence into a time string recognized by SQLServer.
*/
private static String getTime(String str) throws SQLException
{
int i;
// skip over the "t "
i = 2;
// skip any additional spaces
i = skipWhitespace(str, i);
i = skipQuote(str, i);
// i is now up to the point where the date had better start.
if( str.length()-i<8 || str.charAt(i+2)!=':' || str.charAt(i+5)!=':' )
throw new SQLException("Malformed time");
String hour = str.substring(i, i+2);
String minute = str.substring(i+3, i+3+2);
String second = str.substring(i+3+3, i+3+3+2);
// Make sure the year, month, and day are numeric
if( !validDigits(hour) || !validDigits(minute) || !validDigits(second) )
throw new SQLException("Malformed time");
// Make sure there isn't any garbage after the time
i = i+8;
i = skipWhitespace(str, i);
i = skipQuote(str, i);
i = skipWhitespace(str, i);
if( i<str.length() )
throw new SQLException("Malformed time");
return "'" + hour + ":" + minute + ":" + second + "'";
}
/**
* Convert a JDBC SQL escape timestamp sequence into a date-time string recognized by SQLServer.
*/
private static String getTimestamp(String str) throws SQLException
{
int i;
// skip over the "d "
i = 2;
// skip any additional spaces
i = skipWhitespace(str, i);
i = skipQuote(str, i);
// i is now at the point where the date had better start.
if( (str.length()-i)<19 || str.charAt(i+4)!='-' || str.charAt(i+7)!='-' )
throw new SQLException("Malformed date");
String year = str.substring(i, i+4);
String month = str.substring(i+5, i+5+2);
String day = str.substring(i+5+3, i+5+3+2);
// Make sure the year, month, and day are numeric
if( !validDigits(year) || !validDigits(month) || !validDigits(day) )
throw new SQLException("Malformed date");
// Make sure there is at least one space between date and time
i = i+10;
if( !Character.isWhitespace(str.charAt(i)) )
throw new SQLException("Malformed date");
// skip the whitespace
i = skipWhitespace(str, i);
// see if it could be a time
if( str.length()-i<8 || str.charAt(i+2)!=':' || str.charAt(i+5)!=':' )
throw new SQLException("Malformed time");
String hour = str.substring(i, i+2);
String minute = str.substring(i+3, i+3+2);
String second = str.substring(i+3+3, i+3+3+2);
String fraction = "000";
i = i+8;
if( str.length()>i && str.charAt(i)=='.' )
{
fraction = "";
i++;
while( str.length()>i && validDigits(str.substring(i,i+1)) )
fraction = fraction + str.substring(i,++i);
if( fraction.length() > 3 )
fraction = fraction.substring(0,3);
else
while( fraction.length()<3 )
fraction = fraction + "0";
}
// 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 + "'";
}
public String expandEscape(String escapeSequence) throws SQLException
{
String str = new String(escapeSequence);
String result = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -