Skip to content

Commit

Permalink
fixes #354 different behaviour of jTDS MSSQL driver
Browse files Browse the repository at this point in the history
  • Loading branch information
quaso committed Feb 20, 2023
1 parent 8ffe825 commit fc04e75
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public AutodetectJdbcCustomization(DataSource dataSource) {

if (databaseProductName.equals(MICROSOFT_SQL_SERVER)) {
LOG.info("Using MSSQL jdbc-overrides.");
detectedCustomization = new MssqlJdbcCustomization();
detectedCustomization = new MssqlJdbcCustomization(c.getMetaData().getDriverName());
} else if (databaseProductName.equals(POSTGRESQL)) {
LOG.info("Using PostgreSQL jdbc-overrides.");
detectedCustomization = new PostgreSqlJdbcCustomization();
Expand All @@ -67,6 +67,11 @@ public Instant getInstant(ResultSet rs, String columnName) throws SQLException {
return jdbcCustomization.getInstant(rs, columnName);
}

@Override
public byte[] getBytes(ResultSet rs, String columnName) throws SQLException {
return jdbcCustomization.getBytes(rs, columnName);
}

@Override
public boolean supportsExplicitQueryLimitPart() {
return jdbcCustomization.supportsExplicitQueryLimitPart();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public interface JdbcCustomization {
void setInstant(PreparedStatement p, int index, Instant value) throws SQLException;
Instant getInstant(ResultSet rs, String columnName) throws SQLException;

default byte[] getBytes(ResultSet rs, String columnName) throws SQLException {
return rs.getBytes(columnName);
}

boolean supportsExplicitQueryLimitPart();
String getQueryLimitPart(int limit);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ public Void map(ResultSet rs) throws SQLException {
}

String instanceId = rs.getString("task_instance");
byte[] data = rs.getBytes("task_data");
byte[] data = jdbcCustomization.getBytes(rs,"task_data");

Instant executionTime = jdbcCustomization.getInstant(rs, "execution_time");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

import com.github.kagkarlsson.scheduler.task.Execution;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.io.IOException;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.sql.*;
import java.time.Instant;
import java.util.Calendar;
import java.util.List;
Expand All @@ -29,6 +29,13 @@

public class MssqlJdbcCustomization implements JdbcCustomization {

private final String driverName;

public MssqlJdbcCustomization(String driverName) {
this.driverName = driverName;
}


@Override
public String getName() {
return "MSSQL";
Expand All @@ -44,6 +51,27 @@ public Instant getInstant(ResultSet rs, String columnName) throws SQLException {
return Optional.ofNullable(rs.getTimestamp(columnName)).map(Timestamp::toInstant).orElse(null);
}

@Override
public byte[] getBytes(ResultSet rs, String columnName) throws SQLException {
byte[] result;
if (driverName.contains("jTDS")) {
try (Reader reader = ((Clob) rs.getObject(columnName)).getCharacterStream()) {
char[] charArray = new char[8 * 1024];
StringBuilder builder = new StringBuilder();
int numCharsRead;
while ((numCharsRead = reader.read(charArray, 0, charArray.length)) != -1) {
builder.append(charArray, 0, numCharsRead);
}
result = builder.toString().getBytes(StandardCharsets.UTF_16LE);
} catch (IOException e) {
throw new SQLException(e);
}
} else {
result = rs.getBytes(columnName);
}
return result;
}

@Override
public boolean supportsExplicitQueryLimitPart() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class CronSchedule implements Schedule, Serializable {

private static final String DISABLED = "-";
private static final Logger LOG = LoggerFactory.getLogger(CronSchedule.class);

private static final long serialVersionUID = 6577185347687546367L;
private final String pattern;
private final ZoneId zoneId;
private transient ExecutionTime cronExecutionTime; // lazily initialized
Expand Down

0 comments on commit fc04e75

Please sign in to comment.