Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[CONJ-316] Wrong Exception thrown for ScrollType TYPE_SCROLL_INSENSITIVE
  • Loading branch information
rusher committed Jun 26, 2016
1 parent 9df97d3 commit ab05206
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 10 deletions.
8 changes: 2 additions & 6 deletions src/main/java/org/mariadb/jdbc/MariaDbConnection.java
Expand Up @@ -191,9 +191,7 @@ public Statement createStatement() throws SQLException {
* <code>ResultSet</code> constants indicating type and concurrency
*/
public Statement createStatement(final int resultSetType, final int resultSetConcurrency) throws SQLException {
// for now resultSetType and resultSetConcurrency are ignored
// TODO: fix
return createStatement();
return new MariaDbStatement(this, resultSetType);
}

/**
Expand Down Expand Up @@ -277,9 +275,7 @@ public PreparedStatement prepareStatement(final String sql) throws SQLException
*/
public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency)
throws SQLException {
// for now resultSetType and resultSetConcurrency are ignored
// TODO: fix
return prepareStatement(sql);
return internalPrepareStatement(sql, resultSetType);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/mariadb/jdbc/MariaDbStatement.java
Expand Up @@ -998,7 +998,7 @@ public int getResultSetConcurrency() throws SQLException {
* @since 1.2
*/
public int getResultSetType() throws SQLException {
return ResultSet.TYPE_SCROLL_INSENSITIVE;
return resultSetScrollType;
}

/**
Expand Down
Expand Up @@ -126,7 +126,7 @@ public ReadInitialConnectPacket(final ReadPacketFetcher packetFetcher) throws IO
* (see comments about MARIADB_RPL_HACK_PREFIX)
*/
if ((serverCapabilities4FirstBytes & MariaDbServerCapabilities.PLUGIN_AUTH) != 0) {
pluginName = buffer.readString(Charset.forName("ASCII"));
pluginName = buffer.readString(StandardCharsets.US_ASCII);
if (serverVersion.startsWith(MARIADB_RPL_HACK_PREFIX)) {
serverCapabilities = (serverCapabilities4FirstBytes & 0xffffffffL) + (mariaDbAdditionalCapacities << 32);
serverVersion = serverVersion.substring(MARIADB_RPL_HACK_PREFIX.length());
Expand Down
9 changes: 7 additions & 2 deletions src/test/java/org/mariadb/jdbc/ConnectionTest.java
Expand Up @@ -36,8 +36,13 @@ public void testAccessDeniedErrorCode() throws SQLException {
DriverManager.getConnection("jdbc:mysql://" + hostname + ":" + port + "/" + database + "?user=foo");
Assert.fail();
} catch (SQLException e) {
Assert.assertTrue("28000".equals(e.getSQLState()));
Assert.assertEquals(1045, e.getErrorCode());
if (1524 == e.getErrorCode()) {
//GSSAPI plugin not loaded
Assert.assertTrue("HY000".equals(e.getSQLState()));
} else {
Assert.assertTrue("28000".equals(e.getSQLState()));
Assert.assertEquals(1045, e.getErrorCode());
}
}
}

Expand Down
68 changes: 68 additions & 0 deletions src/test/java/org/mariadb/jdbc/ScrollTypeTest.java
@@ -0,0 +1,68 @@
package org.mariadb.jdbc;

import org.junit.BeforeClass;
import org.junit.Test;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import static org.junit.Assert.fail;

public class ScrollTypeTest extends BaseTest {

@BeforeClass()
public static void initClass() throws SQLException {
createTable("resultsSetReadingTest", "id int not null primary key auto_increment, test int");
sharedConnection.createStatement().execute("INSERT INTO resultsSetReadingTest (test) values (1), (2), (3)");
}

@Test
public void scrollInsensitivePrepareStmt() throws SQLException {
try (PreparedStatement stmt = sharedConnection.prepareStatement("SELECT * FROM resultsSetReadingTest",
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)) {
stmt.setFetchSize(2);
try (ResultSet rs = stmt.executeQuery()) {
rs.beforeFirst();
} catch (SQLException sqle) {
fail("beforeFirst() should work on a TYPE_SCROLL_INSENSITIVE result set");
}
}
}

@Test
public void scrollInsensitiveStmt() throws SQLException {
try (Statement stmt = sharedConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)) {
stmt.setFetchSize(2);
try (ResultSet rs = stmt.executeQuery("SELECT * FROM resultsSetReadingTest")) {
rs.beforeFirst();
} catch (SQLException sqle) {
fail("beforeFirst() should work on a TYPE_SCROLL_INSENSITIVE result set");
}
}
}

@Test(expected = SQLException.class)
public void scrollForwardOnlyPrepareStmt() throws SQLException {
try (PreparedStatement stmt = sharedConnection.prepareStatement("SELECT * FROM resultsSetReadingTest",
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) {
stmt.setFetchSize(2);
try (ResultSet rs = stmt.executeQuery()) {
rs.beforeFirst();
fail("beforeFirst() shouldn't work on a TYPE_FORWARD_ONLY result set");
}
}
}

@Test(expected = SQLException.class)
public void scrollForwardOnlyStmt() throws SQLException {
try (Statement stmt = sharedConnection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) {
stmt.setFetchSize(2);
try (ResultSet rs = stmt.executeQuery("SELECT * FROM resultsSetReadingTest")) {
rs.beforeFirst();
fail("beforeFirst() shouldn't work on a TYPE_FORWARD_ONLY result set");
}
}
}
}

1 comment on commit ab05206

@leonardlin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome, thanks!

Please sign in to comment.