FileDocCategorySizeDatePackage
SimpleTextResultSet.javaAPI DocExample51950Sat Feb 03 11:43:42 GMT 2001jdbc.SimpleText

SimpleTextResultSet

public class SimpleTextResultSet extends SimpleTextObject implements ResultSet

Fields Summary
private Hashtable
s2c
private ResultSetMetaData
md
protected SQLWarning
lastWarning
protected SimpleTextIStatement
ownerStatement
protected SimpleTextIConnection
ownerConnection
protected Hashtable
inMemoryColumns
protected Hashtable
inMemoryRows
protected int
rowNum
protected boolean
lastNull
File
SDF
File
SBF
RandomAccessFile
rafSDF
RandomAccessFile
rafSBF
String
currentLine
Hashtable
columnValues
SimpleTextFilter
selectFilter
Constructors Summary
Methods Summary
public voidclearWarnings()

        setWarning(null);
    
public voidclose()

        try {
            if (rafSDF != null) {
                rafSDF.close();
            }
            if (rafSBF != null) {
                rafSBF.close();
            }
        }
        catch (Exception ex) {
        }
    
protected booleanfilterRow(java.util.Hashtable values, jdbc.SimpleText.SimpleTextFilter filter)

        if (filter == null) {
            return true;
        }

        boolean valid = false;

        // Get the column number

        int column = filter.column.colNo;

        // Get the data for the column

        CommonValue value = (CommonValue) values.get(new Integer(column));

        // If we didn't find the column, invalidate the column

        if (value == null) {
            return false;
        }

        switch(filter.column.type) {

        // Perform integer comparisions

        case Types.INTEGER:
            {
                int icol = value.getInt();
                int ifilter = filter.value.getInt();
                switch (filter.operator) {
                case SimpleTextFilter.OP_EQ:
                    valid = (icol == ifilter);
                    break;
                case SimpleTextFilter.OP_GT:
                    valid = (icol > ifilter);
                    break;
                case SimpleTextFilter.OP_LT:
                    valid = (icol < ifilter);
                    break;
                case SimpleTextFilter.OP_NE:
                    valid = (icol != ifilter);
                    break;
                }
            }
            break;

        // By default, compare as a string

        default:
            {
                String scol = value.getString();
                String sfilter = filter.value.getString();
                switch (filter.operator) {
                case SimpleTextFilter.OP_EQ:
                    valid = (scol.equals(sfilter));
                    break;
                case SimpleTextFilter.OP_GT:
                    valid = (scol.compareTo(sfilter) > 0);
                    break;
                case SimpleTextFilter.OP_LT:
                    valid = (scol.compareTo(sfilter) < 0);
                    break;
                case SimpleTextFilter.OP_NE:
                    valid = (!scol.equals(sfilter));
                    break;
                }
            }

            break;
        }

        return valid;
    
public intfindColumn(java.lang.String columnName)

        // Make a mapping cache if we don't already have one.

        if (md == null) {
            md = getMetaData();
            s2c = new Hashtable();
        }

        // Look for the mapping in our cache

        Integer x = (Integer) s2c.get(columnName);

        if (x != null) {
            return (x.intValue());
        }

        // OK, we'll have to use metadata

        for (int i = 1; i <= md.getColumnCount(); i++) {
            if (md.getColumnName(i).equalsIgnoreCase(columnName)) {

                // Success!  Add an entry to the cache

                s2c.put(columnName, new Integer(i));
                return (i);
            }
        }

        throw new SQLException("Column name not found: " + columnName,
                            "S0022");
    
public java.io.InputStreamgetAsciiStream(int columnIndex)

        // Binary InputStreams are the only InputStream types supported

        throw DataTypeNotSupported();
    
public java.io.InputStreamgetAsciiStream(java.lang.String columnName)

        return getAsciiStream(findColumn(columnName));
    
public java.math.BigDecimalgetBigDecimal(int columnIndex, int scale)

        String s = getString(columnIndex);
        java.math.BigDecimal d = null;

        if (s != null) {
            java.math.BigInteger i = new java.math.BigInteger(s);
            d = new java.math.BigDecimal (i, scale);
        }
        return d;
    
public java.math.BigDecimalgetBigDecimal(java.lang.String columnName, int scale)

        return getBigDecimal(findColumn(columnName), scale);
    
