Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[CONJ-260] Add jdbc nString, nCharacterStream, nClob implementation
  • Loading branch information
rusher committed Jun 23, 2016
1 parent 13d30be commit 3616061
Show file tree
Hide file tree
Showing 7 changed files with 294 additions and 52 deletions.
Expand Up @@ -429,12 +429,12 @@ public RowId getRowId(String parameterName) throws SQLException {

@Override
public NClob getNClob(int parameterIndex) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NClob not supported");
return getResult().getNClob(indexToOutputIndex(parameterIndex));
}

@Override
public NClob getNClob(String parameterName) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NClob not supported");
return getResult().getNClob(nameToOutputIndex(parameterName));
}

@Override
Expand All @@ -449,22 +449,22 @@ public SQLXML getSQLXML(String parameterName) throws SQLException {

@Override
public String getNString(int parameterIndex) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NString not supported");
return getResult().getNString(indexToOutputIndex(parameterIndex));
}

@Override
public String getNString(String parameterName) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NString not supported");
return getResult().getNString(nameToOutputIndex(parameterName));
}

@Override
public Reader getNCharacterStream(int parameterIndex) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NCharacterStream not supported");
return getResult().getNCharacterStream(indexToOutputIndex(parameterIndex));
}

@Override
public Reader getNCharacterStream(String parameterName) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NCharacterStream not supported");
return getResult().getNCharacterStream(nameToOutputIndex(parameterName));
}

@Override
Expand Down Expand Up @@ -605,32 +605,32 @@ public void setRowId(String parameterName, RowId rowid) throws SQLException {

@Override
public void setNString(String parameterName, String value) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NStrings not supported");
setNString(nameToIndex(parameterName), value);
}

@Override
public void setNCharacterStream(String parameterName, Reader value, long length) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NCharacterStream not supported");
public void setNCharacterStream(String parameterName, Reader reader, long length) throws SQLException {
setCharacterStream(nameToIndex(parameterName), reader, length);
}

@Override
public void setNCharacterStream(String parameterName, Reader value) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NCharacterStream not supported");
public void setNCharacterStream(String parameterName, Reader reader) throws SQLException {
setCharacterStream(nameToIndex(parameterName), reader);
}

@Override
public void setNClob(String parameterName, NClob value) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NClob not supported");
setClob(nameToIndex(parameterName), value);
}

@Override
public void setNClob(String parameterName, Reader reader, long length) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NClob not supported");
setClob(nameToIndex(parameterName), reader, length);
}

@Override
public void setNClob(String parameterName, Reader reader) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NCLOB not supported");
setClob(nameToIndex(parameterName), reader);
}

@Override
Expand Down
Expand Up @@ -459,12 +459,12 @@ public RowId getRowId(String parameterName) throws SQLException {

@Override
public NClob getNClob(int parameterIndex) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NClob not supported");
return getResult().getNClob(indexToOutputIndex(parameterIndex));
}

@Override
public NClob getNClob(String parameterName) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NClob not supported");
return getResult().getNClob(nameToOutputIndex(parameterName));
}

@Override
Expand All @@ -479,22 +479,21 @@ public SQLXML getSQLXML(String parameterName) throws SQLException {

@Override
public String getNString(int parameterIndex) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NString not supported");
return getResult().getString(indexToOutputIndex(parameterIndex));
}

@Override
public String getNString(String parameterName) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NString not supported");
}
return getResult().getString(nameToOutputIndex(parameterName)); }

@Override
public Reader getNCharacterStream(int parameterIndex) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NCharacterStream not supported");
return getResult().getCharacterStream(indexToOutputIndex(parameterIndex));
}

@Override
public Reader getNCharacterStream(String parameterName) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NCharacterStream not supported");
return getResult().getCharacterStream(nameToOutputIndex(parameterName));
}

@Override
Expand Down Expand Up @@ -636,32 +635,32 @@ public void setRowId(String parameterName, RowId rowid) throws SQLException {

@Override
public void setNString(String parameterName, String value) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NStrings not supported");
setString(nameToIndex(parameterName), value);
}

@Override
public void setNCharacterStream(String parameterName, Reader value, long length) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NCharacterStream not supported");
setCharacterStream(nameToIndex(parameterName), value, length);
}

@Override
public void setNCharacterStream(String parameterName, Reader value) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NCharacterStream not supported");
setCharacterStream(nameToIndex(parameterName), value);
}

@Override
public void setNClob(String parameterName, NClob value) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NClob not supported");
setClob(nameToIndex(parameterName), value);
}

@Override
public void setNClob(String parameterName, Reader reader, long length) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NClob not supported");
setClob(nameToIndex(parameterName), reader, length);
}

@Override
public void setNClob(String parameterName, Reader reader) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NCLOB not supported");
setClob(nameToIndex(parameterName), reader);
}

@Override
Expand Down
30 changes: 19 additions & 11 deletions src/main/java/org/mariadb/jdbc/AbstractMariaDbPrepareStatement.java
Expand Up @@ -562,7 +562,7 @@ public void setRowId(final int parameterIndex, final java.sql.RowId rowid) throw
* @since 1.6
*/
public void setNString(final int parameterIndex, final String value) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NStrings not supported");
setString(parameterIndex, value);
}

/**
Expand All @@ -580,7 +580,7 @@ public void setNString(final int parameterIndex, final String value) throws SQLE
* @throws java.sql.SQLFeatureNotSupportedException if the JDBC driver does not support this method
*/
public void setNCharacterStream(final int parameterIndex, final Reader value, final long length) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NCharstreams not supported");
setCharacterStream(parameterIndex, value, length);
}


Expand Down Expand Up @@ -618,7 +618,7 @@ public void setNCharacterStream(final int parameterIndex, final Reader value) th
* @throws java.sql.SQLFeatureNotSupportedException if the JDBC driver does not support this method
*/
public void setNClob(final int parameterIndex, final java.sql.NClob value) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NClobs not supported");
setClob(parameterIndex, value);
}

/**
Expand All @@ -641,7 +641,7 @@ public void setNClob(final int parameterIndex, final java.sql.NClob value) throw
* @throws java.sql.SQLFeatureNotSupportedException if the JDBC driver does not support this method
*/
public void setNClob(final int parameterIndex, final Reader reader, final long length) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NClobs not supported");
setClob(parameterIndex, reader, length);
}


Expand Down Expand Up @@ -727,7 +727,7 @@ public void setSQLXML(final int parameterIndex, final SQLXML xmlObject) throws S
* @see java.sql.Types
*/
public void setObject(final int parameterIndex, final Object obj, final int targetSqlType, final int scaleOrLength) throws SQLException {
setObject(parameterIndex, obj, targetSqlType);
setInternalObject(parameterIndex, obj, targetSqlType, scaleOrLength);
}

/**
Expand All @@ -749,16 +749,14 @@ public void setObject(final int parameterIndex, final Object obj, final int targ
* @see java.sql.Types
*/
public void setObject(final int parameterIndex, final Object obj, final int targetSqlType) throws SQLException {
setInternalObject(parameterIndex, obj, targetSqlType, Long.MAX_VALUE);
}

private void setInternalObject(final int parameterIndex, final Object obj, final int targetSqlType, final long scaleOrLength) throws SQLException {
switch (targetSqlType) {
case Types.ARRAY:
case Types.CLOB:
case Types.DATALINK:
case Types.JAVA_OBJECT:
case Types.NCHAR:
case Types.NCLOB:
case Types.NVARCHAR:
case Types.LONGNVARCHAR:
case Types.REF:
case Types.ROWID:
case Types.SQLXML:
Expand Down Expand Up @@ -803,9 +801,14 @@ public void setObject(final int parameterIndex, final Object obj, final int targ
case Types.NUMERIC:
setBigDecimal(parameterIndex, new BigDecimal(str));
break;
case Types.CLOB:
case Types.NCLOB:
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
case Types.NCHAR:
case Types.NVARCHAR:
case Types.LONGNVARCHAR:
setString(parameterIndex, str);
break;
case Types.TIMESTAMP:
Expand Down Expand Up @@ -897,11 +900,16 @@ public void setObject(final int parameterIndex, final Object obj, final int targ
setBlob(parameterIndex, (Blob) obj);
} else if (obj instanceof BigInteger) {
setString(parameterIndex, obj.toString());
} else if (obj instanceof Clob) {
setClob(parameterIndex, (Clob) obj);
} else if (obj instanceof InputStream) {
setBinaryStream(parameterIndex, (InputStream) obj, scaleOrLength);
} else if (obj instanceof Reader) {
setCharacterStream(parameterIndex, (Reader) obj, scaleOrLength);
} else {
throw ExceptionMapper.getSqlException("Could not set parameter in setObject, could not convert: " + obj.getClass() + " to "
+ targetSqlType);
}

}

/**
Expand Down
Expand Up @@ -92,11 +92,15 @@ public void send(OutputStream os) throws IOException, QueryException {

@Override
public void handleResultPacket(ReadPacketFetcher packetFetcher) throws QueryException, IOException {
Buffer buffer = packetFetcher.getReusableBuffer();
if (buffer.getByteAt(0) == Packet.ERROR) {
ErrorPacket ep = new ErrorPacket(buffer);
String message = ep.getMessage();
throw new QueryException("Could not connect: " + message, ep.getErrorNumber(), ep.getSqlState());
try {
Buffer buffer = packetFetcher.getReusableBuffer();
if (buffer.getByteAt(0) == Packet.ERROR) {
ErrorPacket ep = new ErrorPacket(buffer);
String message = ep.getMessage();
throw new QueryException("Could not connect: " + message, ep.getErrorNumber(), ep.getSqlState());
}
} catch (EOFException e) {
throw new QueryException("Authentication exception", 1045, "28000", e);
}
}

Expand Down
Expand Up @@ -54,6 +54,7 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS
import org.mariadb.jdbc.*;
import org.mariadb.jdbc.internal.MariaDbType;
import org.mariadb.jdbc.internal.packet.dao.ColumnInformation;
import org.mariadb.jdbc.internal.packet.dao.parameters.SerializableParameter;
import org.mariadb.jdbc.internal.packet.read.Packet;
import org.mariadb.jdbc.internal.packet.read.ReadPacketFetcher;
import org.mariadb.jdbc.internal.packet.result.*;
Expand Down Expand Up @@ -1716,8 +1717,57 @@ public Object getObject(String columnLabel, Map<String, Class<?>> map) throws SQ
}

@SuppressWarnings("unchecked")
public <T> T getObject(int columnIndex, Class<T> arg1) throws SQLException {
return (T) getObject(columnIndex);
public <T> T getObject(int parameterIndex, Class<T> type) throws SQLException {
if (type == null) throw new SQLException("Class type cannot be null");
if (type.equals(String.class)) {
return (T) getString(parameterIndex);
} else if (type.equals(Integer.class)) {
getInt(parameterIndex);
} else if (type.equals(Long.class)) {
return (T) (Long) getLong(parameterIndex);
} else if (type.equals(Short.class)) {
return (T) (Short) getShort(parameterIndex);
} else if (type.equals(Double.class)) {
return (T) (Double) getDouble(parameterIndex);
} else if (type.equals(Float.class)) {
return (T) (Float) getFloat(parameterIndex);
} else if (type.equals(Byte.class)) {
return (T) (Byte) getByte(parameterIndex);
} else if (type.equals(byte[].class)) {
return (T) getBytes(parameterIndex);
} else if (type.equals(Date.class)) {
return (T) getDate(parameterIndex);
} else if (type.equals(Time.class)) {
return (T) getTime(parameterIndex);
} else if (type.equals(Timestamp.class)) {
return (T) getTimestamp(parameterIndex);
} else if (type.equals(Boolean.class)) {
return (T) (Boolean) getBoolean(parameterIndex);
} else if (type.equals(Blob.class)) {
return (T) getBlob(parameterIndex);
} else if (type.equals(Clob.class)) {
return (T) getClob(parameterIndex);
} else if (type.equals(NClob.class)) {
return (T) getNClob(parameterIndex);
} else if (type.equals(InputStream.class)) {
return (T) getBinaryStream(parameterIndex);
} else if (type.equals(Reader.class)) {
return (T) getCharacterStream(parameterIndex);
} else if (type.equals(BigDecimal.class)) {
return (T) getBigDecimal(parameterIndex);
} else if (type.equals(BigInteger.class)) {
return (T) getBigInteger(checkObjectRange(parameterIndex), columnsInformation[parameterIndex - 1]);
} else if (type.equals(Clob.class)) {
return (T) getClob(parameterIndex);
}

Object obj = getObject(parameterIndex);
if (obj == null) return null;
if (obj.getClass().isInstance(type)) {
return (T) obj;
} else {
throw new SQLException("result cannot be cast as '" + type.getName() + "' (is '" + obj.getClass().getName()+"'");
}
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -1858,7 +1908,7 @@ public Reader getNCharacterStream(int columnIndex) throws SQLException {
* {inheritDoc}.
*/
public Reader getNCharacterStream(String columnLabel) throws SQLException {
return getCharacterStream(columnLabel);
return getCharacterStream(findColumn(columnLabel));
}

/**
Expand Down Expand Up @@ -2576,14 +2626,16 @@ public void updateNClob(String columnLabel, Reader reader, long length) throws S
* {inheritDoc}.
*/
public NClob getNClob(int columnIndex) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NClobs are not supported");
byte[] bytes = checkObjectRange(columnIndex);
if (bytes == null) return null;
return new MariaDbClob(bytes);
}

/**
* {inheritDoc}.
*/
public NClob getNClob(String columnLabel) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NClobs are not supported");
return getNClob(findColumn(columnLabel));
}

/**
Expand Down Expand Up @@ -2622,14 +2674,14 @@ public void updateSQLXML(String columnLabel, SQLXML xmlObject) throws SQLExcepti
* {inheritDoc}.
*/
public String getNString(int columnIndex) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NString not supported");
return getString(columnIndex);
}

/**
* {inheritDoc}.
*/
public String getNString(String columnLabel) throws SQLException {
throw ExceptionMapper.getFeatureNotSupportedException("NString not supported");
return getString(findColumn(columnLabel));
}

/**
Expand Down

0 comments on commit 3616061

Please sign in to comment.