FileDocCategorySizeDatePackage
MsqlResultSet.javaAPI DocExample16833Tue Jan 01 00:00:00 GMT 1980COM.imaginary.sql.msql

MsqlResultSet

public class MsqlResultSet extends Object implements Runnable, ResultSet
The MsqlResultSet class implements the JDBC ResultSet interface. This class represents a SQL result set whose values can be retrieved through the class' methods. This class should never be directly referenced. Instead, you should use the JDBC API.
Last modified %D%
version
%A%
author
George Reese (borg@imaginary.com)

Fields Summary
private final int
column_count
private Hashtable
column_map
private boolean
complete
private int
current_row
private byte[]
current_row_data
private MsqlResultSet
field_info
private byte[]
last_column
private MsqlInputStream
input
private ResultSetMetaData
meta_data
private boolean
meta_set
private MsqlException
read_exception
private Vector
rows
private MsqlStatement
statement
private SQLWarning
warnings
Constructors Summary
MsqlResultSet(MsqlStatement s, MsqlInputStream in, int count)


    // Constructs the result set
         
      
	this(s, in, count, false);
	column_count = count;
    
MsqlResultSet(MsqlStatement s, MsqlInputStream in, int count, boolean meta)

	super();
	statement = s;
	input = in;
	column_count = count;
	meta_set = meta;
	(new Thread(this)).start();
    
Methods Summary
public synchronized voidclearWarnings()

	warnings = null;
    
public voidclose()
Closes the result set.

exception
java.sql.SQLException thrown for errors on closing

	synchronized( rows ) {
	    while( !complete ) {
		try {
		    rows.wait();
		}
		catch( InterruptedException e ) {
		}
	    }
	    if( !meta_set && field_info != null ) {
		field_info.close();
	    }
	    input = null;
	}
    
public synchronized intfindColumn(java.lang.String name)

param
name the name of the desired column
return
the column number for the specified column name
exception
java.sql.SQLException thrown on a read error

	Integer num;
    
	if( column_map == null ) {
	    ResultSetMetaData meta;
	    int maxi;
	    
	    // Thanks to Joern Kellermann for this fix
	    meta = getMetaData();
	    column_map = new Hashtable(maxi = meta.getColumnCount());
	    for(int i=1; i<=maxi; i++) {
		column_map.put(meta.getColumnName(i), new Integer(i));
	    }
	}
	num = (Integer)column_map.get(name);
	if( num == null ) {
	    throw new SQLException("Invalid column name: " + name);
	}
	return num.intValue();
    
public synchronized java.io.InputStreamgetAsciiStream(java.lang.String cname)
For performance reasons, you should get values by column number when at all possible.

param
cname the name of the desired column
return
an ASCII input stream for the column
exception
java.sql.SQLException thrown when the column cannot be read

	return getAsciiStream(findColumn(cname));
    
public synchronized java.io.InputStreamgetAsciiStream(int column)

param
column the column number for the desired column
return
an ASCII input stream for the desired column
exception
java.sql.SQLException thrown when the columb cannot be read

	getColumn(column);
	return new MsqlAsciiInputStream(last_column);
    
public synchronized java.math.BigDecimalgetBigDecimal(java.lang.String cname, int scale)

	return getBigDecimal(findColumn(cname), scale);
    
public synchronized java.math.BigDecimalgetBigDecimal(int column, int scale)

	String tmp = getString(column);
	     
	if( tmp == null ) {
	    return new BigDecimal(new BigInteger("0"), scale);
	}
	else {
	    return new BigDecimal(new BigInteger(tmp), scale);
	}
    
public synchronized java.io.InputStreamgetBinaryStream(java.lang.String cname)

	return getBinaryStream(findColumn(cname));
    
public synchronized java.io.InputStreamgetBinaryStream(int column)

	getColumn(column);
	return new ByteArrayInputStream(last_column);
    
