Change H2DB Sequence Data Type

I need to change the return value of a sequence stored in H2DB when I call nextVal through a direct SQL query. H2 return BigInt, and I need BigDecimal.

I cannot distinguish or convert this value, I need H2 to return BigDecimal.

How can i do this?

EDIT: I can't change the beacose Java code I'm testing, so converting the query value from DB is not an option.

+7
source share
2 answers

You can create your own fixed version of H2 if you are allowed to replace the H2 flag file.

In org.h2.expression.Function change

  addFunctionNotDeterministic("NEXTVAL", NEXTVAL, VAR_ARGS, Value.LONG); 

to

  addFunctionNotDeterministic("NEXTVAL", NEXTVAL, VAR_ARGS, Value.DECIMAL); 

and in org.h2.expression.SequenceValue change

 @Override public Value getValue(Session session) { long value = sequence.getNext(session); session.setLastIdentity(ValueLong.get(value)); return ValueLong.get(value); } @Override public int getType() { return Value.LONG; } 

to

 @Override public Value getValue(Session session) { long lv = sequence.getNext(session); ValueDecimal value = ValueDecimal.get(BigDecimal.valueOf(lv)); session.setLastIdentity(value); return value; } @Override public int getType() { return Value.DECIMAL; } 
+4
source

I tried to take the code from the wero answer and make it a function in H2 itself.

This GitHub fork: https://github.com/portofrotterdam/h2database returns BigDecimals instead of BigInteger / longs when using 'MODE = Oracle' in H2, which makes it more compatible with Oracle database.

I requested a drag and drop to the wizard, so maybe this behavior will be available in h2database.

0
source

Source: https://habr.com/ru/post/987664/


All Articles