Skip to content

Commit

Permalink
[misc] code simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed Dec 14, 2016
1 parent cf35e30 commit 16c8313
Show file tree
Hide file tree
Showing 30 changed files with 793 additions and 493 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 @@ -63,7 +63,7 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS
import java.util.*;


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 @@ -92,9 +92,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 @@ -547,7 +547,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 @@ -313,7 +313,7 @@ public PrintWriter getLogWriter() throws SQLException {
* @since 1.4
*/
public void setLogWriter(final PrintWriter out) throws SQLException {

//not implemented
}

/**
Expand All @@ -327,7 +327,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 @@ -342,6 +342,7 @@ public int getLoginTimeout() throws SQLException {
*/
@Override
public void setLoginTimeout(final int seconds) throws SQLException {
urlParser.getOptions().connectTimeout = seconds;
}

/**
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/org/mariadb/jdbc/MariaDbProcedureStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,15 @@ public class MariaDbProcedureStatement extends AbstractCallableProcedureStatemen
/**
* Specific implementation of CallableStatement to handle function call, represent by call like
* {?= call procedure-name[(arg1,arg2, ...)]}.
* @param query query
* @param connection current connection
*
* @param query query
* @param connection current connection
* @param procedureName procedure name
* @param database database
* @param arguments function args
* @param database database
* @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 All @@ -84,7 +83,7 @@ public MariaDbProcedureStatement(String query, MariaDbConnection connection,

private void setParamsAccordingToSetArguments() throws SQLException {
params = new ArrayList<>(this.parameterCount);
for (int index = 0 ; index < this.parameterCount; index++) {
for (int index = 0; index < this.parameterCount; index++) {
params.add(new CallParameter());
}
}
Expand Down Expand Up @@ -128,8 +127,8 @@ public MariaDbProcedureStatement clone() throws CloneNotSupportedException {
* @return either (1) the row count for SQL Data Manipulation Language (DML) statements or (2) 0 for SQL statements
* that return nothing
* @throws SQLException if a database access error occurs; this method is called on a closed
* <code>PreparedStatement</code> or the SQL statement returns a
* <code>ResultSet</code> object
* <code>PreparedStatement</code> or the SQL statement returns a
* <code>ResultSet</code> object
*/
public int executeUpdate() throws SQLException {
validAllParameters();
Expand Down Expand Up @@ -177,13 +176,14 @@ public boolean execute() throws SQLException {

/**
* Valid that all parameters are set.
*
* @throws SQLException if set parameters is not right
*/
private void validAllParameters() throws SQLException {

setInputOutputParameterMap();
//Set value for OUT parameters
for (int index = 0; index < params.size() ; index++) {
for (int index = 0; index < params.size(); index++) {
if (!params.get(index).isInput) {
super.setParameter(index + 1, new NullParameter());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,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 @@ -1171,10 +1171,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 @@ -173,11 +173,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 FailoverLoop(ScheduledExecutorService scheduler) {
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 @@ -139,13 +138,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 @@ -52,9 +52,9 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS
import org.mariadb.jdbc.MariaDbDatabaseMetaData;
import org.mariadb.jdbc.internal.MariaDbServerCapabilities;
import org.mariadb.jdbc.internal.protocol.authentication.DefaultAuthenticationProvider;
import org.mariadb.jdbc.internal.stream.PacketOutputStream;
import org.mariadb.jdbc.internal.util.PidFactory;
import org.mariadb.jdbc.internal.util.Utils;
import org.mariadb.jdbc.internal.stream.PacketOutputStream;
import org.mariadb.jdbc.internal.util.constant.Version;

import java.io.IOException;
Expand Down Expand Up @@ -87,32 +87,31 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS
* databasename: name of schema to use initially
*/
public class SendHandshakeResponsePacket implements InterfaceSendPacket {
private final byte serverLanguage;
private byte packetSeq;
private String username;
private String password;
private byte[] seed;
private long clientCapabilities;
private final byte serverLanguage;
private String database;
private String plugin;
private String connectionAttributes;
private long serverThreadId;

private byte[] connectionAttributesArray;
private int connectionAttributesPosition;

/**
* Initialisation of parameters.
* @param username username
* @param password user password
* @param database initial database connection
* @param clientCapabilities capabilities
* @param serverLanguage serverlanguage
* @param seed seed
* @param packetSeq stream sequence
* @param plugin authentication plugin name
*
* @param username username
* @param password user password
* @param database initial database connection
* @param clientCapabilities capabilities
* @param serverLanguage serverlanguage
* @param seed seed
* @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 @@ -122,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 @@ -133,11 +131,11 @@ public SendHandshakeResponsePacket(final String username,
this.database = database;
this.plugin = plugin;
this.connectionAttributes = connectionAttributes;
this.serverThreadId = serverThreadId;
}

/**
* Send authentication stream.
*
* @param os database socket
* @throws IOException if any connection error occur
*/
Expand All @@ -147,14 +145,14 @@ public void send(final OutputStream os) throws IOException {
final byte[] authData;
switch (plugin) {
case "": //CONJ-274 : permit connection mysql 5.1 db
case DefaultAuthenticationProvider.MYSQL_NATIVE_PASSWORD :
case DefaultAuthenticationProvider.MYSQL_NATIVE_PASSWORD:
try {
authData = Utils.encryptPassword(password, seed);
break;
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Could not use SHA-1, failing", e);
}
case DefaultAuthenticationProvider.MYSQL_CLEAR_PASSWORD :
case DefaultAuthenticationProvider.MYSQL_CLEAR_PASSWORD:
authData = password.getBytes();
break;
default:
Expand All @@ -169,7 +167,7 @@ public void send(final OutputStream os) throws IOException {
.writeInt((int) (clientCapabilities >> 32)); //Maria extended flag

if (username == null || "".equals(username)) username = System.getProperty("user.name"); //permit SSO

writeBuffer.writeString(username) //strlen username
.writeByte((byte) 0); //1

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 @@ -530,8 +530,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 @@ -185,14 +185,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
Loading

0 comments on commit 16c8313

Please sign in to comment.