public synchronized booleangetBoolean(java.lang.String cname)

	return getBoolean(findColumn(cname));
    
public synchronized booleangetBoolean(int column)

	getColumn(column);
	if( wasNull() ) {
	    return false;
	}
	if( last_column.length == 0 ) {
	    return false;
	}
	else if( last_column[0] == '0" || last_column[0] == '\0" ) {
	    return false;
	}
	else {
	    return true;
	}
    
public synchronized bytegetByte(java.lang.String cname)

	return getByte(findColumn(cname));
    
public synchronized bytegetByte(int column)

	getColumn(column);
	if( wasNull() ) {
	    return (byte)0;
	}
	else {
	    return last_column[0];
	}
    
public synchronized byte[]getBytes(java.lang.String cname)

	return getBytes(findColumn(cname));
    
public synchronized byte[]getBytes(int column)

	getColumn(column);
	return last_column;
    
private synchronized voidgetColumn(int column)

	try {
	    last_column = current_row_data[column-1];
	}
	catch( Exception e ) {
	    if( current_row_data == null ) {
		throw new MsqlException("-1:Result set positioned before " +
					"first row.");
	    }
	    throw new MsqlException(e);
	}
    
public java.lang.StringgetCursorName()

	throw new SQLException("mSQL does not support cursors.");
    
public synchronized java.sql.DategetDate(java.lang.String cname)

	return getDate(findColumn(cname));
    
public synchronized java.sql.DategetDate(int column)

	long time = getTimeAsLong(column);

	if( wasNull() ) {
	    return null;
	}
	else {
	    return new Date(time);
	}
    
public synchronized doublegetDouble(java.lang.String cname)

	return getDouble(findColumn(cname));
    
public synchronized doublegetDouble(int column)

	String tmp = getString(column);
	
	if( tmp == null ) return 0;
	try {
	    return Double.valueOf(tmp).doubleValue();
	}
	catch( NumberFormatException e ) {
	    throw new MsqlException(e);
	}
    
public synchronized floatgetFloat(java.lang.String cname)

	return getFloat(findColumn(cname));
    
public synchronized floatgetFloat(int column)

	String tmp = getString(column);

	if( tmp == null ) {
	    return 0;
	}
	try {
	    return Float.valueOf(tmp).floatValue();
	}
	catch( NumberFormatException e ) {
	    throw new MsqlException(e);
	}
    
public synchronized intgetInt(java.lang.String cname)

	return getInt(findColumn(cname));
    
public synchronized intgetInt(int column)

	String tmp = getString(column);
	
	if( tmp == null ) {
	    return 0;
	}
	try {
	    return Integer.parseInt(tmp);
	}
	catch( NumberFormatException e ) {
	    throw new MsqlException(e);
	}
    
public synchronized longgetLong(java.lang.String cname)

	return getLong(findColumn(cname));
    
public synchronized longgetLong(int column)

	String tmp = getString(column);

	if( tmp == null ) {
	    return 0;
	}
	try {
	    return Long.parseLong(tmp);
	}
	catch( NumberFormatException e ) {
	    throw new MsqlException(e);
	}
    
public java.sql.ResultSetMetaDatagetMetaData()

	ResultSetMetaData meta;
	
	synchronized( this ) {
	    if( meta_data != null ) {
		return meta_data;
	    }
	}
	synchronized( rows ) {
	    while( field_info == null ) {
		try {
		    rows.wait();
		}
		catch( InterruptedException e ) {
		    if( field_info == null ) {
			throw new MsqlException(e);
		    }
		}
	    }
	    meta = new MsqlResultSetMetaData(field_info);
	}
	synchronized( this ) {
	    meta_data = meta;
	}
	return meta_data;
    
public synchronized java.lang.ObjectgetObject(java.lang.String cname)

	return getObject(findColumn(cname));
    
public synchronized java.lang.ObjectgetObject(int column)

	ResultSetMetaData meta = getMetaData();
	int type = meta.getColumnType(column);
	
	switch(type) {
	case Types.BIT:
	    return new Boolean(getBoolean(column));
	    
	case Types.TINYINT:
	    return new Character((char)getInt(column));
	    
	case Types.SMALLINT:
	    return new Integer(getShort(column));
	    
	case Types.INTEGER:
	    return new Integer(getInt(column));
	    
	case Types.BIGINT:
	    return new Long(getLong(column));
	    
	case Types.FLOAT:
	    return new Float(getFloat(column));
	    
	case Types.REAL:
	    return new Float(getFloat(column));
	    
	case Types.DOUBLE:
	    return new Double(getDouble(column));
	    
	case Types.NUMERIC:
	    return getBigDecimal(column, 0);

	case Types.DECIMAL:
	    return getBigDecimal(column, 0);

	case Types.CHAR:
	    return getString(column);
	    
	case Types.VARCHAR:
	    return getString(column);
	    
	case Types.LONGVARCHAR:
	    return getString(column);
	    
	case Types.DATE:
	    return getDate(column);
	    
	case Types.TIME:
	    return getTime(column);
	    
	case Types.TIMESTAMP:
	    return getTimestamp(column);
	    
	case Types.BINARY:
	    return getBytes(column);
	    
	case Types.VARBINARY:
	    return getBytes(column);
	    
	case Types.LONGVARBINARY:
	    return getBytes(column);
	    
	default:
	    return getString(column);
	}
    
private byte[][]getRow(int row)

	if( row < 0 ) {
	    throw new SQLException("Attempt to access a non-existent row.");
	}
	synchronized( rows ) {
	    if( read_exception != null ) {
		throw read_exception;
	    }
	    while( rows.size() <= row ) {
		if( complete ) {
		    throw new SQLException("Attempt to access a " +
					   "non-existent row.");
		}
		else {
		    try {
			rows.wait();
		    }
		    catch( InterruptedException e ) {
			throw new MsqlException(e);
		    }
		}
	    }
	}
	return (byte[][])rows.elementAt(row);
    
public synchronized shortgetShort(java.lang.String cname)

	return getShort(findColumn(cname));
    
public synchronized shortgetShort(int column)

	String tmp = getString(column);

	if( tmp == null ) {
	    return 0;
	}
	try {
	    return (short)Short.parseShort(tmp);
	}
	catch( NumberFormatException e ) {
	    throw new MsqlException(e);
	}
    
MsqlStatementgetStatement()

	return statement;
    
public synchronized java.lang.StringgetString(java.lang.String cname)

	return getString(findColumn(cname));
    
public synchronized java.lang.StringgetString(int column)

	getColumn(column);
	if( wasNull() ) {
	    return null;
	}
	else {
	    try {
		return new String(last_column, "8859_9");
	    }
	    catch( UnsupportedEncodingException e ) {
		throw new MsqlException(e);
	    }
	}
    
public synchronized java.sql.TimegetTime(java.lang.String cname)

	return getTime(findColumn(cname));
    
public synchronized java.sql.TimegetTime(int column)

	long time = getTimeAsLong(column);

	if( wasNull() ) {
	    return null;
	}
	else {
	    return new Time(time);
	}
    
private longgetTimeAsLong(int column)

	String tmp = getString(column);  

	if( tmp == null ) {
	    return 0;
	}
	try {
	    return Long.parseLong(tmp);
	}
	catch( NumberFormatException e ) {
	    try {
		SimpleDateFormat format;
		java.util.Date date;
		
		format = new SimpleDateFormat("EEE MMM dd hh:mm:ss z yyyy");
		date = format.parse(tmp, new ParsePosition(0));
		return date.getTime();
	    }
	    catch( Exception real_e ) {
		throw new SQLException("Invalid date format.");
	    }
	}
    
