📄 simpletextresultset.java
字号:
// and null indicator. Returns the absolute column number. //------------------------------------------------------------------------ protected int verify( int column) throws SQLException { clearWarnings(); lastNull = false; SimpleTextColumn col = (SimpleTextColumn) inMemoryColumns.get( new Integer(column)); if (col == null) { throw new SQLException("Invalid column number: " + column); } return col.colNo; } //------------------------------------------------------------------------ // getColumn // Given a row and column number, return the CommonValue object from // the inMemoryRows //------------------------------------------------------------------------ protected CommonValue getColumn( int rowNum, int column) throws SQLException { // 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; } private Hashtable s2c; // Maps strings to column indexes private ResultSetMetaData md; // Our metadata object //------------------------------------------------------------------------ // openSDF // Opens the simple data file //------------------------------------------------------------------------ protected void openSDF( String catalog, String table) throws SQLException { 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); } //------------------------------------------------------------------------ // readLine // Given a random access file object, read the next line. Returns null // if eof //------------------------------------------------------------------------ protected String readLine( RandomAccessFile f) throws SQLException { 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; } //------------------------------------------------------------------------ // readSBF // Given an offset, read the binary file and return a byte array //------------------------------------------------------------------------ protected byte[] readSBF( int offset) throws SQLException { // 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; } //------------------------------------------------------------------------ // getValue // Returns a CommonValue object for the given column //------------------------------------------------------------------------ protected CommonValue getValue( int column) throws SQLException { 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; } //------------------------------------------------------------------------ // filter // Given a Hashtable of column values and a select filter, determine if // the data is valid. Returns true if the row is valid. //------------------------------------------------------------------------ protected boolean filterRow( Hashtable values, SimpleTextFilter filter) throws SQLException { 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; } //------------------------------------------------------------------------ // getColumn // Returns the SimpleTextColumn object for the given column number. // If not found, an exception is thrown //------------------------------------------------------------------------ protected SimpleTextColumn getColumn( int col) throws SQLException { SimpleTextColumn column = (SimpleTextColumn) inMemoryColumns.get(new Integer(col)); if (column == null) { throw new SQLException("Invalid column number: " + col); } return column; } // SQLWarning chain protected SQLWarning lastWarning; // Owning statement object protected SimpleTextIStatement ownerStatement; // Owning connection object protected SimpleTextIConnection ownerConnection; // Hashtable containing a SimpleTextColumn object for each column // in the result set. Used for catalog functions as well as normal // tables protected Hashtable inMemoryColumns; // Hashtable containing another Hashtable for each row. Used for // catalog functions protected Hashtable inMemoryRows; // Current row number (for in-memory result sets) protected int rowNum; // true if the last row accessed was null protected boolean lastNull; // File objects for the simple data file and simple binary file File SDF; File SBF; RandomAccessFile rafSDF; RandomAccessFile rafSBF; // Current text line String currentLine; // Current data values (CommonValue objects) Hashtable columnValues; // SimpleTextFilter object for select WHERE clause SimpleTextFilter selectFilter;}//----------------------------------------------------------------------------// SimpleTextFilter// Class to represent a filter for a SELECT statement (i.e. the WHERE clause).// This class is package-private//----------------------------------------------------------------------------class SimpleTextFilter{ SimpleTextColumn column; int operator; public final static int OP_EQ = 1; public final static int OP_GT = 2; public final static int OP_LT = 3; public final static int OP_NE = 4; CommonValue value;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -