net.sourceforge.jtds.jdbc

Class TdsData


public class TdsData
extends java.lang.Object

Implement TDS data types and related I/O logic.

Implementation notes:

  • This class encapsulates all the knowledge about reading and writing TDS data descriptors and related application data.
  • There are four key methods supplied here:
    1. readType() - Reads the column and parameter meta data.
    2. readData() - Reads actual data values.
    3. writeParam() - Write parameter descriptors and data.
    4. getNativeType() - knows how to map JDBC data types to the equivalent TDS type.
  • Version:
    $Id: TdsData.java,v 1.60 2007/08/19 02:25:28 bheineman Exp $
    Authors:
    Mike Hutchinson
    Alin Sinpalean
    freeTDS project

    Nested Class Summary

    private static class
    TdsData.TypeInfo
    This class implements a descriptor for TDS data types;

    Field Summary

    (package private) static int
    DEFAULT_PRECISION_28
    Default precision for SQL Server 6.5 and 7.
    (package private) static int
    DEFAULT_PRECISION_38
    Default precision for Sybase and SQL Server 2000 and newer.
    (package private) static int
    DEFAULT_SCALE
    Default Decimal Scale.
    private static int
    MS_LONGVAR_MAX
    private static int
    SYBBINARY
    private static int
    SYBBIT
    private static int
    SYBBITN
    private static int
    SYBCHAR
    private static int
    SYBDATE
    private static int
    SYBDATEN
    private static int
    SYBDATETIME
    private static int
    SYBDATETIME4
    private static int
    SYBDATETIMN
    private static int
    SYBDECIMAL
    private static int
    SYBFLT8
    private static int
    SYBFLTN
    private static int
    SYBIMAGE
    private static int
    SYBINT1
    private static int
    SYBINT2
    private static int
    SYBINT4
    private static int
    SYBINT8
    private static int
    SYBINTN
    private static int
    SYBLONGBINARY
    (package private) static int
    SYBLONGDATA
    private static int
    SYBMONEY
    private static int
    SYBMONEY4
    private static int
    SYBMONEYN
    private static int
    SYBNTEXT
    private static int
    SYBNUMERIC
    private static int
    SYBNVARCHAR
    private static int
    SYBREAL
    private static int
    SYBSINT1
    private static int
    SYBSINT8
    private static int
    SYBTEXT
    private static int
    SYBTIME
    private static int
    SYBTIMEN
    private static int
    SYBUINT2
    private static int
    SYBUINT4
    private static int
    SYBUINT8
    private static int
    SYBUINTN
    private static int
    SYBUNIQUE
    private static int
    SYBUNITEXT
    private static int
    SYBVARBINARY
    private static int
    SYBVARCHAR
    private static int
    SYBVARIANT
    private static int
    SYBVOID
    private static int
    SYB_CHUNK_SIZE
    private static int
    SYB_LONGVAR_MAX
    private static int
    UDT_BINARY
    private static int
    UDT_CHAR
    private static int
    UDT_LONGSYSNAME
    private static int
    UDT_NCHAR
    private static int
    UDT_NEWSYSNAME
    private static int
    UDT_NVARCHAR
    private static int
    UDT_SYSNAME
    private static int
    UDT_TIMESTAMP
    private static int
    UDT_UNICHAR
    private static int
    UDT_UNITEXT
    private static int
    UDT_UNIVARCHAR
    private static int
    UDT_VARBINARY
    private static int
    UDT_VARCHAR
    private static int
    VAR_MAX
    private static int
    XSYBBINARY
    private static int
    XSYBCHAR
    private static int
    XSYBNCHAR
    private static int
    XSYBNVARCHAR
    private static int
    XSYBVARBINARY
    private static int
    XSYBVARCHAR
    private static TypeInfo[]
    types
    Array of TDS data type descriptors.

    Constructor Summary

    TdsData()
    Private constructor to prevent users creating an actual instance of this class.

    Method Summary

    private static boolean
    canEncode(String value, String charset)
    Establish if a String can be converted to a byte based character set.
    (package private) static void
    fillInType(ColInfo ci)
    Fill in the TDS native type code and all other fields for a ColInfo instance with the JDBC type set.
    (package private) static int
    getCollation(ResponseStream in, ColInfo ci)
    TDS 8 supplies collation information for character data types.
    private static Object
    getDatetimeValue(ResponseStream in, int type)
    Get a DATETIME value from the server response stream.
    static String
    getMSTypeName(String typeName, int tdsType)
    For SQL 2005 This routine will modify the meta data to allow the caller to distinguish between varchar(max) and text or varbinary(max) and image or nvarchar(max) and ntext.
    private static Object
    getMoneyValue(ResponseStream in, int type)
    Read a MONEY value from the server response stream.
    (package private) static void
    getNativeType(ConnectionJDBC2 connection, ParamInfo pi)
    Retrieve the TDS native type code for the parameter.
    (package private) static int
    getTds5ParamSize(String charset, boolean isWideChar, ParamInfo pi, boolean useParamNames)
    Calculate the size of the parameter descriptor array for TDS 5 packets.
    static int
    getTdsVersion(int rawTdsVersion)
    Extract the TDS protocol version from the value returned by the server in the LOGINACK packet.
    private static Object
    getVariant(ConnectionJDBC2 connection, ResponseStream in)
    Read a MSQL 2000 sql_variant data value from the input stream.
    (package private) static boolean
    isCollation(ColInfo ci)
    Retrieve the collation status of the column.
    (package private) static boolean
    isCurrency(ColInfo ci)
    Retrieve the currency status of the column.
    (package private) static boolean
    isSearchable(ColInfo ci)
    Retrieve the searchable status of the column.
    (package private) static boolean
    isSigned(ColInfo ci)
    Retrieve the signed status of the column.
    (package private) static boolean
    isUnicode(ColInfo ci)
    Determines whether the column is Unicode encoded.
    (package private) static void
    putCollation(RequestStream out, ParamInfo pi)
    TDS 8 requires collation information for char data descriptors.
    private static void
    putDateTimeValue(RequestStream out, DateTime value)
    Output a java.sql.Date/Time/Timestamp value to the server as a Sybase datetime value.
    (package private) static Object
    readData(ConnectionJDBC2 connection, ResponseStream in, ColInfo ci)
    Read the TDS data item from the Response Stream.
    (package private) static int
    readType(ResponseStream in, ColInfo ci)
    Read the TDS datastream and populate the ColInfo parameter with data type and related information.
    (package private) static void
    setColumnCharset(ColInfo ci, ConnectionJDBC2 connection)
    Set the charsetInfo field of ci according to the value of its collation field.
    (package private) static void
    writeParam(RequestStream out, CharsetInfo charsetInfo, byte[] collation, ParamInfo pi)
    Write a parameter to the server request stream.
    (package private) static void
    writeTds5Param(RequestStream out, CharsetInfo charsetInfo, ParamInfo pi)
    Write the actual TDS 5 parameter data.
    (package private) static void
    writeTds5ParamFmt(RequestStream out, String charset, boolean isWideChar, ParamInfo pi, boolean useParamNames)
    Write a TDS 5 parameter format descriptor.

    Field Details

    DEFAULT_PRECISION_28

    (package private) static final int DEFAULT_PRECISION_28
    Default precision for SQL Server 6.5 and 7.
    Field Value:
    28

    DEFAULT_PRECISION_38

    (package private) static final int DEFAULT_PRECISION_38
    Default precision for Sybase and SQL Server 2000 and newer.
    Field Value:
    38

    DEFAULT_SCALE

    (package private) static final int DEFAULT_SCALE
    Default Decimal Scale.
    Field Value:
    10

    MS_LONGVAR_MAX

    private static final int MS_LONGVAR_MAX
    Field Value:
    8000

    SYBBINARY

    private static final int SYBBINARY
    Field Value:
    45

    SYBBIT

    private static final int SYBBIT
    Field Value:
    50

    SYBBITN

    private static final int SYBBITN
    Field Value:
    104

    SYBCHAR

    private static final int SYBCHAR
    Field Value:
    47

    SYBDATE

    private static final int SYBDATE
    Field Value:
    49

    SYBDATEN

    private static final int SYBDATEN
    Field Value:
    123

    SYBDATETIME

    private static final int SYBDATETIME
    Field Value:
    61

    SYBDATETIME4

    private static final int SYBDATETIME4
    Field Value:
    58

    SYBDATETIMN

    private static final int SYBDATETIMN
    Field Value:
    111

    SYBDECIMAL

    private static final int SYBDECIMAL
    Field Value:
    106

    SYBFLT8

    private static final int SYBFLT8
    Field Value:
    62

    SYBFLTN

    private static final int SYBFLTN
    Field Value:
    109

    SYBIMAGE

    private static final int SYBIMAGE
    Field Value:
    34

    SYBINT1

    private static final int SYBINT1
    Field Value:
    48

    SYBINT2

    private static final int SYBINT2
    Field Value:
    52

    SYBINT4

    private static final int SYBINT4
    Field Value:
    56

    SYBINT8

    private static final int SYBINT8
    Field Value:
    127

    SYBINTN

    private static final int SYBINTN
    Field Value:
    38

    SYBLONGBINARY

    private static final int SYBLONGBINARY
    Field Value:
    225

    SYBLONGDATA

    (package private) static final int SYBLONGDATA
    Field Value:
    36

    SYBMONEY

    private static final int SYBMONEY
    Field Value:
    60

    SYBMONEY4

    private static final int SYBMONEY4
    Field Value:
    122

    SYBMONEYN

    private static final int SYBMONEYN
    Field Value:
    110

    SYBNTEXT

    private static final int SYBNTEXT
    Field Value:
    99

    SYBNUMERIC

    private static final int SYBNUMERIC
    Field Value:
    108

    SYBNVARCHAR

    private static final int SYBNVARCHAR
    Field Value:
    103

    SYBREAL

    private static final int SYBREAL
    Field Value:
    59

    SYBSINT1

    private static final int SYBSINT1
    Field Value:
    64

    SYBSINT8

    private static final int SYBSINT8
    Field Value:
    191

    SYBTEXT

    private static final int SYBTEXT
    Field Value:
    35

    SYBTIME

    private static final int SYBTIME
    Field Value:
    51

    SYBTIMEN

    private static final int SYBTIMEN
    Field Value:
    147

    SYBUINT2

    private static final int SYBUINT2
    Field Value:
    65

    SYBUINT4

    private static final int SYBUINT4
    Field Value:
    66

    SYBUINT8

    private static final int SYBUINT8
    Field Value:
    67

    SYBUINTN

    private static final int SYBUINTN
    Field Value:
    68

    SYBUNIQUE

    private static final int SYBUNIQUE
    Field Value:
    36

    SYBUNITEXT

    private static final int SYBUNITEXT
    Field Value:
    174

    SYBVARBINARY

    private static final int SYBVARBINARY
    Field Value:
    37

    SYBVARCHAR

    private static final int SYBVARCHAR
    Field Value:
    39

    SYBVARIANT

    private static final int SYBVARIANT
    Field Value:
    98

    SYBVOID

    private static final int SYBVOID
    Field Value:
    31

    SYB_CHUNK_SIZE

    private static final int SYB_CHUNK_SIZE
    Field Value:
    8192

    SYB_LONGVAR_MAX

    private static final int SYB_LONGVAR_MAX
    Field Value:
    16384

    UDT_BINARY

    private static final int UDT_BINARY
    Field Value:
    3

    UDT_CHAR

    private static final int UDT_CHAR
    Field Value:
    1

    UDT_LONGSYSNAME

    private static final int UDT_LONGSYSNAME
    Field Value:
    42

    UDT_NCHAR

    private static final int UDT_NCHAR
    Field Value:
    24

    UDT_NEWSYSNAME

    private static final int UDT_NEWSYSNAME
    Field Value:
    256

    UDT_NVARCHAR

    private static final int UDT_NVARCHAR
    Field Value:
    25

    UDT_SYSNAME

    private static final int UDT_SYSNAME
    Field Value:
    18

    UDT_TIMESTAMP

    private static final int UDT_TIMESTAMP
    Field Value:
    80

    UDT_UNICHAR

    private static final int UDT_UNICHAR
    Field Value:
    34

    UDT_UNITEXT

    private static final int UDT_UNITEXT
    Field Value:
    36

    UDT_UNIVARCHAR

    private static final int UDT_UNIVARCHAR
    Field Value:
    35

    UDT_VARBINARY

    private static final int UDT_VARBINARY
    Field Value:
    4

    UDT_VARCHAR

    private static final int UDT_VARCHAR
    Field Value:
    2

    VAR_MAX

    private static final int VAR_MAX
    Field Value:
    255

    XSYBBINARY

    private static final int XSYBBINARY
    Field Value:
    173

    XSYBCHAR

    private static final int XSYBCHAR
    Field Value:
    175

    XSYBNCHAR

    private static final int XSYBNCHAR
    Field Value:
    239

    XSYBNVARCHAR

    private static final int XSYBNVARCHAR
    Field Value:
    231

    XSYBVARBINARY

    private static final int XSYBVARBINARY
    Field Value:
    165

    XSYBVARCHAR

    private static final int XSYBVARCHAR
    Field Value:
    167

    types

    private static final TypeInfo[] types
    Array of TDS data type descriptors.

    Constructor Details

    TdsData

    private TdsData()
    Private constructor to prevent users creating an actual instance of this class.

    Method Details

    canEncode

    private static boolean canEncode(String value,
                                     String charset)
    Establish if a String can be converted to a byte based character set.
    Parameters:
    value - The String to test.
    charset - The server character set in force.
    Returns:
    boolean true if string can be converted.

    fillInType

    (package private) static void fillInType(ColInfo ci)
                throws SQLException
    Fill in the TDS native type code and all other fields for a ColInfo instance with the JDBC type set.
    Parameters:
    ci - the ColInfo instance

    getCollation

    (package private) static int getCollation(ResponseStream in,
                                              ColInfo ci)
                throws IOException
    TDS 8 supplies collation information for character data types.
    Parameters:
    in - the server response stream
    ci - the column descriptor
    Returns:
    the number of bytes read from the stream as an int

    getDatetimeValue

    private static Object getDatetimeValue(ResponseStream in,
                                           int type)
                throws IOException,
                       ProtocolException
    Get a DATETIME value from the server response stream.
    Parameters:
    in - The server response stream.
    type - The TDS data type.
    Returns:
    The java.sql.Timestamp value or null.

    getMSTypeName

    public static String getMSTypeName(String typeName,
                                       int tdsType)
    For SQL 2005 This routine will modify the meta data to allow the caller to distinguish between varchar(max) and text or varbinary(max) and image or nvarchar(max) and ntext.
    Parameters:
    typeName - the SQL type returned by sp_columns
    tdsType - the TDS type returned by sp_columns
    Returns:
    the (possibly) modified SQL type name as a String

    getMoneyValue

    private static Object getMoneyValue(ResponseStream in,
                                        int type)
                throws IOException,
                       ProtocolException
    Read a MONEY value from the server response stream.
    Parameters:
    in - The server response stream.
    type - The TDS data type.
    Returns:
    The java.math.BigDecimal value or null.

    getNativeType

    (package private) static void getNativeType(ConnectionJDBC2 connection,
                                                ParamInfo pi)
                throws SQLException
    Retrieve the TDS native type code for the parameter.
    Parameters:
    connection - the connectionJDBC object
    pi - the parameter descriptor

    getTds5ParamSize

    (package private) static int getTds5ParamSize(String charset,
                                                  boolean isWideChar,
                                                  ParamInfo pi,
                                                  boolean useParamNames)
    Calculate the size of the parameter descriptor array for TDS 5 packets.
    Parameters:
    charset - The encoding character set.
    isWideChar - True if multi byte encoding.
    pi - The parameter to describe.
    useParamNames - True if named parameters should be used.
    Returns:
    The size of the parameter descriptor as an int.

    getTdsVersion

    public static int getTdsVersion(int rawTdsVersion)
    Extract the TDS protocol version from the value returned by the server in the LOGINACK packet.
    Parameters:
    rawTdsVersion - the TDS protocol version as returned by the server
    Returns:
    the jTDS internal value for the protocol version (i.e one of the Driver.TDSXX values)

    getVariant

    private static Object getVariant(ConnectionJDBC2 connection,
                                     ResponseStream in)
                throws IOException,
                       ProtocolException
    Read a MSQL 2000 sql_variant data value from the input stream.

    SQL_VARIANT has the following structure:

    1. INT4 total size of data
    2. INT1 TDS data type (text/image/ntext/sql_variant not allowed)
    3. INT1 Length of extra type descriptor information
    4. Optional additional type info required by some types
    5. byte[0...n] the actual data
    Parameters:
    connection - used to obtain collation/charset information
    in - the server response stream
    Returns:
    the SQL_VARIANT data

    isCollation

    (package private) static boolean isCollation(ColInfo ci)
    Retrieve the collation status of the column.

    TDS 8.0 character columns include collation information.

    Parameters:
    ci - the column meta data
    Returns:
    true if the column requires collation data.

    isCurrency

    (package private) static boolean isCurrency(ColInfo ci)
    Retrieve the currency status of the column.
    Parameters:
    ci - The column meta data.
    Returns:
    boolean true if the column is a currency type.

    isSearchable

    (package private) static boolean isSearchable(ColInfo ci)
    Retrieve the searchable status of the column.
    Parameters:
    ci - the column meta data
    Returns:
    true if the column is not a text or image type.

    isSigned

    (package private) static boolean isSigned(ColInfo ci)
    Retrieve the signed status of the column.
    Parameters:
    ci - the column meta data
    Returns:
    true if the column is a signed numeric.

    isUnicode

    (package private) static boolean isUnicode(ColInfo ci)
    Determines whether the column is Unicode encoded.
    Parameters:
    ci - the column meta data
    Returns:
    true if the column is Unicode encoded

    putCollation

    (package private) static void putCollation(RequestStream out,
                                               ParamInfo pi)
                throws IOException
    TDS 8 requires collation information for char data descriptors.
    Parameters:
    out - The Server request stream.
    pi - The parameter descriptor.

    putDateTimeValue

    private static void putDateTimeValue(RequestStream out,
                                         DateTime value)
                throws IOException
    Output a java.sql.Date/Time/Timestamp value to the server as a Sybase datetime value.
    Parameters:
    out - the server request stream
    value - the date value to write

    readData

    (package private) static Object readData(ConnectionJDBC2 connection,
                                             ResponseStream in,
                                             ColInfo ci)
                throws IOException,
                       ProtocolException
    Read the TDS data item from the Response Stream.

    The data size is either implicit in the type for example fixed size integers, or a count field precedes the actual data. The size of the count field varies with the data type.

    Parameters:
    connection - an object reference to the caller of this method; must be a Connection, Statement or ResultSet
    in - The server ResponseStream.
    ci - The ColInfo column descriptor object.
    Returns:
    The data item Object or null.

    readType

    (package private) static int readType(ResponseStream in,
                                          ColInfo ci)
                throws IOException,
                       ProtocolException
    Read the TDS datastream and populate the ColInfo parameter with data type and related information.

    The type infomation conforms to one of the following formats:

    1. [int1 type] - eg SYBINT4.
    2. [int1 type] [int1 buffersize] - eg VARCHAR < 256
    3. [int1 type] [int2 buffersize] - eg VARCHAR > 255.
    4. [int1 type] [int4 buffersize] [int1 tabnamelen] [int1*n tabname] - eg text.
    5. [int1 type] [int4 buffersize] - eg sql_variant.
    6. [int1 type] [int1 buffersize] [int1 precision] [int1 scale] - eg decimal.
    For TDS 8 large character types include a 5 byte collation field after the buffer size.
    Parameters:
    in - The server response stream.
    ci - The ColInfo column descriptor object.
    Returns:
    The number of bytes read from the input stream.

    setColumnCharset

    (package private) static void setColumnCharset(ColInfo ci,
                                                   ConnectionJDBC2 connection)
                throws SQLException
    Set the charsetInfo field of ci according to the value of its collation field.

    The Connection is used to find out whether a specific charset was requested. In this case, the column charset will be ignored.

    Parameters:
    ci - the ColInfo instance to update
    connection - a Connection instance to check whether it has a fixed charset or not

    writeParam

    (package private) static void writeParam(RequestStream out,
                                             CharsetInfo charsetInfo,
                                             byte[] collation,
                                             ParamInfo pi)
                throws IOException
    Write a parameter to the server request stream.
    Parameters:
    out - the server request stream
    charsetInfo - the default character set
    collation - the default SQL Server 2000 collation
    pi - the parameter descriptor

    writeTds5Param

    (package private) static void writeTds5Param(RequestStream out,
                                                 CharsetInfo charsetInfo,
                                                 ParamInfo pi)
                throws IOException,
                       SQLException
    Write the actual TDS 5 parameter data.
    Parameters:
    out - the server RequestStream
    charsetInfo - the encoding character set
    pi - the parameter to output

    writeTds5ParamFmt

    (package private) static void writeTds5ParamFmt(RequestStream out,
                                                    String charset,
                                                    boolean isWideChar,
                                                    ParamInfo pi,
                                                    boolean useParamNames)
                throws IOException
    Write a TDS 5 parameter format descriptor.
    Parameters:
    out - The server RequestStream.
    charset - The encoding character set.
    isWideChar - True if multi byte encoding.
    pi - The parameter to describe.
    useParamNames - True if named parameters should be used.

    Generated on June 12 2008