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

Blob stream #18

Merged
merged 4 commits into from
Feb 19, 2018
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
11 changes: 11 additions & 0 deletions src/main/java/com/microsoft/sqlserver/jdbc/SQLServerBlob.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,21 @@ public byte[] getBytes(long pos,
*/
public long length() throws SQLException {
checkClosed();
if (value == null)
return (long)((PLPInputStream)activeStreams.get(0)).payloadLength;
getBytesFromStream();
return value.length;
}

/**
* Function for the result set to maintain blobs it has created
* @throws SQLException
*/
void fillByteArray() throws SQLException {
if(!isClosed)
getBytesFromStream();
}

/**
* Converts stream to byte[]
* @throws SQLServerException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
Expand Down Expand Up @@ -126,6 +128,7 @@ final void setCurrentRowType(RowType rowType) {
* occurs
*/
private Closeable activeStream;
private List<Object> streamObjects = new ArrayList<Object>();

/**
* A window of fetchSize quickly accessible rows for scrollable result sets
Expand Down Expand Up @@ -576,7 +579,10 @@ private void closeInternal() {

// Mark this ResultSet as closed, then clean up.
isClosed = true;


//fill all unclosed objects which rely on the stream
fillBlobs();

// Discard the current fetch buffer contents.
discardFetchBuffer();

Expand Down Expand Up @@ -2657,6 +2663,7 @@ public Blob getBlob(int i) throws SQLServerException {
checkClosed();
Blob value = (Blob) getValue(i, JDBCType.BLOB);
loggerExternal.exiting(getClassNameLogging(), "getBlob", value);
streamObjects.add(value);
return value;
}

Expand All @@ -2665,6 +2672,7 @@ public Blob getBlob(String colName) throws SQLServerException {
checkClosed();
Blob value = (Blob) getValue(findColumn(colName), JDBCType.BLOB);
loggerExternal.exiting(getClassNameLogging(), "getBlob", value);
streamObjects.add(value);
return value;
}

Expand Down Expand Up @@ -6507,6 +6515,23 @@ final void doServerFetch(int fetchType,
scrollWindow.reset();
}
}

/*
* Iterates through the list of objects which rely on the stream that's about to be closed, filling them with their data
* Will skip over closed blobs, implemented in SQLServerBlob
*/
private void fillBlobs() {
for(Object o : streamObjects) {
if(o instanceof SQLServerBlob) {
try {
((SQLServerBlob) o).fillByteArray();
} catch (SQLException e) {
if (logger.isLoggable(java.util.logging.Level.FINER))
logger.finer(toString() + "Filling blobs before closing: " + e.getMessage());
}
}
}
}

/**
* Discards the contents of the current fetch buffer.
Expand Down