com.caucho.quercus.lib.db
Class JdbcResultResource

java.lang.Object
  extended by com.caucho.quercus.lib.db.JdbcResultResource
Direct Known Subclasses:
MysqliResult, OracleResult, PostgresResult

public class JdbcResultResource
extends java.lang.Object

Represents a JDBC Result value.


Field Summary
protected  java.sql.ResultSetMetaData _metaData
           
protected  java.sql.ResultSet _rs
           
static java.lang.String BLOB
           
static java.lang.String DATE
           
static java.lang.String DATETIME
           
static int FETCH_ASSOC
           
static int FETCH_BOTH
           
static int FETCH_NUM
           
static java.lang.String INTEGER
           
static java.lang.String REAL
           
static java.lang.String STRING
           
static java.lang.String TIME
           
static java.lang.String TIMESTAMP
           
static java.lang.String UNKNOWN
           
static java.lang.String YEAR
           
 
Constructor Summary
JdbcResultResource(Env env, java.sql.ResultSetMetaData metaData, JdbcConnectionResource conn)
          Constructor for JdbcResultResource
JdbcResultResource(Env env, java.sql.Statement stmt, java.sql.ResultSet rs, JdbcConnectionResource conn)
          Constructor for JdbcResultResource
 
Method Summary
 void close()
          Closes the result set.
 ArrayValue fetchArray(Env env, int type)
          Fetch the next line as an array.
 ArrayValue fetchAssoc(Env env)
          Returns an associative array representing the row.
 Value fetchField(Env env, int maxLength, java.lang.String tableName, java.lang.String type)
          Returns an object with the following fields: name, table, max_length, not_null, primary_key, multiple_key, numeric, blob, type, unsigned, zerofill.
 Value fetchObject(Env env)
          Returns an object with properties that correspond to the fetched row and moves the internal data pointer ahead.
 ArrayValue fetchRow(Env env)
          Returns an array representing the row.
 int getAffectedRows()
          Get the number of affected rows.
protected  Value getColumnDate(Env env, java.sql.ResultSet rs, int column)
           
protected  int getColumnNumber(java.lang.String colName)
          Gets the column number.
protected  int getColumnNumber(Value fieldNameOrNumber, int base)
          Gets the column number based on a generic Value.
protected  Value getColumnString(Env env, java.sql.ResultSet rs, java.sql.ResultSetMetaData md, int column)
           
protected  Value getColumnTime(Env env, java.sql.ResultSet rs, int column)
           
protected  Value getColumnTimestamp(Env env, java.sql.ResultSet rs, int column)
           
 Value getColumnValue(Env env, java.sql.ResultSet rs, java.sql.ResultSetMetaData metaData, int column)
          Get the column value in the specified result set.
 JdbcConnectionResource getConnection()
          Get the connection corresponding to this result resource.
 Value getFieldCatalog(int fieldOffset)
          Get the field catalog name.
 int getFieldCount()
          Returns column count.
 Value getFieldLength(Env env, int fieldOffset)
          Get field length.
 Value getFieldName(Env env, int fieldOffset)
          Returns the column name.
 Value getFieldNameAlias(int fieldOffset)
          Returns a StringValue containing the column Alias.
 Value getFieldNotNull(Env env, int fieldOffset)
          Returns the column name.
 int getFieldOffset()
          Get field offset.
 Value getFieldScale(int fieldOffset)
          Get field scale.
 Value getFieldSchema(Env env, int fieldOffset)
          Returns the table corresponding to the field.
 Value getFieldTable(Env env, int fieldOffset)
          Returns the table corresponding to the field.
 Value getFieldType(Env env, int fieldOffset)
          Get a StringValue with the column type.
protected  java.lang.String getFieldType(int fieldOffset, int jdbcType)
          Given the JDBC type of the field at the given offset, return a PHP type string.
protected  java.sql.Statement getJavaStatement()
          Returns the underlying SQL statement associated to this result resource.
protected  Value getJdbcType(int fieldOffset)
          Get type from Types enumeration
 Value getLengths()
          Returns an ArrayValue column lengths in the most recently accessed row.
 java.sql.ResultSetMetaData getMetaData()
          Get the result set meta data.
 Value getNumFields()
          Returns the number of columns returned in query.
 int getNumRows()
          Get the number of rows in this result set.
