⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 escapeprocessor.java

📁 数据仓库工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
//
// 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 com.internetcds.jdbc.tds;

import java.sql.*;


abstract public class EscapeProcessor
{
   public static final String cvsVersion = "$Id: EscapeProcessor.java,v 1.1 2003/04/29 18:07:50 sinisa Exp $";

   String   input;

   public EscapeProcessor(String sql)
   {
      input = sql;
   } // EscapeProcessor()

   abstract public String expandDBSpecificFunction(String escapeSequence) 
      throws SQLException;

   /**
    * is the string made up only of digits?
    * <p>
    * Note-  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)
   {
      boolean  result = true;
      int      i; 

      for(i=0, result = true; result && i<str.length(); i++)
      {
         result = result && Character.isDigit(str.charAt(i));
      }
      return result;
   } // validDigits()

   /**
    * 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;
   } // skipWhitespace()


   /**
    * Given a string and an index into that string, advanvance the index
    * iff it is on a quote character.
    * 
    * @return 
    */
   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;
   } // skipQuote()

   /**
    * 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 + "'";
   } // getDate()


   /**
    * 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 + "'";
   } // getTime()


   /**
    * Convert a JDBC SQL escape timestamp sequence into a date-time string
    * 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 up to 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+1);
            i++;
         }
         if (fraction.length()>3)
         {
            fraction = fraction.substring(0,3);
         }
         else
         {
            while(fraction.length()<3)
            {
               fraction = fraction + "0";
            }
         }         
      }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -