4

Changing the data type of an H2DB sequence

 2 years ago
source link: https://www.codesd.com/item/changing-the-data-type-of-an-h2db-sequence.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Changing the data type of an H2DB sequence

advertisements

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

I can't cast or convert this value, I need H2 returning a BigDecimal.

How I can do that?

EDIT: I can't change the Java code beacuse I'm testing so cast or convert the request value from DB is not a option.


You could create you own patched version of H2, if you are allowed to replace the H2 jar file.

In org.h2.expression.Function change

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

    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;
}

@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;
}


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK