Skip to content

Commit

Permalink
JDBC-243 / JDBC-187 implements stream, reader and blob methods curren…
Browse files Browse the repository at this point in the history
…tly throwing FBDriverNotCapableException
  • Loading branch information
mrotteveel committed Jul 17, 2016
1 parent f0d3558 commit 07453be
Show file tree
Hide file tree
Showing 18 changed files with 570 additions and 497 deletions.
68 changes: 50 additions & 18 deletions src/main/org/firebirdsql/jdbc/AbstractCallableStatement.java
Expand Up @@ -335,8 +335,8 @@ protected boolean internalExecute(boolean sendOutParams) throws SQLException {
field.setNull();
} else if (value instanceof WrapperWithCalendar) {
setField(field, (WrapperWithCalendar)value);
} else if (value instanceof WrapperWithInt) {
setField(field, (WrapperWithInt)value);
} else if (value instanceof WrapperWithLong) {
setField(field, (WrapperWithLong)value);
} else {
field.setObject(value);
}
Expand All @@ -354,20 +354,21 @@ protected boolean internalExecute(boolean sendOutParams) throws SQLException {
return hasResultSet;
}

private void setField(FBField field, WrapperWithInt value) throws SQLException {
private void setField(FBField field, WrapperWithLong value) throws SQLException {
Object obj = value.getValue();

if (obj == null) {
field.setNull();
} else {
int intValue = value.getIntValue();
long longValue = value.getLongValue();

if (obj instanceof InputStream)
field.setBinaryStream((InputStream) obj, intValue);
else if (obj instanceof Reader)
field.setCharacterStream((Reader) obj, intValue);
else
if (obj instanceof InputStream) {
field.setBinaryStream((InputStream) obj, longValue);
} else if (obj instanceof Reader) {
field.setCharacterStream((Reader) obj, longValue);
} else {
throw new TypeConversionException("Cannot convert type " + obj.getClass().getName());
}
}
}

Expand Down Expand Up @@ -1290,14 +1291,29 @@ public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException
}

public void setBinaryStream(int parameterIndex, InputStream inputStream, int length) throws SQLException {
procedureCall.getInputParam(parameterIndex).setValue(
new WrapperWithInt(inputStream, length));
procedureCall.getInputParam(parameterIndex).setValue(new WrapperWithLong(inputStream, length));
}

public void setBinaryStream(int parameterIndex, InputStream inputStream, long length) throws SQLException {
procedureCall.getInputParam(parameterIndex).setValue(new WrapperWithLong(inputStream, length));
}

public void setBinaryStream(int parameterIndex, InputStream inputStream) throws SQLException {
procedureCall.getInputParam(parameterIndex).setValue(inputStream);
}

public void setBlob(int parameterIndex, Blob blob) throws SQLException {
procedureCall.getInputParam(parameterIndex).setValue(blob);
}

public void setBlob(int parameterIndex, InputStream inputStream, long length) throws SQLException {
procedureCall.getInputParam(parameterIndex).setValue(new WrapperWithLong(inputStream, length));
}

public void setBlob(int parameterIndex, InputStream inputStream) throws SQLException {
procedureCall.getInputParam(parameterIndex).setValue(inputStream);
}

public void setBoolean(int parameterIndex, boolean x) throws SQLException {
procedureCall.getInputParam(parameterIndex).setValue(x);
}
Expand All @@ -1311,13 +1327,29 @@ public void setBytes(int parameterIndex, byte[] x) throws SQLException {
}

public void setCharacterStream(int parameterIndex, Reader reader, int length) throws SQLException {
procedureCall.getInputParam(parameterIndex).setValue(new WrapperWithInt(reader, length));
procedureCall.getInputParam(parameterIndex).setValue(new WrapperWithLong(reader, length));
}

public void setCharacterStream(int parameterIndex, Reader reader, long length) throws SQLException {
procedureCall.getInputParam(parameterIndex).setValue(new WrapperWithLong(reader, length));
}

public void setCharacterStream(int parameterIndex, Reader reader) throws SQLException {
procedureCall.getInputParam(parameterIndex).setValue(reader);
}

public void setClob(int parameterIndex, Clob x) throws SQLException {
procedureCall.getInputParam(parameterIndex).setValue(x);
}

public void setClob(int parameterIndex, Reader reader, long length) throws SQLException {
procedureCall.getInputParam(parameterIndex).setValue(new WrapperWithLong(reader, length));
}

public void setClob(int parameterIndex, Reader reader) throws SQLException {
procedureCall.getInputParam(parameterIndex).setValue(reader);
}

public void setDate(int parameterIndex, java.sql.Date x, Calendar cal) throws SQLException {
procedureCall.getInputParam(parameterIndex).setValue(new WrapperWithCalendar(x, cal));
}
Expand Down Expand Up @@ -1473,21 +1505,21 @@ private Calendar getCalendar() {
}
}

private static class WrapperWithInt {
private static class WrapperWithLong {
private final Object value;
private final int intValue;
private final long longValue;

private WrapperWithInt(Object value, int intValue) {
private WrapperWithLong(Object value, long longValue) {
this.value = value;
this.intValue = intValue;
this.longValue = longValue;
}

private Object getValue() {
return value;
}

private int getIntValue() {
return intValue;
private long getLongValue() {
return longValue;
}
}

Expand Down
115 changes: 42 additions & 73 deletions src/main/org/firebirdsql/jdbc/AbstractPreparedStatement.java
Expand Up @@ -233,36 +233,22 @@ public void setNull(int parameterIndex, int sqlType) throws SQLException {
isParamSet[parameterIndex - 1] = true;
}

/**
* Sets the designated parameter to the given input stream, which will have
* the specified number of bytes.
*
* <P>
* <B>Note: </B> This stream object can either be a standard Java stream
* object or your own subclass that implements the standard interface.
*
* @param parameterIndex
* the first parameter is 1, the second is 2, ...
* @param inputStream
* the Java input stream
* @param length
* the number of bytes in the stream
* @exception SQLException
* if a database access error occurs
*/
@Override
public void setBinaryStream(int parameterIndex, InputStream inputStream, int length) throws SQLException {
getField(parameterIndex).setBinaryStream(inputStream, length);
isParamSet[parameterIndex - 1] = true;
}


@Override
public void setBinaryStream(int parameterIndex, InputStream inputStream, long length) throws SQLException {
if (length > Integer.MAX_VALUE)
throw new FBDriverNotCapableException("Only length <= Integer.MAX_VALUE supported");
setBinaryStream(parameterIndex, inputStream, (int)length);
getField(parameterIndex).setBinaryStream(inputStream, length);
isParamSet[parameterIndex - 1] = true;
}

public void setBinaryStream(int parameterIndex, InputStream x) throws SQLException {
throw new FBDriverNotCapableException();
@Override
public void setBinaryStream(int parameterIndex, InputStream inputStream) throws SQLException {
getField(parameterIndex).setBinaryStream(inputStream);
isParamSet[parameterIndex - 1] = true;
}

/**
Expand Down Expand Up @@ -512,8 +498,9 @@ protected FieldDescriptor getParameterDescriptor(int columnIndex) {
*/
protected FBField getField(int columnIndex) throws SQLException {
checkValidity();
if (columnIndex > fields.length)
throw new FBSQLException("Invalid column index.", SQLStateConstants.SQL_STATE_INVALID_COLUMN);
if (columnIndex > fields.length) {
throw new SQLException("Invalid column index: " + columnIndex, SQLStateConstants.SQL_STATE_INVALID_COLUMN);
}

return fields[columnIndex - 1];
}
Expand All @@ -539,16 +526,18 @@ protected FBField getField(int columnIndex) throws SQLException {
* @exception SQLException
* if a database access error occurs
*/
public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException {
@Override
public final void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException {
setBinaryStream(parameterIndex, x, length);
}

public void setAsciiStream(int parameterIndex, InputStream x, long length) throws SQLException {

@Override
public final void setAsciiStream(int parameterIndex, InputStream x, long length) throws SQLException {
setBinaryStream(parameterIndex, x, length);
}

public void setAsciiStream(int parameterIndex, InputStream x)
throws SQLException {
@Override
public final void setAsciiStream(int parameterIndex, InputStream x) throws SQLException {
setBinaryStream(parameterIndex, x);
}

Expand Down Expand Up @@ -929,44 +918,22 @@ private void executeSingleForBatch(RowValue data, List<Long> results) throws SQL
results.add(getLargeUpdateCount());
}

/**
* Sets the designated parameter to the given <code>Reader</code> object,
* which is the given number of characters long. When a very large UNICODE
* value is input to a <code>LONGVARCHAR</code> parameter, it may be more
* practical to send it via a <code>java.io.Reader</code> object. The data
* will be read from the stream as needed until end-of-file is reached. The
* JDBC driver will do any necessary conversion from UNICODE to the database
* char format.
*
* <P>
* <B>Note: </B> This stream object can either be a standard Java stream
* object or your own subclass that implements the standard interface.
*
* @param parameterIndex
* the first parameter is 1, the second is 2, ...
* @param reader
* the java reader which contains the UNICODE data
* @param length
* the number of characters in the stream
* @exception SQLException
* if a database access error occurs
* @since 1.2
* @see <a href="package-summary.html#2.0 API">What Is in the JDBC 2.0 API
* </a>
*/
@Override
public void setCharacterStream(int parameterIndex, Reader reader, int length) throws SQLException {
getField(parameterIndex).setCharacterStream(reader, length);
isParamSet[parameterIndex - 1] = true;
}


@Override
public void setCharacterStream(int parameterIndex, Reader reader, long length) throws SQLException {
if (length > Integer.MAX_VALUE)
throw new FBDriverNotCapableException("Only length <= Integer.MAX_VALUE supported");
setCharacterStream(parameterIndex, reader, (int)length);
getField(parameterIndex).setCharacterStream(reader, length);
isParamSet[parameterIndex - 1] = true;
}

@Override
public void setCharacterStream(int parameterIndex, Reader reader) throws SQLException {
throw new FBDriverNotCapableException();
getField(parameterIndex).setCharacterStream(reader);
isParamSet[parameterIndex - 1] = true;
}

/**
Expand Down Expand Up @@ -1001,10 +968,10 @@ public void setRef(int i, Ref x) throws SQLException {
* @see <a href="package-summary.html#2.0 API">What Is in the JDBC 2.0 API
* </a>
*/
@Override
public void setBlob(int parameterIndex, Blob blob) throws SQLException {
// if the passed BLOB is not instance of our class, copy its content
// into the our BLOB
if (!(blob instanceof FBBlob)) {
// if the passed BLOB is not instance of our class, copy its content into the our BLOB
if (blob != null && !(blob instanceof FBBlob)) {
FBBlob fbb = new FBBlob(gdsHelper, blobListener);
fbb.copyStream(blob.getBinaryStream());
blob = fbb;
Expand All @@ -1013,15 +980,15 @@ public void setBlob(int parameterIndex, Blob blob) throws SQLException {
getField(parameterIndex).setBlob((FBBlob) blob);
isParamSet[parameterIndex - 1] = true;
}


@Override
public void setBlob(int parameterIndex, InputStream inputStream, long length) throws SQLException {
if (length > Integer.MAX_VALUE)
throw new FBDriverNotCapableException("Only length <= Integer.MAX_VALUE supported");
FBBlob blob = new FBBlob(gdsHelper, blobListener);
blob.copyStream(inputStream, (int) length);
blob.copyStream(inputStream, length);
setBlob(parameterIndex, blob);
}

@Override
public void setBlob(int parameterIndex, InputStream inputStream) throws SQLException {
FBBlob blob = new FBBlob(gdsHelper, blobListener);
blob.copyStream(inputStream);
Expand All @@ -1040,9 +1007,9 @@ public void setBlob(int parameterIndex, InputStream inputStream) throws SQLExcep
* if a database access error occurs
* @since 1.2
*/
@Override
public void setClob(int parameterIndex, Clob clob) throws SQLException {
// if the passed BLOB is not instance of our class, copy its content
// into the our BLOB
// if the passed BLOB is not instance of our class, copy its content into the our BLOB
if (!(clob instanceof FBClob)) {
FBClob fbc = new FBClob(new FBBlob(gdsHelper, blobListener));
fbc.copyCharacterStream(clob.getCharacterStream());
Expand All @@ -1052,11 +1019,15 @@ public void setClob(int parameterIndex, Clob clob) throws SQLException {
getField(parameterIndex).setClob((FBClob) clob);
isParamSet[parameterIndex - 1] = true;
}


@Override
public void setClob(int parameterIndex, Reader reader, long length) throws SQLException {
throw new FBDriverNotCapableException();
FBClob clob = new FBClob(new FBBlob(gdsHelper, blobListener));
clob.copyCharacterStream(reader, length);
setClob(parameterIndex, clob);
}

@Override
public void setClob(int parameterIndex, Reader reader) throws SQLException {
FBClob clob = new FBClob(new FBBlob(gdsHelper, blobListener));
clob.copyCharacterStream(reader);
Expand Down Expand Up @@ -1351,8 +1322,6 @@ public boolean execute(String sql, String[] columnNames) throws SQLException {
throw new FBSQLException(METHOD_NOT_SUPPORTED);
}

// TODO Implement large update count method below using SqlCountHolder

public long executeLargeUpdate() throws SQLException {
executeUpdate();
return getLargeUpdateCount();
Expand Down

0 comments on commit 07453be

Please sign in to comment.