Package net.sourceforge.jtds.jdbc
Class Support
- java.lang.Object
-
- net.sourceforge.jtds.jdbc.Support
-
public class Support extends java.lang.Object
This class contains static utility methods designed to support the main driver classes.Implementation notes:
- The methods in this class incorporate some code from previous versions of jTDS to handle dates, BLobs etc.
- This class contains routines to generate runtime messages from the resource file.
- The key data conversion logic used in Statements and result sets is implemented here.
- There is nothing here which is TDS specific.
- Version:
- $Id: Support.java,v 1.56.2.6 2010-05-17 09:36:57 ickzon Exp $
- Author:
- Mike Hutchinson, jTDS project
-
-
Field Summary
Fields Modifier and Type Field Description private static java.math.BigDecimal
BIG_DECIMAL_ONE
private static java.math.BigDecimal
BIG_DECIMAL_ZERO
private static java.sql.Date
DATE_ZERO
private static java.lang.Double
DOUBLE_ONE
private static java.lang.Double
DOUBLE_ZERO
private static java.lang.Float
FLOAT_ONE
private static java.lang.Float
FLOAT_ZERO
private static char[]
hex
Hex constants to use in conversion routines.private static java.lang.Integer
INTEGER_ONE
private static java.lang.Integer
INTEGER_ZERO
private static java.lang.Long
LONG_ONE
private static java.lang.Long
LONG_ZERO
private static java.math.BigInteger
MAX_VALUE_28
private static java.math.BigInteger
MAX_VALUE_38
private static java.math.BigDecimal
MAX_VALUE_LONG_BD
private static java.math.BigInteger
MAX_VALUE_LONG_BI
private static java.math.BigDecimal
MIN_VALUE_LONG_BD
private static java.math.BigInteger
MIN_VALUE_LONG_BI
private static java.sql.Time
TIME_ZERO
private static java.util.HashMap
typeMap
Convert java clases to java.sql.Type constant.
-
Constructor Summary
Constructors Modifier Constructor Description private
Support()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description (package private) static int
calculateNamedPipeBufferSize(int tdsVersion, int packetSize)
Calculate the buffer size to use when buffering theInputStream
for named pipes.(package private) static java.lang.Object
castNumeric(java.lang.Object orig, int sourceType, int targetType)
(package private) static java.lang.Object
convert(java.lang.Object callerReference, java.lang.Object x, int jdbcType, java.lang.String charSet)
Convert an existing data object to the specified JDBC type.static java.lang.Object
convertLOB(java.lang.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(java.lang.StringBuilder buf, java.lang.Object value, boolean isUnicode, JtdsConnection connection)
Embed the data object as a string literal in the buffer supplied.(package private) static byte[]
encodeString(java.lang.String cs, java.lang.String value)
Encode a string into a byte array using the specified character set.(package private) static java.lang.String
getClassName(int jdbcType)
Retrieve the fully qualified java class name for the supplied JDBC Types constant.private static JtdsConnection
getConnection(java.lang.Object callerReference)
Returns the connection for a givenResultSet
,Statement
orConnection
object.(package private) static int
getJdbcType(java.lang.Class typeClass)
Get the JDBC type constant which matches the suppliedClass
.(package private) static int
getJdbcType(java.lang.Object value)
Get the JDBC type constant which matches the supplied Object type.(package private) static java.lang.String
getJdbcTypeName(int jdbcType)
Get a String describing the supplied JDBC type constant.(package private) static java.lang.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 java.lang.String
getStatementKey(java.lang.String sql, ParamInfo[] params, int serverType, java.lang.String catalog, boolean autoCommit, boolean cursor)
Generates a unique statement key for a given SQL statement.static boolean
isWindowsOS()
Checks theos.name
system property to see if it starts with "windows".static java.lang.Throwable
linkException(java.lang.Exception exception, java.lang.Throwable cause)
Link the original cause to anException
.static java.sql.SQLException
linkException(java.sql.SQLException sqle, java.lang.Throwable cause)
Link the original cause to anSQLException
.static java.sql.SQLWarning
linkException(java.sql.SQLWarning sqle, java.lang.Throwable cause)
Link the original cause to anSQLWarning
.(package private) static java.math.BigDecimal
normalizeBigDecimal(java.math.BigDecimal value, int maxPrecision)
Normalize a BigDecimal value so that it fits within the available precision.(package private) static java.lang.String
substituteParameters(java.lang.String sql, ParamInfo[] list, JtdsConnection connection)
Substitute actual data for the parameter markers to simulate parameter substitution in a PreparedStatement.(package private) static java.lang.String
substituteParamMarkers(java.lang.String sql, ParamInfo[] list)
Update the SQL string and replace the ? markers with parameter names eg @P0, @P1 etc.static long
timeFromZone(java.util.Date value, java.util.Calendar target)
Convert a timestamp from a different Timezone.static long
timeToZone(java.util.Date value, java.util.Calendar target)
Convert a timestamp to a different Timezone.static java.lang.String
toHex(byte[] bytes)
Convert a byte[] object to a hex string.
-
-
-
Field Detail
-
INTEGER_ZERO
private static final java.lang.Integer INTEGER_ZERO
-
INTEGER_ONE
private static final java.lang.Integer INTEGER_ONE
-
LONG_ZERO
private static final java.lang.Long LONG_ZERO
-
LONG_ONE
private static final java.lang.Long LONG_ONE
-
FLOAT_ZERO
private static final java.lang.Float FLOAT_ZERO
-
FLOAT_ONE
private static final java.lang.Float FLOAT_ONE
-
DOUBLE_ZERO
private static final java.lang.Double DOUBLE_ZERO
-
DOUBLE_ONE
private static final java.lang.Double DOUBLE_ONE
-
BIG_DECIMAL_ZERO
private static final java.math.BigDecimal BIG_DECIMAL_ZERO
-
BIG_DECIMAL_ONE
private static final java.math.BigDecimal BIG_DECIMAL_ONE
-
DATE_ZERO
private static final java.sql.Date DATE_ZERO
-
TIME_ZERO
private static final java.sql.Time TIME_ZERO
-
MIN_VALUE_LONG_BI
private static final java.math.BigInteger MIN_VALUE_LONG_BI
-
MAX_VALUE_LONG_BI
private static final java.math.BigInteger MAX_VALUE_LONG_BI
-
MIN_VALUE_LONG_BD
private static final java.math.BigDecimal MIN_VALUE_LONG_BD
-
MAX_VALUE_LONG_BD
private static final java.math.BigDecimal MAX_VALUE_LONG_BD
-
MAX_VALUE_28
private static final java.math.BigInteger MAX_VALUE_28
-
MAX_VALUE_38
private static final java.math.BigInteger MAX_VALUE_38
-
typeMap
private static final java.util.HashMap typeMap
Convert java clases to java.sql.Type constant.
-
hex
private static final char[] hex
Hex constants to use in conversion routines.
-
-
Method Detail
-
toHex
public static java.lang.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
.
-
normalizeBigDecimal
static java.math.BigDecimal normalizeBigDecimal(java.math.BigDecimal value, int maxPrecision) throws java.sql.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
. - Throws:
java.sql.SQLException
- If the number is too big.
-
castNumeric
static java.lang.Object castNumeric(java.lang.Object orig, int sourceType, int targetType)
-
convert
static java.lang.Object convert(java.lang.Object callerReference, java.lang.Object x, int jdbcType, java.lang.String charSet) throws java.sql.SQLException
Convert an existing data object to the specified JDBC type.- Parameters:
callerReference
- an object reference to the caller of this method; must be aConnection
,Statement
orResultSet
x
- the data object to convertjdbcType
- the required type constant fromjava.sql.Types
- Returns:
- the converted data object
- Throws:
java.sql.SQLException
- if the conversion is not supported or fails
-
getJdbcType
static int getJdbcType(java.lang.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
.
-
getJdbcType
static int getJdbcType(java.lang.Class typeClass)
Get the JDBC type constant which matches the suppliedClass
.- Parameters:
typeClass
- theClass
to analyse- Returns:
- the JDBC type constant as an
int
-
getJdbcTypeName
static java.lang.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
.
-
getClassName
static java.lang.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
.
-
embedData
static void embedData(java.lang.StringBuilder buf, java.lang.Object value, boolean isUnicode, JtdsConnection connection) throws java.sql.SQLException
Embed the data object as a string literal in the buffer supplied.- Parameters:
buf
- The buffer in which the data will be embedded.value
- The data object.isUnicode
- Set totrue
if Unicode strings should be used, elsefalse
.connection
- TheJtdsConnection
object.- Throws:
java.sql.SQLException
-
getStatementKey
static java.lang.String getStatementKey(java.lang.String sql, ParamInfo[] params, int serverType, java.lang.String catalog, boolean autoCommit, boolean cursor)
Generates a unique statement key for a given SQL statement.- Parameters:
sql
- the sql statement to generate the key forparams
- the statement parametersserverType
- the type of server to generate the key forcatalog
- the catalog is required for uniqueness on Microsoft SQL ServerautoCommit
- true if in auto commit modecursor
- true if this is a prepared cursor- Returns:
- the unique statement key
-
getParameterDefinitions
static java.lang.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
-
substituteParamMarkers
static java.lang.String substituteParamMarkers(java.lang.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 substitutelist
- the parameter list- Returns:
- the modified SQL as a
String
-
substituteParameters
static java.lang.String substituteParameters(java.lang.String sql, ParamInfo[] list, JtdsConnection connection) throws java.sql.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.
- Throws:
java.sql.SQLException
-
encodeString
static byte[] encodeString(java.lang.String cs, java.lang.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[]
.
-
linkException
public static java.sql.SQLWarning linkException(java.sql.SQLWarning sqle, java.lang.Throwable cause)
Link the original cause to anSQLWarning
.This convenience method calls
linkException(Exception, Throwable)
and casts the result for cleaner code elsewhere.- Parameters:
sqle
- TheSQLWarning
to enhance.cause
- TheThrowable
to link.- Returns:
- The original
SQLWarning
object.
-
linkException
public static java.sql.SQLException linkException(java.sql.SQLException sqle, java.lang.Throwable cause)
Link the original cause to anSQLException
.This convenience method calls
linkException(Exception, Throwable)
and casts the result for cleaner code elsewhere.- Parameters:
sqle
- TheSQLException
to enhance.cause
- TheThrowable
to link.- Returns:
- The original
SQLException
object.
-
linkException
public static java.lang.Throwable linkException(java.lang.Exception exception, java.lang.Throwable cause)
Link the original cause to anException
.If running under JVM 1.4+ the
Throwable.initCause(Throwable)
method will be invoked to chain the exception, else the exception is logged via theLogger
class. Modeled after the code written by Brian Heineman.- Parameters:
exception
- TheException
to enhance.cause
- TheThrowable
to link.- Returns:
- The original
Exception
object.
-
timeToZone
public static long timeToZone(java.util.Date value, java.util.Calendar target)
Convert a timestamp to a different Timezone.- Parameters:
value
- the timestamp valuetarget
- theCalendar
containing the TimeZone- Returns:
- the new timestamp value as a
long
-
timeFromZone
public static long timeFromZone(java.util.Date value, java.util.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
.
-
convertLOB
public static java.lang.Object convertLOB(java.lang.Object value) throws java.sql.SQLException
Converts a LOB to the equivalent Java type, i.e.Clob
toString
andBlob
tobyte[]
. 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
- Throws:
java.sql.SQLException
- if an error occurs while reading the LOB contents
-
convertLOBType
public static int convertLOBType(int type)
Converts a LOB type constant to the equivalent Java type constant, i.e.Types.CLOB
toTypes.LONGVARCHAR
andTypes.BLOB
toTypes.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
- aTypes
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
-
isWindowsOS
public static boolean isWindowsOS()
Checks theos.name
system property to see if it starts with "windows".- Returns:
true
ifos.name
starts with "windows", elsefalse
.
-
getConnection
private static JtdsConnection getConnection(java.lang.Object callerReference)
Returns the connection for a givenResultSet
,Statement
orConnection
object.- Parameters:
callerReference
- an object reference to the caller of this method; must be aConnection
,Statement
orResultSet
- Returns:
- a connection
-
calculateNamedPipeBufferSize
static int calculateNamedPipeBufferSize(int tdsVersion, int packetSize)
Calculate the buffer size to use when buffering theInputStream
for named pipes. The buffer size is tied directly to the packet size because each request to theSmbNamedPipe
will send a request for a particular size of packet. In other words, if you only request 1 byte, theSmbNamedPipe
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 connectionpacketSize
- requested packet size for the connection- Returns:
- minimum default packet size if
packetSize == 0
, elsepacketSize
-
-