Skip to content

Commit

Permalink
[misc] deprecating Connection.getUsername(), Connection.getPort() and…
Browse files Browse the repository at this point in the history
… Connection.getHostname()

and adding connection test coverage
  • Loading branch information
rusher committed Jan 24, 2019
1 parent f445006 commit 7e7ad38
Show file tree
Hide file tree
Showing 10 changed files with 690 additions and 295 deletions.
110 changes: 44 additions & 66 deletions src/main/java/org/mariadb/jdbc/MariaDbConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -373,15 +373,13 @@ public PreparedStatement prepareStatement(final String sql, final int resultSetT
* @see ResultSet
*/
public PreparedStatement prepareStatement(final String sql,
final int resultSetType,
final int resultSetConcurrency,
final int resultSetHoldability) throws SQLException {
if (resultSetConcurrency != ResultSet.CONCUR_READ_ONLY) {
throw ExceptionMapper.getFeatureNotSupportedException("Only read-only result sets allowed");
}
//TODO : implement parameters
// resultSetType is ignored since we always are scroll insensitive
return prepareStatement(sql);
final int resultSetType,
final int resultSetConcurrency,
final int resultSetHoldability) throws SQLException {
return internalPrepareStatement(sql,
resultSetType,
resultSetConcurrency,
Statement.NO_GENERATED_KEYS);
}

/**
Expand Down Expand Up @@ -617,7 +615,7 @@ public CallableStatement prepareCall(final String sql, final int resultSetType,
String procedureName = matcher.group(13);
String arguments = matcher.group(16);
if (database == null && sessionStateAware) {
database = getDatabase();
database = protocol.getDatabase();
}

if (database != null && options.cacheCallableStmts) {
Expand Down Expand Up @@ -723,7 +721,7 @@ public String nativeSQL(final String sql) throws SQLException {
* @throws SQLException if there is an error
*/
public boolean getAutoCommit() throws SQLException {
return protocol != null && protocol.getAutocommit();
return protocol.getAutocommit();
}

/**
Expand Down Expand Up @@ -863,7 +861,7 @@ public void setReadOnly(final boolean readOnly) throws SQLException {
stateFlag |= ConnectionState.STATE_READ_ONLY;
protocol.setReadonly(readOnly);
} catch (SQLException e) {
ExceptionMapper.throwException(e, this, null);
throw ExceptionMapper.getException(e, this, null, false);
}
}

Expand Down Expand Up @@ -899,7 +897,7 @@ public void setCatalog(final String catalog) throws SQLException {
stateFlag |= ConnectionState.STATE_DATABASE;
protocol.setCatalog(catalog);
} catch (SQLException e) {
ExceptionMapper.throwException(e, this, null);
throw ExceptionMapper.getException(e, this, null, false);
}
}

Expand All @@ -923,47 +921,35 @@ public boolean versionGreaterOrEqual(int major, int minor, int patch) {
* @see #setTransactionIsolation
*/
public int getTransactionIsolation() throws SQLException {
try (Statement stmt = createStatement()) {
String sql = "SELECT @@tx_isolation";
Statement stmt = createStatement();
String sql = "SELECT @@tx_isolation";

if (!protocol.isServerMariaDb()) {
if ((protocol.getMajorServerVersion() >= 8 && protocol.versionGreaterOrEqual(8, 0, 3))
|| (protocol.getMajorServerVersion() < 8 && protocol.versionGreaterOrEqual(5, 7, 20))) {
sql = "SELECT @@transaction_isolation";
}
if (!protocol.isServerMariaDb()) {
if ((protocol.getMajorServerVersion() >= 8 && protocol.versionGreaterOrEqual(8, 0, 3))
|| (protocol.getMajorServerVersion() < 8 && protocol.versionGreaterOrEqual(5, 7, 20))) {
sql = "SELECT @@transaction_isolation";
}
}

try (ResultSet rs = stmt.executeQuery(sql)) {
if (rs.next()) {
final String response = rs.getString(1);
switch (response) {
case "REPEATABLE-READ":
return Connection.TRANSACTION_REPEATABLE_READ;

case "READ-UNCOMMITTED":
return Connection.TRANSACTION_READ_UNCOMMITTED;

case "READ-COMMITTED":
return Connection.TRANSACTION_READ_COMMITTED;

case "SERIALIZABLE":
return Connection.TRANSACTION_SERIALIZABLE;

default:
if (!protocol.isServerMariaDb()) {
if ((protocol.getMajorServerVersion() >= 8 && protocol
.versionGreaterOrEqual(8, 0, 3))
|| (protocol.getMajorServerVersion() < 8 && protocol
.versionGreaterOrEqual(5, 7, 20))) {
throw ExceptionMapper
.getSqlException("Could not get transaction isolation level: "
+ "Invalid @@transaction_isolation value \"" + response + "\"");
}
}
throw ExceptionMapper.getSqlException("Could not get transaction isolation level: "
+ "Invalid @@tx_isolation value \"" + response + "\"");
}
}
ResultSet rs = stmt.executeQuery(sql);
if (rs.next()) {
final String response = rs.getString(1);
switch (response) {
case "REPEATABLE-READ":
return Connection.TRANSACTION_REPEATABLE_READ;

case "READ-UNCOMMITTED":
return Connection.TRANSACTION_READ_UNCOMMITTED;

case "READ-COMMITTED":
return Connection.TRANSACTION_READ_COMMITTED;

case "SERIALIZABLE":
return Connection.TRANSACTION_SERIALIZABLE;

default:
throw ExceptionMapper.getSqlException("Could not get transaction isolation level: "
+ "Invalid value \"" + response + "\"");
}
}
throw ExceptionMapper.getSqlException("Could not get transaction isolation level");
Expand Down Expand Up @@ -994,7 +980,7 @@ public void setTransactionIsolation(final int level) throws SQLException {
stateFlag |= ConnectionState.STATE_TRANSACTION_ISOLATION;
protocol.setTransactionIsolation(level);
} catch (SQLException e) {
ExceptionMapper.throwException(e, this, null);
throw ExceptionMapper.getException(e, this, null, false);
}
}

Expand Down Expand Up @@ -1074,7 +1060,7 @@ public void reenableWarnings() {
* @since 1.2
*/
public Map<String, Class<?>> getTypeMap() {
return null;
return new HashMap<>();
}

/**
Expand Down Expand Up @@ -1590,11 +1576,9 @@ public <T> T unwrap(final Class<T> iface) throws SQLException {
* @param iface a Class defining an interface.
* @return true if this implements the interface or directly or indirectly wraps an object that
* does.
* @throws SQLException if an error occurs while determining whether this is a wrapper for an
* object with the given interface.
* @since 1.6
*/
public boolean isWrapperFor(final Class<?> iface) throws SQLException {
public boolean isWrapperFor(final Class<?> iface) {
return iface.isInstance(this);
}

Expand All @@ -1603,6 +1587,7 @@ public boolean isWrapperFor(final Class<?> iface) throws SQLException {
*
* @return the username.
*/
@Deprecated
public String getUsername() {
return protocol.getUsername();
}
Expand All @@ -1612,6 +1597,7 @@ public String getUsername() {
*
* @return the hostname.
*/
@Deprecated
public String getHostname() {
return protocol.getHost();
}
Expand All @@ -1621,19 +1607,11 @@ public String getHostname() {
*
* @return the port
*/
@Deprecated
public int getPort() {
return protocol.getPort();
}

/**
* returns the database.
*
* @return the database
*/
private String getDatabase() {
return protocol.getDatabase();
}

protected boolean getPinGlobalTxToPhysicalConnection() {
return protocol.getPinGlobalTxToPhysicalConnection();
}
Expand Down Expand Up @@ -1746,7 +1724,7 @@ public void setNetworkTimeout(Executor executor, final int milliseconds) throws
}

public long getServerThreadId() {
return (protocol != null) ? protocol.getServerThreadId() : -1;
return protocol.getServerThreadId();
}

public boolean canUseServerTimeout() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ protected void setCmdInformation(CmdInformation cmdInformation) {

/**
* Indicate that command / batch is finished, so set current resultSet if needed.
* @return true id has cmdInformation
*/
public boolean commandEnd() {
if (cmdInformation != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public class ComQuery {
* @param out outputStream
* @param clientPrepareResult clientPrepareResult
* @param parameters parameter
* @param queryTimeout query timeout
* @throws IOException if connection fail
*/
public static void sendSubCmd(final PacketOutputStream out,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ public void executeQuery(boolean mustExecuteOnMaster, Results results, final Str
getResult(results);

} catch (SQLException sqlException) {
if ("70100".equals(sqlException.getSQLState()) && 1927 == sqlException.getErrorCode()) {
throw handleIoException(sqlException);
}
throw logQuery.exceptionWithQuery(sql, sqlException, explicitClosed);
} catch (IOException e) {
throw handleIoException(e);
Expand Down Expand Up @@ -1851,7 +1854,7 @@ public void resetStateAfterFailover(long maxRows, int transactionIsolationLevel,
* @param initialException initial Io error
* @return the resulting error to return to client.
*/
public SQLException handleIoException(IOException initialException) {
public SQLException handleIoException(Exception initialException) {
boolean mustReconnect;
boolean driverPreventError = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ void resetStateAfterFailover(long maxRows, int transactionIsolationLevel, String

boolean isServerMariaDb() throws SQLException;

SQLException handleIoException(IOException initialException);
SQLException handleIoException(Exception initialException);

PacketInputStream getReader();

Expand Down
9 changes: 8 additions & 1 deletion src/test/java/org/mariadb/jdbc/BlobTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ public void testPosition() throws SQLException {
assertEquals(11, blob.position(pattern, 1));
pattern = new byte[]{1, 2};
assertEquals(1, blob.position(pattern, 1));

}

@Test(expected = SQLException.class)
Expand Down Expand Up @@ -600,4 +599,12 @@ private void blobDeserializationFilterInternal(boolean addFilter, int filterSize
}
}

@Test
public void connectionBlob() throws SQLException {
Blob blob = sharedConnection.createBlob();
assertArrayEquals(new byte[]{0}, blob.getBytes(1, 1));
blob.setBytes(5, new byte[]{1, 2, 3, 4, 5, 6});
assertArrayEquals(new byte[]{0, 0, 0, 0, 1, 2, 3, 4, 5}, blob.getBytes(1, 9));
}

}
Loading

0 comments on commit 7e7ad38

Please sign in to comment.