public synchronized java.sql.TimestampgetTimestamp(java.lang.String cname)

	return getTimestamp(findColumn(cname));
    
public synchronized java.sql.TimestampgetTimestamp(int column)

	long time = getTimeAsLong(column);

	if( wasNull() ) {
	    return null;
	}
	else {
	    return new Timestamp(time);
	}
    
public synchronized java.io.InputStreamgetUnicodeStream(java.lang.String cname)

	return getUnicodeStream(findColumn(cname));
    
public synchronized java.io.InputStreamgetUnicodeStream(int column)

	getColumn(column);
	return new MsqlUnicodeInputStream(last_column);
    
public synchronized java.sql.SQLWarninggetWarnings()

	return warnings;
    
public synchronized booleannext()
Moves to the next row of data for processing. If there are no more rows to be processed, then it will return false.

return
true if there are results to be processed, false otherwise
exception
java.sql.SQLException thrown if a read error occurs

	current_row++;
	try {
	    current_row_data = getRow(current_row);
	}
	catch( SQLException e ) {
	    return false;
	}
	return true;
    
private voidreadRow(byte[] data)

	byte[][] cols = new byte[column_count][];
	String ascii;

	try {
	    ascii = new String(data, "8859_9");
	}
	catch( UnsupportedEncodingException e ) {
	    throw new MsqlException(e);
	}
	for(int i=0; i<column_count; i++) {
	    // we can get away with this since ascii arabics == unicode arabics
	    int colon = ascii.indexOf(':");
	    byte[] column;
	    int size;

	    try {
		size = Integer.parseInt(ascii.substring(0, colon));
	    }
	    catch( NumberFormatException e ) {
		throw new SQLException("Invalid row data format from mSQL.");
	    }
	    if( size == -2 ) {
		column = null;
		size = 0;
	    }
	    else {
		String tmp = ascii.substring(colon+1, colon+size+1);

		try {
		    column = tmp.getBytes("8859_9");
		}
		catch( UnsupportedEncodingException e ) {
		    throw new MsqlException(e);
		}
	    }
	    cols[i] = column;
	    ascii = ascii.substring(colon+size+1);
	}
	synchronized( rows ) {
	    rows.addElement(cols);
	    rows.notify();
	}
    
public voidrun()

	while( true ) {
	    byte[] data;
	    String tmp;

	    synchronized( input ) {
		try {
		    data = input.read();
		}
		catch( IOException e ) {
		    synchronized( rows ) {
			//statement.passIOException(e);
			read_exception = new MsqlException(e);
			complete = true;
			input = null;
			return;
		    }
		}
	    }
	    try {
		tmp = new String(data, "8859_9");
	    }
	    catch( UnsupportedEncodingException e ) {
		e.printStackTrace();
		synchronized( rows ) {
		    //statement.passIOException(e);
		    read_exception = new MsqlException(e);
		    complete = true;
		    rows.notify();
		}
		return;
	    }
	    if( tmp.startsWith("-1:") ) {
		synchronized( rows ) {
		    read_exception = new MsqlException(tmp);
		    complete = true;
		    rows.notify();
		}
		return;
	    }
	    else if( tmp.startsWith("-100") ) {
		break;
	    }
	    else {
		try {
		    readRow(data);
		}
		catch( SQLException e ) {
		    synchronized( rows ) {
			read_exception = new MsqlException(e);
			complete = true;
			rows.notify();
		    }
		    return;
		}
	    }
	}
	synchronized( rows ) {
	    if( !meta_set ) {
		try {
		    field_info = new MsqlResultSet(statement, input, 6, true);
		}
		catch( SQLException e ) {
		    e.printStackTrace();
		    field_info = null;
		}
	    }
	    complete = true;
	    rows.notify();
	}
    
public synchronized booleanwasNull()

return
true if the last value read was null
exception
java.sql.SQLException never thrown

	if( last_column == null ) {
	    return true;
	}
	else {
	    return false;
	}