Skip to content

Commit

Permalink
[CONJ-225] ResultSet.getString() correction on Numeric and blog field
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed Nov 30, 2015
1 parent c974138 commit 455f36f
Show file tree
Hide file tree
Showing 4 changed files with 395 additions and 218 deletions.
Expand Up @@ -123,10 +123,32 @@ public String getString(Calendar cal) {

switch (columnInfo.getType()) {
case BIT:
if (columnInfo.getLength() == 1) {
if (options.tinyInt1isBit && columnInfo.getLength() == 1) {
return (rawBytes[0] == 0) ? "0" : "1";
}
break;
case TINYINT:
if (options.tinyInt1isBit && columnInfo.getLength() == 1) {
return (rawBytes[0] == 0) ? "0" : "1";
}
return String.valueOf(getInt());
case SMALLINT:
case MEDIUMINT:
return String.valueOf(getInt());
case INTEGER:
if (!columnInfo.isSigned()) {
return String.valueOf(getLong());
}
return String.valueOf(getInt());
case BIGINT:
if (!columnInfo.isSigned()) {
return String.valueOf(getBigInteger());
}
return String.valueOf(getLong());
case DOUBLE:
return String.valueOf(getDouble());
case FLOAT:
return String.valueOf(getFloat());
case TIME:
return getTimeString();
case DATE:
Expand All @@ -137,17 +159,35 @@ public String getString(Calendar cal) {
}
}
break;
case TIMESTAMP:
case DATETIME:
if (isBinaryEncoded) {
case YEAR:
if (options.yearIsDateType) {
try {
return getTimestamp(cal).toString();
return getDate(cal).toString();
} catch (ParseException e) {
}
}
return String.valueOf(getShort());
case TIMESTAMP:
case DATETIME:
try {
return getTimestamp(cal).toString();
} catch (ParseException e) {
}
break;
case DECIMAL:
return getBigDecimal().toString();
case BLOB:
case LONGBLOB:
case MEDIUMBLOB:
case TINYBLOB:
case GEOMETRY:
return new String(getBytes());
case NULL:
return null;
case OLDDECIMAL:
return getString();
default:
break;
return new String(rawBytes, StandardCharsets.UTF_8);
}
return new String(rawBytes, StandardCharsets.UTF_8);
}
Expand Down Expand Up @@ -212,6 +252,8 @@ private String getTimeString() {
return (negative ? "-" : "") + (hourString + ":" + minuteString + ":" + secondString);
}



/**
* Get byte from raw data.
* @return byte
Expand All @@ -224,18 +266,7 @@ public byte getByte() {
if (dataType == MariaDbType.BIT) {
return rawBytes[0];
}
try {
return Byte.valueOf(new String(rawBytes, StandardCharsets.UTF_8));
} catch (NumberFormatException nfe) {
BigDecimal value = new BigDecimal(new String(rawBytes, StandardCharsets.UTF_8));
if (value.compareTo(BigDecimal.valueOf(Byte.MIN_VALUE)) < 0) {
return Byte.MIN_VALUE;
}
if (value.compareTo(BigDecimal.valueOf(Byte.MAX_VALUE)) > 0) {
return Byte.MAX_VALUE;
}
return value.byteValue();
}
return parseByte();
} else {
switch (dataType) {
case BIT:
Expand All @@ -259,22 +290,26 @@ public byte getByte() {
case DOUBLE:
return (byte) getDouble();
default:
try {
return Byte.valueOf(new String(rawBytes, StandardCharsets.UTF_8));
} catch (NumberFormatException nfe) {
BigDecimal value = new BigDecimal(new String(rawBytes, StandardCharsets.UTF_8));
if (value.compareTo(BigDecimal.valueOf(Byte.MIN_VALUE)) < 0) {
return Byte.MIN_VALUE;
}
if (value.compareTo(BigDecimal.valueOf(Byte.MAX_VALUE)) > 0) {
return Byte.MAX_VALUE;
}
return value.byteValue();
}
return parseByte();
}
}
}

private byte parseByte() {
try {
return Byte.valueOf(new String(rawBytes, StandardCharsets.UTF_8));
} catch (NumberFormatException nfe) {
BigDecimal value = new BigDecimal(new String(rawBytes, StandardCharsets.UTF_8));
if (value.compareTo(BigDecimal.valueOf(Byte.MIN_VALUE)) < 0) {
return Byte.MIN_VALUE;
}
if (value.compareTo(BigDecimal.valueOf(Byte.MAX_VALUE)) > 0) {
return Byte.MAX_VALUE;
}
return value.byteValue();
}
}

/**
* Get short from raw data.
* @return short
Expand All @@ -284,18 +319,7 @@ public short getShort() {
return 0;
}
if (!this.isBinaryEncoded) {
try {
return Short.valueOf(new String(rawBytes, StandardCharsets.UTF_8));
} catch (NumberFormatException nfe) {
BigDecimal value = new BigDecimal(new String(rawBytes, StandardCharsets.UTF_8));
if (value.compareTo(BigDecimal.valueOf(Short.MIN_VALUE)) < 0) {
return Short.MIN_VALUE;
}
if (value.compareTo(BigDecimal.valueOf(Short.MAX_VALUE)) > 0) {
return Short.MAX_VALUE;
}
return value.shortValue();
}
return parseShort();
} else {
switch (dataType) {
case BIT:
Expand All @@ -320,22 +344,27 @@ public short getShort() {
case DOUBLE:
return (short) getDouble();
default:
try {
return Short.valueOf(new String(rawBytes, StandardCharsets.UTF_8));
} catch (NumberFormatException nfe) {
BigDecimal bigdecimal = new BigDecimal(new String(rawBytes, StandardCharsets.UTF_8));
if (bigdecimal.compareTo(BigDecimal.valueOf(Short.MIN_VALUE)) < 0) {
return Short.MIN_VALUE;
}
if (bigdecimal.compareTo(BigDecimal.valueOf(Short.MAX_VALUE)) > 0) {
return Short.MAX_VALUE;
}
return bigdecimal.shortValue();
}
return parseShort();
}
}
}

private short parseShort() {
try {
return Short.valueOf(new String(rawBytes, StandardCharsets.UTF_8));
} catch (NumberFormatException nfe) {
BigDecimal bigdecimal = new BigDecimal(new String(rawBytes, StandardCharsets.UTF_8));
if (bigdecimal.compareTo(BigDecimal.valueOf(Short.MIN_VALUE)) < 0) {
return Short.MIN_VALUE;
}
if (bigdecimal.compareTo(BigDecimal.valueOf(Short.MAX_VALUE)) > 0) {
return Short.MAX_VALUE;
}
return bigdecimal.shortValue();
}

}

/**
* Get int from raw data.
* @return int
Expand All @@ -345,18 +374,7 @@ public int getInt() {
return 0;
}
if (!this.isBinaryEncoded) {
try {
return Integer.valueOf(new String(rawBytes, StandardCharsets.UTF_8));
} catch (NumberFormatException nfe) {
BigDecimal value = new BigDecimal(new String(rawBytes, StandardCharsets.UTF_8));
if (value.compareTo(BigDecimal.valueOf(Integer.MIN_VALUE)) < 0) {
return Integer.MIN_VALUE;
}
if (value.compareTo(BigDecimal.valueOf(Integer.MAX_VALUE)) > 0) {
return Integer.MAX_VALUE;
}
return value.intValue();
}
return parseInt();
} else {
switch (dataType) {
case BIT:
Expand Down Expand Up @@ -384,22 +402,26 @@ public int getInt() {
case DOUBLE:
return (int) getDouble();
default:
try {
return Integer.valueOf(new String(rawBytes, StandardCharsets.UTF_8));
} catch (NumberFormatException nfe) {
BigDecimal bigdecimal = new BigDecimal(new String(rawBytes, StandardCharsets.UTF_8));
if (bigdecimal.compareTo(BigDecimal.valueOf(Integer.MIN_VALUE)) < 0) {
return Integer.MIN_VALUE;
}
if (bigdecimal.compareTo(BigDecimal.valueOf(Integer.MAX_VALUE)) > 0) {
return Integer.MAX_VALUE;
}
return bigdecimal.intValue();
}
return parseInt();
}
}
}

private int parseInt() {
try {
return Integer.valueOf(new String(rawBytes, StandardCharsets.UTF_8));
} catch (NumberFormatException nfe) {
BigDecimal bigdecimal = new BigDecimal(new String(rawBytes, StandardCharsets.UTF_8));
if (bigdecimal.compareTo(BigDecimal.valueOf(Integer.MIN_VALUE)) < 0) {
return Integer.MIN_VALUE;
}
if (bigdecimal.compareTo(BigDecimal.valueOf(Integer.MAX_VALUE)) > 0) {
return Integer.MAX_VALUE;
}
return bigdecimal.intValue();
}
}

/**
* Get long from raw data.
* @return long
Expand All @@ -409,18 +431,7 @@ public long getLong() {
return 0;
}
if (!this.isBinaryEncoded) {
try {
return Long.valueOf(new String(rawBytes, StandardCharsets.UTF_8));
} catch (NumberFormatException nfe) {
BigDecimal bigdecimal = new BigDecimal(new String(rawBytes, StandardCharsets.UTF_8));
if (bigdecimal.compareTo(BigDecimal.valueOf(Long.MIN_VALUE)) < 0) {
return Long.MIN_VALUE;
}
if (bigdecimal.compareTo(BigDecimal.valueOf(Long.MAX_VALUE)) > 0) {
return Long.MAX_VALUE;
}
return bigdecimal.longValue();
}
return parseLong();
} else {
switch (dataType) {
case BIT:
Expand Down Expand Up @@ -456,19 +467,23 @@ public long getLong() {
case DOUBLE:
return (long) getDouble();
default:
try {
return Long.valueOf(new String(rawBytes, StandardCharsets.UTF_8));
} catch (NumberFormatException nfe) {
BigDecimal bigdecimal = new BigDecimal(new String(rawBytes, StandardCharsets.UTF_8));
if (bigdecimal.compareTo(BigDecimal.valueOf(Long.MIN_VALUE)) < 0) {
return Long.MIN_VALUE;
}
if (bigdecimal.compareTo(BigDecimal.valueOf(Long.MAX_VALUE)) > 0) {
return Long.MAX_VALUE;
}
return bigdecimal.longValue();
}
return parseLong();
}
}
}

private long parseLong() {
try {
return Long.valueOf(new String(rawBytes, StandardCharsets.UTF_8));
} catch (NumberFormatException nfe) {
BigDecimal bigdecimal = new BigDecimal(new String(rawBytes, StandardCharsets.UTF_8));
if (bigdecimal.compareTo(BigDecimal.valueOf(Long.MIN_VALUE)) < 0) {
return Long.MIN_VALUE;
}
if (bigdecimal.compareTo(BigDecimal.valueOf(Long.MAX_VALUE)) > 0) {
return Long.MAX_VALUE;
}
return bigdecimal.longValue();
}
}

Expand Down Expand Up @@ -1072,7 +1087,7 @@ public Clob getClob() {
* @throws ParseException if data cannot be parse
*/
public Object getObject(int datatypeMappingFlags, Calendar cal) throws ParseException {
if (this.getBytes() == null) {
if (rawBytes == null) {
return null;
}
switch (dataType) {
Expand Down
@@ -1,10 +1,10 @@
package org.mariadb.jdbc.internal.util.constant;

public final class Version {
public static final String version = "1.3.2-SNAPSHOT";
public static final String version = "1.3.3-SNAPSHOT";
public static final int majorVersion = 1;
public static final int minorVersion = 3;
public static final int patchVersion = 2;
public static final int patchVersion = 3;
public static final String qualifier = "SNAPSHOT";

}

0 comments on commit 455f36f

Please sign in to comment.