In Java, the only unsigned numeric type is "char", which is an
unsigned "short" (or mediumint in MySQL). Of course, char's every-day
semantic is not really numeric. The only real potential of mapping the
concept of "unsigned numerics" from MySQL to Java is to choose the
next bigger datatype in Java. This means:
- tinyint maps to byte, whereas tinyint unsigned maps to short
- mediumint maps to short, whereas mediumint unsigned maps to int
- int maps to int, whereas int unsigned maps to long
- etc...
What do you think? At least, a 130 tinyint unsigned in MySQL would
find a proper representation in Java, instead of being overflowed to
-126
public class UnsignedTinyInt {- Indicate in documentation, that if you are dealing with unsigned database fields, you have to be VERY careful providing a value in Java and a link to helper classes documentation.
public static byte parse(short value) {
if(value > 0xFF || value < 0) {
throw new OutOfRangeException(...);
}
return (byte) value;
}
}
2011/5/10 FractalizeR <Fract...@yandex.ru>:
1. Mapping to the next-higher type leads to incompatibility, such as
the one you're concerned with. Besides, it will be possible to try to
insert (short) 300 into a tinyint unsigned column, which will lead to
runtime issues. While this is a good solution for fetching data, it is
a bad one for insertion.
2. Not mapping at all leads to confusion, because Java does not
explicitly support unsigned types. While this is the most correct
solution, it's also the least user-friendly.
Maybe, option Nr. 2 is better and then I could add utility methods to
Record, Store and Result, in order to retrieve unsigned numbers
according to operations as documented here:
http://mindprod.com/jgloss/unsigned.html
A similar thing could be done for inserting data, as you described...
The wrapper types will be open-sourced independently:
http://code.google.com/p/joou/
jOOU with U for Unsigned. There will be another funny face ;-)
I'm hoping to get arithmetic and bitwise operations implemented by
other contributors, not necessarily involved with jOOQ. See also this
question here:
http://stackoverflow.com/questions/8193031/is-there-a-java-library-for-unsigned-number-type-wrappers
Cheers
Lukas
2011/11/19 Lukas Eder <lukas...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.