Skip to content

Commit

Permalink
Merge branch 'release/3.0.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed May 17, 2022
2 parents cc0aa96 + c2f09c7 commit 523e6ed
Show file tree
Hide file tree
Showing 54 changed files with 1,658 additions and 191 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ or maven :
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>3.0.4</version>
<version>3.0.5</version>
</dependency>
```

Expand All @@ -46,7 +46,7 @@ Development snapshot are available on sonatype nexus repository
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>3.0.5-SNAPSHOT</version>
<version>3.0.6-SNAPSHOT</version>
</dependency>
</dependencies>
```
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<artifactId>mariadb-java-client</artifactId>
<packaging>jar</packaging>
<name>mariadb-java-client</name>
<version>3.0.4</version>
<version>3.0.5</version>
<description>JDBC driver for MariaDB and MySQL</description>
<url>https://mariadb.com/kb/en/mariadb/about-mariadb-connector-j/</url>

Expand All @@ -23,7 +23,7 @@
<logback.version>1.3.0-alpha12</logback.version>
<jacoco.version>0.8.7</jacoco.version>
<waffle-jna.version>3.1.1</waffle-jna.version>
<mysql-connector-java.version>8.0.28</mysql-connector-java.version>
<mysql-connector-java.version>8.0.29</mysql-connector-java.version>
<bnd-maven-plugin.version>6.2.0</bnd-maven-plugin.version>
</properties>

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/mariadb/jdbc/BaseCallableStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.mariadb.jdbc.client.result.Result;
import org.mariadb.jdbc.codec.Parameter;
import org.mariadb.jdbc.export.ExceptionFactory;
import org.mariadb.jdbc.util.ParameterList;

/** Common methods for function/stored procedure */
public abstract class BaseCallableStatement extends ServerPreparedStatement
Expand Down Expand Up @@ -167,6 +168,13 @@ public void registerOutParameter(int parameterIndex, int sqlType, int scale) thr
registerOutParameter(parameterIndex, sqlType);
}

@Override
public void clearParameters() throws SQLException {
checkNotClosed();
parameters = new ParameterList();
outputParameters.stream().forEach(index -> parameters.set(index - 1, Parameter.NULL_PARAMETER));
}