public java.io.InputStreamgetBinaryStream(int columnIndex)

        // Verify the column

        verify(columnIndex);

        CommonValue value = null;

        if (inMemoryRows != null) {
            value = getColumn(rowNum, columnIndex);
        }
        else {
            value = getValue(columnIndex);
        }

        int length = -1;

        // Get the length, if possible

        if (value != null) {
            byte b[] = value.getBytes();
            if (b != null) {
                length = b.length;
            }
        }

        SimpleTextInputStream inputStream = new SimpleTextInputStream(
                    value, SimpleTextInputStream.STREAM_TYPE_BINARY,
                    length);

        return inputStream;
    
public java.io.InputStreamgetBinaryStream(java.lang.String columnName)

        return getBinaryStream(findColumn(columnName));
    
public booleangetBoolean(java.lang.String columnName)

        return getBoolean(findColumn(columnName));
    
public booleangetBoolean(int columnIndex)

        // We could coerce the data, but for now an exception is thrown

        throw DataTypeNotSupported();
    
public bytegetByte(java.lang.String columnName)

        return getByte(findColumn(columnName));
    
public bytegetByte(int columnIndex)

        // We could coerce the data, but for now an exception is thrown

        throw DataTypeNotSupported();
    
public byte[]getBytes(int columnIndex)

        // Verify the column

        verify(columnIndex);

        byte b[] = null;

        if (inMemoryRows != null) {
            b = (getColumn(rowNum, columnIndex)).getBytes();
        }
        else {
            CommonValue value = getValue(columnIndex);

            if (value != null) {
                b = value.getBytes();
            }
        }

        if (b == null) {
            lastNull = true;
        }

        return b;
    
public byte[]getBytes(java.lang.String columnName)

        return getBytes(findColumn(columnName));
    
protected CommonValuegetColumn(int rowNum, int column)


        // First, get the row

        Hashtable row = (Hashtable) inMemoryRows.get(new Integer(rowNum));

        if (row == null) {
            throw new SQLException("Invalid row number: " + rowNum);
        }

        // Get the value

        CommonValue value = (CommonValue) row.get(new Integer(column));

        if (value == null) {

            // Column wasn't found.  Return a null value

            value = new CommonValue();
        }

        return value;
    
protected SimpleTextColumngetColumn(int col)

        SimpleTextColumn column = (SimpleTextColumn)
                        inMemoryColumns.get(new Integer(col));

        if (column == null) {
            throw new SQLException("Invalid column number: " + col);
        }

        return column;
    
public java.lang.StringgetCursorName()

        // The SimpleText driver does not support positioned updates

        throw DriverNotCapable();
    
public java.sql.DategetDate(int columnIndex)

        // We could coerce the data, but for now an exception is thrown

        throw DataTypeNotSupported();
    
public java.sql.DategetDate(java.lang.String columnName)

        return getDate(findColumn(columnName));
    
public doublegetDouble(int columnIndex)

        // We could coerce the data, but for now an exception is thrown

        throw DataTypeNotSupported();
    
public doublegetDouble(java.lang.String columnName)

        return getDouble(findColumn(columnName));
    
public floatgetFloat(int columnIndex)

        // We could coerce the data, but for now an exception is thrown

        throw DataTypeNotSupported();
    
public floatgetFloat(java.lang.String columnName)

        return getFloat(findColumn(columnName));
    
public intgetInt(int columnIndex)

        // Verify the column

        verify(columnIndex);

        CommonValue value;

        if (inMemoryRows != null) {
            value = getColumn(rowNum, columnIndex);
        }
        else {
            value = getValue(columnIndex);
        }

        // Check for a null value

        if (value == null) {
            lastNull = true;
            return 0;
        }

        if (value.isNull()) {
            lastNull = true;
            return 0;
        }

        return value.getInt();
    
public intgetInt(java.lang.String columnName)

        return getInt(findColumn(columnName));
    
public longgetLong(int columnIndex)

        // We could coerce the data, but for now an exception is thrown

        throw DataTypeNotSupported();
    
public longgetLong(java.lang.String columnName)

        return getLong(findColumn(columnName));
    
public java.sql.ResultSetMetaDatagetMetaData()

        SimpleTextResultSetMetaData md = new SimpleTextResultSetMetaData();

        md.initialize(inMemoryColumns, ownerConnection.isReadOnly());

        return md;
    
