/*
Derby - Class org.apache.derby.client.am.SqlException
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.apache.derby.client.am;
import java.sql.SQLException;
import org.apache.derby.shared.common.i18n.MessageUtil;
import org.apache.derby.shared.common.error.ExceptionUtil;
import org.apache.derby.shared.common.reference.MessageId;
import org.apache.derby.shared.common.reference.SQLState;
// The signature of the stored procedure SQLCAMessage I have come out so far is as follows:
// SQLCAMessage (
// IN SQLCode INTEGER,
// IN SQLErrml SMALLINT,
// IN SQLErrmc VARCHAR(70),
// IN SQLErrp CHAR(8),
// IN SQLErrd0 INTEGER,
// IN SQLErrd1 INTEGER,
// IN SQLErrd2 INTEGER,
// IN SQLErrd3 INTEGER,
// IN SQLErrd4 INTEGER,
// IN SQLErrd5 INTEGER,
// IN SQLWarn CHAR(11),
// IN SQLState CHAR(5),
// IN Locale CHAR(5),
// IN BufferSize SMALLINT,
// IN LineWidth SMALLINT,
// OUT Message VARCHAR(2400))
//
// Some issues have been identified:
// 1. What would be the schema name of the stored procedue SQLCAMessage?
// 2. What is the format and type of the Locale parameter? If there does, I would really like to know the format of the locale in order to decide the type of the Locale parameter. Even there does not either, the Locale parameter probably still needs to be kept there for future extension, and we need to figure out the format of the locale.
// 3. What would be the format of the output message? Is this full message text ok or do we only need the explanation message corresponding to an SQL code. This somehow matters whether we need the Buffersize and Linewidth parameters for the stored procedure.
// 4. What if the invocation of stored procedure failed (due to, eg, connection dropping)? In this case, we probably need to return some client-side message.
//
// Note that this class does NOT extend java.sql.SQLException. This is because
// in JDBC 4 there will be multiple subclasses of SQLException defined by the
// spec. So we can't also extend SQLException without having to create our
// own mirror hierarchy of subclasses.
//
// When Derby is ready to throw an exception to the application, it catches
// SqlException and converts it to a java.sql.SQLException by calling the
// method getSQLException.
//
// It is also possible that internal routines may call public methods.
// In these cases, it will need to wrap a java.sql.SQLException inside
// a Derby SqlException so that the internal method does not have to throw
// java.sql.SQLException. Otherwise the chain of dependencies would quickly
// force the majority of internal methods to throw java.sql.SQLException.
// You can wrap a java.sql.SQLException inside a SqlException by using
// the constructor new SqlException(java.sql.SQLException wrapMe) nextMsg)) {
setThrowable(new SqlException(sqlca, nextMsg, true));
}
}
// Once all messages are internationalized, these methods should become
// private
private SqlException(LogWriter logWriter, String reason, String sqlState,
int errorCode)
{
this(logWriter, (Throwable)null, reason, sqlState, errorCode);
}
private SqlException(LogWriter logWriter, Throwable throwable,
String reason, String sqlState, int errorCode ) {
message_ = reason;
sqlstate_ = sqlState;
errorcode_ = errorCode;
setThrowable(throwable);
if (logWriter != null) {
logWriter.traceDiagnosable(SqlException.this);
}
}
/**
* Set the cause of this exception based on its type.
* {@code SQLException}s and
* {@code SqlException}s are
* linked with {@code setNextException()} and
* {@code initCause()}.
* All other exception types are linked with {@code initCause()}.
*/
private void setThrowable(Throwable throwable)
{
if ( throwable instanceof SqlException )
{
setNextException((SqlException) throwable);
}
else if ( throwable instanceof SQLException )
{
setNextException((SQLException) throwable );
}
if (throwable != null) {
initCause(throwable);
}
}
/**
* Wrap a SQLException in a SqlException. This is used by internal routines
* so the don't have to throw SQLException, which, through the chain of
* dependencies would force more and more internal routines to throw
* SQLException
*/
public SqlException(SQLException wrapme)
{
wrappedException_ = wrapme;
}
/**
* Convert this SqlException into a java.sql.SQLException
*/
public SQLException getSQLException()
{
if ( wrappedException_ != null )
{
return wrappedException_;
}
// When we have support for JDBC 4 SQLException subclasses, this is
// where we decide which exception to create
SQLException sqle = exceptionFactory.getSQLException(getMessage(), getSQLState(),
getErrorCode());
sqle.initCause(this);
// Set up the nextException chain
if ( nextException_ != null )
{
// The exception chain gets constructed automatically through
// the beautiful power of recursion
sqle.setNextException(nextException_.getSQLException());
}
return sqle;
}
// Label an exception element in a batched update exception chain.
// This text will be prepended onto the exceptions message text dynamically
// when getMessage() is called.
// Called by the Agent.
void setBatchPositionLabel(int index) {
batchPositionLabel_ = getMessageUtil().getTextMessage(MessageId.BATCH_POSITION_ID) +
index + ": ";
}
public Sqlca getSqlca() {
return sqlca_;
}
@Override
public String toString() {
// Match what the embedded driver does in StandardException.toString().
return "ERROR " + getSQLState() + ": " + getMessage();
}
@Override
public String getMessage() {
if ( wrappedException_ != null )
{
return wrappedException_.getMessage();
}
// The Net JDBC message is retrieved and cached if we have a valid
// SQLCA handle.
// It is possible that we don't have one in case of a serialized
// SqlException for instance. In this case, we set the message to the
// last one cached previously (if any available).
// For serialized SqlException, we can serialize the SQLCA as the
// object handle would become invalid, upon deserialization, causing
// the connection and JDBC not being retrievable (hence why it is
// being cached here).
if (sqlca_ != null) {
cachedMessage_ = message_ =
((Sqlca) sqlca_).getJDBCMessage(messageNumber_);
}
else if (cachedMessage_ != null) {
// SQLCA is no longer valid, set the message to the previously
// cached one
message_ = cachedMessage_;
}
if (batchPositionLabel_ != null) {
message_ = batchPositionLabel_ + message_;
}
if ( causeString_ != null ) {
// Append the string indicating the cause of the exception
// (this happens only in JDK13 environments)
message_ += causeString_;
}
return message_;
}
public String getSQLState() {
if ( wrappedException_ != null )
{
return wrappedException_.getSQLState();
}
return sqlstate_;
}
public int getErrorCode() {
if ( wrappedException_ != null )
{
return wrappedException_.getErrorCode();
}
return errorcode_;
}
public SqlException getNextException()
{
if ( wrappedException_ != null )
{
return new SqlException(wrappedException_.getNextException());
}
else
{
return nextException_;
}
}
public void setNextException(SqlException nextException)
{
if ( wrappedException_ != null )
{
wrappedException_.setNextException(nextException.getSQLException());
}
else
{
nextException_ = nextException;
}
}
public void setNextException(SQLException nextException)
{
if ( wrappedException_ != null )
{
wrappedException_.setNextException(nextException);
}
else
{
// Add this exception to the end of the chain
SqlException theEnd = this;
while (theEnd.nextException_ != null) {
theEnd = theEnd.nextException_;
}
theEnd.nextException_ = new SqlException(nextException);
}
}
/**
* Helper method to construct an exception which basically says that
* we encountered an underlying Java exception
*/
static SqlException javaException(LogWriter logWriter, Throwable e) {
return new SqlException(logWriter,
new ClientMessageId (SQLState.JAVA_EXCEPTION),
new Object[] {e.getClass().getName(), e.getMessage()}, e);
}
// Return a single SQLException without the "next" pointing to another SQLException.
// Because the "next" is a private field in java.sql.SQLException,
// we have to create a new SqlException in order to break the chain with "next" as null.
SqlException copyAsUnchainedSQLException(LogWriter logWriter) {
if (sqlca_ != null) {
// server error
return new SqlException(sqlca_, messageNumber_, false);
} else {
return new SqlException(logWriter, getMessage(), getSQLState(), getErrorCode()); // client error
}
}
}
// An intermediate exception encapsulation to provide code-reuse
// for common ResultSet data conversion exceptions.
class ColumnTypeConversionException extends SqlException {
ColumnTypeConversionException(LogWriter logWriter, String targetType,
String sourceType) {
super(logWriter,
new ClientMessageId(SQLState.LANG_DATA_TYPE_GET_MISMATCH),
targetType, sourceType);
}
}
// An intermediate exception encapsulation to provide code-reuse
// for common CrossConverters data conversion exceptions.
class OutsideRangeForDataTypeException extends SqlException {
OutsideRangeForDataTypeException(LogWriter logWriter, String instance) {
super(logWriter,
new ClientMessageId(SQLState.LANG_OUTSIDE_RANGE_FOR_DATATYPE),
instance);
}
}
Ads help maintain this website.