Skip to content

Commit

Permalink
Revert "refactor: replace some usages of initCause (#1037)"
Browse files Browse the repository at this point in the history
This reverts commit 0c29823.
  • Loading branch information
davecramer committed Jan 12, 2018
1 parent 0c29823 commit 70745a1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 39 deletions.
20 changes: 6 additions & 14 deletions pgjdbc/src/main/java/org/postgresql/copy/PGCopyInputStream.java
Expand Up @@ -69,24 +69,20 @@ private void checkClosed() throws IOException {
}


@Override
public int available() throws IOException {
checkClosed();
return (buf != null ? len - at : 0);
}

@Override
public int read() throws IOException {
checkClosed();
return gotBuf() ? buf[at++] : -1;
}

@Override
public int read(byte[] buf) throws IOException {
return read(buf, 0, buf.length);
}

@Override
public int read(byte[] buf, int off, int siz) throws IOException {
checkClosed();
int got = 0;
Expand All @@ -97,14 +93,15 @@ public int read(byte[] buf, int off, int siz) throws IOException {
return got == 0 && !didReadSomething ? -1 : got;
}

@Override
public byte[] readFromCopy() throws SQLException {
byte[] result = buf;
try {
if (gotBuf()) {
if (at > 0 || len < buf.length) {
byte[] ba = new byte[len - at];
System.arraycopy(buf, at, ba, 0, len - at);
for (int i = at; i < len; i++) {
ba[i - at] = buf[i];
}
result = ba;
}
at = len; // either partly or fully returned, buffer is exhausted
Expand All @@ -120,7 +117,6 @@ public byte[] readFromCopy(boolean block) throws SQLException {
return readFromCopy();
}

@Override
public void close() throws IOException {
// Don't complain about a double close.
if (op == null) {
Expand All @@ -131,38 +127,34 @@ public void close() throws IOException {
try {
op.cancelCopy();
} catch (SQLException se) {
throw new IOException("Failed to close copy reader.", se);
IOException ioe = new IOException("Failed to close copy reader.");
ioe.initCause(se);
throw ioe;
}
}
op = null;
}

@Override
public void cancelCopy() throws SQLException {
op.cancelCopy();
}

@Override
public int getFormat() {
return op.getFormat();
}

@Override
public int getFieldFormat(int field) {
return op.getFieldFormat(field);
}

@Override
public int getFieldCount() {
return op.getFieldCount();
}

@Override
public boolean isActive() {
return op != null && op.isActive();
}

@Override
public long getHandledRowCount() {
return op.getHandledRowCount();
}
Expand Down
26 changes: 9 additions & 17 deletions pgjdbc/src/main/java/org/postgresql/copy/PGCopyOutputStream.java
Expand Up @@ -65,7 +65,6 @@ public PGCopyOutputStream(CopyIn op, int bufferSize) {
copyBuffer = new byte[bufferSize];
}

@Override
public void write(int b) throws IOException {
checkClosed();
if (b < 0 || b > 255) {
Expand All @@ -75,18 +74,18 @@ public void write(int b) throws IOException {
write(singleByteBuffer, 0, 1);
}

@Override
public void write(byte[] buf) throws IOException {
write(buf, 0, buf.length);
}

@Override
public void write(byte[] buf, int off, int siz) throws IOException {
checkClosed();
try {
writeToCopy(buf, off, siz);
} catch (SQLException se) {
throw new IOException("Write to copy failed.", se);
IOException ioe = new IOException("Write to copy failed.");
ioe.initCause(se);
throw ioe;
}
}

Expand All @@ -96,7 +95,6 @@ private void checkClosed() throws IOException {
}
}

@Override
public void close() throws IOException {
// Don't complain about a double close.
if (op == null) {
Expand All @@ -106,23 +104,25 @@ public void close() throws IOException {
try {
endCopy();
} catch (SQLException se) {
throw new IOException("Ending write to copy failed.", se);
IOException ioe = new IOException("Ending write to copy failed.");
ioe.initCause(se);
throw ioe;
}
op = null;
}

@Override
public void flush() throws IOException {
try {
op.writeToCopy(copyBuffer, 0, at);
at = 0;
op.flushCopy();
} catch (SQLException e) {
throw new IOException("Unable to flush stream", e);
IOException ioe = new IOException("Unable to flush stream");
ioe.initCause(e);
throw ioe;
}
}

@Override
public void writeToCopy(byte[] buf, int off, int siz) throws SQLException {
if (at > 0
&& siz > copyBuffer.length - at) { // would not fit into rest of our buf, so flush buf
Expand All @@ -137,37 +137,30 @@ public void writeToCopy(byte[] buf, int off, int siz) throws SQLException {
}
}

@Override
public int getFormat() {
return op.getFormat();
}

@Override
public int getFieldFormat(int field) {
return op.getFieldFormat(field);
}

@Override
public void cancelCopy() throws SQLException {
op.cancelCopy();
}

@Override
public int getFieldCount() {
return op.getFieldCount();
}

@Override
public boolean isActive() {
return op.isActive();
}

@Override
public void flushCopy() throws SQLException {
op.flushCopy();
}

@Override
public long endCopy() throws SQLException {
if (at > 0) {
op.writeToCopy(copyBuffer, 0, at);
Expand All @@ -176,7 +169,6 @@ public long endCopy() throws SQLException {
return getHandledRowCount();
}

@Override
public long getHandledRowCount() {
return op.getHandledRowCount();
}
Expand Down
12 changes: 5 additions & 7 deletions pgjdbc/src/main/java/org/postgresql/jdbc/BatchResultHandler.java
Expand Up @@ -51,7 +51,6 @@ public class BatchResultHandler extends ResultHandlerBase {
this.allGeneratedRows = !expectGeneratedKeys ? null : new ArrayList<List<byte[][]>>();
}

@Override
public void handleResultRows(Query fromQuery, Field[] fields, List<byte[][]> tuples,
ResultCursor cursor) {
// If SELECT, then handleCommandStatus call would just be missing
Expand All @@ -74,7 +73,6 @@ public void handleResultRows(Query fromQuery, Field[] fields, List<byte[][]> tup
latestGeneratedRows = tuples;
}

@Override
public void handleCommandStatus(String status, int updateCount, long insertOID) {
if (latestGeneratedRows != null) {
// We have DML. Decrease resultIndex that was just increased in handleResultRows
Expand Down Expand Up @@ -127,7 +125,6 @@ private void updateGeneratedKeys() {
allGeneratedRows.clear();
}

@Override
public void handleWarning(SQLWarning warning) {
pgStatement.addWarning(warning);
}
Expand All @@ -148,15 +145,15 @@ public void handleError(SQLException newError) {
BatchUpdateException batchException = new BatchUpdateException(
GT.tr("Batch entry {0} {1} was aborted: {2} Call getNextException to see other errors in the batch.",
resultIndex, queryString, newError.getMessage()),
newError.getSQLState(), uncompressUpdateCount(), newError);
newError.getSQLState(), uncompressUpdateCount());
batchException.initCause(newError);
super.handleError(batchException);
}
resultIndex++;

super.handleError(newError);
}

@Override
public void handleCompletion() throws SQLException {
updateGeneratedKeys();
SQLException batchException = getException();
Expand All @@ -166,8 +163,9 @@ public void handleCompletion() throws SQLException {
BatchUpdateException newException = new BatchUpdateException(
batchException.getMessage(),
batchException.getSQLState(),
uncompressUpdateCount(),
batchException.getCause());
uncompressUpdateCount()
);
newException.initCause(batchException.getCause());
SQLException next = batchException.getNextException();
if (next != null) {
newException.setNextException(next);
Expand Down
Expand Up @@ -17,7 +17,6 @@ class CallableBatchResultHandler extends BatchResultHandler {
super(statement, queries, parameterLists, false);
}

@Override
public void handleResultRows(Query fromQuery, Field[] fields, List<byte[][]> tuples, ResultCursor cursor) {
/* ignore */
}
Expand Down

0 comments on commit 70745a1

Please sign in to comment.