Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added backwards compatibility for calling Clob#length() on NVarchar columns #1214

Merged
merged 4 commits into from
Jan 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBlob.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ public byte[] getBytes(long pos, int length) throws SQLException {
@Override
public long length() throws SQLException {
checkClosed();
if (value == null && activeStreams.get(0) instanceof PLPInputStream) {
return (long) ((PLPInputStream) activeStreams.get(0)).payloadLength;
if (value == null && activeStreams.get(0) instanceof BaseInputStream) {
return (long) ((BaseInputStream) activeStreams.get(0)).payloadLength;
}
getBytesFromStream();
return value.length;
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerClob.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ abstract class SQLServerClobBase extends SQLServerLob {

private boolean isClosed = false;

private final TypeInfo typeInfo;
protected final TypeInfo typeInfo;

/**
* Active streams which must be closed when the Clob/NClob is closed. Initial size of the array is based on an
Expand Down Expand Up @@ -328,7 +328,13 @@ public String getSubString(long pos, int length) throws SQLException {
public long length() throws SQLException {
checkClosed();
if (null == value && activeStreams.get(0) instanceof BaseInputStream) {
return (long) ((BaseInputStream) activeStreams.get(0)).payloadLength;
int length = ((BaseInputStream) activeStreams.get(0)).payloadLength;
if (null != typeInfo) {
String columnTypeName = typeInfo.getSSTypeName();
return ("nvarchar".equalsIgnoreCase(columnTypeName)
|| "ntext".equalsIgnoreCase(columnTypeName)) ? length / 2 : length;
peterbae marked this conversation as resolved.
Show resolved Hide resolved
}
return (long) length;
} else if (null == value) {
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ public InputStream getAsciiStream() throws SQLException {
return super.getAsciiStream();
}

@Override
public long length() throws SQLException {
// If streaming, every 2 bytes represents 1 character. If not, length() just returns string length
long length = super.length();
return (null == value) ? length / 2 : length;
}

@Override
final JDBCType getJdbcType() {
return JDBCType.NCLOB;
Expand Down