/**
* Retrieves whether the last OUT parameter read had the value of SQL <code>NULL</code>. Note that
* this method should be called only after calling a getter method; otherwise, there is no value
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/mariadb/jdbc/BasePreparedStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,25 @@ public BasePreparedStatement(
this.sql = sql;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder("sql:'" + sql + "'");
sb.append(", parameters:[");
for (int i = 0; i < parameters.size(); i++) {
org.mariadb.jdbc.client.util.Parameter param = parameters.get(i);
if (param == null) {
sb.append("null");
} else {
sb.append(param.bestEffortStringValue(con.getContext()));
}
if (i != parameters.size() - 1) {
sb.append(",");
}
}
sb.append("]");
return sb.toString();
}

/**
* Set PREPARE result
*
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/mariadb/jdbc/ClientPreparedStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -502,4 +502,9 @@ public void close() throws SQLException {
con.fireStatementClosed(this);
super.close();
}

@Override
public String toString() {
return "ClientPreparedStatement{" + super.toString() + '}';
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/mariadb/jdbc/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public class Configuration {

// protocol
private boolean allowMultiQueries = false;
private boolean allowLocalInfile = false;
private boolean allowLocalInfile = true;
private boolean useCompression = false;
private boolean useAffectedRows = false;
private boolean useBulkStmts = true;
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/org/mariadb/jdbc/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class Connection implements java.sql.Connection {
private final boolean canUseServerTimeout;
private final boolean canUseServerMaxRows;
private final int defaultFetchSize;
private final boolean forceTransactionEnd;
private MariaDbPoolConnection poolConnection;

/**
Expand All @@ -58,6 +59,7 @@ public class Connection implements java.sql.Connection {
*/
public Connection(Configuration conf, ReentrantLock lock, Client client) {
this.conf = conf;
this.forceTransactionEnd = Boolean.parseBoolean(conf.nonMappedOptions().getProperty("forceTransactionEnd", "false"));
this.lock = lock;
this.exceptionFactory = client.getExceptionFactory().setConnection(this);
this.client = client;
Expand Down Expand Up @@ -198,7 +200,7 @@ public void setAutoCommit(boolean autoCommit) throws SQLException {
public void commit() throws SQLException {
lock.lock();
try {
if ((client.getContext().getServerStatus() & ServerStatus.IN_TRANSACTION) > 0) {
if (forceTransactionEnd || (client.getContext().getServerStatus() & ServerStatus.IN_TRANSACTION) > 0) {
client.execute(new QueryPacket("COMMIT"), false);
}
} finally {
Expand All @@ -210,7 +212,7 @@ public void commit() throws SQLException {
public void rollback() throws SQLException {
lock.lock();
try {
if ((client.getContext().getServerStatus() & ServerStatus.IN_TRANSACTION) > 0) {
if (forceTransactionEnd || (client.getContext().getServerStatus() & ServerStatus.IN_TRANSACTION) > 0) {
client.execute(new QueryPacket("ROLLBACK"), true);
}
} finally {
Expand Down Expand Up @@ -842,7 +844,7 @@ && getContext().getVersion().getMinorVersion() == 2
}

// in transaction => rollback
if ((client.getContext().getServerStatus() & ServerStatus.IN_TRANSACTION) > 0) {
if (forceTransactionEnd || (client.getContext().getServerStatus() & ServerStatus.IN_TRANSACTION) > 0) {
client.execute(new QueryPacket("ROLLBACK"), true);
}

Expand Down
30 changes: 24 additions & 6 deletions src/main/java/org/mariadb/jdbc/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,35 @@ public static Connection connect(Configuration configuration) throws SQLExceptio
break;

default:
HostAddress hostAddress =
configuration.addresses().isEmpty() ? null : configuration.addresses().get(0);
client =
configuration.transactionReplay()
? new ReplayClient(configuration, hostAddress, lock, false)
: new StandardClient(configuration, hostAddress, lock, false);
ClientInstance<Configuration, HostAddress, ReentrantLock, Boolean, Client> clientInstance =
(configuration.transactionReplay()) ? ReplayClient::new : StandardClient::new;

if (configuration.addresses().isEmpty()) {
// unix socket / windows pipe
client = clientInstance.apply(configuration, null, lock, false);
} else {
// loop until finding
SQLException lastException = null;
for (HostAddress host : configuration.addresses()) {
try {
client = clientInstance.apply(configuration, host, lock, false);
return new Connection(configuration, lock, client);
} catch (SQLException e) {
lastException = e;
}
}
throw lastException;
}
break;
}
return new Connection(configuration, lock, client);
}

@FunctionalInterface
private interface ClientInstance<T, U, V, W, R> {
R apply(T t, U u, V v, W w) throws SQLException;
}

/**
* Connect to the given connection string.
*
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/mariadb/jdbc/FunctionStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,24 @@ protected void preValidParameters() throws SQLException {
parameters = newParameters;
super.validParameters();
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder("FunctionStatement{sql:'" + sql + "'");
sb.append(", parameters:[");
for (int i = 0; i < parameters.size(); i++) {
org.mariadb.jdbc.client.util.Parameter param = parameters.get(i);
if (outputParameters.contains(i + 1)) sb.append("<OUT>");
if (param == null) {
sb.append("null");
} else {
sb.append(param.bestEffortStringValue(con.getContext()));
}
if (i != parameters.size() - 1) {
sb.append(",");
}
}
sb.append("]}");
return sb.toString();
}
}
20 changes: 20 additions & 0 deletions src/main/java/org/mariadb/jdbc/ProcedureStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,24 @@ protected void handleParameterOutput() throws SQLException {
}
}
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder("ProcedureStatement{sql:'" + sql + "'");
sb.append(", parameters:[");
for (int i = 0; i < parameters.size(); i++) {
org.mariadb.jdbc.client.util.Parameter param = parameters.get(i);
if (outputParameters.contains(i + 1)) sb.append("<OUT>");
if (param == null) {
sb.append("null");
} else {
sb.append(param.bestEffortStringValue(con.getContext()));
}
if (i != parameters.size() - 1) {
sb.append(",");
}
}
sb.append("]}");
return sb.toString();
}
}
5 changes: 5 additions & 0 deletions src/main/java/org/mariadb/jdbc/ServerPreparedStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -673,4 +673,9 @@ public void reset() {
lock.unlock();
}
}

@Override
public String toString() {
return "ServerPreparedStatement{" + super.toString() + '}';
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/mariadb/jdbc/client/Column.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public interface Column {
*
* @return precision
*/
long getPrecision();
int getPrecision();

/**
* return column type from column server type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,11 @@ public String createSessionVariableQuery(String serverTz) {
// force client timezone to connection to ensure result of now(), ...
if (conf.timezone() != null && !"disable".equalsIgnoreCase(conf.timezone())) {
boolean mustSetTimezone = true;
ZoneId clientZoneId = ZoneId.of(conf.timezone()).normalized();
TimeZone connectionTz =
"auto".equalsIgnoreCase(conf.timezone())
? TimeZone.getDefault()
: TimeZone.getTimeZone(ZoneId.of(conf.timezone()).normalized());
ZoneId clientZoneId = connectionTz.toZoneId();

// try to avoid timezone consideration if server use the same one
try {
Expand Down Expand Up @@ -824,7 +828,8 @@ public Completion readPacket(
context,
exceptionFactory,
lock,
traceEnable);
traceEnable,
message);
if (completion instanceof StreamingResult && !((StreamingResult) completion).loaded()) {
streamStmt = stmt;
streamMsg = message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ public int readIntLengthEncodedNotNull() {
* @return current pos
*/
public int skipIdentifier() {
int type = buf[pos++] & 0xff;
pos += (type == 252) ? readUnsignedShort() : type;
int len = readLength();
pos += len;
return pos;
}

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/mariadb/jdbc/client/util/Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,12 @@ public interface Parameter {
* @return is null
*/
boolean isNull();

/**
* Methods to return parameter as string if possible (Streaming parameter will return null)
*
* @param context current connection context
* @return null if not available.
*/
String bestEffortStringValue(Context context);
}
21 changes: 21 additions & 0 deletions src/main/java/org/mariadb/jdbc/codec/Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
package org.mariadb.jdbc.codec;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import org.mariadb.jdbc.client.Context;
import org.mariadb.jdbc.client.DataType;
import org.mariadb.jdbc.client.socket.Writer;
import org.mariadb.jdbc.client.socket.impl.PacketWriter;
import org.mariadb.jdbc.plugin.Codec;

public class Parameter<T> implements org.mariadb.jdbc.client.util.Parameter {
Expand Down Expand Up @@ -69,4 +73,21 @@ public int getBinaryEncodeType() {
public boolean isNull() {
return value == null;
}

public String bestEffortStringValue(Context context) {
if (isNull()) return "null";
if (codec.canEncodeLongData()) {
Type it = codec.getClass().getGenericInterfaces()[0];
ParameterizedType parameterizedType = (ParameterizedType) it;
Type typeParameter = parameterizedType.getActualTypeArguments()[0];
return "<" + typeParameter + ">";
}
try {
PacketWriter writer = new PacketWriter(null, 0, 0xffffff, null, null);
codec.encodeText(writer, context, this.value, null, this.length);
return new String(writer.buf(), 4, writer.pos() - 4, StandardCharsets.UTF_8);
} catch (Throwable t) {
return null;
}
}
}
10 changes: 10 additions & 0 deletions src/main/java/org/mariadb/jdbc/export/ExceptionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,16 @@ private SQLException createException(
if ("70100".equals(sqlState)) { // ER_QUERY_INTERRUPTED
return new SQLTimeoutException(msg, sqlState, errorCode);
}
// 4166 : mariadb load data infile disable
// 1148 : 10.2 mariadb load data infile disable
// 3948 : mysql load data infile disable
if ((errorCode == 4166 || errorCode == 3948 || errorCode == 1148) && !conf.allowLocalInfile()) {
return new SQLException(
"Local infile is disabled by connector. Enable `allowLocalInfile` to allow local infile commands",
sqlState,
errorCode,
cause);
}

SQLException returnEx;
String sqlClass = sqlState == null ? "42" : sqlState.substring(0, 2);
Expand Down
Loading

0 comments on commit 523e6ed

Please sign in to comment.