static int getNumRows(java.sql.ResultSet rs)
          Returns number of rows returned in query.
 Value getResultField(Env env, int row, Value field)
          Returns the value at a particular row and column.
 java.sql.ResultSet getResultSet()
          Get the underlying result set.
 java.sql.Statement getStatement()
          Get the underlying statement.
protected  Value getUnicodeColumnString(Env env, java.sql.ResultSet rs, java.sql.ResultSetMetaData md, int column)
           
protected  boolean isInResultString(int columnIndex, java.lang.String substring)
          Return true if the String result at the given index of the ResultSet contains the substring.
protected  boolean isValidFieldOffset(int fieldOffset)
          Return true is the field offset is valid, meaning it is larger than 0 and is less that the max number of fields in this result resource.
 boolean seek(Env env, int rowNumber)
          Seeks to an arbitrary result pointer specified by the offset in the result set represented by result.
 void setAffectedRows(int affectedRows)
          Set the number of affected rows to the specified value.
 boolean setFieldOffset(int fieldOffset)
          Set a value for field offset.
 boolean setRowNumber(int rowNumber)
          Points to the row right before "rowNumber".
static boolean setRowNumber(java.sql.ResultSet rs, int rowNumber)
          Points to the row right before "rowNumber".
 Value toKey()
          Convert this JDBC result resource to a hash code.
 java.lang.String toString()
          Returns a string representation for this object.
 JdbcResultResource validateResult()
          Validate this result set and return it.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

FETCH_ASSOC

public static final int FETCH_ASSOC
See Also:
Constant Field Values

FETCH_NUM

public static final int FETCH_NUM
See Also:
Constant Field Values

FETCH_BOTH

public static final int FETCH_BOTH
See Also:
Constant Field Values

INTEGER

public static final java.lang.String INTEGER
See Also:
Constant Field Values

BLOB

public static final java.lang.String BLOB
See Also:
Constant Field Values

STRING

public static final java.lang.String STRING
See Also:
Constant Field Values

DATE

public static final java.lang.String DATE
See Also:
Constant Field Values

DATETIME

public static final java.lang.String DATETIME
See Also:
Constant Field Values

REAL

public static final java.lang.String REAL
See Also:
Constant Field Values

TIME

public static final java.lang.String TIME
See Also:
Constant Field Values

TIMESTAMP

public static final java.lang.String TIMESTAMP
See Also:
Constant Field Values

UNKNOWN

public static final java.lang.String UNKNOWN
See Also:
Constant Field Values

YEAR

public static final java.lang.String YEAR
See Also:
Constant Field Values

_rs

protected java.sql.ResultSet _rs

_metaData

protected java.sql.ResultSetMetaData _metaData
Constructor Detail

JdbcResultResource

public JdbcResultResource(Env env,
                          java.sql.Statement stmt,
                          java.sql.ResultSet rs,
                          JdbcConnectionResource conn)
Constructor for JdbcResultResource

Parameters:
stmt - the corresponding statement
rs - the corresponding result set
conn - the corresponding connection

JdbcResultResource

public JdbcResultResource(Env env,
                          java.sql.ResultSetMetaData metaData,
                          JdbcConnectionResource conn)
Constructor for JdbcResultResource

Parameters:
metaData - the corresponding result set meta data
conn - the corresponding connection
Method Detail

close

public void close()
Closes the result set.


fetchArray

public ArrayValue fetchArray(Env env,
                             int type)
Fetch the next line as an array.

Parameters:
type - one of FETCH_ASSOC, FETCH_NUM, or FETCH_BOTH (default) By using the FETCH_ASSOC constant this function will behave identically to the mysqli_fetch_assoc(), while FETCH_NUM will behave identically to the mysqli_fetch_row() function. The final option FETCH_BOTH will create a single array with the attributes of both.
Returns:
the next result row as an associative, a numeric array, or both.

fetchAssoc

public ArrayValue fetchAssoc(Env env)
Returns an associative array representing the row.

Returns:
an associative array representing the row or null if there are no more rows in the result set

fetchField

public Value fetchField(Env env,
                        int maxLength,
                        java.lang.String tableName,
                        java.lang.String type)
Returns an object with the following fields: name, table, max_length, not_null, primary_key, multiple_key, numeric, blob, type, unsigned, zerofill.

NOTE: does not have a field for unique_key.

Parameters:
env - the PHP executing environment
maxLength - the field maximum length
tableName - the field table name
type - the field type
Returns:
the next field in the result set or false if no information is available

