/*
* $Id: AnyConnection.java,v 1.25 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.database.PooledConnection;
import anvil.script.Context;
import anvil.util.SQLUtil;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.CallableStatement;
import java.sql.SQLWarning;
///
/// @class Connection
/// Database connection.
///
/**
* class AnyConnection
*
* @author: Jani Lehtim�ki
*/
public class AnyConnection extends AnyAbstractClass
{
private PooledConnection _connectionimpl;
private Connection _connection;
public AnyConnection(PooledConnection connectionimpl)
{
_connectionimpl = connectionimpl;
}
public AnyConnection(Connection connection)
{
_connection = connection;
}
public final anvil.script.ClassType classOf() {
return __class__;
}
public Object toObject()
{
if (_connection != null) {
return _connection;
} else {
return _connectionimpl;
}
}
public Connection getConnection() throws SQLException
{
Connection conn = _connection;
if (conn != null) {
return conn;
}
conn = (Connection)_connectionimpl.getConnection();
if (conn != null) {
return conn;
}
throw new SQLException("Connection has gone away");
}
/// @method query
/// Executes given 'querystring' in this connection.
/// @synopsis object query(string querystring, [ object parameters, ... ] )
/// @see anvil.sql#query
/// @param querystring see anvil.sql#query for details
/// @return see anvil.sql#query for details
/// @throws SQLError if an error occured
public static final Object[] p_query = { null, "query", "parameters" };
public Any m_query(Context context, String query, Any[] parameters)
{
int n = parameters.length;
Connection conn = null;
try {
conn = getConnection();
Statement stmt = conn.createStatement();
stmt.setEscapeProcessing(false);
String queryString = null;
if (n == 0) {
queryString = query;
} else {
queryString = SQLUtil.buildQueryString(query, parameters, 0);
}
Any returnValue;
if (stmt.execute(queryString)) {
returnValue = new AnyResultSet(stmt.getResultSet(), stmt);
// statement is closed when resultset.close() is called
} else {
if (stmt.getClass().getSuperclass().getName().equals("com.mysql.jdbc.Statement")) {
com.mysql.jdbc.Statement s = (com.mysql.jdbc.Statement)stmt;
long l = s.getLongUpdateCount();
if (l == -1) {
l = s.getLastInsertID();
}
returnValue = Any.create(l);
} else {
returnValue = Any.create(stmt.getUpdateCount());
}
stmt.close();
}
return returnValue;
} catch (SQLException e) {
if (conn != null) {
try {
conn.close();
} catch (Exception e2) {
}
}
throw context.exception(e);
}
}
/// @method prepare
/// Creates a Statement object for sending parameterized SQL statements
/// to the database. A SQL statement with or without IN parameters
/// can be pre-compiled and stored in a PreparedStatement object. This
/// object can then be used to efficiently execute this statement multiple times.
/// @synopsis Statement prepare(string querystring)
/// @return Statement object
/// @throws SQLError if an error occured
public static final Object[] p_prepare = { null, "query" };
public Any m_prepare(Context context, String query)
{
Connection conn = null;
try {
conn = getConnection();
PreparedStatement stmt = conn.prepareStatement(query);
return new AnyStatement(stmt);
} catch (SQLException e) {
if (conn != null) {
try {
conn.close();
} catch (Exception e2) {
}
}
throw context.exception(e);
}
}
/// @method call
/// Creates a Statement object for calling database stored procedures.
/// The Statement provides methods for setting up its IN and OUT parameters,
/// and methods for executing the call to a stored procedure.
/// @synopsis Statement call(string querystring)
/// @return Statement object
/// @throws SQLError if an error occured
public static final Object[] p_call = { null, "query" };
public Any m_call(Context context, String query)
{
Connection conn = null;
try {
conn = getConnection();
CallableStatement stmt = conn.prepareCall(query);
return new AnyStatement(stmt);
} catch (SQLException e) {
if (conn != null) {
try {
conn.close();
} catch (Exception e2) {
}
}
throw context.exception(e);
}
}
/// @method commit
/// Commits statements.
/// @synopsis Connection commit()
/// @throws SQLError if an error occured
public Any m_commit(Context context)
{
try {
getConnection().commit();
return this;
} catch (SQLException e) {
throw context.exception(e);
}
}
/// @method rollback
/// Rollbacks statements.
/// @synopsis Connection rollback()
/// @throws SQLError if an error occured
public Any m_rollback(Context context)
{
try {
getConnection().rollback();
return this;
} catch (SQLException e) {
throw context.exception(e);
}
}
/// @method release
/// Releases connection back to pool.
/// @synopsis Connection release()
public Any m_release()
{
if (_connectionimpl != null) {
_connectionimpl.release();
}
return this;
}
/// @method close
/// Closes this connection. Only effective if this connection
/// was not retrieved from the connection pool.
/// @synopsis Connection close()
/// @throws SQLError if an error occured
public Any m_close(Context context)
{
try {
if (_connection != null) {
_connection.close();
}
return this;
} catch (SQLException e) {
throw context.exception(e);
}
}
/// @method isReadOnly
/// Checks if connection is marked read-only.
/// @synopsis boolean isReadOnly()
/// @return true if read-only
/// @throws SQLError if an error occured
public Any m_isReadOnly(Context context)
{
try {
return Any.create(getConnection().isReadOnly());
} catch (SQLException e) {
throw context.exception(e);
}
}
/// @method setReadOnly
/// Sets the read only status of connection to 'readOnly'
/// (database may perform certain optimizations
/// according to this information).
/// @synopsis Connection setReadOnly(boolean readOnly)
/// @param readOnly
/// @throws SQLError if an error occured
public static final Object[] p_setReadOnly = { null, "readOnly" };
public Any m_setReadOnly(Context context, boolean readOnly)
{
try {
getConnection().setReadOnly(readOnly);
return this;
} catch (SQLException e) {
throw context.exception(e);
}
}
/// @method getAutoCommit
/// Checks if connection is auto committing after each statement.
/// @synopsis boolean getAutoCommit()
/// @return true if is auto committing
/// @throws SQLError if an error occured
public Any m_getAutoCommit(Context context)
{
try {
return Any.create(getConnection().getAutoCommit());
} catch (SQLException e) {
throw context.exception(e);
}
}
/// @method setAutoCommit
/// Sets the auto commit status of connection to 'autoCommit'.
/// @synopsis void setAutoCommit(boolean autoCommit)
/// @param autoCommit
/// @throws SQLError if an error occured
public static final Object[] p_setAutoCommit = { null, "enabled" };
public Any m_setAutoCommit(Context context, boolean enabled)
{
try {
getConnection().setAutoCommit(enabled);
return TRUE;
} catch (SQLException e) {
throw context.exception(e);
}
}
/// @method getCatalog
/// @synopsis string getCatalog()
/// @return the catalog used
/// @throws SQLError if an error occured
public Any m_getCatalog(Context context)
{
try {
return Any.create(getConnection().getCatalog());
} catch (SQLException e) {
throw context.exception(e);
}
}
/// @method setCatalog
/// Sets the catalog to 'catalog'.
/// @synopsis Connection setCatalog(string catalog)
/// @param catalog
/// @throws SQLError if an error occured
public static final Object[] p_setCatalog = { null, "catalog" };
public Any m_setCatalog(Context context, String catalog)
{
try {
getConnection().setCatalog(catalog);
return this;
} catch (SQLException e) {
throw context.exception(e);
}
}
public static final anvil.script.compiler.NativeClass __class__ =
new anvil.script.compiler.NativeClass("Connection", AnyConnection.class,
//DOC{{
""+
"\n" +
" @class Connection\n" +
" Database connection.\n" +
"\n" +
" @method query\n" +
" Executes given 'querystring' in this connection.\n" +
" @synopsis object query(string querystring, [ object parameters, ... ] )\n" +
" @see anvil.sql#query\n" +
" @param querystring see anvil.sql#query for details\n" +
" @return see anvil.sql#query for details\n" +
" @throws SQLError if an error occured\n" +
" @method prepare\n" +
" Creates a Statement object for sending parameterized SQL statements \n" +
" to the database. A SQL statement with or without IN parameters \n" +
" can be pre-compiled and stored in a PreparedStatement object. This\n" +
" object can then be used to efficiently execute this statement multiple times. \n" +
" @synopsis Statement prepare(string querystring)\n" +
" @return Statement object\n" +
" @throws SQLError if an error occured\n" +
" @method call\n" +
" Creates a Statement object for calling database stored procedures. \n" +
" The Statement provides methods for setting up its IN and OUT parameters, \n" +
" and methods for executing the call to a stored procedure. \n" +
" @synopsis Statement call(string querystring)\n" +
" @return Statement object\n" +
" @throws SQLError if an error occured\n" +
" @method commit\n" +
" Commits statements.\n" +
" @synopsis Connection commit()\n" +
" @throws SQLError if an error occured\n" +
" @method rollback\n" +
" Rollbacks statements.\n" +
" @synopsis Connection rollback()\n" +
" @throws SQLError if an error occured\n" +
" @method release\n" +
" Releases connection back to pool.\n" +
" @synopsis Connection release()\n" +
" @method close\n" +
" Closes this connection. Only effective if this connection\n" +
" was not retrieved from the connection pool.\n" +
" @synopsis Connection close()\n" +
" @throws SQLError if an error occured\n" +
" @method isReadOnly\n" +
" Checks if connection is marked read-only.\n" +
" @synopsis boolean isReadOnly()\n" +
" @return true if read-only\n" +
" @throws SQLError if an error occured\n" +
" @method setReadOnly\n" +
" Sets the read only status of connection to 'readOnly'\n" +
" (database may perform certain optimizations \n" +
" according to this information).\n" +
" @synopsis Connection setReadOnly(boolean readOnly)\n" +
" @param readOnly \n" +
" @throws SQLError if an error occured\n" +
" @method getAutoCommit\n" +
" Checks if connection is auto committing after each statement.\n" +
" @synopsis boolean getAutoCommit()\n" +
" @return true if is auto committing\n" +
" @throws SQLError if an error occured\n" +
" @method setAutoCommit\n" +
" Sets the auto commit status of connection to 'autoCommit'.\n" +
" @synopsis void setAutoCommit(boolean autoCommit)\n" +
" @param autoCommit\n" +
" @throws SQLError if an error occured\n" +
" @method getCatalog\n" +
" @synopsis string getCatalog()\n" +
" @return the catalog used\n" +
" @throws SQLError if an error occured\n" +
" @method setCatalog\n" +
" Sets the catalog to 'catalog'.\n" +
" @synopsis Connection setCatalog(string catalog)\n" +
" @param catalog\n" +
" @throws SQLError if an error occured\n"
//}}DOC
);
static {
SQLModule.class.getName();
}
}