net.sourceforge.jtds.jdbc

Class Support


public class Support
extends java.lang.Object

This class contains static utility methods designed to support the main driver classes.

Implementation notes:

  1. The methods in this class incorporate some code from previous versions of jTDS to handle dates, BLobs etc.
  2. This class contains routines to generate runtime messages from the resource file.
  3. The key data conversion logic used in Statements and result sets is implemented here.
  4. There is nothing here which is TDS specific.
Version:
$Id: Support.java,v 1.56 2007/07/08 18:42:14 bheineman Exp $
Authors:
Mike Hutchinson
jTDS project

Field Summary

private static BigDecimal
BIG_DECIMAL_ONE
private static BigDecimal
BIG_DECIMAL_ZERO
private static java.sql.Date
DATE_ZERO
private static Double
DOUBLE_ONE
private static Double
DOUBLE_ZERO
private static Float
FLOAT_ONE
private static Float
FLOAT_ZERO
private static Integer
INTEGER_ONE
private static Integer
INTEGER_ZERO
private static Long
LONG_ONE
private static Long
LONG_ZERO
private static BigInteger
MAX_VALUE_28
private static BigInteger
MAX_VALUE_38
private static java.sql.Time
TIME_ZERO
private static GregorianCalendar
cal
Static utility Calendar object.
private static char[]
hex
Hex constants to use in conversion routines.
private static HashMap
typeMap
Convert java clases to java.sql.Type constant.

Constructor Summary

Support()

Method Summary

(package private) static int
calculateNamedPipeBufferSize(int tdsVersion, int packetSize)
Calculate the buffer size to use when buffering the InputStream for named pipes.
(package private) static Object
convert(Object callerReference, Object x, int jdbcType, String charSet)
Convert an existing data object to the specified JDBC type.
static Object
convertLOB(Object value)
Converts a LOB to the equivalent Java type, i.e.
static int
convertLOBType(int type)
Converts a LOB type constant to the equivalent Java type constant, i.e.
(package private) static void
embedData(StringBuffer buf, Object value, boolean isUnicode, ConnectionJDBC2 connection)
Embed the data object as a string literal in the buffer supplied.
(package private) static byte[]
encodeString(String cs, String value)
Encode a string into a byte array using the specified character set.
(package private) static String
getClassName(int jdbcType)
Retrieve the fully qualified java class name for the supplied JDBC Types constant.
private static ConnectionJDBC2
getConnection(Object callerReference)
Returns the connection for a given ResultSet, Statement or Connection object.
(package private) static int
getJdbcType(Class typeClass)
Get the JDBC type constant which matches the supplied Class.
(package private) static int
getJdbcType(Object value)
Get the JDBC type constant which matches the supplied Object type.
(package private) static String
getJdbcTypeName(int jdbcType)
Get a String describing the supplied JDBC type constant.
(package private) static String
getParameterDefinitions(ParamInfo[] parameters)
Constructs a parameter definition string for use with sp_executesql, sp_prepare, sp_prepexec, sp_cursoropen, sp_cursorprepare and sp_cursorprepexec.
(package private) static String
getStatementKey(String sql, ParamInfo[] params, int serverType, String catalog, boolean autoCommit, boolean cursor)
Generates a unique statement key for a given SQL statement.
static boolean
isWindowsOS()
Checks the os.name system property to see if it starts with "windows".
static Throwable
linkException(Exception exception, Throwable cause)
Link the original cause to an Exception.
static SQLException
linkException(SQLException sqle, Throwable cause)
Link the original cause to an SQLException.
static SQLWarning
linkException(SQLWarning sqle, Throwable cause)
Link the original cause to an SQLWarning.
(package private) static BigDecimal
normalizeBigDecimal(BigDecimal value, int maxPrecision)
Normalize a BigDecimal value so that it fits within the available precision.
(package private) static String
substituteParamMarkers(String sql, ParamInfo[] list)
Update the SQL string and replace the ? markers with parameter names eg @P0, @P1 etc.
(package private) static String
substituteParameters(String sql, ParamInfo[] list, ConnectionJDBC2 connection)
Substitute actual data for the parameter markers to simulate parameter substitution in a PreparedStatement.
static long
timeFromZone(java.util.Date value, Calendar target)
Convert a timestamp from a different Timezone.
static long
timeToZone(java.util.Date value, Calendar target)
Convert a timestamp to a different Timezone.
static String
toHex(byte[] bytes)
Convert a byte[] object to a hex string.

Field Details

BIG_DECIMAL_ONE

private static final BigDecimal BIG_DECIMAL_ONE

BIG_DECIMAL_ZERO

private static final BigDecimal BIG_DECIMAL_ZERO

DATE_ZERO

private static final java.sql.Date DATE_ZERO

DOUBLE_ONE

private static final Double DOUBLE_ONE

DOUBLE_ZERO

private static final Double DOUBLE_ZERO

FLOAT_ONE

private static final Float FLOAT_ONE

FLOAT_ZERO

private static final Float FLOAT_ZERO

INTEGER_ONE

private static final Integer INTEGER_ONE

INTEGER_ZERO

private static final Integer INTEGER_ZERO

LONG_ONE

private static final Long LONG_ONE

LONG_ZERO

private static final Long LONG_ZERO

MAX_VALUE_28

private static final BigInteger MAX_VALUE_28

MAX_VALUE_38

private static final BigInteger MAX_VALUE_38

TIME_ZERO

private static final java.sql.Time TIME_ZERO

cal

private static final GregorianCalendar cal
Static utility Calendar object.

hex

private static final char[] hex
Hex constants to use in conversion routines.

typeMap

private static final HashMap typeMap
Convert java clases to java.sql.Type constant.

Constructor Details

Support

private Support()

Method Details

calculateNamedPipeBufferSize

(package private) static int calculateNamedPipeBufferSize(int tdsVersion,
                                                          int packetSize)
Calculate the buffer size to use when buffering the InputStream for named pipes.

The buffer size is tied directly to the packet size because each request to the SmbNamedPipe will send a request for a particular size of packet. In other words, if you only request 1 byte, the SmbNamedPipe will send a request out and only ask for 1 byte back. Buffering the expected packet size ensures that all of the data will be returned in the buffer without wasting any space.

Parameters:
tdsVersion - the TDS version for the connection
packetSize - requested packet size for the connection
Returns:
minimum default packet size if packetSize == 0, else packetSize

convert

(package private) static Object convert(Object callerReference,
                                        Object x,
                                        int jdbcType,
                                        String charSet)
            throws SQLException
Convert an existing data object to the specified JDBC type.
Parameters:
callerReference - an object reference to the caller of this method; must be a Connection, Statement or ResultSet
x - the data object to convert
jdbcType - the required type constant from java.sql.Types
Returns:
the converted data object

convertLOB

public static Object convertLOB(Object value)
            throws SQLException
Converts a LOB to the equivalent Java type, i.e. Clob to String and Blob to byte[]. If the value passed is not a LOB object, it is left unchanged and no exception is thrown; the idea is to transparently convert only LOBs.
Parameters:
value - an object that may be a LOB
Returns:
if the value was a LOB, the equivalent Java object, otherwise the original value

convertLOBType

public static int convertLOBType(int type)
Converts a LOB type constant to the equivalent Java type constant, i.e. Types.CLOB to Types.LONGVARCHAR and Types.BLOB to Types.LONGVARBINARY. If the type passed is not that of a LOB, it is left unchanged and no exception is thrown; the idea is to transparently convert only LOB types.
Parameters:
type - a Types constant defining a JDBC type, possibly a LOB
Returns:
if the type was that of a LOB, the equivalent Java object type, otherwise the original type

embedData

(package private) static void embedData(StringBuffer buf,
                                        Object value,
                                        boolean isUnicode,
                                        ConnectionJDBC2 connection)
            throws SQLException
Embed the data object as a string literal in the buffer supplied.
Parameters:
buf - The buffer in which the data will be embeded.
value - The data object.
isUnicode - Set to true if Unicode strings should be used, else false.
connection - The ConnectionJDBC2 object.

encodeString

(package private) static byte[] encodeString(String cs,
                                             String value)
Encode a string into a byte array using the specified character set.
Parameters:
cs - The Charset name.
value - The value to encode.
Returns:
The value of the String as a byte[].

getClassName

(package private) static String getClassName(int jdbcType)
Retrieve the fully qualified java class name for the supplied JDBC Types constant.
Parameters:
jdbcType - The JDBC Types constant.
Returns:
The fully qualified java class name as a String.

getConnection

