File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\table\RangeTable.java.html

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