📄 csvresultset.java
字号:
/**
Copyright (C) 2002-2003 Together
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 org.relique.jdbc.csv;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.*;
import java.util.Calendar;
import java.util.Map;
/**
* This class implements the ResultSet interface for the CsvJdbc driver.
*
* @author Zoran Milakovic
*/
public class CsvResultSet implements ResultSet {
/** Metadata for this ResultSet */
protected ResultSetMetaData resultSetMetaData;
/** Statement that produced this ResultSet */
protected Statement statement;
/** Helper class that performs the actual file reads */
protected CsvReader reader;
/** Table referenced by the Statement */
protected String tableName;
/** Array of available columns for referenced table */
protected String[] columnNames;
protected String[] columnTypes;
protected String[] whereColumnNames;
protected String[] whereColumnValues;
/** Last column name index read */
protected int lastIndexRead = -1;
/** InputStream to keep track of */
protected InputStream is;
/**
* Constructor for the CsvResultSet object
*
* @param statement Statement that produced this ResultSet
* @param reader Helper class that performs the actual file reads
* @param tableName Table referenced by the Statement
* @param columnNames Array of available columns for referenced table
*/
// protected CsvResultSet(Statement statement, CsvReader reader,
// String tableName, String[] columnNames, String[] columnTypes) {
// this.statement = statement;
// this.reader = reader;
// this.tableName = tableName;
// this.columnNames = columnNames;
// this.columnTypes = columnTypes;
// if(columnNames[0].equals("*")) {
// this.columnNames = reader.getColumnNames();
// }
// }
/**
* Constructor for the CsvResultSet object
*
* @param statement Statement that produced this ResultSet
* @param reader Helper class that performs the actual file reads
* @param tableName Table referenced by the Statement
* @param columnNames Array of available columns for referenced table
*/
protected CsvResultSet(Statement statement,
CsvReader reader,
String tableName,
String[] columnNames,
String[] columnWhereNames,
String[] columnWhereValues,
String[] columnTypes) {
this.statement = statement;
this.reader = reader;
this.tableName = tableName;
this.columnNames = columnNames;
this.whereColumnNames = columnWhereNames;
this.whereColumnValues = columnWhereValues;
this.columnTypes = columnTypes;
if(columnNames[0].equals("*")) {
this.columnNames = reader.getColumnNames();
}
}
/**
* Moves the cursor down one row from its current position.
* A <code>ResultSet</code> cursor is initially positioned
* before the first row; the first call to the method
* <code>next</code> makes the first row the current row; the
* second call makes the second row the current row, and so on.
*
* <P>If an input stream is open for the current row, a call
* to the method <code>next</code> will
* implicitly close it. A <code>ResultSet</code> object's
* warning chain is cleared when a new row is read.
*
* @return <code>true</code> if the new current row is valid;
* <code>false</code> if there are no more rows
* @exception SQLException if a database access error occurs
*/
public boolean next() throws SQLException {
mainLoop:
while(reader.next()) {
boolean isRow = true;
out:
for (int i = 0; i < this.whereColumnNames.length; i++) {
//csv driver make difference between null value and empty string ""
// if (whereColumnValues[i] == null || whereColumnValues[i].equals("null"))
// whereColumnValues[i] = "";
if ( !( Utils.compareValues( reader.getColumn(this.whereColumnNames[i]), this.whereColumnValues[i]) ) ) {
isRow = false;
break out;
}
// if (! (reader.getColumn(this.whereColumnNames[i]).equals(this.whereColumnValues[i]))) {
// isRow = false;
// break out;
// }
}
if (isRow == true)
return true;
}
return false;
}
/**
* Releases this <code>ResultSet</code> object's database and
* JDBC resources immediately instead of waiting for
* this to happen when it is automatically closed.
*
* <P><B>Note:</B> A <code>ResultSet</code> object
* is automatically closed by the
* <code>Statement</code> object that generated it when
* that <code>Statement</code> object is closed,
* re-executed, or is used to retrieve the next result from a
* sequence of multiple results. A <code>ResultSet</code> object
* is also automatically closed when it is garbage collected.
*
* @exception SQLException if a database access error occurs
*/
public void close() throws SQLException {
reader.close();
}
/**
* Reports whether
* the last column read had a value of SQL <code>NULL</code>.
* Note that you must first call one of the getter methods
* on a column to try to read its value and then call
* the method <code>wasNull</code> to see if the value read was
* SQL <code>NULL</code>.
*
* @return <code>true</code> if the last column value read was SQL
* <code>NULL</code> and <code>false</code> otherwise
* @exception SQLException if a database access error occurs
*/
public boolean wasNull() throws SQLException {
if(lastIndexRead >= 0) {
return getString(lastIndexRead) == null;
} else {
throw new SQLException("No previous getter method called");
}
}
//======================================================================
// Methods for accessing results by column index
//======================================================================
/**
* Retrieves the value of the designated column in the current row
* of this <code>ResultSet</code> object as
* a <code>String</code> in the Java programming language.
*
* @param columnIndex the first column is 1, the second is 2, ...
* @return the column value; if the value is SQL <code>NULL</code>, the
* value returned is <code>null</code>
* @exception SQLException if a database access error occurs
*/
public String getString(int columnIndex) throws SQLException {
// perform pre-accessor method processing
preAccessor(columnIndex);
// use CsvReader.getColumn(String) to retrieve the column
if (columnIndex < 1 || columnIndex > columnNames.length) {
throw new SQLException("Column not found: invalid index: "+columnIndex);
}
String retValue = reader.getColumn(columnNames[columnIndex-1]);
//replace spec. characters
// retValue = this.formatString(retValue);
return retValue;
}
/**
* Retrieves the value of the designated column in the current row
* of this <code>ResultSet</code> object as
* a <code>boolean</code> in the Java programming language.
*
* @param columnIndex the first column is 1, the second is 2, ...
* @return the column value; if the value is SQL <code>NULL</code>, the
* value returned is <code>false</code>
* @exception SQLException if a database access error occurs
*/
public boolean getBoolean(int columnIndex) throws SQLException {
String str = getString(columnIndex);
return (str == null) ? false : Boolean.valueOf(str).booleanValue();
}
/**
* Retrieves the value of the designated column in the current row
* of this <code>ResultSet</code> object as
* a <code>byte</code> in the Java programming language.
*
* @param columnIndex the first column is 1, the second is 2, ...
* @return the column value; if the value is SQL <code>NULL</code>, the
* value returned is <code>0</code>
* @exception SQLException if a database access error occurs
*/
public byte getByte(int columnIndex) throws SQLException {
String str = getString(columnIndex);
return (str == null) ? 0 : Byte.parseByte(str);
}
/**
* Retrieves the value of the designated column in the current row
* of this <code>ResultSet</code> object as
* a <code>short</code> in the Java programming language.
*
* @param columnIndex the first column is 1, the second is 2, ...
* @return the column value; if the value is SQL <code>NULL</code>, the
* value returned is <code>0</code>
* @exception SQLException if a database access error occurs
*/
public short getShort(int columnIndex) throws SQLException {
String str = getString(columnIndex);
return (str == null) ? 0 : Short.parseShort(str);
}
/**
* Gets the value of the designated column in the current row
* of this <code>ResultSet</code> object as
* an <code>int</code> in the Java programming language.
*
* @param columnIndex the first column is 1, the second is 2, ...
* @return the column value; if the value is SQL <code>NULL</code>, the
* value returned is <code>0</code>
* @exception SQLException if a database access error occurs
*/
public int getInt(int columnIndex) throws SQLException {
String str = getString(columnIndex);
return (str == null) ? 0 : Integer.parseInt(str);
}
/**
* Retrieves the value of the designated column in the current row
* of this <code>ResultSet</code> object as
* a <code>long</code> in the Java programming language.
*
* @param columnIndex the first column is 1, the second is 2, ...
* @return the column value; if the value is SQL <code>NULL</code>, the
* value returned is <code>0</code>
* @exception SQLException if a database access error occurs
*/
public long getLong(int columnIndex) throws SQLException {
String str = getString(columnIndex);
return (str == null) ? 0L : Long.parseLong(str);
}
/**
* Gets the value of the designated column in the current row
* of this <code>ResultSet</code> object as
* a <code>float</code> in the Java programming language.
*
* @param columnIndex the first column is 1, the second is 2, ...
* @return the column value; if the value is SQL <code>NULL</code>, the
* value returned is <code>0</code>
* @exception SQLException if a database access error occurs
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -