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

SYSTEM_RANGE now supports parameters

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