提交 bfdaa836 authored 作者: Thomas Mueller's avatar Thomas Mueller

SYSTEM_RANGE now supports parameters

上级 39c4fb8f
......@@ -138,6 +138,7 @@ import org.h2.value.ValueBytes;
import org.h2.value.ValueDate;
import org.h2.value.ValueDecimal;
import org.h2.value.ValueInt;
import org.h2.value.ValueLong;
import org.h2.value.ValueString;
import org.h2.value.ValueTime;
import org.h2.value.ValueTimestamp;
......@@ -858,7 +859,6 @@ public class Parser {
private TableFilter readTableFilter(boolean fromOuter) throws SQLException {
Table table;
String alias = null;
Schema mainSchema = database.getSchema(Constants.SCHEMA_MAIN);
if (readIf("(")) {
if (isToken("SELECT") || isToken("FROM")) {
int start = lastParseIndex;
......@@ -893,10 +893,11 @@ public class Parser {
} else {
String tableName = readIdentifierWithSchema(null);
if (readIf("(")) {
Schema mainSchema = database.getSchema(Constants.SCHEMA_MAIN);
if (tableName.equals(RangeTable.NAME)) {
long min = readLong();
Expression min = readExpression();
read(",");
long max = readLong();
Expression max = readExpression();
read(")");
table = new RangeTable(mainSchema, min, max);
} else {
......@@ -907,7 +908,7 @@ public class Parser {
table = new FunctionTable(mainSchema, session, (FunctionCall) func);
}
} else if ("DUAL".equals(tableName)) {
table = new RangeTable(mainSchema, 1, 1);
table = getDualTable();
} else {
table = readTableOrView(tableName);
}
......@@ -1497,8 +1498,7 @@ public class Parser {
if (!readIf("FROM")) {
// select without FROM: convert to SELECT ... FROM
// SYSTEM_RANGE(1,1)
Schema main = database.findSchema(Constants.SCHEMA_MAIN);
Table dual = new RangeTable(main, 1, 1);
Table dual = getDualTable();
TableFilter filter = new TableFilter(session, dual, null, rightsChecked, currentSelect);
command.addTableFilter(filter, true);
} else {
......@@ -1533,6 +1533,12 @@ public class Parser {
setSQL(command, "SELECT", start);
return command;
}
private Table getDualTable() throws SQLException {
Schema main = database.findSchema(Constants.SCHEMA_MAIN);
Expression one = ValueExpression.get(ValueLong.get(1));
return new RangeTable(main, one, one);
}
private void setSQL(Prepared command, String start, int startIndex) {
String sql = originalSQL.substring(startIndex, lastParseIndex).trim();
......
......@@ -21,12 +21,11 @@ import org.h2.table.RangeTable;
*/
public class RangeIndex extends BaseIndex {
private long min, max;
private RangeTable table;
public RangeIndex(RangeTable table, IndexColumn[] columns, long min, long max) {
public RangeIndex(RangeTable table, IndexColumn[] columns) {
initBaseIndex(table, 0, "RANGE_INDEX", columns, IndexType.createNonUnique(true));
this.min = min;
this.max = max;
this.table = table;
}
public void close(Session session) throws SQLException {
......@@ -41,6 +40,8 @@ public class RangeIndex extends BaseIndex {
}
public Cursor find(Session session, SearchRow first, SearchRow last) throws SQLException {
long min = table.getMin(session);
long max = table.getMax(session);
long start = Math.max(min, first == null ? min : first.getValue(0).getLong());
long end = Math.min(max, last == null ? max : last.getValue(0).getLong());
return new RangeCursor(start, end);
......@@ -75,7 +76,7 @@ public class RangeIndex extends BaseIndex {
}
public Cursor findFirstOrLast(Session session, boolean first) throws SQLException {
long pos = first ? min : max;
long pos = first ? table.getMin(session) : table.getMax(session);
return new RangeCursor(pos, pos);
}
......
......@@ -9,6 +9,7 @@ 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;
......@@ -25,9 +26,10 @@ import org.h2.value.Value;
public class RangeTable extends Table {
public static final String NAME = "SYSTEM_RANGE";
private final long min, max;
public RangeTable(Schema schema, long min, long max) throws SQLException {
private Expression min, max;
private boolean optimized;
public RangeTable(Schema schema, Expression min, Expression max) throws SQLException {
super(schema, 0, NAME, true);
Column[] cols = new Column[]{
new Column("X", Value.LONG)
......@@ -46,7 +48,7 @@ public class RangeTable extends Table {
}
public String getSQL() {
return NAME + "(" + min + ", " + max + ")";
return NAME + "(" + min.getSQL() + ", " + max.getSQL() + ")";
}
public void lock(Session session, boolean exclusive, boolean force) throws SQLException {
......@@ -91,7 +93,7 @@ public class RangeTable extends Table {
}
public long getRowCount(Session session) throws SQLException {
return max - min;
return getMax(session) - getMin(session);
}
public String getTableType() {
......@@ -99,7 +101,25 @@ public class RangeTable extends Table {
}
public Index getScanIndex(Session session) throws SQLException {
return new RangeIndex(this, IndexColumn.wrap(columns), min, max);
return new RangeIndex(this, IndexColumn.wrap(columns));
}
public long getMin(Session s) throws SQLException {
optimize(s);
return min.getValue(s).getLong();
}
public long getMax(Session s) throws SQLException {
optimize(s);
return max.getValue(s).getLong();
}
private void optimize(Session s) throws SQLException {
if (!optimized) {
min = min.optimize(s);
max = max.optimize(s);
optimized = true;
}
}
public ObjectArray getIndexes() {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论