01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (http://h2database.com/html/license.html).
04: * Initial Developer: H2 Group
05: */
06: package org.h2.expression;
07:
08: import java.sql.SQLException;
09:
10: import org.h2.command.Prepared;
11: import org.h2.engine.Session;
12: import org.h2.message.Message;
13: import org.h2.table.ColumnResolver;
14: import org.h2.table.TableFilter;
15: import org.h2.value.Value;
16: import org.h2.value.ValueInt;
17:
18: /**
19: * Represents the ROWNUM function.
20: */
21: public class Rownum extends Expression {
22:
23: private Prepared prepared;
24:
25: public Rownum(Prepared prepared) {
26: this .prepared = prepared;
27: }
28:
29: public Value getValue(Session session) throws SQLException {
30: return ValueInt.get(prepared.getCurrentRowNumber());
31: }
32:
33: public int getType() {
34: return Value.INT;
35: }
36:
37: public void mapColumns(ColumnResolver resolver, int level)
38: throws SQLException {
39: }
40:
41: public Expression optimize(Session session) throws SQLException {
42: return this ;
43: }
44:
45: public void setEvaluatable(TableFilter tableFilter, boolean b) {
46: }
47:
48: public int getScale() {
49: return 0;
50: }
51:
52: public long getPrecision() {
53: return ValueInt.PRECISION;
54: }
55:
56: public int getDisplaySize() {
57: return ValueInt.DISPLAY_SIZE;
58: }
59:
60: public String getSQL() {
61: return "ROWNUM()";
62: }
63:
64: public void updateAggregate(Session session) throws SQLException {
65: }
66:
67: public boolean isEverything(ExpressionVisitor visitor) {
68: switch (visitor.type) {
69: case ExpressionVisitor.OPTIMIZABLE_MIN_MAX_COUNT_ALL:
70: return false;
71: case ExpressionVisitor.DETERMINISTIC:
72: return false;
73: case ExpressionVisitor.INDEPENDENT:
74: return false;
75: case ExpressionVisitor.EVALUATABLE:
76: return true;
77: case ExpressionVisitor.SET_MAX_DATA_MODIFICATION_ID:
78: // if everything else is the same, the rownum is the same
79: return true;
80: case ExpressionVisitor.READONLY:
81: return true;
82: case ExpressionVisitor.NOT_FROM_RESOLVER:
83: return true;
84: case ExpressionVisitor.GET_DEPENDENCIES:
85: return true;
86: default:
87: throw Message.getInternalError("type=" + visitor.type);
88: }
89: }
90:
91: public int getCost() {
92: return 0;
93: }
94:
95: }
|