1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
|
/*
Derby - Class org.apache.derbyTesting.functionTests.tests.lang.RenameTableTest
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.derbyTesting.functionTests.tests.lang;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import junit.framework.Test;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.TestConfiguration;
/**
* Various tests for RENAME TABLE
*
*/
public class RenameTableTest extends BaseJDBCTestCase {
private static final String PRIMARY_VIOLATION = "23505";
private static final String FOREIGN_VIOLATION = "23503";
private static final String DEFERRED_PRIMARY_VIOLATION = "23506";
private static final String DEFERRED_FOREIGN_VIOLATION = "23516";
private static final String USER_ERROR = "38000";
public RenameTableTest(String name) {
super(name);
}
public static Test suite() {
return TestConfiguration.embeddedSuite(RenameTableTest.class);
}
protected void setUp() throws Exception {
super.setUp();
getConnection().setAutoCommit(false);
}
protected void tearDown() throws Exception {
super.tearDown();
}
/**
* Tests that we can't rename a Non-Existing Table.
*
* @exception SQLException
*/
public void testRenameNonExistingTable() throws SQLException {
Statement s = createStatement();
assertStatementError("42Y55", s, "rename table notexists to notexists1");
}
/**
* Tests that we can't rename a table with an existed table name
*
* @exception SQLException
*/
public void testExistedNameForRenameTable() throws SQLException {
Statement s = createStatement();
s.executeUpdate("create table t1(c11 int not null primary key)");
s.executeUpdate("create table t2(c21 int not null primary key)");
assertStatementError("X0Y32", s, "rename table t1 to t2");
s.executeUpdate("drop table t1");
s.executeUpdate("drop table t2");
}
/**
* Tests that we cannot rename a System Table.
*
* @exception SQLException
*/
public void testRenameSystemTable() throws SQLException {
Statement s = createStatement();
assertStatementError("42X62", s, "rename table sys.systables to fake");
}
/**
* Tests that we cannot rename a View
*
* @exception SQLException
*/
public void testRenameTableWithViews() throws SQLException {
Statement s = createStatement();
s.executeUpdate("create table t1(c11 int not null primary key)");
s.executeUpdate("insert into t1 values 11");
s.executeUpdate("create view v1 as select * from t1");
assertStatementError("42Y62", s, "rename table v1 to fake");
assertStatementError("X0Y23", s, "rename table t1 to fake");
s.executeUpdate("drop view v1");
s.executeUpdate("drop table t1");
}
// -- cannot rename a table when there is an open cursor on it
// Bug 2994 ( https://issues.apache.org/jira/browse/DERBY-2994 )
//
/*
* public void testRenameOpenCursoredTable() throws SQLException { Statement
* s = createStatement(ResultSet.TYPE_FORWARD_ONLY ,
* ResultSet.CONCUR_UPDATABLE); assertUpdateCount(s , 0 , "create table
* t2(c21 int not null primary key)"); assertUpdateCount(s , 1 , "insert
* into t2 values(21)"); assertUpdateCount(s , 1 , "insert into t2
* values(22)");
*
* ResultSet rs = s.executeQuery("select * from t2"); rs.next();
* assertStatementError("X0X95" , s , "rename table t2 to fake"); }
*/
/**
* Tests that We can rename a table when there is an index defined on it
*
* @exception SQLException
*/
public void testRenameWithIndex() throws SQLException {
Statement s = createStatement();
s.executeUpdate("create table t3(c31 int not null)");
s.executeUpdate("create index i1_t3 on t3(c31)");
// -- can rename a table when there is an index defined on it
assertUpdateCount(s, 0, "rename table t3 to t3r");
s.executeUpdate("drop table t3r");
}
/**
* Test Rename Table With PreparedStatement.
*
* @exception SQLException
*/
public void testRenameWithPreparedStatement() throws SQLException {
Statement s = createStatement();
s.executeUpdate("create table t3(c31 int not null primary key)");
s.executeUpdate("insert into t3 values 31");
s.executeUpdate("insert into t3 values 32");
s.executeUpdate("insert into t3 values 33");
PreparedStatement pstmt = prepareStatement("select * from t3 where c31 > ?");
pstmt.setInt(1, 30);
ResultSet rs = pstmt.executeQuery();
rs.next();
rs.close();
// -- can rename with no errors
assertUpdateCount(s, 0, "rename table t3 to t3r");
// -- but the execute statement will fail
pstmt.setInt(1, 30);
try {
ResultSet rs1 = pstmt.executeQuery();
fail("Table/View t3 Doesn't exists:");
} catch (SQLException e) {
assertSQLState("42X05", e);
}
s.executeUpdate("drop table t3r");
}
// -- creating a table with triggers defined on it
/**
* Test that we can RENAME a TABLE when there is trigger defined on it.
*
* @exception SQLException
*/
public void testRenameTableWithTriggersOnIt() throws SQLException {
Statement s = createStatement();
s.executeUpdate("create table t6 (c61 int default 1)");
s.executeUpdate("create table t7(c71 int)");
// -- bug 5684
s
.executeUpdate("create trigger t7insert after insert on t7 referencing new as NEWROW for each row insert into t6 values(NEWROW.c71)");
s.executeUpdate("insert into t7 values(1)");
// -- bug 5683. Should fail
assertStatementError("X0Y25", s, "rename table t7 to t7r");
assertStatementError("42X05", s, "select * from t7r");
ResultSet rs = s.executeQuery("select * from t7");
rs.next();
rs.close();
// DERBY-2041: Rename of table referenced in a trigger action
// should fail and leave trigger intact.
assertStatementError("X0Y25", s, "rename table t6 to t6r");
s.execute("insert into t7 values(3)");
JDBC.assertFullResultSet(
s.executeQuery("select * from t6 order by c61"),
new String[][] {{"1"}, {"3"}});
assertStatementError("42X05", s, "select * from t7r");
// Clean Up
s.executeUpdate("drop table t7");
s.executeUpdate("drop table t6");
}
/**
* RENAME TABLE should fail when check constraints on it.
*
* @exception SQLException
*/
public void testRenameWithCheckConstraintsOnIt() throws SQLException {
Statement s = createStatement();
s.executeUpdate("create table tcheck (i int check(i>5))");
assertStatementError("X0Y25", s, "rename table tcheck to tcheck1");
s.executeUpdate("drop table tcheck");
// - Rename should pass after dropping the check constriant
s
.executeUpdate("create table tcheck (i int, j int, constraint tcon check (i+j>2))");
assertStatementError("X0Y25", s, "rename table tcheck to tcheck1");
s.executeUpdate("alter table tcheck drop constraint tcon");
s.executeUpdate("rename table tcheck to tcheck1");
// select * from tcheck1;
s.executeUpdate("drop table tcheck1");
}
/**
* Tests that rename table invalidates stored statement plans (DERBY-4479).
*
* By issuing the *identical* create table statement after the rename,
* we check to see whether the compiled statements from the first
* create table statement were properly invalidated by the rename.
*
* @exception SQLException
*/
public void testRenameInvalidation_derby_4479()
throws SQLException
{
getConnection().setAutoCommit(true);
Statement s = createStatement();
s.executeUpdate("create table a (x int not null primary key)");
s.executeUpdate("rename table a to b");
s.executeUpdate("create table a (x int not null primary key)");
s.executeUpdate("drop table a");
s.executeUpdate("drop table b");
}
/**
* Test that you can rename a table referenced by a foreign key.
* See DERBY-6672.
*/
public void test_6672_1() throws Exception
{
Connection conn = getConnection();
conn.setAutoCommit( true );
goodStatement
(
conn,
"create procedure runStatementAndRaiseError( text varchar( 128 ) )\n" +
"language java parameter style java modifies sql data\n" +
"external name 'org.apache.derbyTesting.functionTests.tests.lang.RenameTableTest.runStatementAndRaiseError'\n"
);
//
// Basic test. Rename tables and try to violate constraints.
//
goodStatement( conn, "create table primary1( a int primary key )" );
goodStatement( conn, "create table foreign1( a int references primary1( a ) )" );
goodStatement( conn, "rename table primary1 to primary2" );
goodStatement( conn, "rename table foreign1 to foreign2" );
goodStatement( conn, "rename column primary2.a to b" );
goodStatement( conn, "rename column foreign2.a to b" );
goodStatement( conn, "insert into primary2 values 1" );
assertPreparedStatementError
(
PRIMARY_VIOLATION,
conn.prepareStatement( "insert into primary2 values 1" )
);
goodStatement( conn, "insert into foreign2 values 1" );
assertPreparedStatementError
(
FOREIGN_VIOLATION,
conn.prepareStatement( "insert into foreign2 values 2" )
);
//
// Simple test with deferrable constraints. Rename tables and violate
// constraints.
//
goodStatement( conn, "create table primary10( a int primary key initially deferred )" );
goodStatement( conn, "create table foreign10( a int references primary10( a ) initially deferred )" );
conn.setAutoCommit( false );
try {
goodStatement( conn, "insert into primary10 values 1" );
goodStatement( conn, "rename table primary10 to primary20" );
goodStatement( conn, "insert into primary20 values 1" );
try {
conn.commit();
fail( "Commit should not have succeeded!" );
}
catch (SQLException se)
{
assertEquals( DEFERRED_PRIMARY_VIOLATION, se.getSQLState() );
}
goodStatement( conn, "insert into primary10 values 1" );
goodStatement( conn, "rename table primary10 to primary20" );
goodStatement( conn, "rename table foreign10 to foreign20" );
goodStatement( conn, "insert into foreign20 values 2" );
try {
conn.commit();
fail( "Commit should not have succeeded!" );
}
catch (SQLException se)
{
assertEquals( DEFERRED_FOREIGN_VIOLATION, se.getSQLState() );
}
}
finally
{
conn.setAutoCommit( true );
}
//
// Try to rename the tables in a savepoint which is rolled back.
// See DERBY-6670.
//
goodStatement( conn, "create table primary100( a int primary key initially deferred )" );
goodStatement( conn, "create table foreign100( a int references primary100( a ) initially deferred )" );
conn.setAutoCommit( false );
try {
goodStatement( conn, "insert into primary100 values 1, 1" );
try {
conn.prepareStatement( "call runStatementAndRaiseError( 'rename table primary100 to primary200' )" ).execute();
fail( "Statement should have failed!" );
}
catch (SQLException se)
{
assertEquals( USER_ERROR, se.getSQLState() );
}
try {
conn.commit();
fail( "Commit should not have succeeded!" );
}
catch (SQLException se)
{
assertEquals( DEFERRED_PRIMARY_VIOLATION, se.getSQLState() );
}
goodStatement( conn, "insert into primary100 values 1" );
goodStatement( conn, "rename table primary100 to primary200" );
goodStatement( conn, "insert into foreign100 values 2" );
try {
conn.prepareStatement( "call runStatementAndRaiseError( 'rename table foreign100 to foreign200' )" ).execute();
fail( "Statement should have failed!" );
}
catch (SQLException se)
{
assertEquals( USER_ERROR, se.getSQLState() );
}
try {
conn.commit();
fail( "Commit should not have succeeded!" );
}
catch (SQLException se)
{
assertEquals( DEFERRED_FOREIGN_VIOLATION, se.getSQLState() );
}
}
finally
{
conn.setAutoCommit( true );
}
}
public static void runStatementAndRaiseError( String text )
throws SQLException
{
Connection conn = DriverManager.getConnection( "jdbc:default:connection" );
conn.prepareStatement( text ).execute();
throw new SQLException( "Oh sorry, an error occurred." );
}
}
|