Debian Codegraph @

src/main/org/h2/expression/Rownum.java

raw
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;
7
8 import org.h2.command.Prepared;
9 import org.h2.engine.SessionLocal;
10 import org.h2.message.DbException;
11 import org.h2.value.TypeInfo;
12 import org.h2.value.Value;
13 import org.h2.value.ValueBigint;
14
15 /**
16 * Represents the ROWNUM function.
17 */
18 public final class Rownum extends Operation0 {
19
20 private final Prepared prepared;
21
22 private boolean singleRow;
23
24 public Rownum(Prepared prepared) {
25 if (prepared == null) {
26 throw DbException.getInternalError();
27 }
28 this.prepared = prepared;
29 }
30
31 @Override
32 public Value getValue(SessionLocal session) {
33 return ValueBigint.get(prepared.getCurrentRowNumber());
34 }
35
36 @Override
37 public TypeInfo getType() {
38 return TypeInfo.TYPE_BIGINT;
39 }
40
41 @Override
42 public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags) {
43 return builder.append("ROWNUM()");
44 }
45
46 @Override
47 public Expression optimize(SessionLocal session) {
48 return singleRow ? ValueExpression.get(ValueBigint.get(1L)) : this;
49 }
50
51 @Override
52 public boolean isEverything(ExpressionVisitor visitor) {
53 switch (visitor.getType()) {
54 case ExpressionVisitor.QUERY_COMPARABLE:
55 case ExpressionVisitor.OPTIMIZABLE_AGGREGATE:
56 case ExpressionVisitor.DETERMINISTIC:
57 case ExpressionVisitor.INDEPENDENT:
58 case ExpressionVisitor.EVALUATABLE:
59 return false;
60 case ExpressionVisitor.DECREMENT_QUERY_LEVEL:
61 if (visitor.getQueryLevel() > 0) {
62 singleRow = true;
63 }
64 //$FALL-THROUGH$
65 default:
66 return true;
67 }
68 }
69
70 @Override
71 public int getCost() {
72 return 0;
73 }
74
75 }