Skip to content

Commit

Permalink
refactor: replace some usages of initCause (#1037)
Browse files Browse the repository at this point in the history
replace some usages of initCause with appropriate constructor
  • Loading branch information
AlexElin authored and davecramer committed Jan 12, 2018
1 parent 2e8c2b6 commit 0c29823
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 20 deletions.
20 changes: 14 additions & 6 deletions pgjdbc/src/main/java/org/postgresql/copy/PGCopyInputStream.java
Expand Up @@ -69,20 +69,24 @@ 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 @@ -93,15 +97,14 @@ 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];
for (int i = at; i < len; i++) {
ba[i - at] = buf[i];
}
System.arraycopy(buf, at, ba, 0, len - at);
result = ba;
}
at = len; // either partly or fully returned, buffer is exhausted
Expand All @@ -117,6 +120,7 @@ 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 @@ -127,34 +131,38 @@ public void close() throws IOException {
try {
op.cancelCopy();
} catch (SQLException se) {
IOException ioe = new IOException("Failed to close copy reader.");
ioe.initCause(se);
throw ioe;
throw new IOException("Failed to close copy reader.", se);
}
}
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: 17 additions & 9 deletions pgjdbc/src/main/java/org/postgresql/copy/PGCopyOutputStream.java
Expand Up @@ -65,6 +65,7 @@ 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 @@ -74,18 +75,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) {
IOException ioe = new IOException("Write to copy failed.");
ioe.initCause(se);
throw ioe;
throw new IOException("Write to copy failed.", se);
}
}

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

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

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

@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,30 +137,37 @@ 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 @@ -169,6 +176,7 @@ public long endCopy() throws SQLException {
return getHandledRowCount();
}

@Override
public long getHandledRowCount() {
return op.getHandledRowCount();
}
Expand Down
12 changes: 7 additions & 5 deletions pgjdbc/src/main/java/org/postgresql/jdbc/BatchResultHandler.java
Expand Up @@ -51,6 +51,7 @@ 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 @@ -73,6 +74,7 @@ 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 @@ -125,6 +127,7 @@ private void updateGeneratedKeys() {
allGeneratedRows.clear();
}

@Override
public void handleWarning(SQLWarning warning) {
pgStatement.addWarning(warning);
}
Expand All @@ -145,15 +148,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());
batchException.initCause(newError);
newError.getSQLState(), uncompressUpdateCount(), newError);
super.handleError(batchException);
}
resultIndex++;

super.handleError(newError);
}

@Override
public void handleCompletion() throws SQLException {
updateGeneratedKeys();
SQLException batchException = getException();
Expand All @@ -163,9 +166,8 @@ public void handleCompletion() throws SQLException {
BatchUpdateException newException = new BatchUpdateException(
batchException.getMessage(),
batchException.getSQLState(),
uncompressUpdateCount()
);
newException.initCause(batchException.getCause());
uncompressUpdateCount(),
batchException.getCause());
SQLException next = batchException.getNextException();
if (next != null) {
newException.setNextException(next);
Expand Down
Expand Up @@ -17,6 +17,7 @@ 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 0c29823

Please sign in to comment.