1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* Copyright 2004-2009 H2 Group. Multiple-Licensed under the H2 License,
* Version 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.table;
import java.sql.SQLException;
import org.h2.engine.Session;
import org.h2.expression.Expression;
import org.h2.index.Index;
import org.h2.index.IndexType;
import org.h2.index.RangeIndex;
import org.h2.message.Message;
import org.h2.result.Row;
import org.h2.schema.Schema;
import org.h2.util.ObjectArray;
import org.h2.value.Value;
/**
* The table SYSTEM_RANGE is a virtual table that generates incrementing numbers
* with a given start end end point.
*/
public class RangeTable extends Table {
/**
* The name of the range table.
*/
public static final String NAME = "SYSTEM_RANGE";
private Expression min, max;
private boolean optimized;
/**
* Create a new range with the given start and end expressions.
*
* @param schema the schema (always the main schema)
* @param min the start expression
* @param max the end expression
*/
public RangeTable(Schema schema, Expression min, Expression max) throws SQLException {
super(schema, 0, NAME, true, true);
Column[] cols = new Column[]{
new Column("X", Value.LONG)
};
this.min = min;
this.max = max;
setColumns(cols);
}
public String getDropSQL() {
return null;
}
public String getCreateSQL() {
return null;
}
public String getSQL() {
return NAME + "(" + min.getSQL() + ", " + max.getSQL() + ")";
}
public void lock(Session session, boolean exclusive, boolean force) {
// nothing to do
}
public void close(Session session) {
// nothing to do
}
public void unlock(Session s) {
// nothing to do
}
public boolean isLockedExclusively() {
return false;
}
public Index addIndex(Session session, String indexName, int indexId, IndexColumn[] cols, IndexType indexType, int headPos, String comment) throws SQLException {
throw Message.getUnsupportedException("SYSTEM_RANGE");
}
public void removeRow(Session session, Row row) throws SQLException {
throw Message.getUnsupportedException("SYSTEM_RANGE");
}
public void addRow(Session session, Row row) throws SQLException {
throw Message.getUnsupportedException("SYSTEM_RANGE");
}
public void checkSupportAlter() throws SQLException {
throw Message.getUnsupportedException("SYSTEM_RANGE");
}
public void checkRename() throws SQLException {
throw Message.getUnsupportedException("SYSTEM_RANGE");
}
public boolean canGetRowCount() {
return true;
}
public boolean canDrop() {
return false;
}
public long getRowCount(Session session) throws SQLException {
return Math.max(0, getMax(session) - getMin(session) + 1);
}
public String getTableType() {
throw Message.throwInternalError();
}
public Index getScanIndex(Session session) {
return new RangeIndex(this, IndexColumn.wrap(columns));
}
/**
* Calculate and get the start value of this range.
*
* @param session the session
* @return the start value
*/
public long getMin(Session session) throws SQLException {
optimize(session);
return min.getValue(session).getLong();
}
/**
* Calculate and get the end value of this range.
*
* @param session the session
* @return the end value
*/
public long getMax(Session session) throws SQLException {
optimize(session);
return max.getValue(session).getLong();
}
private void optimize(Session s) throws SQLException {
if (!optimized) {
min = min.optimize(s);
max = max.optimize(s);
optimized = true;
}
}
public ObjectArray<Index> getIndexes() {
return null;
}
public void truncate(Session session) throws SQLException {
throw Message.getUnsupportedException("SYSTEM_RANGE");
}
public long getMaxDataModificationId() {
return 0;
}
public Index getUniqueIndex() {
return null;
}
public long getRowCountApproximation() {
return 100;
}
public boolean isDeterministic() {
return true;
}
}