| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.expression.function; |
| 7 |
|
| 8 |
import org.h2.command.Parser; |
| 9 |
import org.h2.engine.Database; |
| 10 |
import org.h2.engine.SessionLocal; |
| 11 |
import org.h2.expression.Expression; |
| 12 |
import org.h2.expression.ExpressionColumn; |
| 13 |
import org.h2.expression.ExpressionVisitor; |
| 14 |
import org.h2.message.DbException; |
| 15 |
import org.h2.schema.Schema; |
| 16 |
import org.h2.schema.Sequence; |
| 17 |
import org.h2.util.StringUtils; |
| 18 |
import org.h2.value.TypeInfo; |
| 19 |
import org.h2.value.Value; |
| 20 |
|
| 21 |
/** |
| 22 |
* NEXTVAL() and CURRVAL() compatibility functions. |
| 23 |
*/ |
| 24 |
public final class CompatibilitySequenceValueFunction extends Function1_2 { |
| 25 |
|
| 26 |
private final boolean current; |
| 27 |
|
| 28 |
public CompatibilitySequenceValueFunction(Expression left, Expression right, boolean current) { |
| 29 |
super(left, right); |
| 30 |
this.current = current; |
| 31 |
} |
| 32 |
|
| 33 |
@Override |
| 34 |
public Value getValue(SessionLocal session, Value v1, Value v2) { |
| 35 |
String schemaName, sequenceName; |
| 36 |
if (v2 == null) { |
| 37 |
Parser p = new Parser(session); |
| 38 |
String sql = v1.getString(); |
| 39 |
Expression expr = p.parseExpression(sql); |
| 40 |
if (expr instanceof ExpressionColumn) { |
| 41 |
ExpressionColumn seq = (ExpressionColumn) expr; |
| 42 |
schemaName = seq.getOriginalTableAliasName(); |
| 43 |
if (schemaName == null) { |
| 44 |
schemaName = session.getCurrentSchemaName(); |
| 45 |
sequenceName = sql; |
| 46 |
} else { |
| 47 |
sequenceName = seq.getColumnName(session, -1); |
| 48 |
} |
| 49 |
} else { |
| 50 |
throw DbException.getSyntaxError(sql, 1); |
| 51 |
} |
| 52 |
} else { |
| 53 |
schemaName = v1.getString(); |
| 54 |
sequenceName = v2.getString(); |
| 55 |
} |
| 56 |
Database database = session.getDatabase(); |
| 57 |
Schema s = database.findSchema(schemaName); |
| 58 |
if (s == null) { |
| 59 |
schemaName = StringUtils.toUpperEnglish(schemaName); |
| 60 |
s = database.getSchema(schemaName); |
| 61 |
} |
| 62 |
Sequence seq = s.findSequence(sequenceName); |
| 63 |
if (seq == null) { |
| 64 |
sequenceName = StringUtils.toUpperEnglish(sequenceName); |
| 65 |
seq = s.getSequence(sequenceName); |
| 66 |
} |
| 67 |
return (current ? session.getCurrentValueFor(seq) : session.getNextValueFor(seq, null)).convertTo(type); |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public Expression optimize(SessionLocal session) { |
| 72 |
left = left.optimize(session); |
| 73 |
if (right != null) { |
| 74 |
right = right.optimize(session); |
| 75 |
} |
| 76 |
type = session.getMode().decimalSequences ? TypeInfo.TYPE_NUMERIC_BIGINT : TypeInfo.TYPE_BIGINT; |
| 77 |
return this; |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
public boolean isEverything(ExpressionVisitor visitor) { |
| 82 |
switch (visitor.getType()) { |
| 83 |
case ExpressionVisitor.INDEPENDENT: |
| 84 |
case ExpressionVisitor.DETERMINISTIC: |
| 85 |
case ExpressionVisitor.QUERY_COMPARABLE: |
| 86 |
return false; |
| 87 |
case ExpressionVisitor.READONLY: |
| 88 |
if (!current) { |
| 89 |
return false; |
| 90 |
} |
| 91 |
} |
| 92 |
return super.isEverything(visitor); |
| 93 |
} |
| 94 |
|
| 95 |
@Override |
| 96 |
public String getName() { |
| 97 |
return current ? "CURRVAL" : "NEXTVAL"; |
| 98 |
} |
| 99 |
|
| 100 |
} |