001: /*
002: *
003: * Derby - Class URCoveringIndexTest
004: *
005: * Licensed to the Apache Software Foundation (ASF) under one or more
006: * contributor license agreements. See the NOTICE file distributed with
007: * this work for additional information regarding copyright ownership.
008: * The ASF licenses this file to You under the Apache License, Version 2.0
009: * (the "License"); you may not use this file except in compliance with
010: * the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
017: * either express or implied. See the License for the specific
018: * language governing permissions and limitations under the License.
019: */
020: package org.apache.derbyTesting.functionTests.tests.jdbcapi;
021:
022: import org.apache.derbyTesting.functionTests.util.TestUtil;
023: import org.apache.derbyTesting.junit.BaseJDBCTestCase;
024: import org.apache.derbyTesting.junit.JDBC;
025:
026: import junit.framework.*;
027: import java.sql.*;
028:
029: /**
030: * Tests updatable result sets when there is a index that includes all data for
031: * the query (covering index).
032: *
033: * DERBY-1087
034: *
035: * @author Fernanda Pizzorno
036: */
037: public class URCoveringIndexTest extends BaseJDBCTestCase {
038:
039: /** Creates a new instance of SURBaseTest */
040: public URCoveringIndexTest(String name) {
041: super (name);
042: }
043:
044: /**
045: * Set up the connection to the database.
046: */
047: public void setUp() throws Exception {
048: Connection con = getConnection();
049: con.setAutoCommit(false);
050:
051: String createTableWithPK = "CREATE TABLE tableWithPK ("
052: + "c1 int primary key," + "c2 int)";
053: String insertData = "INSERT INTO tableWithPK values (1, 1)";
054: Statement stmt = con.createStatement();
055: stmt.execute(createTableWithPK);
056:
057: stmt.execute(insertData);
058:
059: stmt.close();
060: }
061:
062: private void testUpdateUpdatedTupleWithCoveringIndex(
063: boolean scroll, boolean usePositionedUpdate)
064: throws SQLException {
065:
066: SQLWarning w = null;
067: int resultsetType = scroll ? ResultSet.TYPE_SCROLL_INSENSITIVE
068: : ResultSet.TYPE_FORWARD_ONLY;
069:
070: Connection con = getConnection();
071:
072: if (!(con.getMetaData().supportsResultSetConcurrency(
073: resultsetType, ResultSet.CONCUR_UPDATABLE))) {
074: return;
075: }
076:
077: Statement updStmt = con.createStatement(resultsetType,
078: ResultSet.CONCUR_UPDATABLE);
079: Statement roStmt = con.createStatement();
080:
081: ResultSet rs = updStmt
082: .executeQuery("SELECT c1 FROM tableWithPK");
083: rs.next();
084: int orig_c1 = rs.getInt(1);
085: roStmt.executeUpdate("UPDATE tableWithPK SET c1 = "
086: + (orig_c1 + 10) + "WHERE c1 = " + rs.getInt(1));
087: rs.clearWarnings();
088: if (usePositionedUpdate) {
089: roStmt.executeUpdate("UPDATE tableWithPK set c1 = "
090: + (orig_c1 + 20) + "WHERE CURRENT OF "
091: + rs.getCursorName());
092: w = roStmt.getWarnings();
093: } else {
094: rs.updateInt(1, (orig_c1 + 20));
095: rs.updateRow();
096: w = rs.getWarnings();
097: }
098: assertTrue("Update should not produce any warnings ", w == null);
099: rs.close();
100:
101: rs = roStmt.executeQuery("SELECT c1 FROM tableWithPK");
102: rs.next();
103: assertEquals("Expecting c1 to be " + orig_c1 + " + 20", rs
104: .getInt(1), (orig_c1 + 20));
105: rs.close();
106: roStmt.close();
107: updStmt.close();
108:
109: }
110:
111: /**
112: * Updates a previously updated row with a covering index using positioned
113: * updates and scrollable result sets.
114: */
115: public void testUpdateUpdatedTupleScrollPostitioned()
116: throws SQLException {
117: testUpdateUpdatedTupleWithCoveringIndex(true, true);
118: }
119:
120: /**
121: * Updates a previously updated row with a covering index using updateRow
122: * and scrollable result sets.
123: */
124: public void testUpdateUpdatedTupleScrollUpdateRow()
125: throws SQLException {
126: testUpdateUpdatedTupleWithCoveringIndex(true, false);
127: }
128:
129: /**
130: * Updates a previously updated row with a covering index using positioned
131: * updates and forward only result sets.
132: */
133: public void testUpdateUpdatedTupleFOPositioned()
134: throws SQLException {
135: testUpdateUpdatedTupleWithCoveringIndex(false, true);
136: }
137:
138: /**
139: * Updates a previously updated row with a covering index using updateRow
140: * and forward only result sets.
141: */
142: public void testUpdateUpdatedTupleFOUpdateRow() throws SQLException {
143: testUpdateUpdatedTupleWithCoveringIndex(false, false);
144: }
145: }
|