1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://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.Session; |
10 | import org.h2.message.DbException; |
11 | import org.h2.table.ColumnResolver; |
12 | import org.h2.table.TableFilter; |
13 | import org.h2.value.Value; |
14 | import org.h2.value.ValueInt; |
15 | |
16 | /** |
17 | * Represents the ROWNUM function. |
18 | */ |
19 | public class Rownum extends Expression { |
20 | |
21 | private final Prepared prepared; |
22 | |
23 | public Rownum(Prepared prepared) { |
24 | this.prepared = prepared; |
25 | } |
26 | |
27 | @Override |
28 | public Value getValue(Session session) { |
29 | return ValueInt.get(prepared.getCurrentRowNumber()); |
30 | } |
31 | |
32 | @Override |
33 | public int getType() { |
34 | return Value.INT; |
35 | } |
36 | |
37 | @Override |
38 | public void mapColumns(ColumnResolver resolver, int level) { |
39 | // nothing to do |
40 | } |
41 | |
42 | @Override |
43 | public Expression optimize(Session session) { |
44 | return this; |
45 | } |
46 | |
47 | @Override |
48 | public void setEvaluatable(TableFilter tableFilter, boolean b) { |
49 | // nothing to do |
50 | } |
51 | |
52 | @Override |
53 | public int getScale() { |
54 | return 0; |
55 | } |
56 | |
57 | @Override |
58 | public long getPrecision() { |
59 | return ValueInt.PRECISION; |
60 | } |
61 | |
62 | @Override |
63 | public int getDisplaySize() { |
64 | return ValueInt.DISPLAY_SIZE; |
65 | } |
66 | |
67 | @Override |
68 | public String getSQL() { |
69 | return "ROWNUM()"; |
70 | } |
71 | |
72 | @Override |
73 | public void updateAggregate(Session session) { |
74 | // nothing to do |
75 | } |
76 | |
77 | @Override |
78 | public boolean isEverything(ExpressionVisitor visitor) { |
79 | switch(visitor.getType()) { |
80 | case ExpressionVisitor.QUERY_COMPARABLE: |
81 | case ExpressionVisitor.OPTIMIZABLE_MIN_MAX_COUNT_ALL: |
82 | case ExpressionVisitor.DETERMINISTIC: |
83 | case ExpressionVisitor.INDEPENDENT: |
84 | return false; |
85 | case ExpressionVisitor.EVALUATABLE: |
86 | case ExpressionVisitor.READONLY: |
87 | case ExpressionVisitor.NOT_FROM_RESOLVER: |
88 | case ExpressionVisitor.GET_DEPENDENCIES: |
89 | case ExpressionVisitor.SET_MAX_DATA_MODIFICATION_ID: |
90 | case ExpressionVisitor.GET_COLUMNS: |
91 | // if everything else is the same, the rownum is the same |
92 | return true; |
93 | default: |
94 | throw DbException.throwInternalError("type="+visitor.getType()); |
95 | } |
96 | } |
97 | |
98 | @Override |
99 | public int getCost() { |
100 | return 0; |
101 | } |
102 | |
103 | } |