public java.lang.ObjectgetObject(int columnIndex)


        // Verify the column

        verify(columnIndex);

        // Get the data type of the column

        int type = (getColumn(columnIndex)).type;

        // The CommonValue for the column

        CommonValue value;

        if (inMemoryRows != null) {
            value = getColumn(rowNum, columnIndex);
        }
        else {
            value = getValue(columnIndex);
        }

        // Check for a null value

        if (value == null) {
            lastNull = true;
            return null;
        }

        if (value.isNull()) {
            lastNull = true;
            return null;
        }

        Object o = null;

        // Return the appropriate object for the given type

        switch(type) {
        case Types.VARCHAR:
            o = value.getString();
            break;
        case Types.INTEGER:
            o = new Integer(value.getInt());
            break;
        case Types.VARBINARY:
            o = value.getBytes();
            break;
        default:
            throw DataTypeNotSupported();
        }
        return o;
    
public java.lang.ObjectgetObject(java.lang.String columnName)

        return getObject(findColumn(columnName));
    
public shortgetShort(java.lang.String columnName)

        return getShort(findColumn(columnName));
    
public shortgetShort(int columnIndex)

        // We could coerce the data, but for now an exception is thrown

        throw DataTypeNotSupported();
    
public java.lang.StringgetString(java.lang.String columnName)

        return getString(findColumn(columnName));
    
public java.lang.StringgetString(int columnIndex)


        // Verify the column

        verify(columnIndex);

        String s = null;

        if (inMemoryRows != null) {
            s = (getColumn(rowNum, columnIndex)).getString();
        }
        else {
            CommonValue value = getValue(columnIndex);

            if (value != null) {
                s = value.getString();
            }
        }

        if (s == null) {
            lastNull = true;
        }

        return s;
    
public java.sql.TimegetTime(int columnIndex)

        // We could coerce the data, but for now an exception is thrown

        throw DataTypeNotSupported();
    
public java.sql.TimegetTime(java.lang.String columnName)

        return getTime(findColumn(columnName));
    
public java.sql.TimestampgetTimestamp(int columnIndex)

        // We could coerce the data, but for now an exception is thrown

        throw DataTypeNotSupported();
    
public java.sql.TimestampgetTimestamp(java.lang.String columnName)

        return getTimestamp(findColumn(columnName));
    
public java.io.InputStreamgetUnicodeStream(int columnIndex)

        // Binary InputStreams are the only InputStream types supported

        throw DataTypeNotSupported();
    
public java.io.InputStreamgetUnicodeStream(java.lang.String columnName)

        return getUnicodeStream(findColumn(columnName));
    
protected CommonValuegetValue(int column)

        CommonValue value;

        // Get the column definition (we already know it's there)

        SimpleTextColumn col = (SimpleTextColumn) inMemoryColumns.get(
                                    new Integer(column));

//        value = (CommonValue) columnValues.get(new Integer(column));
        value = (CommonValue) columnValues.get(new Integer(col.colNo));

        if (value == null) {
            return null;
        }

        switch(col.type) {

        // For binary types, read the binary file

        case Types.VARBINARY:
            {
                byte b[] = readSBF(value.getInt());
                value = new CommonValue(b);
            }
            break;
        }

        return value;

    
public java.sql.SQLWarninggetWarnings()

        return lastWarning;
    
public voidinitialize(SimpleTextIStatement statement, java.lang.String catalog, java.lang.String table, java.util.Hashtable columns, jdbc.SimpleText.SimpleTextFilter filter)

        // Save the owning statement object

        ownerStatement = statement;
        ownerConnection = ownerStatement.getConnection();

        // Save the in-memory column definitions

        inMemoryColumns = columns;

        // Save the select WHERE filter

        selectFilter = filter;

        // If a table was given, open it now

        if (table != null) {
            openSDF(catalog, table);
        }
    
public voidinitialize(SimpleTextIStatement statement, java.util.Hashtable columns, java.util.Hashtable rows)

        // Save the in-memory rows (used for catalog functions)

        inMemoryRows = rows;
        rowNum = 0;

        initialize(statement, null, null, columns, null);
    
