home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 1997 May
/
Pcwk0597.iso
/
sybase
/
starbuck
/
samples.z
/
Query.java
< prev
next >
Wrap
Text File
|
1996-12-09
|
14KB
|
427 lines
/*
* Query.java
*
*
* Copyright 1996 Sybase, Inc. All rights reserved.
*/
/**
* <P>The Query class provides an interface to a NetImpact Dynamo query.
*
* <P>The DBConnection class supports a CreateQuery() method, which is currently
* the only way to create a Query.
* (SQL tags in the document template are not yet available,
* until some design changes are made to Dynamo)
* <P>The current document corresponds to the template that originated this request
*
* <P>Lifetime: can be created from DBConnection.createQuery()
* Becomes garbage when the servlet exits.
*
* <P>See SQL Anywhere documentation on NetImpact Dynamo for a description
* of the methods, but be aware that, in Java, the first character
* of a method name is always in lower case.
*/
public class Query extends DynamoConnection
//*****************************************
{
//-------------------------------------------------------------
// Constructor
//-------------------------------------------------------------
/**
* Constructs a new Query object with the specified handle and name.
* Used from DynamoConnection.getQuery() to access query on the current template.
* @param handle handle to Dynamo client
* @param name name of the query object
* @exception DynamoException If an error has occured
*/
public Query(int handle, String name) throws DynamoException
//**********************************************************
{
super(handle);
_name = name;
_qid = 0;
validate(true); // link up, throw exception if no query exists
}
/**
* Constructs a new Query object with the specified handle.
* Used from DBConnection.createQuery() to create an unnamed query object
* that will be associated with no particular query name
* @param handle handle to Dynamo client
* @param name name of the query object
* @param counter number of query objects
* @exception DynamoException If an error has occured
*/
public Query(int handle, String name, int counter) throws DynamoException
//***********************************************************************
{
super(handle);
_name = name + counter;
_qid = 0;
// no validation, that is done by DBConnection.createQuery
}
//-------------------------------------------------------------
// Properties
//-------------------------------------------------------------
/**
* Return a boolean indicating if the query object is valid
* @return true if the query object is valid; false otherwise
* @exception DynamoException If an error has occured
*/
public boolean getValid() throws DynamoException
//***********************************************
{
return validate(false);
}
/**
* Returns the name of this query object
*/
public String getName()
//*********************
{
return _name;
}
/**
* Returns the string name of the given column of the result set.
* Columns start at column 1 up to and including getColumnCount()
* @param colnum index of the column whose name is to be returned
* @exception DynamoException If an error has occured
*/
public native String getColumnLabel(int colnum) throws DynamoException;
//********************************************************************
/**
* Returns the number of columns in the result set
* @exception DynamoException If an error has occured
*/
public int getColumnCount() throws DynamoException
//************************************************
{
return getIntQueryProperty("GetColumnCount");
}
/**
* Returns a boolean indicating if result set is empty
* @return true if the result is empty; false otherwise
* @exception DynamoException If an error has occured
*/
public boolean getEmpty() throws DynamoException
//**********************************************
{
if (getIntQueryProperty("GetEmpty") == 0)
return true;
return false;
}
/**
* Returns the number of rows in the result set
* @exception DynamoException If an error has occured
*/
public int getRowCount() throws DynamoException
//*********************************************
{
return getIntQueryProperty("GetRowCount");
}
/**
* Returns current ODBC error code of this query object
* @exception DynamoException If an error has occured
*/
public int getErrorCode() throws DynamoException
//**********************************************
{
return getIntQueryProperty("GetErrorCode");
}
/**
* Returns ODBC error information for this query object
* @exception DynamoException If an error has occured
*/
public String getErrorInfo() throws DynamoException
//*************************************************
{
return getStringQueryProperty("GetErrorInfo");
}
/**
* Returns ODBC state of query. See your ODBC driver manuals
* @exception DynamoException If an error has occured
*/
public int getState() throws DynamoException
//******************************************
{
return getIntQueryProperty("GetState");
}
// The names and return values mirror JDBC, with the following exceptions:
// - column retrieval based on column number only, not by column name
// - no Date or Timestamp or Time support (lost by DynamoScript!)
// - no iostream for LONGVARCHAR (support in our ODBC?)
// - no byte[] return value (to be done)
// getObject()/getValue() return a Java object as follows:
// null -- NULL column
// Boolean -- SQL Bit result
// Int -- Any of the SQL integer values
// Double -- SQL Float or Double
// String -- other SQL fields converted to String
/**
* Returns the value in the current row of the specified
* column as an Object
* @param column index of the column
* @exception DynamoException If an error has occured
*/
public Object getObject(int column) throws DynamoException
//********************************************************
{
return getQueryValue(column);
}
/**
* Returns the value in the current row of the specified
* column as an Object
* @param column index of the column
* @exception DynamoException If an error has occured
*/
public Object getValue(int column) throws DynamoException
//*******************************************************
{
return getQueryValue(column);
}
/**
* Returns the value in the current row of the specified
* column as a String
* @param index of the column
* @exception DynamoException If an error has occured
*/
public String getString(int column) throws DynamoException
//********************************************************
{
Object o = getQueryValue(column);
if (o == null)
return "";
else if (o instanceof String)
return (String) o;
else
throw new DynamoException("Not a string");
}
/**
* Returns the value in the current row of the specified
* column as a boolean
* @param index of the column
* @exception DynamoException If an error has occured
*/
public boolean getBoolean(int column) throws DynamoException
//**********************************************************
{
Object o = getQueryValue(column);
if (o == null)
return false;
else if (o instanceof Boolean)
return ((Boolean) o).booleanValue();
else
throw new DynamoException("Not a boolean");
}
/**
* Returns the value in the current row of the specified
* column as a int
* @param index of the column
* @exception DynamoException If an error has occured
*/
public int getInt(int column) throws DynamoException
//**************************************************
{
Object o = getQueryValue(column);
if (o == null)
return 0;
else if (o instanceof Integer)
return ((Integer) o).intValue();
else
throw new DynamoException("Not an integer");
}
/**
* Returns the value in the current row of the specified
* column as a double
* @param index of the column
* @exception DynamoException If an error has occured
*/
public double getDouble(int column) throws DynamoException
//********************************************************
{
Object o = getQueryValue(column);
if (o == null)
return 0.0;
else if (o instanceof Double)
return ((Double) o).doubleValue();
else
throw new DynamoException("Not a string");
}
//-------------------------------------------------------------
// Public Methods
//-------------------------------------------------------------
/**
* Re-execute the SQL query (possibly changed with setSQL)
* Should be followed by a moveFirst() to reposition the new result set.
* @return true for success; false otherwise
* @exception DynamoException If an error has occured
*/
public native boolean refresh() throws DynamoException;
//****************************************************
/**
* Updates the select statement for a query.
* @param sqlStatement new SQL statement
* @return true for success; false otherwise
* @exception DynamoException If an error has occured
*/
public native boolean setSQL(String sqlStatement) throws DynamoException;
//**********************************************************************
/**
* Executes the current SQL query
* @return true for success; false otherwise
* @exception DynamoException If an error has occured
*/
public boolean execute() throws DynamoException
//*********************************************
{
if (getIntQueryProperty("Execute") == 0)
return false;
return true;
}
/**
* Moves the current row to the specified row in the result set
* @param row index of row in the result set to move to
* @return true if the new row is valid; false otherwise
* @exception DynamoException If an error has occured
*/
public boolean move(int row) throws DynamoException
//*************************************************
{
return queryMove(true, row);
}
/**
* Moves the current row to the first row in the result set
* @return true if the new row is valid; false otherwise
* @exception DynamoException If an error has occured
*/
public boolean moveFirst() throws DynamoException
//***********************************************
{
return move(1);
}
/**
* Moves the current row to the last row in the result set
* @return true if the new row is valid; false otherwise
* @exception DynamoException If an error has occured
*/
public boolean moveLast() throws DynamoException
//**********************************************
{
return move(-1);
}
/**
* Moves the current row to the next row in the result set
* @return true if the new row is valid; false otherwise
* @exception DynamoException If an error has occured
*/
public boolean moveNext() throws DynamoException
//**********************************************
{
return moveRelative(1);
}
/**
* Moves the current row to the previous row in the result set
* @return true if the new row is valid; false otherwise
* @exception DynamoException If an error has occured
*/
public boolean movePrevious() throws DynamoException
//**************************************************
{
return moveRelative(-1);
}
/**
* Moves to a row relative to the current row
* @param amount number of rows to move
* @return true if the new row is valid; false otherwise
* @exception DynamoException If an error has occured
*/
public boolean moveRelative(int amount) throws DynamoException
//************************************************************
{
return queryMove(false, amount);
}
//-------------------------------------------------------------
// Private Methods
//-------------------------------------------------------------
private native boolean validate(boolean checkValid) throws DynamoException;
private native boolean queryMove(boolean absolute, int amount);
private native int getIntQueryProperty(String name);
private native String getStringQueryProperty(String name);
private native String getQueryValue(int column);
//-------------------------------------------------------------
// Data
//-------------------------------------------------------------
private protected String _name;
private protected int _qid;
};