|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package testsuite.simple; |
|
|
|
import java.sql.DatabaseMetaData; |
|
import java.sql.ResultSet; |
|
import java.sql.SQLException; |
|
import java.sql.Statement; |
|
|
|
import testsuite.BaseTestCase; |
|
|
|
import com.mysql.jdbc.NotUpdatable; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class UpdatabilityTest extends BaseTestCase { |
|
|
|
|
|
|
|
|
|
|
|
|
|
public UpdatabilityTest(String name) { |
|
super(name); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
junit.textui.TestRunner.run(UpdatabilityTest.class); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void setUp() throws Exception { |
|
super.setUp(); |
|
createTestTable(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void testAliasedTables() throws Exception { |
|
DatabaseMetaData dbmd = this.conn.getMetaData(); |
|
|
|
if (versionMeetsMinimum(4, 1)) { |
|
Statement scrollableStmt = null; |
|
|
|
try { |
|
scrollableStmt = this.conn.createStatement( |
|
ResultSet.TYPE_SCROLL_INSENSITIVE, |
|
ResultSet.CONCUR_UPDATABLE); |
|
this.rs = scrollableStmt |
|
.executeQuery("SELECT pos1 AS p1, pos2 AS P2, char_field AS cf FROM UPDATABLE AS UPD LIMIT 1"); |
|
this.rs.next(); |
|
this.rs.close(); |
|
this.rs = null; |
|
|
|
scrollableStmt.close(); |
|
scrollableStmt = null; |
|
} finally { |
|
if (this.rs != null) { |
|
try { |
|
this.rs.close(); |
|
} catch (SQLException sqlEx) { |
|
; |
|
} |
|
|
|
this.rs = null; |
|
} |
|
|
|
if (scrollableStmt != null) { |
|
try { |
|
scrollableStmt.close(); |
|
} catch (SQLException sqlEx) { |
|
; |
|
} |
|
|
|
scrollableStmt = null; |
|
} |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void testBogusTable() throws SQLException { |
|
this.stmt.executeUpdate("DROP TABLE IF EXISTS BOGUS_UPDATABLE"); |
|
this.stmt.executeUpdate("CREATE TABLE BOGUS_UPDATABLE (field1 int)"); |
|
|
|
Statement scrollableStmt = null; |
|
|
|
try { |
|
scrollableStmt = this.conn.createStatement( |
|
ResultSet.TYPE_SCROLL_INSENSITIVE, |
|
ResultSet.CONCUR_UPDATABLE); |
|
this.rs = scrollableStmt |
|
.executeQuery("SELECT * FROM BOGUS_UPDATABLE"); |
|
|
|
try { |
|
this.rs.moveToInsertRow(); |
|
fail("ResultSet.moveToInsertRow() should not succeed on non-updatable table"); |
|
} catch (NotUpdatable noUpdate) { |
|
|
|
} |
|
} finally { |
|
if (scrollableStmt != null) { |
|
try { |
|
scrollableStmt.close(); |
|
} catch (SQLException sqlEx) { |
|
; |
|
} |
|
} |
|
|
|
this.stmt.executeUpdate("DROP TABLE IF EXISTS BOGUS_UPDATABLE"); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void testMultiKeyTable() throws SQLException { |
|
this.stmt.executeUpdate("DROP TABLE IF EXISTS MULTI_UPDATABLE"); |
|
this.stmt |
|
.executeUpdate("CREATE TABLE MULTI_UPDATABLE (field1 int NOT NULL, field2 int NOT NULL, PRIMARY KEY (field1, field2))"); |
|
|
|
Statement scrollableStmt = null; |
|
|
|
try { |
|
scrollableStmt = this.conn.createStatement( |
|
ResultSet.TYPE_SCROLL_INSENSITIVE, |
|
ResultSet.CONCUR_UPDATABLE); |
|
this.rs = scrollableStmt |
|
.executeQuery("SELECT field1 FROM MULTI_UPDATABLE"); |
|
|
|
try { |
|
this.rs.moveToInsertRow(); |
|
fail("ResultSet.moveToInsertRow() should not succeed on query that does not select all primary keys"); |
|
} catch (NotUpdatable noUpdate) { |
|
|
|
} |
|
} finally { |
|
if (scrollableStmt != null) { |
|
try { |
|
scrollableStmt.close(); |
|
} catch (SQLException sqlEx) { |
|
|
|
} |
|
} |
|
|
|
this.stmt.executeUpdate("DROP TABLE IF EXISTS MULTI_UPDATABLE"); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void testUpdatability() throws SQLException { |
|
Statement scrollableStmt = null; |
|
|
|
try { |
|
scrollableStmt = this.conn.createStatement( |
|
ResultSet.TYPE_SCROLL_INSENSITIVE, |
|
ResultSet.CONCUR_UPDATABLE); |
|
this.rs = scrollableStmt |
|
.executeQuery("SELECT * FROM UPDATABLE ORDER BY pos1"); |
|
|
|
this.rs.getMetaData().getColumnCount(); |
|
|
|
while (this.rs.next()) { |
|
int rowPos = this.rs.getInt(1); |
|
this.rs.updateString(3, "New Data" + (100 - rowPos)); |
|
this.rs.updateRow(); |
|
} |
|
|
|
|
|
|
|
|
|
this.rs.moveToInsertRow(); |
|
this.rs.updateInt(1, 400); |
|
this.rs.updateInt(2, 400); |
|
this.rs.updateString(3, "New Data" + (100 - 400)); |
|
this.rs.insertRow(); |
|
|
|
|
|
int rememberedPosition = this.rs.getRow(); |
|
this.rs.moveToInsertRow(); |
|
this.rs.moveToCurrentRow(); |
|
assertTrue("ResultSet.moveToCurrentRow() failed", |
|
this.rs.getRow() == rememberedPosition); |
|
this.rs.close(); |
|
this.rs = scrollableStmt |
|
.executeQuery("SELECT * FROM UPDATABLE ORDER BY pos1"); |
|
|
|
boolean dataGood = true; |
|
|
|
while (this.rs.next()) { |
|
int rowPos = this.rs.getInt(1); |
|
|
|
if (!this.rs.getString(3).equals("New Data" + (100 - rowPos))) { |
|
dataGood = false; |
|
} |
|
} |
|
|
|
assertTrue("Updates failed", dataGood); |
|
|
|
|
|
|
|
int newPrimaryKeyId = 99999; |
|
this.rs.absolute(1); |
|
this.rs.updateInt(1, newPrimaryKeyId); |
|
this.rs.updateRow(); |
|
|
|
int savedPrimaryKeyId = this.rs.getInt(1); |
|
assertTrue("Updated primary key does not match", |
|
(newPrimaryKeyId == savedPrimaryKeyId)); |
|
|
|
|
|
this.rs.absolute(1); |
|
|
|
int primaryKey = this.rs.getInt(1); |
|
int originalValue = this.rs.getInt(2); |
|
this.rs.updateInt(2, -3); |
|
this.rs.cancelRowUpdates(); |
|
|
|
int newValue = this.rs.getInt(2); |
|
assertTrue("ResultSet.cancelRowUpdates() failed", |
|
newValue == originalValue); |
|
|
|
|
|
|
|
this.rs.absolute(1); |
|
primaryKey = this.rs.getInt(1); |
|
this.stmt |
|
.executeUpdate("UPDATE UPDATABLE SET char_field='foo' WHERE pos1=" |
|
+ primaryKey); |
|
this.rs.refreshRow(); |
|
assertTrue("ResultSet.refreshRow failed", this.rs.getString( |
|
"char_field").equals("foo")); |
|
|
|
|
|
this.rs.last(); |
|
|
|
int oldLastRow = this.rs.getRow(); |
|
this.rs.deleteRow(); |
|
this.rs.last(); |
|
assertTrue("ResultSet.deleteRow() failed", |
|
this.rs.getRow() == (oldLastRow - 1)); |
|
this.rs.close(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} finally { |
|
if (scrollableStmt != null) { |
|
try { |
|
scrollableStmt.close(); |
|
} catch (SQLException sqlEx) { |
|
; |
|
} |
|
} |
|
} |
|
} |
|
|
|
private void createTestTable() throws SQLException { |
|
|
|
|
|
|
|
try { |
|
this.stmt.executeUpdate("DROP TABLE UPDATABLE"); |
|
} catch (SQLException SQLE) { |
|
; |
|
} |
|
|
|
this.stmt |
|
.executeUpdate("CREATE TABLE UPDATABLE (pos1 int not null, pos2 int not null, char_field VARCHAR(32), PRIMARY KEY (pos1, pos2))"); |
|
|
|
for (int i = 0; i < 100; i++) { |
|
this.stmt.executeUpdate("INSERT INTO UPDATABLE VALUES (" + i + ", " |
|
+ i + ",'StringData" + i + "')"); |
|
} |
|
} |
|
} |
|
|