All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.microsoft.sqlserver.jdbc.ParameterUtils Maven / Gradle / Ivy

There is a newer version: 12.8.1.jre11
Show newest version
//---------------------------------------------------------------------------------------------------------------------------------
// File: ParameterUtils.java
//
//
// Microsoft JDBC Driver for SQL Server
// Copyright(c) Microsoft Corporation
// All rights reserved.
// MIT License
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the ""Software""), 
//  to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
//  and / or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
//  IN THE SOFTWARE.
//---------------------------------------------------------------------------------------------------------------------------------
 
 
package com.microsoft.sqlserver.jdbc;

/**
* ParameterUtils provides utility a set of methods to manipulate parameter values.
*/

final class ParameterUtils
{
	static byte[] HexToBin(String hexV) throws SQLServerException
	{
		int len = hexV.length();
		char orig[] = hexV.toCharArray();
		if ((len% 2) !=0)
			SQLServerException.makeFromDriverError(null, null,
			         SQLServerException.getErrString("R_stringNotInHex"), null, false);
		byte[] bin = new byte[len/2];
		for (int i=0; i= 'A' && CTX <= 'F' )
			{
			ret = (byte)(CTX - 'A' + 10);	
			}
		else if(CTX >= 'a' && CTX <= 'f' )
			{
			ret = (byte)(CTX - 'a' + 10);	
			}
		else
		if(CTX >= '0' && CTX <= '9' )
			{
			ret = (byte)(CTX - '0');	
			}
		else
			{
			SQLServerException.makeFromDriverError(null, null,
			         SQLServerException.getErrString("R_stringNotInHex"), null, false);
			}
		return ret;
	}



  /**
   * Locates the first occurrence of [c] in [sql] starting at [offset],
   * where [sql] is a SQL statement string, which may contain any
   * combination of:
   *
   * - Literals, enclosed in single quotes (')
   * - Literals, enclosed in double quotes (")
   * - Escape sequences, enclosed in square brackets ([])
   * - Escaped escapes or literal delimiters (i.e. '', "", or ]]) in the above
   * - Single-line comments, beginning in -- and continuing to EOL
   * - Multi-line comments, enclosed in C-style comment delimiters
   *
   * and [c] is not contained any of the above.
   *
   * @param c the character to search for
   * @param sql the SQL string to search in
   * @param offset the offset into [sql] to start searching from
   * @return Offset into [sql] where [c] occurs, or sql.length if [c] is not found.
   * @throws SQLServerException when [sql] does not follow 
   */
  @SuppressWarnings({"fallthrough"})
  static int scanSQLForChar(char ch, String sql, int offset)
  {
    char chQuote;
    char chTmp;
    final int len  = sql.length();

    while (offset < len)
    {
        switch (chTmp = sql.charAt(offset++))
        {
          case '/':
            if (offset == len)
              break;

            if (sql.charAt(offset) == '*')
            {   // If '/* ... */' comment
                while (++offset < len)
                {   // Go thru comment.
                    if (sql.charAt(offset) == '*' &&
                        offset+1 < len &&
                        sql.charAt(offset+1) == '/')
                    {   // If end of comment
                        offset += 2;
                        break;
                    }
                }
                break;
            }
            else if (sql.charAt(offset) == '-')
                break;

            //  Fall through - will fail next if and end up in default case
          case '-':
            if (sql.charAt(offset) == '-')
            {   //  If '-- ... \n' comment
                while (++offset < len)
                {   // Go thru comment.
                    if (sql.charAt(offset) == '\n' || sql.charAt(offset) == '\r')
                    {   // If end of comment
                        offset++;
                        break;
                    }
                }
                break;
            }
            //  Fall through to test character
          default:
            if (ch == chTmp)
                return offset - 1;
            break;

          case '[':
            chTmp = ']';
          case '\'':
          case '"':
            chQuote = chTmp;
            while (offset < len)
            {
                if (sql.charAt(offset++) == chQuote)
                {
                    if (len == offset || sql.charAt(offset) != chQuote)
                      break;

                    ++offset;
                }
            }
            break;
        }
    }
    
    return len;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy