📄 i18nresultset.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.webdocwf.util.i18njdbc;
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.Enumeration;
import java.util.Map;
/**
* This class implements the ResultSet interface for the I18nJdbc driver.
*
* @author Zoran Milakovic
* @author Zeljko Kovacevic
*/
public class I18nResultSet implements ResultSet {
/** Metadata for this ResultSet */
protected ResultSetMetaData resultSetMetaData;
/**Properties */
protected I18nProperties properties;
/** 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;
protected Enumeration enum;
private String currKey = "";
protected I18nConnection connection;
/**
* Constructor for the I18nResultSet object
*
* @param statement I18nStatement that produced this ResultSet
* @param tableName Table referenced by the Statement
* @param columnNames Array of available columns for referenced table
* @param columnWhereNames Array of column names in where clause
* @param columnWhereValues Array of values in where clause
*/
protected I18nResultSet(I18nStatement statement,
String tableName,
String[] columnNames,
String[] columnWhereNames,
String[] columnWhereValues) throws SQLException {
try {
this.connection = (I18nConnection) statement.getConnection();
} catch (SQLException e) {
throw e;
}
this.tableName = tableName;
this.columnNames = columnNames;
this.whereColumnNames = columnWhereNames;
this.whereColumnValues = columnWhereValues;
this.properties = statement.getProperties();
this.enum = statement.getProperties().keys();
if (columnNames[0].equals("*")) {
this.columnNames = this.connection.getColumnNames();
}
}
/**
* Constructor for the I18nResultSet object
*
* @param statement I18nPreparedStatement that produced this ResultSet
* @param tableName Table referenced by the Statement
* @param columnNames Array of available columns for referenced table
* @param columnWhereNames Array of column names in where clause
* @param columnWhereValues Array of values in where clause
*/
protected I18nResultSet(I18nPreparedStatement statement,
String tableName,
String[] columnNames,
String[] columnWhereNames,
String[] columnWhereValues) throws SQLException {
try {
this.connection = (I18nConnection) statement.getConnection();
} catch (SQLException e) {
throw e;
}
this.tableName = tableName;
this.columnNames = columnNames;
this.whereColumnNames = columnWhereNames;
this.whereColumnValues = columnWhereValues;
this.properties = statement.getProperties();
this.enum = statement.getProperties().keys();
if (columnNames[0].equals("*")) {
this.columnNames = this.connection.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 {
boolean retVal = false;
mainLoop : while (enum.hasMoreElements()) {
this.currKey = enum.nextElement().toString();
retVal = true;
out : for (int i = 0; i < this.whereColumnNames.length; i++) {
if (whereColumnValues[i] == null || whereColumnValues[i].equals("null"))
whereColumnValues[i] = "";
if (this.whereColumnNames[i].equalsIgnoreCase(this.connection.getNameColumn())) {
if (!this.currKey.equals(this.whereColumnValues[i])) {
retVal = false;
break out;
}
} else if (this.whereColumnNames[i].equalsIgnoreCase(this.connection.getValueColumn())) {
if (!(properties.getProperty(this.currKey).equals(this.whereColumnValues[i]))) {
retVal = false;
break out;
}
}
}
if (retVal == 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
String retValue = "";
preAccessor(columnIndex);
if (columnIndex < 1 || columnIndex > columnNames.length) {
throw new SQLException("Column not found: invalid index: " + columnIndex);
} else if (columnIndex >= 1) {
// I18nConnection conn = (I18nConnection)this.statement.getConnection();
String colName = this.connection.getNameColumn();
String colValue = this.connection.getValueColumn();
String column = columnNames[columnIndex - 1];
if (column.equalsIgnoreCase(colName)) {
retValue = this.currKey;
} else if (column.equalsIgnoreCase(colValue)) {
retValue = properties.getProperty(this.currKey);
}
}
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.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -