Skip to content

Commit

Permalink
[CONJ-425] CallableStatement getObject class according to java.sql.Ty…
Browse files Browse the repository at this point in the history
…pes value
  • Loading branch information
rusher committed Feb 7, 2017
1 parent a95e3ba commit daf7c38
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 36 deletions.
2 changes: 2 additions & 0 deletions documentation/changelog.creole
Expand Up @@ -24,6 +24,8 @@
* CONJ-418 : ResultSet.last() isLast() afterLast() and isAfterLast() correction when streaming
* CONJ-415 : ResultSet.absolute() should not always return true
* CONJ-392 : Aurora cluster endpoint detection fails when time_zone doesn't match system_time_zone
* CONJ-425 : CallableStatement getObject class according to java.sql.Types value
== 1.5.7
* CONJ-407 : handling failover when packet > max_allowed_packet reset the connection state.
Expand Down
Expand Up @@ -52,6 +52,7 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS

package org.mariadb.jdbc;

import org.mariadb.jdbc.internal.MariaDbType;
import org.mariadb.jdbc.internal.queryresults.resultset.MariaSelectResultSet;
import org.mariadb.jdbc.internal.util.ExceptionMapper;

Expand Down Expand Up @@ -126,10 +127,11 @@ public ParameterMetaData getParameterMetaData() throws SQLException {
* @throws SQLException exception
*/
private int nameToIndex(String parameterName) throws SQLException {
parameterMetadata.readMetadataFromDbIfRequired();
for (int i = 1; i <= parameterMetadata.getParameterCount(); i++) {
String name = parameterMetadata.getName(i + 1);
String name = parameterMetadata.getName(i);
if (name != null && name.equalsIgnoreCase(parameterName)) {
return i + 1;
return i;
}
}
throw new SQLException("there is no parameter with the name " + parameterName);
Expand Down Expand Up @@ -337,20 +339,29 @@ public Timestamp getTimestamp(String parameterName, Calendar cal) throws SQLExce
return getResult().getTimestamp(nameToOutputIndex(parameterName), cal);
}


@Override
public Object getObject(int parameterIndex) throws SQLException {
Class<?> classType = MariaDbType.classFromJavaType(getParameter(parameterIndex).outputSqlType);
if (classType != null) {
return getResult().getObject(indexToOutputIndex(parameterIndex), classType);
}
return getResult().getObject(indexToOutputIndex(parameterIndex));

}

@Override
public Object getObject(int parameterIndex, Map<String, Class<?>> map) throws SQLException {
return getResult().getObject(indexToOutputIndex(parameterIndex), map);
public Object getObject(String parameterName) throws SQLException {
int index = nameToIndex(parameterName);
Class<?> classType = MariaDbType.classFromJavaType(getParameter(index).outputSqlType);
if (classType != null) {
return getResult().getObject(indexToOutputIndex(index), classType);
}
return getResult().getObject(indexToOutputIndex(index));
}

@Override
public Object getObject(String parameterName) throws SQLException {
return getResult().getObject(nameToOutputIndex(parameterName));
public Object getObject(int parameterIndex, Map<String, Class<?>> map) throws SQLException {
return getResult().getObject(indexToOutputIndex(parameterIndex), map);
}

@Override
Expand Down Expand Up @@ -523,7 +534,7 @@ public Reader getCharacterStream(String parameterName) throws SQLException {
*/
public void registerOutParameter(int parameterIndex, int sqlType, String typeName) throws SQLException {
CallParameter callParameter = getParameter(parameterIndex);
callParameter.sqlType = sqlType;
callParameter.outputSqlType = sqlType;
callParameter.typeName = typeName;
callParameter.isOutput = true;
}
Expand Down
Expand Up @@ -52,6 +52,7 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS

package org.mariadb.jdbc;

import org.mariadb.jdbc.internal.MariaDbType;
import org.mariadb.jdbc.internal.queryresults.resultset.MariaSelectResultSet;
import org.mariadb.jdbc.internal.util.ExceptionMapper;

Expand Down Expand Up @@ -365,22 +366,31 @@ public Timestamp getTimestamp(String parameterName, Calendar cal) throws SQLExce
return getResult().getTimestamp(nameToOutputIndex(parameterName), cal);
}


@Override
public Object getObject(int parameterIndex) throws SQLException {
return getResult().getObject(indexToOutputIndex(parameterIndex));
public Object getObject(int parameterIndex, Map<String, Class<?>> map) throws SQLException {
return getResult().getObject(indexToOutputIndex(parameterIndex), map);
}

@Override
public Object getObject(int parameterIndex, Map<String, Class<?>> map) throws SQLException {
return getResult().getObject(indexToOutputIndex(parameterIndex), map);
public Object getObject(int parameterIndex) throws SQLException {
Class<?> classType = MariaDbType.classFromJavaType(getParameter(parameterIndex).outputSqlType);
if (classType != null) {
return getResult().getObject(indexToOutputIndex(parameterIndex), classType);
}
return getResult().getObject(indexToOutputIndex(parameterIndex));
}

@Override
public Object getObject(String parameterName) throws SQLException {
return getResult().getObject(nameToOutputIndex(parameterName));
int index = nameToIndex(parameterName);
Class<?> classType = MariaDbType.classFromJavaType(getParameter(index).outputSqlType);
if (classType != null) {
return getResult().getObject(indexToOutputIndex(index), classType);
}
return getResult().getObject(indexToOutputIndex(index));
}


@Override
public Object getObject(String parameterName, Map<String, Class<?>> map) throws SQLException {
return getResult().getObject(nameToOutputIndex(parameterName), map);
Expand Down Expand Up @@ -551,7 +561,7 @@ public Reader getCharacterStream(String parameterName) throws SQLException {
*/
public void registerOutParameter(int parameterIndex, int sqlType, String typeName) throws SQLException {
CallParameter callParameter = getParameter(parameterIndex);
callParameter.sqlType = sqlType;
callParameter.outputSqlType = sqlType;
callParameter.typeName = typeName;
callParameter.isOutput = true;
}
Expand Down
83 changes: 83 additions & 0 deletions src/main/java/org/mariadb/jdbc/internal/MariaDbType.java
Expand Up @@ -53,6 +53,9 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;


Expand Down Expand Up @@ -104,6 +107,86 @@ public enum MariaDbType {
this.className = className;
}

/**
* Permit to know java result class according to java.sql.Types.
*
* @param type java.sql.Type value
* @return Class name.
*/
public static Class classFromJavaType(int type) {
switch (type) {
case Types.BOOLEAN:
case Types.BIT:
return Boolean.class;

case Types.TINYINT:
return Byte.class;

case Types.SMALLINT:
return Short.class;

case Types.INTEGER:
return Integer.class;

case Types.BIGINT:
return Long.class;

case Types.DOUBLE:
case Types.FLOAT:
return Double.class;

case Types.REAL:
return Float.class;

case Types.TIMESTAMP:
return Timestamp.class;

case Types.DATE:
return Date.class;

case Types.VARCHAR:
case Types.NVARCHAR:
case Types.CHAR:
case Types.NCHAR:
case Types.LONGVARCHAR:
case Types.LONGNVARCHAR:
case Types.CLOB:
case Types.NCLOB:
return String.class;

case Types.DECIMAL:
case Types.NUMERIC:
return BigDecimal.class;

case Types.VARBINARY:
case Types.BINARY:
case Types.LONGVARBINARY:
case Types.BLOB:
case Types.JAVA_OBJECT:
return byte[].class;

case Types.NULL:
return null;

case Types.TIME:
return Time.class;

default:
//DISTINCT
//STRUCT
//ARRAY
//REF
//DATALINK
//ROWID
//SQLXML
//REF_CURSOR
//TIME_WITH_TIMEZONE
//TIMESTAMP_WITH_TIMEZONE
break;
}
return null;
}

/**
* Is type numeric.
* @param type mariadb type
Expand Down
Expand Up @@ -1872,54 +1872,104 @@ public Object getObject(String columnLabel, Map<String, Class<?>> map) throws SQ
@SuppressWarnings("unchecked")
public <T> T getObject(int parameterIndex, Class<T> type) throws SQLException {
if (type == null) throw new SQLException("Class type cannot be null");
byte[] rawBytes = checkObjectRange(parameterIndex);
ColumnInformation col = columnsInformation[parameterIndex - 1];

if (type.equals(String.class)) {
return (T) getString(parameterIndex);
return (T) getString(rawBytes, col, cal);

} else if (type.equals(Integer.class)) {
getInt(parameterIndex);
if (rawBytes == null) return null;
return (T) (Integer) getInt(rawBytes, col);

} else if (type.equals(Long.class)) {
return (T) (Long) getLong(parameterIndex);
if (rawBytes == null) return null;
return (T) (Long) getLong(rawBytes, col);

} else if (type.equals(Short.class)) {
return (T) (Short) getShort(parameterIndex);
if (rawBytes == null) return null;
return (T) (Short) getShort(rawBytes, col);

} else if (type.equals(Double.class)) {
return (T) (Double) getDouble(parameterIndex);
if (rawBytes == null) return null;
return (T) (Double) getDouble(rawBytes, col);

} else if (type.equals(Float.class)) {
return (T) (Float) getFloat(parameterIndex);
if (rawBytes == null) return null;
return (T) (Float) getFloat(rawBytes, col);

} else if (type.equals(Byte.class)) {
return (T) (Byte) getByte(parameterIndex);
return (T) (Byte) getByte(rawBytes, col);

} else if (type.equals(byte[].class)) {
return (T) getBytes(parameterIndex);
return (T) rawBytes;

} else if (type.equals(Date.class)) {
return (T) getDate(parameterIndex);
try {
return (T) getDate(rawBytes, col, cal);
} catch (ParseException e) {
throw ExceptionMapper.getSqlException("Could not parse column as date, was: \""
+ getString(rawBytes, col)
+ "\"", e);
}

} else if (type.equals(Time.class)) {
return (T) getTime(parameterIndex);
try {
return (T) getTime(rawBytes, col, cal);
} catch (ParseException e) {
throw ExceptionMapper.getSqlException("Could not parse column as time, was: \""
+ getString(rawBytes, col)
+ "\"", e);
}
} else if (type.equals(Timestamp.class)) {
return (T) getTimestamp(parameterIndex);
try {
return (T) getTimestamp(rawBytes, col, cal);
} catch (ParseException e) {
throw ExceptionMapper.getSqlException("Could not parse column as timestamp, was: \""
+ getString(rawBytes, col)
+ "\"", e);
}

} else if (type.equals(Boolean.class)) {
return (T) (Boolean) getBoolean(parameterIndex);
return (T) (Boolean) getBoolean(rawBytes, col);
} else if (type.equals(Blob.class)) {
return (T) getBlob(parameterIndex);
if (rawBytes == null) return null;
return (T) new MariaDbBlob(rawBytes);

} else if (type.equals(Clob.class)) {
return (T) getClob(parameterIndex);
if (rawBytes == null) return null;
return (T) new MariaDbClob(rawBytes);

} else if (type.equals(NClob.class)) {
return (T) getNClob(parameterIndex);
if (rawBytes == null) return null;
return (T) new MariaDbClob(rawBytes);

} else if (type.equals(InputStream.class)) {
return (T) getBinaryStream(parameterIndex);
if (rawBytes == null) return null;
return (T) new ByteArrayInputStream(rawBytes);

} else if (type.equals(Reader.class)) {
return (T) getCharacterStream(parameterIndex);
String value = getString(rawBytes, col);
if (value == null) return null;
return (T) new StringReader(value);

} else if (type.equals(BigDecimal.class)) {
return (T) getBigDecimal(parameterIndex);
return (T) getBigDecimal(rawBytes, col);

} else if (type.equals(BigInteger.class)) {
return (T) getBigInteger(checkObjectRange(parameterIndex), columnsInformation[parameterIndex - 1]);
return (T) getBigInteger(rawBytes, col);

} else if (type.equals(Clob.class)) {
return (T) getClob(parameterIndex);
if (rawBytes == null) return null;
return (T) new MariaDbClob(rawBytes);
}

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() + "'");
throw new SQLException("result cannot be cast as '" + type.getName() + "' (is '" + obj.getClass().getName() + "')");
}
}

Expand Down

0 comments on commit daf7c38

Please sign in to comment.