public booleannext()

        boolean rc = true;
        boolean validRow = false;

        // In memory result set, get the next row

        if (inMemoryRows != null) {
            rowNum++;

            // No more rows, return end-of-file

            if (rowNum > inMemoryRows.size()) {
                rc = false;
            }
        }
        else {

            // Not in-memory, read the next line from the file until a
            // valid row has been found

            while (!validRow) {

                currentLine = readLine(rafSDF);

                if (currentLine == null) {
                    rc = false;
                    break;
                }

                // We'll cheat a little bit here.  We'll use the SQL
                // parser to break the line up, then treat it as a
                // comma separated list (much like a select list)

                String data[] = ownerConnection.parseSQL(currentLine);

                Hashtable dataList = new Hashtable();

                ownerStatement.buildList(data, 0, "", dataList);

                // Now go through each data element and create a
                // CommonValue object.  Then, put the CommonValue object
                // on our columnValues list

                SimpleTextColumn column;
                columnValues = new Hashtable();
                String s;
                CommonValue value;

                for (int i = 1; i <= dataList.size(); i++) {
                    column = (SimpleTextColumn) dataList.get(new Integer(i));

                    // Get the data item

                    s = column.name;

                    // Remove any quotes
                    if (s.startsWith("'") &&
                        s.endsWith("'")) {
                        s = s.substring(1, s.length() - 1);
                    }

                    // Create a CommonValue object using the string

                    value = new CommonValue(s);

                    // Create a CommonValue object

                    columnValues.put(new Integer(i), value);

                }

                // Filter the row, if necessary

                validRow = filterRow(columnValues, selectFilter);

            }
        }
        return rc;
    
protected voidopenSDF(java.lang.String catalog, java.lang.String table)

        String fullName = catalog + "/" + table +
                    SimpleTextDefine.DATA_FILE_EXT;

        String sbfName = catalog + "/" + table +
                    SimpleTextDefine.BINARY_FILE_EXT;

        // Make sure the file exists

        SDF = new File(fullName);
        SBF = new File(sbfName);

        if (!SDF.exists()) {
            throw new SQLException("Text file does not exist: " + fullName);
        }

        try {
            // Create our random access object (read only)

            rafSDF = new RandomAccessFile(SDF, "r");
        }
        catch (Exception ex) {
            throw new SQLException("Unable to access file: " +ex.getMessage());
        }

        // Read past the first line (the column definitions).  Before
        // we got to this point, the Statement object verified that
        // it is a valid file

        readLine(rafSDF);
    
protected java.lang.StringreadLine(java.io.RandomAccessFile f)

        String s = null;

        try {
            if (f.getFilePointer() >= f.length()) {
                return null;
            }

            s = f.readLine();
        }
        catch (Exception ex) {
            throw new SQLException("Error reading file: " + ex.getMessage());
        }
        return s;
    
protected byte[]readSBF(int offset)


        // Invalid offset, return null

        if (offset < 0) {
            return null;
        }

        byte b[] = null;

        // First time, check to make sure it exists

        if (rafSBF == null) {
            if (!SBF.exists()) {
                throw new SQLException("Binary file does not exist");
            }
        }

        try {

            // First time, create random access file object

            if (rafSBF == null) {
                rafSBF = new RandomAccessFile(SBF, "r");
            }

            // Position to the given offset

            rafSBF.seek(offset);

            // Make sure there is enough file to read an int

            if ((rafSBF.getFilePointer() + 4) > rafSBF.length()) {
                throw new SQLException("Attempt to read beyond end-of-file");
            }

            // Read the length of the data

            int len = rafSBF.readInt();

            // Make sure there's enough data to read

            if ((rafSBF.getFilePointer() + len) > rafSBF.length()) {
                throw new SQLException("Attempt to read beyond end-of-file");
            }

            b = new byte[len];
            rafSBF.read(b);

        }
        catch (Exception ex) {
            throw new SQLException("Unable to access SBF: " + ex.getMessage());
        }

        return b;
    
protected voidsetWarning(java.sql.SQLWarning warning)

        if (warning == null) {
            lastWarning = null;
        }
        else {
            SQLWarning chain = lastWarning;

            // Find the end of the chain

            while (chain.getNextWarning() != null) {
                chain = chain.getNextWarning();
            }

            // We're at the end of the chain.  Add the new warning

            chain.setNextWarning(warning);
        }
    
protected intverify(int column)

        clearWarnings();
        lastNull = false;

        SimpleTextColumn col = (SimpleTextColumn) inMemoryColumns.get(
                                    new Integer(column));

        if (col == null) {
            throw new SQLException("Invalid column number: " + column);
        }
            return col.colNo;
    
public booleanwasNull()

        return lastNull;