Author: bpendleton
Date: Wed Aug 19 23:38:59 2015
New Revision: 1696690
URL: http://svn.apache.org/r1696690
Log:
DERBY-6773: Derby throws plain SQLIntegrityConstraintViolationException
This patch includes contributions from Abhinav Gupta (abhinavgupta2004 at gmail dot com)
This patch enables use of the new Derby-specific subclass
of SQLIntegrityConstraintViolationException in both the embedded
and client-server configurations of the SQLExceptionFactory.
In addition, a number of new tests are added to verify that
the new exception is thrown and can be caught as we expect,
and that the accessor methods on the new exception allow
applications to determine which table name and constraint
name were responsible for the exception.
We still have a couple cleanup tasks before DERBY-6773 is
fully complete, but as of this patch the new behavior is
in effect.
Modified:
db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/insane.derby.jar.lastcontents
db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/sane.derby.jar.lastcontents
db/derby/code/trunk/java/client/org/apache/derby/client/am/SQLExceptionFactory.java
db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/SQLExceptionFactory.java
db/derby/code/trunk/java/shared/org/apache/derby/shared/common/error/DerbySQLIntegrityConstraintViolationException.java
db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/ConstraintCharacteristicsTest.java
db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/ForeignKeysDeferrableTest.java
Modified: db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/insane.derby.jar.lastcontents
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/insane.derby.jar.lastcontents?rev=1696690&r1=1696689&r2=1696690&view=diff
==============================================================================
--- db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/insane.derby.jar.lastcontents
(original)
+++ db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/insane.derby.jar.lastcontents
Wed Aug 19 23:38:59 2015
@@ -1415,6 +1415,7 @@ org.apache.derby.security.SystemPermissi
org.apache.derby.shared.common.error.ExceptionSeverity.class
org.apache.derby.shared.common.error.MessageUtils.class
org.apache.derby.shared.common.error.ShutdownException.class
+org.apache.derby.shared.common.error.DerbySQLIntegrityConstraintViolationException.class
org.apache.derby.shared.common.reference.JDBC40Translation.class
org.apache.derby.shared.common.reference.MessageId.class
org.apache.derby.shared.common.reference.SQLState.class
Modified: db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/sane.derby.jar.lastcontents
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/sane.derby.jar.lastcontents?rev=1696690&r1=1696689&r2=1696690&view=diff
==============================================================================
--- db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/sane.derby.jar.lastcontents
(original)
+++ db/derby/code/trunk/java/build/org/apache/derbyBuild/lastgoodjarcontents/sane.derby.jar.lastcontents
Wed Aug 19 23:38:59 2015
@@ -1418,6 +1418,7 @@ org.apache.derby.security.SystemPermissi
org.apache.derby.shared.common.error.ExceptionSeverity.class
org.apache.derby.shared.common.error.MessageUtils.class
org.apache.derby.shared.common.error.ShutdownException.class
+org.apache.derby.shared.common.error.DerbySQLIntegrityConstraintViolationException.class
org.apache.derby.shared.common.reference.JDBC40Translation.class
org.apache.derby.shared.common.reference.MessageId.class
org.apache.derby.shared.common.reference.SQLState.class
Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/SQLExceptionFactory.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/SQLExceptionFactory.java?rev=1696690&r1=1696689&r2=1696690&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/SQLExceptionFactory.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/SQLExceptionFactory.java Wed
Aug 19 23:38:59 2015
@@ -72,7 +72,14 @@ public class SQLExceptionFactory {
} else if (sqlState.startsWith(SQLState.SQL_DATA_PREFIX)) {
ex = new SQLDataException(message, sqlState, errCode);
} else if (sqlState.startsWith(SQLState.INTEGRITY_VIOLATION_PREFIX)) {
- ex = new DerbySQLIntegrityConstraintViolationException(message, sqlState,
+ if ( sqlState.equals( SQLState.LANG_NULL_INTO_NON_NULL ) )
+ ex = new SQLIntegrityConstraintViolationException(message, sqlState,
+ errCode);
+ else if ( sqlState.equals( SQLState.LANG_CHECK_CONSTRAINT_VIOLATED ) )
+ ex = new DerbySQLIntegrityConstraintViolationException(message, sqlState,
+ errCode, args[1], args[0]);
+ else
+ ex = new DerbySQLIntegrityConstraintViolationException(message, sqlState,
errCode, args[0], args[1]);
} else if (sqlState.startsWith(SQLState.AUTHORIZATION_SPEC_PREFIX)) {
ex = new SQLInvalidAuthorizationSpecException(message, sqlState,
Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/SQLExceptionFactory.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/SQLExceptionFactory.java?rev=1696690&r1=1696689&r2=1696690&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/SQLExceptionFactory.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/SQLExceptionFactory.java Wed
Aug 19 23:38:59 2015
@@ -34,6 +34,7 @@ import org.apache.derby.iapi.error.Stand
import org.apache.derby.iapi.jdbc.ExceptionFactory;
import org.apache.derby.iapi.services.i18n.MessageService;
import org.apache.derby.shared.common.reference.SQLState;
+import org.apache.derby.shared.common.error.DerbySQLIntegrityConstraintViolationException;
/**
*Class to create SQLException
@@ -82,8 +83,15 @@ public class SQLExceptionFactory extends
} else if (sqlState.startsWith(SQLState.SQL_DATA_PREFIX)) {
ex = new SQLDataException(message, sqlState, severity, ferry);
} else if (sqlState.startsWith(SQLState.INTEGRITY_VIOLATION_PREFIX)) {
- ex = new SQLIntegrityConstraintViolationException(message, sqlState,
+ if ( sqlState.equals( SQLState.LANG_NULL_INTO_NON_NULL ) )
+ ex = new SQLIntegrityConstraintViolationException(message, sqlState,
severity, ferry);
+ else if ( sqlState.equals( SQLState.LANG_CHECK_CONSTRAINT_VIOLATED ) )
+ ex = new DerbySQLIntegrityConstraintViolationException(message, sqlState,
+ severity, ferry, args[1], args[0]);
+ else
+ ex = new DerbySQLIntegrityConstraintViolationException(message, sqlState,
+ severity, ferry, args[0], args[1]);
} else if (sqlState.startsWith(SQLState.AUTHORIZATION_SPEC_PREFIX)) {
ex = new SQLInvalidAuthorizationSpecException(message, sqlState,
severity, ferry);
Modified: db/derby/code/trunk/java/shared/org/apache/derby/shared/common/error/DerbySQLIntegrityConstraintViolationException.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/shared/org/apache/derby/shared/common/error/DerbySQLIntegrityConstraintViolationException.java?rev=1696690&r1=1696689&r2=1696690&view=diff
==============================================================================
--- db/derby/code/trunk/java/shared/org/apache/derby/shared/common/error/DerbySQLIntegrityConstraintViolationException.java
(original)
+++ db/derby/code/trunk/java/shared/org/apache/derby/shared/common/error/DerbySQLIntegrityConstraintViolationException.java
Wed Aug 19 23:38:59 2015
@@ -31,8 +31,8 @@ public class DerbySQLIntegrityConstrai
Object argsTwo)
{
super( reason, SQLState, vendorCode, cause );
- tableName = argsOne.toString();
- constraintName = argsTwo.toString();
+ tableName = argsTwo.toString();
+ constraintName = argsOne.toString();
}
public DerbySQLIntegrityConstraintViolationException(
@@ -43,8 +43,8 @@ public class DerbySQLIntegrityConstrai
Object argsTwo)
{
super( reason, SQLState, vendorCode );
- tableName = argsOne.toString();
- constraintName = argsTwo.toString();
+ tableName = argsTwo.toString();
+ constraintName = argsOne.toString();
}
public String getTableName() { return tableName; }
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/ConstraintCharacteristicsTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/ConstraintCharacteristicsTest.java?rev=1696690&r1=1696689&r2=1696690&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/ConstraintCharacteristicsTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/ConstraintCharacteristicsTest.java
Wed Aug 19 23:38:59 2015
@@ -55,6 +55,7 @@ import org.apache.derbyTesting.junit.Sup
import org.apache.derbyTesting.junit.SystemPropertyTestSetup;
import org.apache.derbyTesting.junit.TestConfiguration;
import org.apache.derbyTesting.junit.XATestUtil;
+import org.apache.derby.shared.common.error.DerbySQLIntegrityConstraintViolationException;
public class ConstraintCharacteristicsTest extends BaseJDBCTestCase
{
@@ -184,6 +185,8 @@ public class ConstraintCharacteristicsTe
"testDerby6670_b"));
suite.addTest(new ConstraintCharacteristicsTest(
"testManySimilarDuplicates"));
+ suite.addTest(new ConstraintCharacteristicsTest(
+ "testDerby6773"));
final Properties systemProperties = new Properties();
systemProperties.setProperty(
@@ -869,6 +872,16 @@ public class ConstraintCharacteristicsTe
assertStatementError(LANG_CHECK_CONSTRAINT_VIOLATED,
s,
"insert into t values (-2,30)");
+ // Test the DERBY-6773 support:
+ try {
+ s.execute( "insert into t values (-2,30)" );
+ fail();
+ }
+ catch ( DerbySQLIntegrityConstraintViolationException dsicve ) {
+ assertSQLState(LANG_CHECK_CONSTRAINT_VIOLATED, dsicve);
+ assertEquals( "\"APP\".\"T\"", dsicve.getTableName() );
+ assertEquals( "C", dsicve.getConstraintName() );
+ }
// Now set deferred mode in one of two ways: by specifying
// ALL or by naming our index explicitly.
@@ -897,10 +910,30 @@ public class ConstraintCharacteristicsTe
assertStatementError(LANG_DEFERRED_CHECK_VIOLATION_S,
s,
sCF + " immediate");
+ // Test the DERBY-6773 support:
+ try {
+ s.execute( sCF + " immediate" );
+ fail();
+ }
+ catch ( DerbySQLIntegrityConstraintViolationException dsicve ) {
+ assertSQLState(LANG_DEFERRED_CHECK_VIOLATION_S, dsicve);
+ assertEquals( "\"APP\".\"T\"", dsicve.getTableName() );
+ assertEquals( "C", dsicve.getConstraintName() );
+ }
// Now try to commit, which should lead to rollback
- assertCommitError(LANG_DEFERRED_CHECK_VIOLATION_T,
- getConnection());
+ //assertCommitError(LANG_DEFERRED_CHECK_VIOLATION_T,
+ // getConnection());
+ // Test the DERBY-6773 support:
+ try {
+ getConnection().commit();
+ fail();
+ }
+ catch ( DerbySQLIntegrityConstraintViolationException dsicve ) {
+ assertSQLState(LANG_DEFERRED_CHECK_VIOLATION_T, dsicve);
+ assertEquals( "\"APP\".\"T\"", dsicve.getTableName() );
+ assertEquals( "C", dsicve.getConstraintName() );
+ }
// Verify that contents are the same as before we did the
// duplicate inserts
@@ -1769,6 +1802,16 @@ public class ConstraintCharacteristicsTe
LANG_DUPLICATE_KEY_CONSTRAINT, stmt,
"insert into table1 values(4,44,111)");
+ // Test the DERBY-6773 support:
+ try {
+ stmt.execute( "insert into table1 values(1,22,222)");
+ fail();
+ }
+ catch ( DerbySQLIntegrityConstraintViolationException dsicve ) {
+ assertSQLState(LANG_DUPLICATE_KEY_CONSTRAINT, dsicve);
+ assertEquals( "TABLE1", dsicve.getTableName() );
+ assertTrue( dsicve.getConstraintName().startsWith( "SQL" ) );
+ }
} finally {
stmt.executeUpdate("drop table table1");
}
@@ -2831,6 +2874,53 @@ public class ConstraintCharacteristicsTe
}
/**
+ * DERBY-6773: check the Derby-specific subclass of the standard
+ * SQLIntegrityConstraintViolationException.
+ */
+ public void testDerby6773() throws Exception
+ {
+ final Statement s = createStatement();
+ s.executeUpdate( "create table Application " +
+ " (id bigint generated by default as identity," +
+ " name varchar(255)," +
+ " shortName varchar(32)," +
+ " userId varchar(32)," +
+ " primary key (id))" );
+ s.executeUpdate( "create unique index UK_APPLICATION_SHORTNAME " +
+ " on Application (shortName)" );
+ s.executeUpdate( "create unique index UK_APPLICATION_NAME " +
+ " on Application (name)" );
+ s.executeUpdate( "insert into Application (name, shortName, userId) " +
+ " VALUES ('fooApp', 'Foo Application 0', 'me')" );
+ try
+ {
+ s.executeUpdate(
+ "insert into Application (name, shortName, userId) " +
+ " VALUES ('fooApp', 'Foo Application 1', 'me')" );
+ fail();
+ }
+ catch ( DerbySQLIntegrityConstraintViolationException dsicve )
+ {
+ assertEquals( "APPLICATION", dsicve.getTableName() );
+ assertEquals( "UK_APPLICATION_NAME", dsicve.getConstraintName() );
+ }
+
+ try
+ {
+ s.executeUpdate(
+ "insert into Application (name, shortName, userId) " +
+ " VALUES ('BarApp', 'Foo Application 0', 'me')" );
+ fail();
+ }
+ catch ( DerbySQLIntegrityConstraintViolationException dsicve )
+ {
+ assertEquals( "APPLICATION", dsicve.getTableName() );
+ assertEquals( "UK_APPLICATION_SHORTNAME",
+ dsicve.getConstraintName() );
+ }
+ }
+
+ /**
* Privileged lookup of the LCC from a Connection.
*/
public static LanguageConnectionContext getLCC( final Connection conn )
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/ForeignKeysDeferrableTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/ForeignKeysDeferrableTest.java?rev=1696690&r1=1696689&r2=1696690&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/ForeignKeysDeferrableTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/ForeignKeysDeferrableTest.java
Wed Aug 19 23:38:59 2015
@@ -30,6 +30,7 @@ import org.apache.derbyTesting.junit.Bas
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.SupportFilesSetup;
+import org.apache.derby.shared.common.error.DerbySQLIntegrityConstraintViolationException;
import static org.apache.derbyTesting.junit.TestConfiguration.clientServerSuite;
import static org.apache.derbyTesting.junit.TestConfiguration.embeddedSuite;
@@ -266,11 +267,33 @@ public class ForeignKeysDeferrableTest e
// ...InsertResultSet.normalInsertCore(InsertResultSet.java:1028)
assertStatementError(LANG_FK_VIOLATION, s, DIRECT_INSERT_SQL);
+
+ // Test the DERBY-6773 support:
+ try {
+ s.execute( DIRECT_INSERT_SQL );
+ fail();
+ }
+ catch ( DerbySQLIntegrityConstraintViolationException dsicve ) {
+ assertSQLState(LANG_FK_VIOLATION, dsicve);
+ assertEquals( "T_D_R", dsicve.getTableName() );
+ assertEquals( "C_D_R", dsicve.getConstraintName() );
+ }
s.executeUpdate("set constraints c_d_r deferred");
s.executeUpdate(DIRECT_INSERT_SQL);
- assertCommitError(LANG_DEFERRED_FK_CONSTRAINT_T, getConnection());
+ // Use the slightly wordier form of:
+ //assertCommitError(LANG_DEFERRED_FK_CONSTRAINT_T, getConnection());
+ // to exercise the DERBY-6773 support:
+ try {
+ getConnection().commit();
+ fail();
+ }
+ catch ( DerbySQLIntegrityConstraintViolationException dsicve ) {
+ assertSQLState(LANG_DEFERRED_FK_CONSTRAINT_T, dsicve);
+ assertEquals( "\"APP\".\"T_D_R\"", dsicve.getTableName() );
+ assertEquals( "C_D_R", dsicve.getConstraintName() );
+ }
// Now see deferred check succeed by actually adding referenced key
// *after* the insert of the referencing row. Also check that setting
@@ -294,6 +317,17 @@ public class ForeignKeysDeferrableTest e
"set constraints c_d_r immediate");
assertStatementError(LANG_DEFERRED_FK_CONSTRAINT_S, s,
"set constraints c_d_r immediate");
+ // Test the DERBY-6773 support:
+ try {
+ s.execute( "set constraints c_d_r immediate" );
+ fail();
+ }
+ catch ( DerbySQLIntegrityConstraintViolationException dsicve ) {
+ assertSQLState(LANG_DEFERRED_FK_CONSTRAINT_S, dsicve);
+ assertEquals( "\"APP\".\"T_D_R\"", dsicve.getTableName() );
+ assertEquals( "C_D_R", dsicve.getConstraintName() );
+ }
+
s.executeUpdate("delete from t_d_r where i=3");
commit();
@@ -1305,7 +1339,15 @@ public class ForeignKeysDeferrableTest e
// Commit. Should fail because the PRIMARY KEY constraint of T5
// is violated. Was not detected before DERBY-6665.
- assertCommitError(LANG_DEFERRED_DUPLICATE_KEY_CONSTRAINT_T,
- getConnection());
+ // Test the DERBY-6773 support, too.
+ try {
+ getConnection().commit();
+ fail();
+ }
+ catch ( DerbySQLIntegrityConstraintViolationException dsicve ) {
+ assertSQLState(LANG_DEFERRED_DUPLICATE_KEY_CONSTRAINT_T, dsicve);
+ assertEquals( "D6665_T5", dsicve.getTableName() );
+ assertTrue( dsicve.getConstraintName().startsWith( "SQL" ) );
+ }
}
}
|