/*
* $Id: AnyStatement.java,v 1.11 2002/09/16 08:05:03 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.core.sql;
import anvil.core.Any;
import anvil.core.AnyAbstractClass;
import anvil.core.AnyString;
import anvil.core.AnyList;
import anvil.core.Array;
import anvil.script.Context;
import anvil.util.SQLUtil;
import anvil.java.util.BindingEnumeration;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.CallableStatement;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
///
/// @class Statement
/// JDBC prepared or callable statement.
///
/// @reference columnIndex
/// @synopsis object Statement[<i>int columnIndex</i>] ;
/// Returns the value of parameter at given index.
/// @synopsis object Statement[<i>string columnName</i>] ;
/// Returns the value of parameter at column with given name.
/// @synopsis Statement[<i>int columnIndex</i>] = <i>value</i> ;
/// Sets the value of parameter at given index.
/// @synopsis Statement[<i>string columnName</i>] = <i>value</i> ;
/// Returns the value of parameter with given name.
/// @param columnIndex Index of column, from 0 (inclusive) to
/// <code>getColumnCount()-1</code>.
/// @param columnName Name of column
///
/// @attribute columnName
/// @synopsis object <b>Statement</b>.<i>columnName</i> ;
/// Returns the value of parameter with given name.
/// @synopsis object <b>Statement</b>.<i>columnName</i> = <i>value</i> ;
/// Sets the value of parameter with given name.
/// @param columnName Name of column
/// @param value value to assign
///
/**
* class AnyStatement
*
* @author: Jani Lehtim�ki
*/
public class AnyStatement extends AnyMetaData
{
protected PreparedStatement _statement;
protected CallableStatement _callable;
protected int _index = 0;
public AnyStatement(PreparedStatement statement)
{
super(null);
_statement = statement;
if (statement instanceof CallableStatement) {
_callable = (CallableStatement)statement;
}
}
public final anvil.script.ClassType classOf()
{
return __class__;
}
public Object toObject()
{
return _statement;
}
protected final ResultSetMetaData getMetaData() throws SQLException
{
if (_metadata == null) {
_metadata = _statement.getMetaData();
}
return _metadata;
}
public Any getAttribute(Context context, String attribute)
{
CallableStatement stmt = _callable;
if (stmt != null) {
try {
return SQLUtil.getField(stmt, getMetaData(), toColumnIndex(attribute));
} catch (SQLException e) {
throw context.exception(e);
}
} else {
return super.getAttribute(context, attribute);
}
}
public Any checkAttribute(Context context, String attribute)
{
return getAttribute(context, attribute);
}
public Any setAttribute(Context context, String attribute, Any value)
{
CallableStatement stmt = _callable;
try {
SQLUtil.setField(_statement, toColumnIndex(attribute), value);
} catch (SQLException e) {
throw context.exception(e);
}
return value;
}
public Any getReference(Context context, Any index)
{
CallableStatement stmt = _callable;
if (stmt != null) {
try {
return SQLUtil.getField(stmt, getMetaData(), toColumnIndex(index));
} catch (SQLException e) {
throw context.exception(e);
}
} else {
return super.getReference(context, index);
}
}
public Any checkReference(Context context, Any index)
{
return getReference(context, index);
}
public Any setReference(Context context, Any index, Any value)
{
CallableStatement stmt = _callable;
try {
_index = toColumnIndex(index);
SQLUtil.setField(_statement, _index, value);
} catch (SQLException e) {
throw context.exception(e);
}
return value;
}
public Any setReference(Context context, Any value)
{
CallableStatement stmt = _callable;
try {
SQLUtil.setField(_statement, ++_index, value);
} catch (SQLException e) {
throw context.exception(e);
}
return value;
}
/// @method query
/// Executes the SQL query in this Statement object and returns
/// the result set generated by the query.
/// @synopsis ResultSet query()
/// @return ResultSet
/// @throws SQLError if an error occured
public Any m_query(Context context)
{
try {
return new AnyResultSet(_statement.executeQuery());
} catch (SQLException e) {
throw context.exception(e);
}
}
/// @method update
/// Executes the SQL update statement in this Statement object.
/// Also statements returning nothing can be executed.
/// @synopsis int update()
/// @return Number of rows affected.
/// @throws SQLError if an error occured
public Any m_update(Context context)
{
try {
return Any.create(_statement.executeUpdate());
} catch (SQLException e) {
throw context.exception(e);
}
}
/// @method clear
/// Clears the current parameter values immediately.
///
/// <p>In general, parameter values remain in force for repeated use
/// of a Statement. Setting a parameter value automatically clears
/// its previous value. However, in some cases it is useful to
/// immediately release the resourcesused by the current parameter
/// values; this can be done by calling <code>clear</code>.</p>
///
/// @synopsis Statement clear()
/// @return this
/// @throws SQLError if an error occured
public Any m_clear(Context context)
{
try {
_index = 0;
_statement.clearParameters();
return this;
} catch (SQLException e) {
throw context.exception(e);
}
}
/// @method register
/// Registers the OUT parameter in ordinal position parameterIndex
/// to the JDBC type sqlType. All OUT parameters must be registered
/// before a stored procedure is executed.
/// @synopsis Statement register(int column, int type)
/// @synopsis Statement register(int column, int type, int scale)
/// @param column Column index, <code>0<=column<getColumnCount()</code>
/// @param type Type code
/// @param scale the desired number of digits to the right of the
//// decimal point. It must be greater than or equal to zero.
/// @return this
/// @throws SQLError if an error occured
public static final Object[] p_register = { null, "column", "type", "*scale", null };
public Any m_register(Context context, int column, int type, Any scale)
{
CallableStatement stmt = _callable;
if (stmt != null) {
try {
if (scale == null) {
stmt.registerOutParameter(column, type);
} else {
stmt.registerOutParameter(column, type, scale.toInt());
}
} catch (SQLException e) {
throw context.exception(e);
}
}
return this;
}
/// @method reset
/// Resets the column pointer back to start.
/// @synopsis Statement reset()
public Any m_reset(Context context)
{
_index = 0;
return this;
}
/// @method close
/// Closes this result set, releasing all the resources associated with it.
/// @synopsis boolean close()
/// @return true if operation was successful
/// @throws SQLError if an error occured
public Any m_close(Context context)
{
try {
_statement.close();
if (_statement != null) {
_statement.close();
}
return TRUE;
} catch (SQLException e) {
throw context.exception(e);
}
}
public static final anvil.script.compiler.NativeClass __class__ =
new anvil.script.compiler.NativeClass("Statement", AnyStatement.class,
AnyMetaData.__class__,
//DOC{{
""+
"\n" +
" @class Statement\n" +
" JDBC prepared or callable statement.\n" +
"\n" +
" @reference columnIndex\n" +
" @synopsis object Statement[<i>int columnIndex</i>] ;\n" +
" Returns the value of parameter at given index.\n" +
" @synopsis object Statement[<i>string columnName</i>] ;\n" +
" Returns the value of parameter at column with given name.\n" +
" @synopsis Statement[<i>int columnIndex</i>] = <i>value</i> ;\n" +
" Sets the value of parameter at given index.\n" +
" @synopsis Statement[<i>string columnName</i>] = <i>value</i> ;\n" +
" Returns the value of parameter with given name.\n" +
" @param columnIndex Index of column, from 0 (inclusive) to\n" +
" <code>getColumnCount()-1</code>.\n" +
" @param columnName Name of column\n" +
"\n" +
" @attribute columnName\n" +
" @synopsis object <b>Statement</b>.<i>columnName</i> ;\n" +
" Returns the value of parameter with given name.\n" +
" @synopsis object <b>Statement</b>.<i>columnName</i> = <i>value</i> ;\n" +
" Sets the value of parameter with given name.\n" +
" @param columnName Name of column\n" +
" @param value value to assign\n" +
"\n" +
" @method query\n" +
" Executes the SQL query in this Statement object and returns \n" +
" the result set generated by the query.\n" +
" @synopsis ResultSet query()\n" +
" @return ResultSet\n" +
" @throws SQLError if an error occured\n" +
" @method update\n" +
" Executes the SQL update statement in this Statement object.\n" +
" Also statements returning nothing can be executed.\n" +
" @synopsis int update()\n" +
" @return Number of rows affected.\n" +
" @throws SQLError if an error occured\n" +
" @method clear \n" +
" Clears the current parameter values immediately. \n" +
"\n" +
" <p>In general, parameter values remain in force for repeated use \n" +
" of a Statement. Setting a parameter value automatically clears \n" +
" its previous value. However, in some cases it is useful to \n" +
" immediately release the resourcesused by the current parameter \n" +
" values; this can be done by calling <code>clear</code>.</p>\n" +
"\n" +
" @synopsis Statement clear()\n" +
" @return this\n" +
" @throws SQLError if an error occured\n" +
" @method register \n" +
" Registers the OUT parameter in ordinal position parameterIndex \n" +
" to the JDBC type sqlType. All OUT parameters must be registered \n" +
" before a stored procedure is executed. \n" +
" @synopsis Statement register(int column, int type)\n" +
" @synopsis Statement register(int column, int type, int scale)\n" +
" @param column Column index, <code>0<=column<getColumnCount()</code>\n" +
" @param type Type code\n" +
" @param scale the desired number of digits to the right of the \n" +
"/ decimal point. It must be greater than or equal to zero.\n" +
" @return this\n" +
" @throws SQLError if an error occured\n" +
" @method reset \n" +
" Resets the column pointer back to start.\n" +
" @synopsis Statement reset()\n" +
" @method close \n" +
" Closes this result set, releasing all the resources associated with it. \n" +
" @synopsis boolean close()\n" +
" @return true if operation was successful\n" +
" @throws SQLError if an error occured\n"
//}}DOC
);
static {
SQLModule.class.getName();
}
}