📄 tinysqlresultset.java
字号:
/*
* The tinySQLResultSet class for the tinySQL JDBC Driver
*
* A lot of this code is based on or directly taken from
* George Reese's (borg@imaginary.com) mSQL driver.
*
* So, it's probably safe to say:
*
* Portions of this code Copyright (c) 1996 George Reese
*
* The rest of it:
*
* Copyright 1996, Brian C. Jepson
* (bjepson@ids.net)
*
* $Author: davis $
* $Date: 2004/12/18 21:30:40 $
* $Revision: 1.1 $
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.sqlmagic.tinysql;
import java.sql.Date;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Ref;
import java.sql.Array;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Hashtable;
import java.text.ParsePosition;
/**
* @author Thomas Morgner <mgs@sherito.org>
* <ul>
* <li>tinySQLResultSet now holds a reference
* to statement which created it. If the resultset was not created by an statement,
* <code>null</code> is returned (f.i. used in DatabaseMetaData-Queries).
* <li>Changed the code to support setting a fetchsize for queries.
* <li>Parsing of dates corrected to format yyyyMMdd
* <li>Now supporting the FetchDirection methods, getType().
* <li>Concurrency is not returned as CONCUR_READ_ONLY as lowest common standard.
* <li>getColumnTypes returns now values given in tsResultSet and does not try to
* guess from strings.
* </ul>
*/
public class tinySQLResultSet implements java.sql.ResultSet {
// The Statement that created this resultset
private tinySQLStatement statement=(tinySQLStatement)null;
private tinySQLPreparedStatement preparedStatement=(tinySQLPreparedStatement)null;
/**
*
* The tsResultSet
*
*/
private tsResultSet result;
/**
*
* A tsRow object to hold the current row
*
*/
private tsRow current_row;
/**
*
* The index of the current row
*
*/
private int current_row_index = 0;
/**
*
* The meta data for this result set.
*
*/
private tinySQLResultSetMetaData meta;
/**
*
* A Hashtable that maps column names to columns
*
*/
private Hashtable column_map = null;
/**
*
* Given a tsResultSet, this will construct a new tinySQLResultSet
* @param res the tsResultSet from a query
*
*/
public tinySQLResultSet(tsResultSet res, tinySQLStatement inputStatement) {
result = res;
this.statement = inputStatement;
}
public tinySQLResultSet(tsResultSet res, tinySQLPreparedStatement inputStatement) {
result = res;
this.preparedStatement = inputStatement;
}
/**
*
* Advance to the next row in the result set.
* @see java.sql.ResultSet#next
* @exception SQLException thrown in case of error
* @return true if there are any more rows to process, otherwise false.
*
*/
public synchronized boolean next() throws SQLException {
try {
// automatically return false if the
// result set is empty
//
if( result.size() < 1 ) {
return false;
}
// increment the current row index
//
current_row_index++;
// retrieve the row at the current_row_index and store
// it in the current_row field.
//
current_row = result.rowAt(current_row_index - 1);
// If no rows was retrieved, return false to indicate
// that there are no more results.
if (current_row == null)
{
return false;
}
return true;
} catch( Exception e ) {
throw new SQLException(e.getMessage());
}
}
/**
*
* Provides a method to close a ResultSet
*
* @see java.sql.ResultSet#close
*
*/
public void close() throws SQLException {
}
/**
*
* Returns whether or not the last column read was null.
* tinySQL doesn't have nulls, so this is inconsequential...
* @see java.sql.ResultSet#wasNull
* @return true if the column was null, false otherwise
*
*/
public boolean wasNull() throws SQLException {
return false;
}
/**
*
* Gets the value of a column (by index) as a String.
* @see java.sql.ResultSet#getString
* @exception SQLException thrown for bogus column index
* @param column the column index
* @return the column's String value
*
*/
public String getString(int column) throws SQLException
{
String columnValue;
// retrieve the column at the specified index. tinySQL
// has a column offset of zero, while JDBC uses one.
// Because of this, I need to subtract one from the
// index to get the tinySQL index.
//
if ( current_row == (tsRow)null ) return (String)null;
tsColumn col = result.columnAtIndex(column-1);
// return the column's value
//
columnValue = current_row.columnAsString(col);
/*
* Reformat date columns to YYYY-MM-DD - this is standard default
* Java format for getString on Date columns.
*/
if ( Utils.isDateColumn(col.type) )
columnValue = UtilString.toStandardDate(columnValue);
return columnValue;
}
/**
*
* Get the value of a column in the current row as a Java byte.
*
* @param columnIndex the first column is 1, the second is 2, ...
* @return the column value
*
*/
public byte getByte(int column) throws SQLException {
// get the column as a string
//
String str = getString(column);
// if it's a blank string, return 0,
// otherwise, cast the first character
// in the string to byte and return it.
//
if( str.equals("") ) {
return 0;
} else {
return (byte)str.charAt(0);
}
}
/**
*
* Get the value of a column in the current row as boolean
* @see java.sql.ResultSet#getBoolean
* @exception SQLException Harum Scarum's coming down fast, Clay...
* @param column the column index
* @return false for "", null, or "0"; true otherwise
*/
public boolean getBoolean(int column) throws SQLException {
try {
// get the column as a string
//
String str = getString(column);
// a blank string is false
//
if( str.equals("") ) return false;
// a zero is false
//
if( str.equals("0") ) return false;
// nothing is true... everything is permitted
//
return true;
} catch( Exception e ) {
throw new SQLException(e.getMessage());
}
}
/**
*
* Get the value of a column in the current row as a short.
* @see java.sql.ResultSet#getShort
* @exception SQLException D'ohh!
* @param column the column being retrieved
* @return the column as a short
*
*/
public short getShort(int column)
throws SQLException
{
// get the column as a string
//
String str = getString(column);
// if it's null, return 0
//
if( str == null ) return 0;
// try to convert it to an integer, and cast it to short
//
try
{
return ( short )( Integer.valueOf( str.trim( )).intValue( ));
}
catch( NumberFormatException e )
{
throw new SQLException( "tinySQL invalid short Number: " + e.getMessage( ));
}
}
/**
*
* Retrieve a column from the current row as an int
* @see java.sql.ResultSet#getInt
* @exception SQLException bad things... the wind began to howl...
* @param column the column being retrieved
* @return the column as an integer
*
*/
public int getInt(int column)
throws SQLException
{
int dotAt;
// get the column as a string
//
String str = getString(column);
// if it's null, return Integer.MIN_VALUE
//
if( str == null ) return Integer.MIN_VALUE;
// try to convert this string to integer
//
dotAt = str.indexOf(".");
if ( dotAt > -1 ) str = str.substring(0,dotAt);
try
{
return Integer.valueOf( str.trim( )).intValue( );
}
catch( NumberFormatException e )
{
return Integer.MIN_VALUE;
}
}
/**
*
* Get the value of a column in the current row as a long
* @see java.sql.ResultSet#getLong
* @exception SQLException in case of an error
* @param column the column being retrieved
* @return the column as a long
*
*/
public long getLong(int column)
throws SQLException
{
// get the column as a string
//
String str = getString(column);
// it it's null, return 0
if( str == null ) return 0;
// try to convert the String to a long
//
try
{
return Long.valueOf( str.trim( )).longValue( );
}
catch( NumberFormatException e )
{
throw new SQLException( "tinySQL invalid Long Number: " + e.getMessage( ));
}
}
/**
*
* Return a column as a float.
* @see java.sql.ResultSet#getFloat
* @exception SQLException in case of error
* @param column the column being retrieved
* @return the column as a float
*
*/
public float getFloat(int column)
throws SQLException
{
// get the column as a string
//
String str = getString(column);
// if it's null, assume zero
//
if( str == null ) return 0;
// try to perform the conversion
//
try
{
return Float.valueOf( str.trim( )).floatValue( );
}
catch( NumberFormatException e )
{
throw new SQLException( "Invalid Number: " + e.getMessage( ));
}
}
/**
*
* Return a column as a double
* @see java.sql.ResultSet#getDouble
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -