Skip to content

Commit

Permalink
[misc] code simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed Nov 24, 2016
1 parent 21f4ff3 commit d81eec3
Show file tree
Hide file tree
Showing 30 changed files with 656 additions and 634 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS
import java.sql.*;
import java.util.Calendar;

public abstract class AbstractMariaDbPrepareStatement extends MariaDbStatement implements PreparedStatement, Cloneable {
public abstract class AbstractPrepareStatement extends MariaDbStatement implements PreparedStatement, Cloneable {
protected boolean useFractionalSeconds;
protected boolean hasLongData = false;

public AbstractMariaDbPrepareStatement(MariaDbConnection connection, int resultSetScrollType) {
public AbstractPrepareStatement(MariaDbConnection connection, int resultSetScrollType) {
super(connection, resultSetScrollType);
}

Expand All @@ -76,8 +76,8 @@ public AbstractMariaDbPrepareStatement(MariaDbConnection connection, int resultS

protected abstract Calendar cal();

public AbstractMariaDbPrepareStatement clone() throws CloneNotSupportedException {
return (AbstractMariaDbPrepareStatement) super.clone();
public AbstractPrepareStatement clone() throws CloneNotSupportedException {
return (AbstractPrepareStatement) super.clone();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS
import java.util.List;


public class MariaDbClientPreparedStatement extends AbstractMariaDbPrepareStatement implements Cloneable {
public class MariaDbClientPreparedStatement extends AbstractPrepareStatement implements Cloneable {
private static Logger logger = LoggerFactory.getLogger(MariaDbClientPreparedStatement.class);
private String sqlQuery;
private ClientPrepareResult prepareResult;
Expand Down Expand Up @@ -95,9 +95,9 @@ public MariaDbClientPreparedStatement(MariaDbConnection connection, String sql,

if (prepareResult == null) {
if (options.rewriteBatchedStatements) {
prepareResult = ClientPrepareResult.createRewritableParts(sqlQuery, connection.noBackslashEscapes);
prepareResult = ClientPrepareResult.rewritableParts(sqlQuery, connection.noBackslashEscapes);
} else {
prepareResult = ClientPrepareResult.createParameterParts(sqlQuery, connection.noBackslashEscapes);
prepareResult = ClientPrepareResult.parameterParts(sqlQuery, connection.noBackslashEscapes);
}
if (options.cachePrepStmts && sql.length() < 1024) {
String key = new StringBuilder(this.protocol.getDatabase()).append("-").append(sqlQuery).toString();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/mariadb/jdbc/MariaDbConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ private CallableStatement createNewCallableStatement(String query, String proced
if (isFunction) {
return new MariaDbFunctionStatement(this, database, databaseAndProcedure, arguments);
} else {
return new MariaDbProcedureStatement(query, this, procedureName, database, arguments);
return new MariaDbProcedureStatement(query, this, procedureName, database);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/mariadb/jdbc/MariaDbDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ public PrintWriter getLogWriter() throws SQLException {
* @since 1.4
*/
public void setLogWriter(final PrintWriter out) throws SQLException {

//not implemented
}

/**
Expand All @@ -328,7 +328,7 @@ public void setLogWriter(final PrintWriter out) throws SQLException {
* @since 1.4
*/
public int getLoginTimeout() throws SQLException {
return 0;
return urlParser.getOptions().connectTimeout == null ? 0 : urlParser.getOptions().connectTimeout;
}

/**
Expand All @@ -343,6 +343,7 @@ public int getLoginTimeout() throws SQLException {
*/
@Override
public void setLoginTimeout(final int seconds) throws SQLException {
urlParser.getOptions().connectTimeout = seconds;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,10 @@ public class MariaDbProcedureStatement extends AbstractCallableProcedureStatemen
* @param connection current connection
* @param procedureName procedure name
* @param database database
* @param arguments function args
* @throws SQLException exception
*/
public MariaDbProcedureStatement(String query, MariaDbConnection connection,
String procedureName, String database,
String arguments) throws SQLException {
String procedureName, String database) throws SQLException {
super(connection, query, ResultSet.TYPE_FORWARD_ONLY);
this.parameterMetadata = new CallableParameterMetaData(connection, database, procedureName, false);
setParamsAccordingToSetArguments();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS
import java.sql.*;
import java.util.*;

public class MariaDbServerPreparedStatement extends AbstractMariaDbPrepareStatement implements Cloneable {
public class MariaDbServerPreparedStatement extends AbstractPrepareStatement implements Cloneable {
private static Logger logger = LoggerFactory.getLogger(MariaDbServerPreparedStatement.class);

String sql;
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/org/mariadb/jdbc/MariaDbStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -1212,10 +1212,11 @@ public boolean isCloseOnCompletion() throws SQLException {
* @throws SQLException if close has error
*/
public void checkCloseOnCompletion(ResultSet resultSet) throws SQLException {
if (mustCloseOnCompletion && !closed && executionResult != null) {
if (resultSet.equals(executionResult.getResultSet())) {
close();
}
if (mustCloseOnCompletion
&& !closed
&& executionResult != null
&& resultSet.equals(executionResult.getResultSet())) {
close();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,9 @@ public void reconnectFailedConnection(SearchFilter searchFilter) throws QueryExc
loopAddress.add(masterProtocol.getHostAddress());
}

if (!isSecondaryHostFail()) {
if (secondaryProtocol != null) {
loopAddress.remove(secondaryProtocol.getHostAddress());
loopAddress.add(secondaryProtocol.getHostAddress());
}
if (!isSecondaryHostFail() && secondaryProtocol != null) {
loopAddress.remove(secondaryProtocol.getHostAddress());
loopAddress.add(secondaryProtocol.getHostAddress());
}

if (urlParser.getHostAddresses().size() <= 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,21 +76,19 @@ public static void removeListener(Listener listener) {
protected void doRun() {
Listener listener;
while (!isUnschedule() && (listener = queue.poll()) != null) {
if (!listener.isExplicitClosed() && listener.hasHostFail()) {
if (listener.canRetryFailLoop()) {
try {
SearchFilter filter = listener.getFilterForFailedHost();
filter.setFailoverLoop(true);
listener.reconnectFailedConnection(filter);
if (listener.hasHostFail() && !listener.isExplicitClosed()) {
queue.add(listener);
}

//reconnection done !
} catch (Exception e) {
//FailoverLoop search connection failed
if (!listener.isExplicitClosed() && listener.hasHostFail() && listener.canRetryFailLoop()) {
try {
SearchFilter filter = listener.getFilterForFailedHost();
filter.setFailoverLoop(true);
listener.reconnectFailedConnection(filter);
if (listener.hasHostFail() && !listener.isExplicitClosed()) {
queue.add(listener);
}

//reconnection done !
} catch (Exception e) {
//FailoverLoop search connection failed
queue.add(listener);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ public class ColumnInformation {
Buffer buffer;
private short charsetNumber;
private long length;
private long fixlength;
private MariaDbType type;
private byte decimals;
private short flags;
Expand Down Expand Up @@ -140,13 +139,13 @@ public ColumnInformation(Buffer buffer) {
buffer.skipLengthEncodedBytes(); /* original table */
buffer.skipLengthEncodedBytes(); /* name */
buffer.skipLengthEncodedBytes(); /* org_name */
buffer.readByte(); //fixlength field
lazyPositionFromEnd = buffer.limit - buffer.position;
} else {
//permit to avoid reading the 6th String encode data, almost never needed
buffer.position = buffer.limit - lazyPositionFromEnd;
}

fixlength = buffer.readByte();
charsetNumber = buffer.readShort();
length = buffer.readInt();
type = MariaDbType.fromServer(buffer.readByte() & 0xff);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ public class SendHandshakeResponsePacket implements InterfaceSendPacket {
private String database;
private String plugin;
private String connectionAttributes;
private long serverThreadId;

private byte[] connectionAttributesArray;
private int connectionAttributesPosition;
Expand All @@ -113,7 +112,6 @@ public class SendHandshakeResponsePacket implements InterfaceSendPacket {
* @param packetSeq stream sequence
* @param plugin authentication plugin name
* @param connectionAttributes connection attributes option
* @param serverThreadId threadId;
*/
public SendHandshakeResponsePacket(final String username,
final String password,
Expand All @@ -123,8 +121,7 @@ public SendHandshakeResponsePacket(final String username,
final byte[] seed,
byte packetSeq,
String plugin,
String connectionAttributes,
long serverThreadId) {
String connectionAttributes) {
this.packetSeq = packetSeq;
this.username = username;
this.password = password;
Expand All @@ -134,7 +131,6 @@ public SendHandshakeResponsePacket(final String username,
this.database = database;
this.plugin = plugin;
this.connectionAttributes = connectionAttributes;
this.serverThreadId = serverThreadId;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,12 @@ public void ancestorAdded(AncestorEvent ancestorEvent) {

@Override
public void ancestorMoved(AncestorEvent ancestorEvent) {
//do nothing
}

@Override
public void ancestorRemoved(AncestorEvent ancestorEvent) {
//do nothing
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -535,8 +535,7 @@ private void authentication(byte exchangeCharset, long clientCapabilities, byte[
seed,
packetSeq,
plugin,
options.connectionAttributes,
serverThreadId);
options.connectionAttributes);
cap.send(writer);
Buffer buffer = packetFetcher.getPacket();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,12 @@ public PrepareResult executeBatch() throws QueryException {
if (binaryProtocol) {
if (readPrepareStmtResult) {
parameterTypeHeader = new MariaDbType[paramCount];
if (prepareResult == null) {
if (protocol.getOptions().cachePrepStmts) {
String key = new StringBuilder(protocol.getDatabase()).append("-").append(sql).toString();
prepareResult = protocol.prepareStatementCache().get(key);
if (prepareResult != null && !((ServerPrepareResult) prepareResult).incrementShareCounter()) {
//in cache but been de-allocated
prepareResult = null;
}
if (prepareResult == null && protocol.getOptions().cachePrepStmts) {
String key = new StringBuilder(protocol.getDatabase()).append("-").append(sql).toString();
prepareResult = protocol.prepareStatementCache().get(key);
if (prepareResult != null && !((ServerPrepareResult) prepareResult).incrementShareCounter()) {
//in cache but been de-allocated
prepareResult = null;
}
}
statementId = (prepareResult == null) ? -1 : ((ServerPrepareResult) prepareResult).getStatementId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS


public class AbstractQueryProtocol extends AbstractConnectProtocol implements Protocol {
private static Logger logger = LoggerFactory.getLogger(AbstractQueryProtocol.class);

private int transactionIsolationLevel = 0;
private InputStream localInfileInputStream;
Expand Down Expand Up @@ -782,21 +781,6 @@ private void sendLocalFile(ExecutionResult executionResult, String fileName) thr
getResult(executionResult, ResultSet.TYPE_FORWARD_ONLY, false, true);
}

private ServerPrepareResult getPrepareResultFromCacheIfNeeded(ServerPrepareResult serverPrepareResult, String sql)
throws UnsupportedEncodingException {
if (serverPrepareResult == null) {
if (options.cachePrepStmts) {
String key = new StringBuilder(database).append("-").append(sql).toString();
serverPrepareResult = serverPrepareStatementCache.get(key);
if (serverPrepareResult != null && !serverPrepareResult.incrementShareCounter()) {
//in cache but been de-allocated
return null;
}
}
}
return serverPrepareResult;
}

@Override
public boolean getAutocommit() {
lock.lock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ public MariaDbX509TrustManager(Options options) throws QueryException {
*/
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String string) throws CertificateException {
if (trustManager == null) return;
trustManager.checkClientTrusted(x509Certificates, string);
}

/**
Expand All @@ -196,9 +198,7 @@ public void checkClientTrusted(X509Certificate[] x509Certificates, String string
*/
@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String string) throws CertificateException {
if (trustManager == null) {
return;
}
if (trustManager == null) return;
trustManager.checkServerTrusted(x509Certificates, string);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,20 +352,21 @@ public void fetchAllStreaming() throws SQLException {
while (readNextValue(resultSet)) {
//fetch all results
}

resultSetSize = resultSet.size();

//retrieve other results if needed
if (protocolTmp.hasMoreResults()) {
if (this.statement != null) {
this.statement.getMoreResults();
}
if (protocolTmp.hasMoreResults() && this.statement != null) {
this.statement.getMoreResults();
}

} catch (IOException ioexception) {
throw new QueryException("Could not close resultset : " + ioexception.getMessage(), -1, CONNECTION_EXCEPTION, ioexception);
}
} catch (QueryException queryException) {
ExceptionMapper.throwException(queryException, null, this.statement);
}

dataFetchTime++;
streaming = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,27 +143,35 @@ public OutputStream getOutputStream() {
}

public void setTcpNoDelay(boolean bool) {
//do nothing
}

public void setKeepAlive(boolean bool) {
//do nothing
}

public void setReceiveBufferSize(int size) {
//do nothing
}

public void setSendBufferSize(int size) {
//do nothing
}

public void setSoLinger(boolean bool, int value) {
//do nothing
}

@Override
public void setSoTimeout(int timeout) {
//do nothing
}

public void shutdownInput() {
//do nothing
}

public void shutdownOutput() {
//do nothing
}
}

0 comments on commit d81eec3

Please sign in to comment.