Skip to content

Commit

Permalink
[misc] test coverage addition
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed Apr 7, 2021
1 parent b42a595 commit 1003c60
Show file tree
Hide file tree
Showing 32 changed files with 901 additions and 215 deletions.
13 changes: 10 additions & 3 deletions src/main/java/org/mariadb/jdbc/ClientPreparedStatement.java
Expand Up @@ -82,6 +82,7 @@ private List<Completion> executeInternalPreparedBatch() throws SQLException {
checkNotClosed();
long serverCapabilities = con.getContext().getServerCapabilities();
if (autoGeneratedKeys != Statement.RETURN_GENERATED_KEYS
&& batchParameters.size() > 1
&& (serverCapabilities & Capabilities.MARIADB_CLIENT_STMT_BULK_OPERATIONS) > 0
&& con.getContext().getConf().useBulkStmts()) {
return executeBatchBulk();
Expand Down Expand Up @@ -406,9 +407,15 @@ public int[] executeBatch() throws SQLException {
try {
List<Completion> res = executeInternalPreparedBatch();
results = res;
int[] updates = new int[res.size()];
for (int i = 0; i < res.size(); i++) {
updates[i] = (int) ((OkPacket) res.get(i)).getAffectedRows();
int[] updates = new int[batchParameters.size()];
if (res.size() != batchParameters.size()) {
for (int i = 0; i < batchParameters.size(); i++) {
updates[i] = Statement.SUCCESS_NO_INFO;
}
} else {
for (int i = 0; i < Math.min(res.size(), batchParameters.size()); i++) {
updates[i] = (int) ((OkPacket) res.get(i)).getAffectedRows();
}
}
currResult = results.remove(0);
return updates;
Expand Down
219 changes: 204 additions & 15 deletions src/main/java/org/mariadb/jdbc/Configuration.java
Expand Up @@ -62,7 +62,7 @@
* jdbc:mariadb://address=(type=master)(host=master1),address=(port=3307)(type=slave)(host=slave1)/database?user=greg&password=pass}
* <br>
*/
public class Configuration implements Cloneable {
public class Configuration {

private static final Pattern URL_PARAMETER =
Pattern.compile("(\\/([^\\?]*))?(\\?(.+))*", Pattern.DOTALL);
Expand Down Expand Up @@ -155,6 +155,132 @@ public class Configuration implements Cloneable {

private Configuration() {}

private Configuration(
String user,
String password,
String database,
List<HostAddress> addresses,
HaMode haMode,
Properties nonMappedOptions,
String timezone,
boolean autocommit,
TransactionIsolation transactionIsolation,
int defaultFetchSize,
int maxQuerySizeToLog,
boolean pinGlobalTxToPhysicalConnection,
String geometryDefaultType,
String socketFactory,
Integer connectTimeout,
String pipe,
String localSocket,
boolean tcpKeepAlive,
boolean tcpAbortiveClose,
String localSocketAddress,
int socketTimeout,
boolean useReadAheadInput,
String tlsSocketType,
SslMode sslMode,
String serverSslCert,
String keyStore,
String keyStorePassword,
String keyStoreType,
String enabledSslCipherSuites,
String enabledSslProtocolSuites,
boolean allowMultiQueries,
boolean allowLocalInfile,
boolean useCompression,
boolean useAffectedRows,
boolean useBulkStmts,
boolean cachePrepStmts,
int prepStmtCacheSize,
boolean useServerPrepStmts,
CredentialPlugin credentialType,
String sessionVariables,
String connectionAttributes,
String servicePrincipalName,
boolean blankTableNameMeta,
boolean tinyInt1isBit,
boolean yearIsDateType,
boolean dumpQueriesOnException,
boolean includeInnodbStatusInDeadlockExceptions,
boolean includeThreadDumpInDeadlockExceptions,
int retriesAllDown,
String galeraAllowedState,
boolean transactionReplay,
boolean pool,
String poolName,
int maxPoolSize,
int minPoolSize,
Integer maxIdleTime,
boolean registerJmxPool,
int poolValidMinDelay,
boolean useResetConnection,
String serverRsaPublicKeyFile,
boolean allowPublicKeyRetrieval) {
this.user = user;
this.password = password;
this.database = database;
this.addresses = addresses;
this.haMode = haMode;
this.nonMappedOptions = nonMappedOptions;
this.timezone = timezone;
this.autocommit = autocommit;
this.transactionIsolation = transactionIsolation;
this.defaultFetchSize = defaultFetchSize;
this.maxQuerySizeToLog = maxQuerySizeToLog;
this.pinGlobalTxToPhysicalConnection = pinGlobalTxToPhysicalConnection;
this.geometryDefaultType = geometryDefaultType;
this.socketFactory = socketFactory;
this.connectTimeout = connectTimeout;
this.pipe = pipe;
this.localSocket = localSocket;
this.tcpKeepAlive = tcpKeepAlive;
this.tcpAbortiveClose = tcpAbortiveClose;
this.localSocketAddress = localSocketAddress;
this.socketTimeout = socketTimeout;
this.useReadAheadInput = useReadAheadInput;
this.tlsSocketType = tlsSocketType;
this.sslMode = sslMode;
this.serverSslCert = serverSslCert;
this.keyStore = keyStore;
this.keyStorePassword = keyStorePassword;
this.keyStoreType = keyStoreType;
this.enabledSslCipherSuites = enabledSslCipherSuites;
this.enabledSslProtocolSuites = enabledSslProtocolSuites;
this.allowMultiQueries = allowMultiQueries;
this.allowLocalInfile = allowLocalInfile;
this.useCompression = useCompression;
this.useAffectedRows = useAffectedRows;
this.useBulkStmts = useBulkStmts;
this.cachePrepStmts = cachePrepStmts;
this.prepStmtCacheSize = prepStmtCacheSize;
this.useServerPrepStmts = useServerPrepStmts;
this.credentialType = credentialType;
this.sessionVariables = sessionVariables;
this.connectionAttributes = connectionAttributes;
this.servicePrincipalName = servicePrincipalName;
this.blankTableNameMeta = blankTableNameMeta;
this.tinyInt1isBit = tinyInt1isBit;
this.yearIsDateType = yearIsDateType;
this.dumpQueriesOnException = dumpQueriesOnException;
this.includeInnodbStatusInDeadlockExceptions = includeInnodbStatusInDeadlockExceptions;
this.includeThreadDumpInDeadlockExceptions = includeThreadDumpInDeadlockExceptions;
this.retriesAllDown = retriesAllDown;
this.galeraAllowedState = galeraAllowedState;
this.transactionReplay = transactionReplay;
this.pool = pool;
this.poolName = poolName;
this.maxPoolSize = maxPoolSize;
this.minPoolSize = minPoolSize;
this.maxIdleTime = maxIdleTime;
this.registerJmxPool = registerJmxPool;
this.poolValidMinDelay = poolValidMinDelay;
this.useResetConnection = useResetConnection;
this.serverRsaPublicKeyFile = serverRsaPublicKeyFile;
this.allowPublicKeyRetrieval = allowPublicKeyRetrieval;
this.initialUrl = buildUrl(this);
}

private Configuration(
String database,
List<HostAddress> addresses,
Expand Down Expand Up @@ -299,6 +425,18 @@ private Configuration(
if (keyStorePassword != null) this.keyStorePassword = keyStorePassword;
if (keyStoreType != null) this.keyStoreType = keyStoreType;

// *************************************************************
// host primary check
// *************************************************************
boolean first = true;
for (HostAddress host : addresses) {
boolean primary = haMode != HaMode.REPLICATION || first;
if (host.primary == null) {
host.primary = primary;
}
first = false;
}

// *************************************************************
// option value verification
// *************************************************************
Expand All @@ -320,12 +458,6 @@ private Configuration(
}
}

public void updateAuth(String user, String password) {
this.user = user;
this.password = password;
buildUrl(this);
}

/**
* Tell if mariadb driver accept url string. (Correspond to interface
* java.jdbc.Driver.acceptsURL() method)
Expand Down Expand Up @@ -512,10 +644,69 @@ private static HaMode parseHaMode(String url, int separator) {
}
}

public Configuration clone(String username, String password) throws CloneNotSupportedException {
Configuration conf = (Configuration) super.clone();
conf.updateAuth(username, password);
return conf;
public Configuration clone(String username, String password) {
return new Configuration(
username,
password,
this.database,
this.addresses,
this.haMode,
this.nonMappedOptions,
this.timezone,
this.autocommit,
this.transactionIsolation,
this.defaultFetchSize,
this.maxQuerySizeToLog,
this.pinGlobalTxToPhysicalConnection,
this.geometryDefaultType,
this.socketFactory,
this.connectTimeout,
this.pipe,
this.localSocket,
this.tcpKeepAlive,
this.tcpAbortiveClose,
this.localSocketAddress,
this.socketTimeout,
this.useReadAheadInput,
this.tlsSocketType,
this.sslMode,
this.serverSslCert,
this.keyStore,
this.keyStorePassword,
this.keyStoreType,
this.enabledSslCipherSuites,
this.enabledSslProtocolSuites,
this.allowMultiQueries,
this.allowLocalInfile,
this.useCompression,
this.useAffectedRows,
this.useBulkStmts,
this.cachePrepStmts,
this.prepStmtCacheSize,
this.useServerPrepStmts,
this.credentialType,
this.sessionVariables,
this.connectionAttributes,
this.servicePrincipalName,
this.blankTableNameMeta,
this.tinyInt1isBit,
this.yearIsDateType,
this.dumpQueriesOnException,
this.includeInnodbStatusInDeadlockExceptions,
this.includeThreadDumpInDeadlockExceptions,
this.retriesAllDown,
this.galeraAllowedState,
this.transactionReplay,
this.pool,
this.poolName,
this.maxPoolSize,
this.minPoolSize,
this.maxIdleTime,
this.registerJmxPool,
this.poolValidMinDelay,
this.useResetConnection,
this.serverRsaPublicKeyFile,
this.allowPublicKeyRetrieval);
}

public String database() {
Expand Down Expand Up @@ -1062,14 +1253,12 @@ public Builder haMode(HaMode haMode) {
return this;
}

public Builder addresses(String host, int port) {
this._addresses = new ArrayList<>();
public Builder addHost(String host, int port) {
this._addresses.add(HostAddress.from(host, port));
return this;
}

public Builder addresses(String host, int port, boolean master) {
this._addresses = new ArrayList<>();
public Builder addHost(String host, int port, boolean master) {
this._addresses.add(HostAddress.from(host, port, master));
return this;
}
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/org/mariadb/jdbc/Connection.java
Expand Up @@ -75,8 +75,12 @@ public Connection(Configuration conf, ReentrantLock lock, Client client) throws
this.exceptionFactory = client.getExceptionFactory().setConnection(this);
this.client = client;
Context context = this.client.getContext();
this.canUseServerTimeout = context.getVersion().versionGreaterOrEqual(10, 1, 2);
this.canUseServerMaxRows = context.getVersion().versionGreaterOrEqual(10, 3, 0);
this.canUseServerTimeout =
context.getVersion().isMariaDBServer()
&& context.getVersion().versionGreaterOrEqual(10, 1, 2);
this.canUseServerMaxRows =
context.getVersion().isMariaDBServer()
&& context.getVersion().versionGreaterOrEqual(10, 3, 0);
this.defaultFetchSize = context.getConf().defaultFetchSize();
}

Expand Down Expand Up @@ -456,7 +460,6 @@ public CallableStatement prepareCall(String sql, int resultSetType, int resultSe
if (isFunction) {
return new FunctionStatement(
this,
poolConnection,
database,
databaseAndProcedure,
(arguments == null) ? "()" : arguments,
Expand Down Expand Up @@ -734,8 +737,8 @@ public boolean isWrapperFor(Class<?> iface) throws SQLException {
return iface.isInstance(this);
}

public HostAddress getHostAddress() {
return client.getHostAddress();
public int getWaitTimeout() {
return client.getWaitTimeout();
}

public Client getClient() {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/mariadb/jdbc/FunctionStatement.java
Expand Up @@ -30,7 +30,6 @@ public class FunctionStatement extends BaseCallableStatement implements Callable

public FunctionStatement(
Connection con,
MariaDbPoolConnection poolConnection,
String databaseName,
String procedureName,
String arguments,
Expand All @@ -41,7 +40,7 @@ public FunctionStatement(
int resultSetConcurrency)
throws SQLException {
super(
"SELECT " + procedureName + ((arguments == null) ? "()" : arguments),
"SELECT " + procedureName + arguments,
con,
lock,
databaseName,
Expand Down
15 changes: 0 additions & 15 deletions src/main/java/org/mariadb/jdbc/HostAddress.java
Expand Up @@ -32,8 +32,6 @@ public class HostAddress {
public String host;
public int port;
public Boolean primary;
public int cacheMaxAllowedPacket;
public int waitTimeout;

/**
* Constructor.
Expand Down Expand Up @@ -120,19 +118,6 @@ private static int getPort(String portString) throws SQLException {
}
}

public int getCacheMaxAllowedPacket() {
return cacheMaxAllowedPacket;
}

public int getWaitTimeout() {
return waitTimeout;
}

public void setCache(int cacheMaxAllowedPacket, int waitTimeout) {
this.cacheMaxAllowedPacket = cacheMaxAllowedPacket;
this.waitTimeout = waitTimeout;
}

private static HostAddress parseParameterHostAddress(String str, HaMode haMode, boolean first)
throws SQLException {
String host = null;
Expand Down

0 comments on commit 1003c60

Please sign in to comment.