Modified:
branches/branch_3_1/connector-j/src/com/mysql/jdbc/ResultSet.java
Log:
Merge of unsigned native integral values from 5.0 branch when using prepared statements.
Modified: branches/branch_3_1/connector-j/src/com/mysql/jdbc/ResultSet.java
===================================================================
--- branches/branch_3_1/connector-j/src/com/mysql/jdbc/ResultSet.java 2006-01-18 01:03:32 UTC (rev 4818)
+++ branches/branch_3_1/connector-j/src/com/mysql/jdbc/ResultSet.java 2006-01-18 16:49:17 UTC (rev 4819)
@@ -2971,148 +2971,6 @@
}
/**
- * Get the value of a column in the current row as a Java byte.
- *
- * @param columnIndex
- * the first column is 1, the second is 2,...
- *
- * @return the column value; 0 if SQL NULL
- *
- * @exception SQLException
- * if a database access error occurs
- */
- protected byte getNativeByte(int columnIndex) throws SQLException {
- checkRowPos();
-
- checkColumnBounds(columnIndex);
-
- if (this.thisRow[columnIndex - 1] == null) {
- this.wasNullFlag = true;
-
- return 0;
- }
-
- try {
- if (this.thisRow[columnIndex - 1] == null) {
- this.wasNullFlag = true;
- } else {
- this.wasNullFlag = false;
- }
- } catch (NullPointerException E) {
- this.wasNullFlag = true;
- }
-
- if (this.wasNullFlag) {
- return 0;
- }
-
- columnIndex--;
-
- Field field = this.fields[columnIndex];
-
- switch (field.getMysqlType()) {
- case MysqlDefs.FIELD_TYPE_TINY:
-
- byte valueAsByte = ((byte[]) this.thisRow[columnIndex])[0];
-
- if (field.isUnsigned()) {
- short valueAsShort = (short)(valueAsByte + (short)256);
-
- if (this.connection.getJdbcCompliantTruncation()) {
- if (valueAsShort > Byte.MAX_VALUE) {
- throwRangeException(String.valueOf(valueAsShort),
- columnIndex + 1, Types.TINYINT);
- }
- }
-
- return (byte)valueAsShort;
- }
-
- return valueAsByte;
- case MysqlDefs.FIELD_TYPE_SHORT:
- case MysqlDefs.FIELD_TYPE_YEAR:
- int valueAsShort = getNativeShort(columnIndex + 1);
-
- if (this.connection.getJdbcCompliantTruncation()) {
- if (valueAsShort < Byte.MIN_VALUE
- || valueAsShort > Byte.MAX_VALUE) {
- throwRangeException(String.valueOf(valueAsShort),
- columnIndex + 1, Types.TINYINT);
- }
- }
-
- return (byte) valueAsShort;
- case MysqlDefs.FIELD_TYPE_INT24:
- case MysqlDefs.FIELD_TYPE_LONG:
- int valueAsInt = getNativeInt(columnIndex + 1);
-
- if (this.connection.getJdbcCompliantTruncation()) {
- if (valueAsInt < Byte.MIN_VALUE || valueAsInt > Byte.MAX_VALUE) {
- throwRangeException(String.valueOf(valueAsInt),
- columnIndex + 1, Types.TINYINT);
- }
- }
-
- return (byte) valueAsInt;
-
- case MysqlDefs.FIELD_TYPE_FLOAT:
- float valueAsFloat = getNativeFloat(columnIndex + 1);
-
- if (this.connection.getJdbcCompliantTruncation()) {
- if (valueAsFloat < Byte.MIN_VALUE
- || valueAsFloat > Byte.MAX_VALUE) {
-
- throwRangeException(String.valueOf(valueAsFloat),
- columnIndex + 1, Types.TINYINT);
- }
- }
-
- return (byte) valueAsFloat;
-
- case MysqlDefs.FIELD_TYPE_DOUBLE:
- double valueAsDouble = getNativeDouble(columnIndex + 1);
-
- if (this.connection.getJdbcCompliantTruncation()) {
- if (valueAsDouble < Byte.MIN_VALUE
- || valueAsDouble > Byte.MAX_VALUE) {
- throwRangeException(String.valueOf(valueAsDouble),
- columnIndex + 1, Types.TINYINT);
- }
- }
-
- return (byte) valueAsDouble;
-
- case MysqlDefs.FIELD_TYPE_LONGLONG:
- long valueAsLong = getNativeLong(columnIndex + 1);
-
- if (this.connection.getJdbcCompliantTruncation()) {
- if (valueAsLong < Byte.MIN_VALUE
- || valueAsLong > Byte.MAX_VALUE) {
- throwRangeException(String.valueOf(valueAsLong),
- columnIndex + 1, Types.TINYINT);
- }
- }
-
- return (byte) valueAsLong;
-
- default:
- if (this.useUsageAdvisor) {
- issueConversionViaParsingWarning("getByte()", columnIndex,
- this.thisRow[columnIndex], this.fields[columnIndex],
- new int[] { MysqlDefs.FIELD_TYPE_DOUBLE,
- MysqlDefs.FIELD_TYPE_TINY,
- MysqlDefs.FIELD_TYPE_SHORT,
- MysqlDefs.FIELD_TYPE_LONG,
- MysqlDefs.FIELD_TYPE_LONGLONG,
- MysqlDefs.FIELD_TYPE_FLOAT });
- }
-
- return getByteFromString(getNativeString(columnIndex + 1),
- columnIndex + 1);
- }
- }
-
- /**
* Get the value of a column in the current row as a Java byte array.
*
* <p>
@@ -3239,7 +3097,7 @@
return String.valueOf(booleanVal);
case Types.TINYINT:
- byte tinyintVal = getNativeByte(columnIndex);
+ byte tinyintVal = getNativeByte(columnIndex, false);
if (this.wasNullFlag) {
return null;
@@ -3255,7 +3113,7 @@
case Types.SMALLINT:
- int intVal = getNativeInt(columnIndex);
+ int intVal = getNativeInt(columnIndex, false);
if (this.wasNullFlag) {
return null;
@@ -3270,7 +3128,7 @@
return String.valueOf(intVal);
case Types.INTEGER:
- intVal = getNativeInt(columnIndex);
+ intVal = getNativeInt(columnIndex, false);
if (this.wasNullFlag) {
return null;
@@ -3289,7 +3147,7 @@
case Types.BIGINT:
if (!field.isUnsigned()) {
- longVal = getNativeLong(columnIndex);
+ longVal = getNativeLong(columnIndex, false, true);
if (this.wasNullFlag) {
return null;
@@ -3298,7 +3156,7 @@
return String.valueOf(longVal);
}
- longVal = getNativeLong(columnIndex);
+ longVal = getNativeLong(columnIndex, false, false);
if (this.wasNullFlag) {
return null;
@@ -3707,236 +3565,6 @@
}
/**
- * Get the value of a column in the current row as a Java int.
- *
- * @param columnIndex
- * the first column is 1, the second is 2,...
- *
- * @return the column value; 0 if SQL NULL
- *
- * @exception SQLException
- * if a database access error occurs
- */
- protected int getNativeInt(int columnIndex) throws SQLException {
- checkRowPos();
- checkColumnBounds(columnIndex);
-
- columnIndex--; // / JDBC is 1-based
-
- if (this.thisRow[columnIndex] == null) {
- this.wasNullFlag = true;
-
- return 0;
- }
-
- this.wasNullFlag = false;
-
- Field f = this.fields[columnIndex];
-
- switch (f.getMysqlType()) {
-
- case MysqlDefs.FIELD_TYPE_TINY:
- byte tinyintVal = getNativeByte(columnIndex + 1);
-
- if (!f.isUnsigned() || tinyintVal >= 0) {
- return tinyintVal;
- }
-
- return tinyintVal + 256;
- case MysqlDefs.FIELD_TYPE_SHORT:
- short asShort = getNativeShort(columnIndex + 1);
-
- if (!f.isUnsigned() || asShort >= 0) {
- return asShort;
- }
-
- return asShort + 65536;
- case MysqlDefs.FIELD_TYPE_YEAR:
- return getNativeShort(columnIndex + 1);
- case MysqlDefs.FIELD_TYPE_INT24:
- case MysqlDefs.FIELD_TYPE_LONG:
- byte[] bits = (byte[]) this.thisRow[columnIndex];
-
- int valueAsInt = (bits[0] & 0xff) | ((bits[1] & 0xff) << 8)
- | ((bits[2] & 0xff) << 16) | ((bits[3] & 0xff) << 24);
-
- return valueAsInt;
- case MysqlDefs.FIELD_TYPE_LONGLONG:
- long valueAsLong = getNativeLong(columnIndex + 1);
-
- if (this.connection.getJdbcCompliantTruncation()) {
- if (valueAsLong < Integer.MIN_VALUE
- || valueAsLong > Integer.MAX_VALUE) {
- throwRangeException(String.valueOf(valueAsLong),
- columnIndex + 1, Types.INTEGER);
- }
- }
-
- return (int) valueAsLong;
- case MysqlDefs.FIELD_TYPE_DOUBLE:
- double valueAsDouble = getNativeDouble(columnIndex + 1);
-
- if (this.connection.getJdbcCompliantTruncation()) {
- if (valueAsDouble < Integer.MIN_VALUE
- || valueAsDouble > Integer.MAX_VALUE) {
- throwRangeException(String.valueOf(valueAsDouble),
- columnIndex + 1, Types.INTEGER);
- }
- }
-
- return (int) valueAsDouble;
- case MysqlDefs.FIELD_TYPE_FLOAT:
- float valueAsFloat = getNativeFloat(columnIndex + 1);
-
- if (this.connection.getJdbcCompliantTruncation()) {
- if (valueAsFloat < Integer.MIN_VALUE
- || valueAsFloat > Integer.MAX_VALUE) {
- throwRangeException(String.valueOf(valueAsFloat),
- columnIndex + 1, Types.INTEGER);
- }
- }
-
- return (int) valueAsFloat;
-
- default:
-
- if (this.useUsageAdvisor) {
- issueConversionViaParsingWarning("getInt()", columnIndex,
- this.thisRow[columnIndex], this.fields[columnIndex],
- new int[] { MysqlDefs.FIELD_TYPE_DOUBLE,
- MysqlDefs.FIELD_TYPE_TINY,
- MysqlDefs.FIELD_TYPE_SHORT,
- MysqlDefs.FIELD_TYPE_LONG,
- MysqlDefs.FIELD_TYPE_LONGLONG,
- MysqlDefs.FIELD_TYPE_FLOAT });
- }
-
- String stringVal = getNativeString(columnIndex + 1);
-
- return getIntFromString(stringVal, columnIndex + 1);
- }
- }
-
- /**
- * Get the value of a column in the current row as a Java long.
- *
- * @param columnIndex
- * the first column is 1, the second is 2,...
- *
- * @return the column value; 0 if SQL NULL
- *
- * @exception SQLException
- * if a database access error occurs
- */
- protected long getNativeLong(int columnIndex) throws SQLException {
- checkRowPos();
- checkColumnBounds(columnIndex);
-
- columnIndex--; // / JDBC is 1-based
-
- if (this.thisRow[columnIndex] == null) {
- this.wasNullFlag = true;
-
- return 0;
- }
-
- this.wasNullFlag = false;
-
- Field f = this.fields[columnIndex];
-
- switch (f.getMysqlType()) {
- case MysqlDefs.FIELD_TYPE_TINY:
- if (!f.isUnsigned()) {
- return getNativeByte(columnIndex + 1);
- }
-
- return getNativeInt(columnIndex + 1);
- case MysqlDefs.FIELD_TYPE_SHORT:
- if (!f.isUnsigned()) {
- return getNativeShort(columnIndex + 1);
- }
-
- return getNativeInt(columnIndex + 1);
- case MysqlDefs.FIELD_TYPE_YEAR:
-
- return getNativeShort(columnIndex + 1);
- case MysqlDefs.FIELD_TYPE_INT24:
- case MysqlDefs.FIELD_TYPE_LONG:
- int asInt = getNativeInt(columnIndex + 1);
-
- if (!f.isUnsigned() || asInt >= 0) {
- return asInt;
- }
-
- return asInt + 4294967296L;
- case MysqlDefs.FIELD_TYPE_LONGLONG:
-
- byte[] bits = (byte[]) this.thisRow[columnIndex];
-
- long valueAsLong = (bits[0] & 0xff)
- | ((long) (bits[1] & 0xff) << 8)
- | ((long) (bits[2] & 0xff) << 16)
- | ((long) (bits[3] & 0xff) << 24)
- | ((long) (bits[4] & 0xff) << 32)
- | ((long) (bits[5] & 0xff) << 40)
- | ((long) (bits[6] & 0xff) << 48)
- | ((long) (bits[7] & 0xff) << 56);
-
- //
- // TODO: Actually do this check
- //
- // if (f.isUnsigned()) {
- // if (this.connection.getJdbcCompliantTruncation()) {
- // throwRangeException(String.valueOf(valueAsDouble), columnIndex +
- // 1, Types.BIGINT);
- // }
- // }
-
- return valueAsLong;
- case MysqlDefs.FIELD_TYPE_DOUBLE:
- double valueAsDouble = getNativeDouble(columnIndex + 1);
-
- if (this.connection.getJdbcCompliantTruncation()) {
- if (valueAsDouble < Long.MIN_VALUE
- || valueAsDouble > Long.MAX_VALUE) {
- throwRangeException(String.valueOf(valueAsDouble),
- columnIndex + 1, Types.BIGINT);
- }
- }
-
- return (long) valueAsDouble;
- case MysqlDefs.FIELD_TYPE_FLOAT:
- float valueAsFloat = getNativeFloat(columnIndex + 1);
-
- if (this.connection.getJdbcCompliantTruncation()) {
- if (valueAsFloat < Long.MIN_VALUE
- || valueAsFloat > Long.MAX_VALUE) {
- throwRangeException(String.valueOf(valueAsFloat),
- columnIndex + 1, Types.BIGINT);
- }
- }
-
- return (long) valueAsFloat;
- default:
-
- if (this.useUsageAdvisor) {
- issueConversionViaParsingWarning("getLong()", columnIndex,
- this.thisRow[columnIndex], this.fields[columnIndex],
- new int[] { MysqlDefs.FIELD_TYPE_DOUBLE,
- MysqlDefs.FIELD_TYPE_TINY,
- MysqlDefs.FIELD_TYPE_SHORT,
- MysqlDefs.FIELD_TYPE_LONG,
- MysqlDefs.FIELD_TYPE_LONGLONG,
- MysqlDefs.FIELD_TYPE_FLOAT });
- }
-
- String stringVal = getNativeString(columnIndex + 1);
-
- return getLongFromString(stringVal, columnIndex + 1);
- }
- }
-
- /**
* JDBC 2.0 Get a REF(<structured-type>) column.
*
* @param i
@@ -7646,6 +7274,408 @@
}
/**
+ * Get the value of a column in the current row as a Java byte.
+ *
+ * @param columnIndex
+ * the first column is 1, the second is 2,...
+ *
+ * @return the column value; 0 if SQL NULL
+ *
+ * @exception SQLException
+ * if a database access error occurs
+ */
+ protected byte getNativeByte(int columnIndex) throws SQLException {
+ return getNativeByte(columnIndex, true);
+ }
+
+ protected byte getNativeByte(int columnIndex, boolean overflowCheck) throws SQLException {
+ checkRowPos();
+
+ checkColumnBounds(columnIndex);
+
+ if (this.thisRow[columnIndex - 1] == null) {
+ this.wasNullFlag = true;
+
+ return 0;
+ }
+
+ try {
+ if (this.thisRow[columnIndex - 1] == null) {
+ this.wasNullFlag = true;
+ } else {
+ this.wasNullFlag = false;
+ }
+ } catch (NullPointerException E) {
+ this.wasNullFlag = true;
+ }
+
+ if (this.wasNullFlag) {
+ return 0;
+ }
+
+ columnIndex--;
+
+ Field field = this.fields[columnIndex];
+
+ switch (field.getMysqlType()) {
+ case MysqlDefs.FIELD_TYPE_TINY:
+ byte valueAsByte = ((byte[]) this.thisRow[columnIndex])[0];
+
+ if (!field.isUnsigned()) {
+ return valueAsByte;
+ }
+
+ short valueAsShort = (valueAsByte >= 0) ?
+ valueAsByte : (short)(valueAsByte + (short)256);
+
+ if (overflowCheck && this.connection.getJdbcCompliantTruncation()) {
+ if (valueAsShort > Byte.MAX_VALUE) {
+ throwRangeException(String.valueOf(valueAsShort),
+ columnIndex + 1, Types.TINYINT);
+ }
+ }
+
+ return (byte)valueAsShort;
+
+ case MysqlDefs.FIELD_TYPE_SHORT:
+ case MysqlDefs.FIELD_TYPE_YEAR:
+ valueAsShort = getNativeShort(columnIndex + 1);
+
+ if (overflowCheck && this.connection.getJdbcCompliantTruncation()) {
+ if (valueAsShort < Byte.MIN_VALUE
+ || valueAsShort > Byte.MAX_VALUE) {
+ throwRangeException(String.valueOf(valueAsShort),
+ columnIndex + 1, Types.TINYINT);
+ }
+ }
+
+ return (byte) valueAsShort;
+ case MysqlDefs.FIELD_TYPE_INT24:
+ case MysqlDefs.FIELD_TYPE_LONG:
+ int valueAsInt = getNativeInt(columnIndex + 1, false);
+
+ if (overflowCheck && this.connection.getJdbcCompliantTruncation()) {
+ if (valueAsInt < Byte.MIN_VALUE || valueAsInt > Byte.MAX_VALUE) {
+ throwRangeException(String.valueOf(valueAsInt),
+ columnIndex + 1, Types.TINYINT);
+ }
+ }
+
+ return (byte) valueAsInt;
+
+ case MysqlDefs.FIELD_TYPE_FLOAT:
+ float valueAsFloat = getNativeFloat(columnIndex + 1);
+
+ if (overflowCheck && this.connection.getJdbcCompliantTruncation()) {
+ if (valueAsFloat < Byte.MIN_VALUE
+ || valueAsFloat > Byte.MAX_VALUE) {
+
+ throwRangeException(String.valueOf(valueAsFloat),
+ columnIndex + 1, Types.TINYINT);
+ }
+ }
+
+ return (byte) valueAsFloat;
+
+ case MysqlDefs.FIELD_TYPE_DOUBLE:
+ double valueAsDouble = getNativeDouble(columnIndex + 1);
+
+ if (overflowCheck && this.connection.getJdbcCompliantTruncation()) {
+ if (valueAsDouble < Byte.MIN_VALUE
+ || valueAsDouble > Byte.MAX_VALUE) {
+ throwRangeException(String.valueOf(valueAsDouble),
+ columnIndex + 1, Types.TINYINT);
+ }
+ }
+
+ return (byte) valueAsDouble;
+
+ case MysqlDefs.FIELD_TYPE_LONGLONG:
+ long valueAsLong = getNativeLong(columnIndex + 1, false, true);
+
+ if (overflowCheck && this.connection.getJdbcCompliantTruncation()) {
+ if (valueAsLong < Byte.MIN_VALUE
+ || valueAsLong > Byte.MAX_VALUE) {
+ throwRangeException(String.valueOf(valueAsLong),
+ columnIndex + 1, Types.TINYINT);
+ }
+ }
+
+ return (byte) valueAsLong;
+
+ default:
+ if (this.useUsageAdvisor) {
+ issueConversionViaParsingWarning("getByte()", columnIndex,
+ this.thisRow[columnIndex], this.fields[columnIndex],
+ new int[] { MysqlDefs.FIELD_TYPE_DOUBLE,
+ MysqlDefs.FIELD_TYPE_TINY,
+ MysqlDefs.FIELD_TYPE_SHORT,
+ MysqlDefs.FIELD_TYPE_LONG,
+ MysqlDefs.FIELD_TYPE_LONGLONG,
+ MysqlDefs.FIELD_TYPE_FLOAT });
+ }
+
+ return getByteFromString(getNativeString(columnIndex + 1),
+ columnIndex + 1);
+ }
+ }
+
+ /**
+ * Get the value of a column in the current row as a Java int.
+ *
+ * @param columnIndex
+ * the first column is 1, the second is 2,...
+ *
+ * @return the column value; 0 if SQL NULL
+ *
+ * @exception SQLException
+ * if a database access error occurs
+ */
+ protected int getNativeInt(int columnIndex) throws SQLException {
+ return getNativeInt(columnIndex, false);
+ }
+
+ protected int getNativeInt(int columnIndex, boolean overflowCheck) throws SQLException {
+ checkRowPos();
+ checkColumnBounds(columnIndex);
+
+ columnIndex--; // / JDBC is 1-based
+
+ if (this.thisRow[columnIndex] == null) {
+ this.wasNullFlag = true;
+
+ return 0;
+ }
+
+ this.wasNullFlag = false;
+
+ Field f = this.fields[columnIndex];
+
+ switch (f.getMysqlType()) {
+
+ case MysqlDefs.FIELD_TYPE_TINY:
+ byte tinyintVal = getNativeByte(columnIndex + 1, false);
+
+ if (!f.isUnsigned() || tinyintVal >= 0) {
+ return tinyintVal;
+ }
+
+ return tinyintVal + 256;
+ case MysqlDefs.FIELD_TYPE_SHORT:
+ case MysqlDefs.FIELD_TYPE_YEAR:
+ short asShort = getNativeShort(columnIndex + 1, false);
+
+ if (!f.isUnsigned() || asShort >= 0) {
+ return asShort;
+ }
+
+ return asShort + 65536;
+ case MysqlDefs.FIELD_TYPE_INT24:
+ case MysqlDefs.FIELD_TYPE_LONG:
+ byte[] bits = (byte[]) this.thisRow[columnIndex];
+
+ int valueAsInt = (bits[0] & 0xff) | ((bits[1] & 0xff) << 8)
+ | ((bits[2] & 0xff) << 16) | ((bits[3] & 0xff) << 24);
+
+ if (!f.isUnsigned()) {
+ return valueAsInt;
+ }
+
+ long valueAsLong = (valueAsInt >= 0) ?
+ valueAsInt : valueAsInt + 4294967296L;
+
+ if (overflowCheck && this.connection.getJdbcCompliantTruncation() &&
+ valueAsLong > Integer.MAX_VALUE) {
+ throwRangeException(String.valueOf(valueAsLong),
+ columnIndex + 1, Types.INTEGER);
+ }
+
+ return (int)valueAsLong;
+ case MysqlDefs.FIELD_TYPE_LONGLONG:
+ valueAsLong = getNativeLong(columnIndex + 1, false, true);
+
+ if (overflowCheck && this.connection.getJdbcCompliantTruncation()) {
+ if (valueAsLong < Integer.MIN_VALUE
+ || valueAsLong > Integer.MAX_VALUE) {
+ throwRangeException(String.valueOf(valueAsLong),
+ columnIndex + 1, Types.INTEGER);
+ }
+ }
+
+ return (int) valueAsLong;
+ case MysqlDefs.FIELD_TYPE_DOUBLE:
+ double valueAsDouble = getNativeDouble(columnIndex + 1);
+
+ if (overflowCheck && this.connection.getJdbcCompliantTruncation()) {
+ if (valueAsDouble < Integer.MIN_VALUE
+ || valueAsDouble > Integer.MAX_VALUE) {
+ throwRangeException(String.valueOf(valueAsDouble),
+ columnIndex + 1, Types.INTEGER);
+ }
+ }
+
+ return (int) valueAsDouble;
+ case MysqlDefs.FIELD_TYPE_FLOAT:
+ valueAsDouble = getNativeFloat(columnIndex + 1);
+
+ if (overflowCheck && this.connection.getJdbcCompliantTruncation()) {
+ if (valueAsDouble < Integer.MIN_VALUE
+ || valueAsDouble > Integer.MAX_VALUE) {
+ throwRangeException(String.valueOf(valueAsDouble),
+ columnIndex + 1, Types.INTEGER);
+ }
+ }
+
+ return (int) valueAsDouble;
+
+ default:
+
+ if (this.useUsageAdvisor) {
+ issueConversionViaParsingWarning("getInt()", columnIndex,
+ this.thisRow[columnIndex], this.fields[columnIndex],
+ new int[] { MysqlDefs.FIELD_TYPE_DOUBLE,
+ MysqlDefs.FIELD_TYPE_TINY,
+ MysqlDefs.FIELD_TYPE_SHORT,
+ MysqlDefs.FIELD_TYPE_LONG,
+ MysqlDefs.FIELD_TYPE_LONGLONG,
+ MysqlDefs.FIELD_TYPE_FLOAT });
+ }
+
+ String stringVal = getNativeString(columnIndex + 1);
+
+ return getIntFromString(stringVal, columnIndex + 1);
+ }
+ }
+
+ /**
+ * Get the value of a column in the current row as a Java long.
+ *
+ * @param columnIndex
+ * the first column is 1, the second is 2,...
+ *
+ * @return the column value; 0 if SQL NULL
+ *
+ * @exception SQLException
+ * if a database access error occurs
+ */
+ protected long getNativeLong(int columnIndex) throws SQLException {
+ return getNativeLong(columnIndex, false, true);
+ }
+
+ protected long getNativeLong(int columnIndex, boolean overflowCheck,
+ boolean expandUnsignedLong) throws SQLException {
+ checkRowPos();
+ checkColumnBounds(columnIndex);
+
+ columnIndex--; // / JDBC is 1-based
+
+ if (this.thisRow[columnIndex] == null) {
+ this.wasNullFlag = true;
+
+ return 0;
+ }
+
+ this.wasNullFlag = false;
+
+ Field f = this.fields[columnIndex];
+
+ switch (f.getMysqlType()) {
+ case MysqlDefs.FIELD_TYPE_TINY:
+ if (!f.isUnsigned()) {
+ return getNativeByte(columnIndex + 1);
+ }
+
+ return getNativeInt(columnIndex + 1);
+ case MysqlDefs.FIELD_TYPE_SHORT:
+ if (!f.isUnsigned()) {
+ return getNativeShort(columnIndex + 1);
+ }
+
+ return getNativeInt(columnIndex + 1);
+ case MysqlDefs.FIELD_TYPE_YEAR:
+
+ return getNativeShort(columnIndex + 1);
+ case MysqlDefs.FIELD_TYPE_INT24:
+ case MysqlDefs.FIELD_TYPE_LONG:
+ int asInt = getNativeInt(columnIndex + 1);
+
+ if (!f.isUnsigned() || asInt >= 0) {
+ return asInt;
+ }
+
+ return asInt + 4294967296L;
+ case MysqlDefs.FIELD_TYPE_LONGLONG:
+
+ byte[] bits = (byte[]) this.thisRow[columnIndex];
+
+ long valueAsLong = (bits[0] & 0xff)
+ | ((long) (bits[1] & 0xff) << 8)
+ | ((long) (bits[2] & 0xff) << 16)
+ | ((long) (bits[3] & 0xff) << 24)
+ | ((long) (bits[4] & 0xff) << 32)
+ | ((long) (bits[5] & 0xff) << 40)
+ | ((long) (bits[6] & 0xff) << 48)
+ | ((long) (bits[7] & 0xff) << 56);
+
+ if (!f.isUnsigned() || !expandUnsignedLong) {
+ return valueAsLong;
+ }
+
+ BigInteger asBigInt = convertLongToUlong(valueAsLong);
+
+ if (overflowCheck && this.connection.getJdbcCompliantTruncation() &&
+ ((asBigInt.compareTo(new BigInteger(String.valueOf(Long.MAX_VALUE))) > 0 ) ||
+ (asBigInt.compareTo(new BigInteger(String.valueOf(Long.MIN_VALUE))) < 0))) {
+ throwRangeException(asBigInt.toString(),
+ columnIndex + 1, Types.BIGINT);
+ }
+
+ return getLongFromString(asBigInt.toString(), columnIndex + 1);
+
+ case MysqlDefs.FIELD_TYPE_DOUBLE:
+ double valueAsDouble = getNativeDouble(columnIndex + 1);
+
+ if (overflowCheck && this.connection.getJdbcCompliantTruncation()) {
+ if (valueAsDouble < Long.MIN_VALUE
+ || valueAsDouble > Long.MAX_VALUE) {
+ throwRangeException(String.valueOf(valueAsDouble),
+ columnIndex + 1, Types.BIGINT);
+ }
+ }
+
+ return (long) valueAsDouble;
+ case MysqlDefs.FIELD_TYPE_FLOAT:
+ valueAsDouble = getNativeFloat(columnIndex + 1);
+
+ if (overflowCheck && this.connection.getJdbcCompliantTruncation()) {
+ if (valueAsDouble < Long.MIN_VALUE
+ || valueAsDouble > Long.MAX_VALUE) {
+ throwRangeException(String.valueOf(valueAsDouble),
+ columnIndex + 1, Types.BIGINT);
+ }
+ }
+
+ return (long) valueAsDouble;
+ default:
+
+ if (this.useUsageAdvisor) {
+ issueConversionViaParsingWarning("getLong()", columnIndex,
+ this.thisRow[columnIndex], this.fields[columnIndex],
+ new int[] { MysqlDefs.FIELD_TYPE_DOUBLE,
+ MysqlDefs.FIELD_TYPE_TINY,
+ MysqlDefs.FIELD_TYPE_SHORT,
+ MysqlDefs.FIELD_TYPE_LONG,
+ MysqlDefs.FIELD_TYPE_LONGLONG,
+ MysqlDefs.FIELD_TYPE_FLOAT });
+ }
+
+ String stringVal = getNativeString(columnIndex + 1);
+
+ return getLongFromString(stringVal, columnIndex + 1);
+ }
+ }
+
+ /**
* Get the value of a column in the current row as a Java short.
*
* @param columnIndex
@@ -7657,6 +7687,10 @@
* if a database access error occurs
*/
protected short getNativeShort(int columnIndex) throws SQLException {
+ return getNativeShort(columnIndex, true);
+ }
+
+ protected short getNativeShort(int columnIndex, boolean overflowCheck) throws SQLException {
checkRowPos();
checkColumnBounds(columnIndex);
@@ -7687,27 +7721,26 @@
byte[] bits = (byte[]) this.thisRow[columnIndex];
short asShort = (short) ((bits[0] & 0xff) | ((bits[1] & 0xff) << 8));
-
+
if (!f.isUnsigned()) {
return asShort;
}
int valueAsInt = asShort & 0xffff;
- if (this.connection.getJdbcCompliantTruncation() &&
+ if (overflowCheck && this.connection.getJdbcCompliantTruncation() &&
valueAsInt > Short.MAX_VALUE) {
throwRangeException(String.valueOf(valueAsInt),
columnIndex + 1, Types.SMALLINT);
}
return (short)valueAsInt;
-
case MysqlDefs.FIELD_TYPE_INT24:
case MysqlDefs.FIELD_TYPE_LONG:
if (!f.isUnsigned()) {
- valueAsInt = getNativeInt(columnIndex + 1);
+ valueAsInt = getNativeInt(columnIndex + 1, false);
- if (this.connection.getJdbcCompliantTruncation() &&
+ if (overflowCheck && this.connection.getJdbcCompliantTruncation() &&
valueAsInt > Short.MAX_VALUE ||
valueAsInt < Short.MIN_VALUE) {
throwRangeException(String.valueOf(valueAsInt),
@@ -7717,9 +7750,9 @@
return (short)valueAsInt;
}
- long valueAsLong = getNativeLong(columnIndex + 1);
+ long valueAsLong = getNativeLong(columnIndex + 1, false, true);
- if (this.connection.getJdbcCompliantTruncation() &&
+ if (overflowCheck && this.connection.getJdbcCompliantTruncation() &&
valueAsLong > Short.MAX_VALUE) {
throwRangeException(String.valueOf(valueAsLong),
columnIndex + 1, Types.SMALLINT);
@@ -7728,21 +7761,35 @@
return (short)valueAsLong;
case MysqlDefs.FIELD_TYPE_LONGLONG:
- valueAsLong = getNativeLong(columnIndex + 1);
+ valueAsLong = getNativeLong(columnIndex + 1, false, false);
+
+ if (!f.isUnsigned()) {
+ if (overflowCheck && this.connection.getJdbcCompliantTruncation()) {
+ if (valueAsLong < Short.MIN_VALUE
+ || valueAsLong > Short.MAX_VALUE) {
+ throwRangeException(String.valueOf(valueAsLong),
+ columnIndex + 1, Types.SMALLINT);
+ }
+ }
- if (this.connection.getJdbcCompliantTruncation()) {
- if (valueAsLong < Integer.MIN_VALUE
- || valueAsLong > Integer.MAX_VALUE) {
- throwRangeException(String.valueOf(valueAsLong),
- columnIndex + 1, Types.INTEGER);
- }
+ return (short) valueAsLong;
}
+
+ BigInteger asBigInt = convertLongToUlong(valueAsLong);
+
+ if (overflowCheck && this.connection.getJdbcCompliantTruncation() &&
+ ((asBigInt.compareTo(new BigInteger(String.valueOf(Short.MAX_VALUE))) > 0 ) ||
+ (asBigInt.compareTo(new BigInteger(String.valueOf(Short.MIN_VALUE))) < 0))) {
+ throwRangeException(asBigInt.toString(),
+ columnIndex + 1, Types.SMALLINT);
+ }
+
+ return (short)getIntFromString(asBigInt.toString(), columnIndex + 1);
- return (short) valueAsLong;
case MysqlDefs.FIELD_TYPE_DOUBLE:
double valueAsDouble = getNativeDouble(columnIndex + 1);
- if (this.connection.getJdbcCompliantTruncation()) {
+ if (overflowCheck && this.connection.getJdbcCompliantTruncation()) {
if (valueAsDouble < Short.MIN_VALUE
|| valueAsDouble > Short.MAX_VALUE) {
throwRangeException(String.valueOf(valueAsDouble),
@@ -7754,7 +7801,7 @@
case MysqlDefs.FIELD_TYPE_FLOAT:
float valueAsFloat = getNativeFloat(columnIndex + 1);
- if (this.connection.getJdbcCompliantTruncation()) {
+ if (overflowCheck && this.connection.getJdbcCompliantTruncation()) {
if (valueAsFloat < Short.MIN_VALUE
|| valueAsFloat > Short.MAX_VALUE) {
throwRangeException(String.valueOf(valueAsFloat),
| Thread |
|---|
| • Connector/J commit: r4819 - branches/branch_3_1/connector-j/src/com/mysql/jdbc | mmatthews | 18 Jan |