fetchObject

public Value fetchObject(Env env)
Returns an object with properties that correspond to the fetched row and moves the internal data pointer ahead.

Parameters:
env - the PHP executing environment
Returns:
an object representing the current fetched row

fetchRow

public ArrayValue fetchRow(Env env)
Returns an array representing the row.

Returns:
an array containing the fecthed row

getAffectedRows

public int getAffectedRows()
Get the number of affected rows.

Returns:
the number of affected rows

getColumnNumber

protected int getColumnNumber(Value fieldNameOrNumber,
                              int base)
                       throws java.sql.SQLException
Gets the column number based on a generic Value.

Parameters:
fieldNameOrNumber - the field index or it's name
base - the numbering base: 0 or 1 (usually zero).
Returns:
the column number (always 0-based) or -1 on error
Throws:
java.sql.SQLException

getColumnNumber

protected int getColumnNumber(java.lang.String colName)
                       throws java.sql.SQLException
Gets the column number.

Returns:
the column number (0-based) or -1 on error
Throws:
java.sql.SQLException

getColumnValue

public Value getColumnValue(Env env,
                            java.sql.ResultSet rs,
                            java.sql.ResultSetMetaData metaData,
                            int column)
                     throws java.sql.SQLException
Get the column value in the specified result set.

Parameters:
env - the PHP executing environment
rs - the result set
metaData - the result set meta data
column - the column number
Returns:
the column value
Throws:
java.sql.SQLException

getUnicodeColumnString

protected Value getUnicodeColumnString(Env env,
                                       java.sql.ResultSet rs,
                                       java.sql.ResultSetMetaData md,
                                       int column)
                                throws java.io.IOException,
                                       java.sql.SQLException
Throws:
java.io.IOException
java.sql.SQLException

getColumnString

protected Value getColumnString(Env env,
                                java.sql.ResultSet rs,
                                java.sql.ResultSetMetaData md,
                                int column)
                         throws java.sql.SQLException
Throws:
java.sql.SQLException

getColumnTime

protected Value getColumnTime(Env env,
                              java.sql.ResultSet rs,
                              int column)
                       throws java.sql.SQLException
Throws:
java.sql.SQLException

getColumnDate

protected Value getColumnDate(Env env,
                              java.sql.ResultSet rs,
                              int column)
                       throws java.sql.SQLException
Throws:
java.sql.SQLException

getColumnTimestamp

protected Value getColumnTimestamp(Env env,
                                   java.sql.ResultSet rs,
                                   int column)
                            throws java.sql.SQLException
Throws:
java.sql.SQLException

getConnection

public JdbcConnectionResource getConnection()
Get the connection corresponding to this result resource.

Returns:
a JDBC connection resource

getFieldCatalog

public Value getFieldCatalog(int fieldOffset)
Get the field catalog name.

Parameters:
fieldOffset - the field number
Returns:
the field catalog name

getFieldCount

public int getFieldCount()
Returns column count.

Returns:
the number of columns in the result set

isValidFieldOffset

protected boolean isValidFieldOffset(int fieldOffset)
Return true is the field offset is valid, meaning it is larger than 0 and is less that the max number of fields in this result resource.


isInResultString

protected boolean isInResultString(int columnIndex,
                                   java.lang.String substring)
                            throws java.sql.SQLException
Return true if the String result at the given index of the ResultSet contains the substring.

Throws:
java.sql.SQLException

getFieldLength

public Value getFieldLength(Env env,
                            int fieldOffset)
Get field length. This is the length of the field as defined in the table declaration.

Parameters:
env - the PHP executing environment
fieldOffset - the field number (0-based)
Returns:
length of field for specified column

getFieldName

public Value getFieldName(Env env,
                          int fieldOffset)
Returns the column name.

Parameters:
env - the PHP executing environment
fieldOffset - 0-based field offset
Returns:
a StringValue containing the column name

getFieldNameAlias

public Value getFieldNameAlias(int fieldOffset)
Returns a StringValue containing the column Alias.

Parameters:
fieldOffset - 0-based field offset
Returns:
the column alias

getFieldNotNull

public Value getFieldNotNull(Env env,
                             int fieldOffset)
Returns the column name.

Parameters:
env - the PHP executing environment
fieldOffset - 0-based field offset
Returns:
int(1) if the column is nullable, int(1) if it is not

getFieldOffset

public int getFieldOffset()
Get field offset.

Returns:
the current field offset

getFieldScale

public Value getFieldScale(int fieldOffset)
Get field scale.

Parameters:
fieldOffset - the field offset
Returns:
number of digits to the right of the decimal point

getFieldTable

public Value getFieldTable(Env env,
                           int fieldOffset)
Returns the table corresponding to the field.

Parameters:
env - the PHP executing environment
fieldOffset - the field number
Returns:
the field table name

getFieldSchema

public Value getFieldSchema(Env env,
                            int fieldOffset)
Returns the table corresponding to the field.

Parameters:
env - the PHP executing environment
fieldOffset - the field number
Returns:
the field table name

getFieldType

public Value getFieldType(Env env,
                          int fieldOffset)
Get a StringValue with the column type.

Parameters:
env - the PHP executing environment
fieldOffset - need to add 1 because java is 1 based index and quercus is 0 based
Returns:
a StringValue containing the column type

getFieldType

protected java.lang.String getFieldType(int fieldOffset,
                                        int jdbcType)
Given the JDBC type of the field at the given offset, return a PHP type string.


getJavaStatement

protected java.sql.Statement getJavaStatement()
Returns the underlying SQL statement associated to this result resource.


getJdbcType

protected Value getJdbcType(int fieldOffset)
Get type from Types enumeration

Parameters:
fieldOffset - the field number (0-based)
Returns:
the JDBC type

getLengths

public Value getLengths()
Returns an ArrayValue column lengths in the most recently accessed row. If a fetch function has not yet been called this will return BooleanValue.FALSE

Returns:
an ArrayValue of column lengths in the most recently accessed row

getMetaData

public java.sql.ResultSetMetaData getMetaData()
                                       throws java.sql.SQLException
Get the result set meta data.

Returns:
the meta data for this result set
Throws:
java.sql.SQLException

getNumFields

public Value getNumFields()
Returns the number of columns returned in query.

Returns:
the number of columns for this result set

getNumRows

public int getNumRows()
Get the number of rows in this result set.

Returns:
the number of rows in this result set

getNumRows

public static int getNumRows(java.sql.ResultSet rs)
Returns number of rows returned in query. last() call is efficient for Mysql because the driver just adjusts the result index. It is very inefficient for Postgres because that driver iterates over the result set.

Parameters:
rs - a result set
Returns:
the number of rows in the specified result set

getResultField

public Value getResultField(Env env,
                            int row,
                            Value field)
Returns the value at a particular row and column.

Parameters:
env - the PHP executing environment
row - a particular row to get the field value from
field - the field name or number
Returns:
the value of the specified field

getResultSet

public java.sql.ResultSet getResultSet()
Get the underlying result set.

Returns:
the underlying ResultSet object

getStatement

public java.sql.Statement getStatement()
Get the underlying statement.

Returns:
the underlying Statement object

seek

public boolean seek(Env env,
                    int rowNumber)
Seeks to an arbitrary result pointer specified by the offset in the result set represented by result. Returns TRUE on success or FALSE on failure

Parameters:
env - the PHP executing environment
rowNumber - the row offset
Returns:
true on success or false on failure

setAffectedRows

public void setAffectedRows(int affectedRows)
Set the number of affected rows to the specified value.

Parameters:
affectedRows - the new number of affected rows

setFieldOffset

public boolean setFieldOffset(int fieldOffset)
Set a value for field offset. This method will return true when the field offset is valid, otherwise it will set the field offset to the invalid value and return false.

Parameters:
fieldOffset - PHP is 0-based

setRowNumber

public boolean setRowNumber(int rowNumber)
Points to the row right before "rowNumber". Next fetchArray will increment to proper row.

Parameters:
rowNumber - the row offset
Returns:
true on success or false on failure

setRowNumber

public static boolean setRowNumber(java.sql.ResultSet rs,
                                   int rowNumber)
Points to the row right before "rowNumber". Next fetchArray will increment to proper row.

Parameters:
rs - the result set to move the row pointer
rowNumber - the row offset
Returns:
true on success or false on failure

toKey

public Value toKey()
Convert this JDBC result resource to a hash code.

Returns:
a hash code of this JDBC result resource

toString

public java.lang.String toString()
Returns a string representation for this object.

Overrides:
toString in class java.lang.Object
Returns:
a string representation for this object

validateResult

public JdbcResultResource validateResult()
Validate this result set and return it.

Returns:
the validated result set