*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ---------------
* MemoryResult.java
* ---------------
* Author: Volker Berlin
*
*/
package smallsql.database;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
class MemoryResult extends DataSource {
ExpressionValue[] currentRow;
Expressions expressions = new Expressions(); private int rowIdx = -1;
private List rowList = new ArrayList();
MemoryResult(){}
MemoryResult(Object[][] data, int colCount) throws SQLException{
for(int r=0; r<data.length; r++){
Object[] row = data[r];
currentRow = new ExpressionValue[row.length];
addRow(currentRow);
for(int c=0; c<row.length; c++){
(currentRow[c] = new ExpressionValue()).set( row[c], -1);
if(r == 0){
expressions.add(currentRow[c]);
}
}
}
if(data.length == 0){
for(int i=0; i<colCount; i++){
ExpressionValue expr = new ExpressionValue();
expr.set( null, -1 );
expressions.add(expr);
}
}
}
final void addRow(ExpressionValue[] row){
rowList.add(row);
}
final boolean isScrollable(){
return true;
}
final void beforeFirst(){
rowIdx = -1;
currentRow = null;
}
final boolean isBeforeFirst(){
return rowIdx < 0;
}
final boolean isFirst(){
return rowIdx == 0 && currentRow != null;
}
final boolean first(){
rowIdx = 0;
return move();
}
final boolean previous(){
if(rowIdx-- < 0) rowIdx = -1;
return move();
}
final boolean next(){
rowIdx++;
return move();
}
final boolean last(){
rowIdx = rowList.size() - 1;
return move();
}
final boolean isLast(){
return rowIdx == rowList.size() - 1 && currentRow != null;
}
final boolean isAfterLast(){
return rowIdx >= rowList.size();
}
final void afterLast(){
rowIdx = rowList.size();
currentRow = null;
}
final boolean absolute(int row) throws SQLException{
if(row == 0) throw Utils.createSQLException("Row 0 is invalid for method absolute().");
rowIdx = (row > 0) ?
Math.min( row, rowList.size() ) :
Math.min( row +rowList.size(), -1 );
return move();
}
final boolean relative(int rows){
if(rows == 0) return (currentRow != null);
rowIdx = Math.min( Math.max( rowIdx + rows, -1), rowList.size());
return move();
}
final int getRow(){
return currentRow == null ? 0 : rowIdx+1;
}
final long getRowPosition(){
return rowIdx;
}
final void setRowPosition(long rowPosition) throws Exception{
rowIdx = (int)rowPosition;
move();
}
final boolean rowInserted(){
return false;
}
final boolean rowDeleted(){
return false;
}
void nullRow(){
throw new Error();
}
void noRow(){
currentRow = null;
}
final private boolean move(){
if(rowIdx < rowList.size() && rowIdx >= 0){
currentRow = (ExpressionValue[])rowList.get(rowIdx);
return true;
}
currentRow = null;
return false;
}
boolean isNull( int colIdx ) throws Exception{
return get( colIdx ).isNull();
}
boolean getBoolean( int colIdx ) throws Exception{
return get( colIdx ).getBoolean();
}
int getInt( int colIdx ) throws Exception{
return get( colIdx ).getInt();
}
long getLong( int colIdx ) throws Exception{
return get( colIdx ).getLong();
}
float getFloat( int colIdx ) throws Exception{
return get( colIdx ).getFloat();
}
double getDouble( int colIdx ) throws Exception{
return get( colIdx ).getDouble();
}
long getMoney( int colIdx ) throws Exception{
return get( colIdx ).getMoney();
}
MutableNumeric getNumeric( int colIdx ) throws Exception{
return get( colIdx ).getNumeric();
}
Object getObject( int colIdx ) throws Exception{
return get( colIdx ).getObject();
}
String getString( int colIdx ) throws Exception{
return get( colIdx ).getString();
}
byte[] getBytes( int colIdx ) throws Exception{
return get( colIdx ).getBytes();
}
int getDataType( int colIdx ){
return expressions.get( colIdx ).getDataType();
}
final TableView getTableView(){
return null;
}
final void deleteRow() throws Exception{
throw Utils.createSQLException("ResultSet is read only.");
}
final void updateRow(Expression[] updateValues) throws Exception{
throw Utils.createSQLException("ResultSet is read only.");
}
final void insertRow(Expression[] updateValues) throws Exception{
throw Utils.createSQLException("ResultSet is read only.");
}
private Expression get(int colIdx) throws Exception{
if(currentRow == null) throw Utils.createSQLException("No current row.");
return currentRow[ colIdx ];
}
int getRowCount(){
return rowList.size();
}
void execute() throws Exception{
}
}