📄 statement.java
字号:
//
// Copyright 1998 CDS Networks, Inc., Medford Oregon
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. All advertising materials mentioning features or use of this software
// must display the following acknowledgement:
// This product includes software developed by CDS Networks, Inc.
// 4. The name of CDS Networks, Inc. may not be used to endorse or promote
// products derived from this software without specific prior
// written permission.
//
// THIS SOFTWARE IS PROVIDED BY CDS NETWORKS, INC. ``AS IS'' AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL CDS NETWORKS, INC. BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
//
/**
* A Statement object is used for executing a static SQL statement and
* obtaining the results produced by it.
*
* <p>Only one ResultSet per Statement can be open at any point in time.
* Therefore, if the reading of one ResultSet is interleaved with the
* reading of another, each must have been generated by different
* Statements. All statement execute methods implicitly close a
* statement's current ResultSet if an open one exists.
*
* @see java.sql.Statement
* @see ResultSet
* @version $Id: Statement.java,v 1.4 2004/01/21 14:05:05 zoran Exp $
*/
package com.internetcds.jdbc.tds;
import java.sql.*;
public class Statement implements java.sql.Statement
{
public static final String cvsVersion = "$Id: Statement.java,v 1.4 2004/01/21 14:05:05 zoran Exp $";
private java.sql.Connection connection; // The connection who created us
// ResultSet currentResults = null; // The current results
protected SQLWarningChain warningChain; // The warnings chain.
protected int timeout = 0; // The timeout for a query
protected Tds tds = null;
protected java.sql.ResultSet results = null;
private java.sql.ResultSetMetaData metaResults = null;
private boolean escapeProcessing = true;
protected int updateCount = -1;
private int maxFieldSize = (1<<31)-1;
private int maxRows = 0;
private boolean isClosed = false;
/**
* Constructor for a Statement. It simply sets the connection
* that created us.
*
* @param connection_ the Connection instantation that creates us
* @param tds_ a TDS instance to use for communication with server.
*/
public Statement(
Object connection_,
Tds tds_)
throws SQLException
{
tds = tds_;
connection = (java.sql.Connection)connection_;
warningChain = new SQLWarningChain();
}
private void NotImplemented() throws java.sql.SQLException
{
throw new SQLException("Not Implemented");
}
protected void finalize()
throws Throwable
{
super.finalize();
if (tds != null)
{
close();
}
}
/**
* Execute a SQL statement that retruns a single ResultSet
*
* @param Sql typically a static SQL SELECT statement
* @return a ResulSet that contains the data produced by the query
* @exception SQLException if a database access error occurs
*/
public java.sql.ResultSet executeQuery(String sql) throws SQLException
{
if (execute(sql))
{
startResultSet();
}
return results;
}
/**
* Execute a SQL INSERT, UPDATE or DELETE statement. In addition
* SQL statements that return nothing such as SQL DDL statements
* can be executed
*
* Any IDs generated for AUTO_INCREMENT fields can be retrieved
* by looking through the SQLWarning chain of this statement
* for warnings of the form "LAST_INSERTED_ID = 'some number',
* COMMAND = 'your sql'".
*
* @param Sql a SQL statement
* @return either a row count, or 0 for SQL commands
* @exception SQLException if a database access error occurs
*/
public int executeUpdate(String sql) throws SQLException
{
if (execute(sql))
{
startResultSet();
closeResults();
throw new SQLException("executeUpdate can't return a result set");
}
else
{
return getUpdateCount();
}
}
protected void closeResults()
throws java.sql.SQLException
{
if (results != null)
{
results.close();
results = null;
}
}
private void skipToEnd()
throws java.sql.SQLException, java.io.IOException,
com.internetcds.jdbc.tds.TdsUnknownPacketSubType,
com.internetcds.jdbc.tds.TdsException
{
boolean done;
PacketResult tmp;
do
{
tmp = tds.processSubPacket();
done = (tmp instanceof PacketEndTokenResult)
&& (! ((PacketEndTokenResult)tmp).moreResults());
} while (! done);
}
public void commit()
throws java.sql.SQLException, java.io.IOException, com.internetcds.jdbc.tds.TdsUnknownPacketSubType, com.internetcds.jdbc.tds.TdsException
{
String sql = "IF @@TRANCOUNT > 0 COMMIT TRAN ";
if (tds == null)
{
throw new SQLException("Statement is closed");
}
executeQuery(sql);
skipToEnd();
}
public void rollback()
throws java.sql.SQLException, java.io.IOException, com.internetcds.jdbc.tds.TdsUnknownPacketSubType, com.internetcds.jdbc.tds.TdsException
{
String sql = "IF @@TRANCOUNT > 0 ROLLBACK TRAN ";
if (tds == null)
{
throw new SQLException("Statement is closed");
}
executeQuery(sql);
skipToEnd();
}
/**
* In many cases, it is desirable to immediately release a
* Statement's database and JDBC resources instead of waiting
* for this to happen when it is automatically closed. The
* close method provides this immediate release.
*
* <p><B>Note:</B> A Statement is automatically closed when it is
* garbage collected. When a Statement is closed, its current
* ResultSet, if one exists, is also closed.
*
* @exception SQLException if a database access error occurs (why?)
*/
public void close() throws SQLException
{
closeResults();
// Rollback any pending transactions
/* try
{
rollback();
}
catch (com.internetcds.jdbc.tds.TdsUnknownPacketSubType e)
{
throw new SQLException("Unknown packet. \n" + e.getMessage());
}
catch (com.internetcds.jdbc.tds.TdsException e)
{
// XXX
// ignore this for now
}
catch (java.io.IOException e)
{
// XXX
// ignore this for now
}
*/
// now we need to relinquish the connection
if (tds != null)
{
Tds tmpTds = tds;
tds = null;
try
{
((ConnectionHelper)connection).relinquish(tmpTds);
}
catch(TdsException e)
{
throw new SQLException("Internal Error: " + e.getMessage());
}
}
try
{
((ConnectionHelper)connection).markAsClosed(this);
}
catch(TdsException e)
{
throw new SQLException(e.getMessage());
}
this.isClosed = true;
}
/**
* The maxFieldSize limit (in bytes) is the maximum amount of
* data returned for any column value; it only applies to
* BINARY, VARBINARY, LONGVARBINARY, CHAR, VARCHAR and LONGVARCHAR
* columns. If the limit is exceeded, the excess data is silently
* discarded.
*
* @return the current max column size limit; zero means unlimited
* @exception SQLException if a database access error occurs
*/
public int getMaxFieldSize() throws SQLException
{
return maxFieldSize;
}
/**
* Sets the maxFieldSize
*
* @param max the new max column size limit; zero means unlimited
* @exception SQLException if size exceeds buffer size
*/
public void setMaxFieldSize(int max) throws SQLException
{
maxFieldSize = max;
}
/**
* The maxRows limit is set to limit the number of rows that
* any ResultSet can contain. If the limit is exceeded, the
* excess rows are silently dropped.
*
* @return the current maximum row limit; zero means unlimited
* @exception SQLException if a database access error occurs
*/
public int getMaxRows() throws SQLException
{
return maxRows;
}
/**
* Set the maximum number of rows
*
* @param max the new max rows limit; zero means unlimited
* @exception SQLException if a database access error occurs
* @see getMaxRows
*/
public void setMaxRows(int max) throws SQLException
{
if (maxRows < 0)
{
throw new SQLException("Negative row count");
}
maxRows = max;
this.executeUpdate("set rowcount " + maxRows);
}
/**
* If escape scanning is on (the default), the driver will do escape
* substitution before sending the SQL to the database.
*
* @param enable true to enable; false to disable
* @exception SQLException if a database access error occurs
*/
public void setEscapeProcessing(boolean enable) throws SQLException
{
escapeProcessing = enable;
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -