📄 packetrowresult.java
字号:
//
// 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 net.sourceforge.jtds.jdbc;
import java.math.BigDecimal;
import java.sql.*;
import java.util.Vector;
/**
* encapsulate the information from one row of a result set.
*
*@author Craig Spannring
*@created 17 March 2001
*/
public class PacketRowResult extends PacketResult {
Vector row = null;
Context context;
boolean wasNull = false;
/**
* /** @todo Description of the Field
*/
public final static String cvsVersion = "$Id: PacketRowResult.java,v 1.3 2002/10/22 09:50:10 alin_sinpalean Exp $";
public PacketRowResult( Context context )
{
super( TdsDefinitions.TDS_ROW );
this.context = context;
row = new Vector( realColumnCount() );
row.setSize( realColumnCount() );
}
public Context getContext()
{
return context;
}
/**
* Sets the component at the specified index of this vector to be the
* specified object. The previous component at that position is discarded.
*
* <UL>Note- <\UL>Unlike the vector class this class starts the index with
* 1, not 0.
*
*@param obj The object to store
*@param index Index to store the element at. First element is
* at index 1
*/
public void setElementAt( int index, Object obj )
throws SQLException
{
if ( index < 1 || index > realColumnCount() ) {
throw new SQLException( "Bad index " + index );
}
row.setElementAt( obj, index - 1 );
}
private int realColumnCount()
{
return context.getColumnInfo().realColumnCount();
}
public int getColumnType( int index )
throws SQLException
{
return context.getColumnInfo().getJdbcType( index );
}
/**
* get an element at the specified index <p>
*
*
* <UL>Note- <\UL>Unlike the vector class this starts the index with 1, not
* 0.
*
*@param index Index to get the element from. First element is
* at index 1
*@return The ElementAt value
*@exception TdsException @todo Description of Exception
*/
public Object getElementAt( int index )
throws TdsException
{
if ( index < 1 || index > realColumnCount() ) {
throw new TdsException( "Bad index " + index );
}
return row.elementAt( index - 1 );
}
public Object getObject( int columnIndex )
throws SQLException
{
// This method is implicitly coupled to the getRow() method in the
// Tds class. Every type that getRow() could return must
// be handled in this method.
//
// The object type returned by getRow() must correspond with the
// jdbc SQL type in the switch statement below.
//
// Note- The JDBC spec (version 1.20) does not define the type
// of the Object returned for LONGVARCHAR data.
// XXX- Needs modifications for JDBC 2.0
try
{
Object tmp = getElementAt( columnIndex );
wasNull = false;
if( tmp == null )
{
wasNull = true;
return null;
}
else
{
switch ( getColumnType( columnIndex ) )
{
case java.sql.Types.CHAR:
case java.sql.Types.VARCHAR:
if( tmp instanceof String )
return tmp;
else
throw new SQLException( "Was expecting CHAR data. Got"
+ tmp.getClass().getName() );
case java.sql.Types.TINYINT:
case java.sql.Types.SMALLINT:
case java.sql.Types.INTEGER:
if( tmp instanceof Integer )
return tmp;
if(! (tmp instanceof Number) )
throw new SQLException("Can't convert "+tmp.getClass().getName()+
" to Integer.");
return new Integer(((Number)tmp).intValue());
case java.sql.Types.BIGINT:
if( tmp instanceof Long )
return tmp;
if(! (tmp instanceof Number))
throw new SQLException("Internal error");
return new Long(((Number)tmp).longValue());
case java.sql.Types.REAL:
if (! (tmp instanceof Float))
throw new SQLException("Internal error");
return tmp;
case java.sql.Types.FLOAT:
case java.sql.Types.DOUBLE:
if( tmp instanceof Double )
return tmp;
else if ( tmp instanceof Number )
return new Double( ( ( Number ) tmp ).doubleValue() );
else
throw new SQLException( "Was expecting Numeric data. Got"
+ tmp.getClass().getName() );
case java.sql.Types.DATE:
// XXX How do the time types hold up with timezones?
if( !( tmp instanceof Timestamp ) )
throw new SQLException( "Internal error" );
// java.util.Calendar cal = new java.util.GregorianCalendar();
// cal.setTime(getTimestamp(columnIndex));
// result = cal.getTime();
return new Date( ( ( Timestamp ) tmp ).getTime() );
case java.sql.Types.TIME:
if( !( tmp instanceof Timestamp ) )
throw new SQLException( "Internal error" );
return new Time( ( ( Timestamp ) tmp ).getTime() );
case java.sql.Types.TIMESTAMP:
if( !( tmp instanceof Timestamp ) )
throw new SQLException( "Internal error" );
return tmp;
case java.sql.Types.BINARY:
case java.sql.Types.VARBINARY:
return getBytes( columnIndex );
case java.sql.Types.DECIMAL:
case java.sql.Types.NUMERIC:
if( tmp instanceof BigDecimal )
return tmp;
else if( tmp instanceof Number )
return new BigDecimal( ( ( Number ) tmp ).doubleValue() );
else
throw new SQLException( "Was expecting NUMERIC data. Got"
+ tmp.getClass().getName() );
case java.sql.Types.LONGVARCHAR:
if( tmp instanceof TdsAsciiInputStream )
return tmp.toString();
else if( tmp instanceof java.lang.String )
return tmp;
else
throw new SQLException( "Was expecting LONGVARCHAR data. "
+ "Got "
+ tmp.getClass().getName() );
case java.sql.Types.LONGVARBINARY:
throw new SQLException( "Not implemented" );
case java.sql.Types.NULL:
throw new SQLException( "Not implemented" );
case java.sql.Types.OTHER:
throw new SQLException( "Not implemented" );
case java.sql.Types.BIT:
if( tmp instanceof Boolean )
return tmp;
else
throw new SQLException( "Was expecting BIT data. "
+ "Got"
+ tmp.getClass().getName() );
default:
throw new SQLException( "Unknown datatype "
+ getColumnType( columnIndex ));
}
}
}
catch ( net.sourceforge.jtds.jdbc.TdsException e )
{
throw new SQLException( e.getMessage() );
}
}
public byte[] getBytes( int columnIndex ) throws SQLException
{
byte result[];
try {
Object tmp = getElementAt( columnIndex );
wasNull = false;
if ( tmp == null ) {
wasNull = true;
result = null;
}
else if ( tmp instanceof byte[] ) {
result = ( byte[] ) tmp;
}
else if ( tmp instanceof String ) {
result = context.getEncoder().getBytes( ( String ) tmp );
}
else {
throw new SQLException( "Can't convert column " + columnIndex
+ " from "
+ tmp.getClass().getName()
+ " to byte[]" );
}
}
catch ( TdsException e ) {
throw new SQLException( e.getMessage() );
}
return result;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -