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

refactor: merge PgPreparedStatement#setBinaryStream int and long methods #3165

Merged
merged 3 commits into from
May 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 13 additions & 18 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgPreparedStatement.java
Expand Up @@ -504,24 +504,12 @@ public void setUnicodeStream(@Positive int parameterIndex, @Nullable InputStream
@Override
public void setBinaryStream(@Positive int parameterIndex, @Nullable InputStream x,
@NonNegative int length) throws SQLException {
checkClosed();

if (x == null) {
setNull(parameterIndex, Types.VARBINARY);
return;
}

if (length < 0) {
throw new PSQLException(GT.tr("Invalid stream length {0}.", length),
PSQLState.INVALID_PARAMETER_VALUE);
}

// Version 7.2 supports BinaryStream for the PG bytea type
// As the spec/javadoc for this method indicate this is to be used for
// large binary values (i.e. LONGVARBINARY) PG doesn't have a separate
// long binary datatype, but with toast the bytea datatype is capable of
// handling very large values.
preparedParameters.setBytea(parameterIndex, x, length);
setBinaryStream(parameterIndex, x, (long) length);
}

@Override
Expand Down Expand Up @@ -1600,16 +1588,23 @@ public void setCharacterStream(@Positive int parameterIndex,
public void setBinaryStream(@Positive int parameterIndex, @Nullable InputStream value,
@NonNegative @IntRange(from = 0, to = Integer.MAX_VALUE) long length)
throws SQLException {
checkClosed();

if (value == null) {
setNull(parameterIndex, Types.VARBINARY);
return;
}

//noinspection ConstantConditions
if (length > Integer.MAX_VALUE) {
throw new PSQLException(GT.tr("Object is too large to send over the protocol."),
PSQLState.NUMERIC_CONSTANT_OUT_OF_RANGE);
} else if (length < 0) {
throw new PSQLException(GT.tr("Invalid stream length {0}.", length),
PSQLState.INVALID_PARAMETER_VALUE);
}
if (value == null) {
preparedParameters.setNull(parameterIndex, Oid.BYTEA);
} else {
preparedParameters.setBytea(parameterIndex, value, (int) length);
}

preparedParameters.setBytea(parameterIndex, value, (int) length);
}

@Override
Expand Down