private static ConnectionJDBC2 getConnection(Object callerReference)
Returns the connection for a given ResultSet, Statement or Connection object.
Parameters:
callerReference - an object reference to the caller of this method; must be a Connection, Statement or ResultSet
Returns:
a connection

getJdbcType

(package private) static int getJdbcType(Class typeClass)
Get the JDBC type constant which matches the supplied Class.
Parameters:
typeClass - the Class to analyse
Returns:
the JDBC type constant as an int

getJdbcType

(package private) static int getJdbcType(Object value)
Get the JDBC type constant which matches the supplied Object type.
Parameters:
value - The object to analyse.
Returns:
The JDBC type constant as an int.

getJdbcTypeName

(package private) static String getJdbcTypeName(int jdbcType)
Get a String describing the supplied JDBC type constant.
Parameters:
jdbcType - The constant to be decoded.
Returns:
The text decode of the type constant as a String.

getParameterDefinitions

(package private) static String getParameterDefinitions(ParamInfo[] parameters)
Constructs a parameter definition string for use with sp_executesql, sp_prepare, sp_prepexec, sp_cursoropen, sp_cursorprepare and sp_cursorprepexec.
Parameters:
parameters - Parameters to construct the definition for
Returns:
a parameter definition string

getStatementKey

(package private) static String getStatementKey(String sql,
                                                ParamInfo[] params,
                                                int serverType,
                                                String catalog,
                                                boolean autoCommit,
                                                boolean cursor)
Generates a unique statement key for a given SQL statement.
Parameters:
sql - the sql statment to generate the key for
params - the statement parameters
serverType - the type of server to generate the key for
catalog - the catalog is required for uniqueness on Microsoft SQL Server
autoCommit - true if in auto commit mode
cursor - true if this is a prepared cursor
Returns:
the unique statement key

isWindowsOS

public static boolean isWindowsOS()
Checks the os.name system property to see if it starts with "windows".
Returns:
true if os.name starts with "windows", else false.

linkException

public static Throwable linkException(Exception exception,
                                      Throwable cause)
Parameters:
exception - The Exception to enhance.
cause - The Throwable to link.
Returns:
The original Exception object.

linkException

public static SQLException linkException(SQLException sqle,
                                         Throwable cause)
Parameters:
sqle - The SQLException to enhance.
cause - The Throwable to link.
Returns:
The original SQLException object.

linkException

public static SQLWarning linkException(SQLWarning sqle,
                                       Throwable cause)
Parameters:
sqle - The SQLWarning to enhance.
cause - The Throwable to link.
Returns:
The original SQLWarning object.

normalizeBigDecimal

(package private) static BigDecimal normalizeBigDecimal(BigDecimal value,
                                                        int maxPrecision)
            throws SQLException
Normalize a BigDecimal value so that it fits within the available precision.
Parameters:
value - The decimal value to normalize.
maxPrecision - The decimal precision supported by the server (assumed to be a value of either 28 or 38).
Returns:
The possibly normalized decimal value as a BigDecimal.

substituteParamMarkers

(package private) static String substituteParamMarkers(String sql,
                                                       ParamInfo[] list)
Update the SQL string and replace the ? markers with parameter names eg @P0, @P1 etc.
Parameters:
sql - the SQL containing markers to substitute
list - the parameter list
Returns:
the modified SQL as a String

substituteParameters

(package private) static String substituteParameters(String sql,
                                                     ParamInfo[] list,
                                                     ConnectionJDBC2 connection)
            throws SQLException
Substitute actual data for the parameter markers to simulate parameter substitution in a PreparedStatement.
Parameters:
sql - The SQL containing parameter markers to substitute.
list - The parameter descriptors.
connection - The current connection.
Returns:
The modified SQL statement.

timeFromZone

public static long timeFromZone(java.util.Date value,
                                Calendar target)
Convert a timestamp from a different Timezone.
Parameters:
value - the timestamp value.
target - the Calendar containing the TimeZone.
Returns:
The new timestamp value as a long.

timeToZone

public static long timeToZone(java.util.Date value,
                              Calendar target)
Convert a timestamp to a different Timezone.
Parameters:
value - the timestamp value
target - the Calendar containing the TimeZone
Returns:
the new timestamp value as a long

toHex

public static String toHex(byte[] bytes)
Convert a byte[] object to a hex string.
Parameters:
bytes - The byte array to convert.
Returns:
The hex equivalent as a String.

Generated on September 18 2009