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

--no commit message

--no commit message
上级 3954b942
......@@ -36,9 +36,8 @@ import org.h2.value.ValueNull;
import org.h2.value.ValueString;
/**
* @author Thomas
* Implements the integrated aggregate functions, such as COUNT, MAX, SUM.
*/
public class Aggregate extends Expression {
// TODO incompatibility to hsqldb: aggregates: hsqldb uses automatic data
// type for sum if value is too big,
......@@ -344,7 +343,7 @@ public class Aggregate extends Expression {
public long getPrecision() {
return precision;
}
public int getDisplaySize() {
return displaySize;
}
......
......@@ -18,6 +18,9 @@ import org.h2.value.ValueInt;
import org.h2.value.ValueLong;
import org.h2.value.ValueNull;
/**
* Data stored while calculating an aggregate.
*/
public class AggregateData {
private final int aggregateType;
private long count;
......
......@@ -12,11 +12,9 @@ import org.h2.table.ColumnResolver;
import org.h2.table.TableFilter;
import org.h2.value.Value;
/**
* @author Thomas
* A column alias as in SELECT 'Hello' AS NAME ...
*/
public class Alias extends Expression {
private final String alias;
......@@ -26,7 +24,7 @@ public class Alias extends Expression {
this.expr = expression;
this.alias = alias;
}
public Expression getNonAliasExpression() {
return expr;
}
......@@ -59,23 +57,23 @@ public class Alias extends Expression {
public long getPrecision() {
return expr.getPrecision();
}
public int getDisplaySize() {
return expr.getDisplaySize();
}
public boolean isAutoIncrement() {
return expr.isAutoIncrement();
}
}
public String getSQL() {
return expr.getSQL() + " AS " + Parser.quoteIdentifier(alias);
}
public void updateAggregate(Session session) throws SQLException {
expr.updateAggregate(session);
}
public String getAlias() {
return alias;
}
......@@ -87,7 +85,7 @@ public class Alias extends Expression {
public boolean isEverything(ExpressionVisitor visitor) {
return expr.isEverything(visitor);
}
public int getCost() {
return expr.getCost();
}
......
......@@ -22,9 +22,8 @@ import org.h2.value.ValueNull;
import org.h2.value.ValueString;
/**
* @author Thomas
* Pattern matching comparison expression: WHERE NAME LIKE ?
*/
public class CompareLike extends Condition {
private final CompareMode compareMode;
......@@ -129,7 +128,7 @@ public class CompareLike extends Condition {
// (at prepare time)
// otherwise we would need to prepare at execute time,
// which is maybe slower (but maybe not in this case!)
// TODO optimizer: like: check what other databases do!
// TODO optimizer: like: check what other databases do
if (!right.isValueSet()) {
return;
}
......
......@@ -8,7 +8,7 @@ import org.h2.value.Value;
import org.h2.value.ValueBoolean;
/**
* @author Thomas
* Represents a condition returning a boolean value, or NULL.
*/
public abstract class Condition extends Expression {
......@@ -23,7 +23,7 @@ public abstract class Condition extends Expression {
public long getPrecision() {
return ValueBoolean.PRECISION;
}
public int getDisplaySize() {
return ValueBoolean.DISPLAY_SIZE;
}
......
......@@ -17,7 +17,7 @@ import org.h2.value.ValueBoolean;
import org.h2.value.ValueNull;
/**
* @author Thomas
* An 'and' or 'or' condition as in WHERE ID=1 AND NAME=?
*/
public class ConditionAndOr extends Condition {
......@@ -126,7 +126,7 @@ public class ConditionAndOr extends Condition {
}
// TODO optimization: convert ((A=1 AND B=2) OR (A=1 AND B=3)) to (A=1 AND (B=2 OR B=3))
// this optimization does not work in the following case, but NOT is optimized before:
// CREATE TABLE TEST(A INT, B INT);
// CREATE TABLE TEST(A INT, B INT);
// INSERT INTO TEST VALUES(1, NULL);
// SELECT * FROM TEST WHERE NOT (B=A AND B=0); // no rows
// SELECT * FROM TEST WHERE NOT (B=A AND B=0 AND A=0); // 1, NULL
......@@ -219,7 +219,7 @@ public class ConditionAndOr extends Condition {
public int getCost() {
return left.getCost() + right.getCost();
}
public Expression optimizeInJoin(Session session, Select select) throws SQLException {
if (andOrType == AND) {
Expression l = left.optimizeInJoin(session, select);
......
......@@ -15,9 +15,8 @@ import org.h2.value.Value;
import org.h2.value.ValueBoolean;
/**
* @author Thomas
* An 'exists' condition as in WHERE EXISTS(SELECT ...)
*/
public class ConditionExists extends Condition {
private final Query query;
......
......@@ -24,9 +24,8 @@ import org.h2.value.ValueBoolean;
import org.h2.value.ValueNull;
/**
* @author Thomas
* An 'in' condition with a list of values, as in WHERE NAME IN(...)
*/
public class ConditionIn extends Condition {
private final Database database;
......
......@@ -21,7 +21,7 @@ import org.h2.value.ValueBoolean;
import org.h2.value.ValueNull;
/**
* @author Thomas
* An 'in' condition with a subquery, as in WHERE ID IN(SELECT ...)
*/
public class ConditionInSelect extends Condition {
private Database database;
......
......@@ -13,6 +13,9 @@ import org.h2.table.TableFilter;
import org.h2.value.Value;
import org.h2.value.ValueNull;
/**
* A NOT condition.
*/
public class ConditionNot extends Condition {
private Expression condition;
......
......@@ -14,101 +14,287 @@ import org.h2.table.TableFilter;
import org.h2.util.StringUtils;
import org.h2.value.Value;
/**
* An expression is a operation, a value, or a function in a query.
*/
public abstract class Expression {
private boolean addedToFilter;
/**
* Return the resulting value for the current row.
*
* @param session the session
* @return the result
*/
public abstract Value getValue(Session session) throws SQLException;
/**
* Return the data type. The data type may not be known before the optimization phase.
*
* @param session the session
* @return the type
*/
public abstract int getType();
/**
* Map the columns of the resolver to expression columns.
*
* @param resolver the column resolver
* @return level the subquery nesting level
*/
public abstract void mapColumns(ColumnResolver resolver, int level) throws SQLException;
/**
* Try to optimize the expression.
*
* @param session the session
* @return the optimized expression
*/
public abstract Expression optimize(Session session) throws SQLException;
public abstract void setEvaluatable(TableFilter tableFilter, boolean b);
/**
* Tell the expression columns whether the table filter can return values now.
* This is used when optimizing the query.
*
* @param tableFilter the table filter
* @param value true if the table filter can return value
*/
public abstract void setEvaluatable(TableFilter tableFilter, boolean value);
/**
* Get the scale of this expression.
*
* @return the scale
*/
public abstract int getScale();
/**
* Get the precision of this expression.
*
* @return the precision
*/
public abstract long getPrecision();
/**
* Get the display size of this expression.
*
* @return the display size
*/
public abstract int getDisplaySize();
/**
* Get the SQL statement of this expression.
* This may not always be the original SQL statement,
* specially after optimization.
*
* @return the SQL statement
*/
public abstract String getSQL();
/**
* Update an aggregate value.
* This method is called at statement execution time once for each row.
*
* @param session the session
*/
public abstract void updateAggregate(Session session) throws SQLException;
/**
* Check if this expression and all sub-expressions can fulfill a criteria.
* If any part returns false, the result is false.
*
* @param visitor the visitor
* @return if the criteria can be fulfilled
*/
public abstract boolean isEverything(ExpressionVisitor visitor);
/**
* Estimate the cost to process the expression.
* Used when optimizing the query, to calculate the query plan
* with the lowest estimated cost.
*
* @return the estimated cost
*/
public abstract int getCost();
/**
* Check if this expression and all sub-expressions can fulfill a criteria.
* This is a convenience function.
*
* @param expressionVisitorType the visitor type
* @return if the criteria can be fulfilled
*/
public final boolean isEverything(int expressionVisitorType) {
ExpressionVisitor visitor = ExpressionVisitor.get(expressionVisitorType);
return isEverything(visitor);
}
/**
* If it is possible, return the negated expression. This is used
* to optimize NOT expressions: NOT ID>10 can be converted to
* ID<=10. Returns null if negating is not possible.
*
* @param session the session
* @return the negated expression, or null
*/
public Expression getNotIfPossible(Session session) {
// by default it is not possible
return null;
}
/**
* Check if this expression will always return the same value.
*
* @return if the expression is constant
*/
public boolean isConstant() {
return false;
}
/**
* Is the value of a parameter set.
*
* @return if it is set
*/
public boolean isValueSet() {
return false;
}
/**
* Check if this is an auto-increment column.
*
* @return true if it is an auto-increment column
*/
public boolean isAutoIncrement() {
return false;
}
/**
* Get the value in form of a boolean expression.
* Returns true, false, or null.
* In this database, everything can be a condition.
*
* @param session the session
* @return the result
*/
public Boolean getBooleanValue(Session session) throws SQLException {
// TODO optimization: is this required?
return getValue(session).getBoolean();
}
/**
* Create index conditions if possible and attach them to the table filter.
*
* @param session the session
* @param filter the table filter
*/
public void createIndexConditions(Session session, TableFilter filter) throws SQLException {
// default is do nothing
}
/**
* Get the column name or alias name of this expression.
*
* @return the column name
*/
public String getColumnName() {
return getAlias();
}
/**
* Get the schema name, or null
*
* @return the schema name
*/
public String getSchemaName() {
return null;
}
}
/**
* Get the table name, or null
*
* @return the table name
*/
public String getTableName() {
return null;
}
/**
* Check whether this expression is a column and can store null values.
*
* @return whether null values are allowed
*/
public int getNullable() {
return Column.NULLABLE_UNKNOWN;
}
/**
* Get the table alias name or null
* if this expression does not represent a column.
*
* @return the table alias name
*/
public String getTableAlias() {
return null;
}
/**
* Get the alias name of a column or SQL expression
* if it is not an aliased expression.
*
* @return the alias name
*/
public String getAlias() {
return StringUtils.unEnclose(getSQL());
}
/**
* Only returns true if the expression is a wildcard.
*
* @return if this expression is a wildcard
*/
public boolean isWildcard() {
return false;
}
/**
* Returns the main expression, skipping aliases.
*
* @return the expression
*/
public Expression getNonAliasExpression() {
return this;
}
/**
* Add conditions to a table filter if they can be evaluated.
*
* @param filter the table filter
* @param outerJoin if the expression is part of an outer join
*/
public void addFilterConditions(TableFilter filter, boolean outerJoin) {
if (!addedToFilter && !outerJoin && isEverything(ExpressionVisitor.EVALUATABLE)) {
filter.addFilterCondition(this, false);
addedToFilter = true;
}
}
/**
* Convert this expression to a String.
*
* @return the string representation
*/
public String toString() {
return getSQL();
}
/**
* Optimize IN(...) expressions if possible.
*
* @param session the session
* @param select the query
* @return the optimized expression
*/
public Expression optimizeInJoin(Session session, Select select) throws SQLException {
return this;
}
......
......@@ -23,6 +23,9 @@ import org.h2.table.TableFilter;
import org.h2.value.Value;
import org.h2.value.ValueBoolean;
/**
* A expression that represents a column of a table or view.
*/
public class ExpressionColumn extends Expression {
private Database database;
private String schemaName;
......
......@@ -12,6 +12,10 @@ import org.h2.table.TableFilter;
import org.h2.value.Value;
import org.h2.value.ValueArray;
/**
* A list of expressions, as in (ID, NAME).
* The result of this expression is an array.
*/
public class ExpressionList extends Expression {
private Expression[] list;
......@@ -66,7 +70,7 @@ public class ExpressionList extends Expression {
public long getPrecision() {
return Integer.MAX_VALUE;
}
public int getDisplaySize() {
return Integer.MAX_VALUE;
}
......
......@@ -7,25 +7,29 @@ package org.h2.expression;
import org.h2.table.ColumnResolver;
import org.h2.table.Table;
/**
* The visitor pattern is used to iterate through all expressions of a query
* to optimize a statement.
*/
public class ExpressionVisitor {
// Is the value independent on unset parameters or on columns of a higher level query, or sequence values (that means can it be evaluated right now)
public static final int INDEPENDENT = 0;
// Are all aggregates MIN(column), MAX(column), or COUNT(*)?
public static final int OPTIMIZABLE_MIN_MAX_COUNT_ALL = 1;
// Does the expression return the same results for the same parameters?
public static final int DETERMINISTIC = 2;
// Can the expression be evaluated, that means are all columns set to 'evaluatable'?
public static final int EVALUATABLE = 3;
// Request to set the latest modification id
public static final int SET_MAX_DATA_MODIFICATION_ID = 4;
// Does the expression have no side effects (change the data)?
public static final int READONLY = 5;
// Does an expression have no relation to the given table filter?
public static final int NOT_FROM_RESOLVER = 6;
......@@ -34,23 +38,23 @@ public class ExpressionVisitor {
public int type;
private long maxDataModificationId;
private ColumnResolver resolver;
public static ExpressionVisitor get(int type) {
return new ExpressionVisitor(type);
}
public long getMaxDataModificationId() {
return maxDataModificationId;
}
private ExpressionVisitor(int type) {
this.type = type;
}
public void queryLevel(int offset) {
queryLevel += offset;
}
public ColumnResolver getResolver() {
return resolver;
}
......
......@@ -56,9 +56,8 @@ import org.h2.value.ValueTimestamp;
import org.h2.value.ValueUuid;
/**
* @author Thomas
* This class implements most built-in functions of this database.
*/
public class Function extends Expression implements FunctionCall {
// TODO functions: add function hashcode(value)
......@@ -1707,7 +1706,7 @@ public class Function extends Expression implements FunctionCall {
ValueResultSet vr = ValueResultSet.get(getSimpleResultSet(result, Integer.MAX_VALUE));
return vr;
}
SimpleResultSet getSimpleResultSet(LocalResult rs, int maxrows) throws SQLException {
int columnCount = rs.getVisibleColumnCount();
SimpleResultSet simple = new SimpleResultSet();
......
......@@ -10,6 +10,10 @@ import org.h2.engine.Session;
import org.h2.value.Value;
import org.h2.value.ValueResultSet;
/**
* This interface is used by the built-in functions,
* as well as the user defined functions.
*/
public interface FunctionCall {
String getName();
......
......@@ -4,6 +4,9 @@
*/
package org.h2.expression;
/**
* This class contains information about a built-in function.
*/
class FunctionInfo {
String name;
int type;
......
......@@ -21,8 +21,11 @@ import org.h2.value.DataType;
import org.h2.value.Value;
import org.h2.value.ValueNull;
/**
* This class wrapps a user defined aggregate.
*/
public class JavaAggregate extends Expression {
private final UserAggregate userAggregate;
private final Select select;
private AggregateFunction aggregate;
......@@ -48,11 +51,11 @@ public class JavaAggregate extends Expression {
public long getPrecision() {
return Integer.MAX_VALUE;
}
public int getDisplaySize() {
return Integer.MAX_VALUE;
}
public int getScale() {
return 0;
}
......@@ -115,7 +118,7 @@ public class JavaAggregate extends Expression {
args[i].setEvaluatable(tableFilter, b);
}
}
private AggregateFunction getInstance() throws SQLException {
AggregateFunction agg = userAggregate.getInstance();
agg.init(userConnection);
......@@ -164,5 +167,5 @@ public class JavaAggregate extends Expression {
agg.add(argValues);
}
}
}
......@@ -15,6 +15,9 @@ import org.h2.value.Value;
import org.h2.value.ValueNull;
import org.h2.value.ValueResultSet;
/**
* This class wrapps a user defined function.
*/
public class JavaFunction extends Expression implements FunctionCall {
private FunctionAlias functionAlias;
......
......@@ -16,6 +16,9 @@ import org.h2.value.Value;
import org.h2.value.ValueNull;
import org.h2.value.ValueString;
/**
* A mathematical expression, or string concatenation.
*/
public class Operation extends Expression {
public static final int CONCAT = 0, PLUS = 1, MINUS = 2, MULTIPLY = 3, DIVIDE = 4, NEGATE = 5;
private int opType;
......@@ -202,7 +205,7 @@ public class Operation extends Expression {
}
return left.getPrecision();
}
public int getDisplaySize() {
if (right != null) {
switch (opType) {
......
......@@ -15,7 +15,7 @@ import org.h2.value.ValueBoolean;
import org.h2.value.ValueNull;
/**
* @author Thomas
* A parameter of a prepared statement.
*/
public class Parameter extends Expression implements ParameterInterface {
......@@ -72,7 +72,7 @@ public class Parameter extends Expression implements ParameterInterface {
public boolean isConstant() {
return false;
}
public boolean isValueSet() {
return value != null;
}
......@@ -88,10 +88,10 @@ public class Parameter extends Expression implements ParameterInterface {
public long getPrecision() {
return value == null ? 0 : value.getPrecision();
}
public int getDisplaySize() {
return value == null ? 0 : value.getDisplaySize();
}
}
public void updateAggregate(Session session) {
// nothing to do
......@@ -118,11 +118,11 @@ public class Parameter extends Expression implements ParameterInterface {
throw Message.getInternalError("type="+visitor.type);
}
}
public int getCost() {
return 0;
}
public Expression getNotIfPossible(Session session) {
return new Comparison(session, Comparison.EQUAL, this, ValueExpression.get(ValueBoolean.get(false)));
}
......
......@@ -8,6 +8,9 @@ import java.sql.SQLException;
import org.h2.value.Value;
/**
* The interface for client side (remote) and server side parameters.
*/
public interface ParameterInterface {
void setValue(Value value);
Value getParamValue() throws SQLException;
......
......@@ -10,15 +10,18 @@ import org.h2.constant.ErrorCode;
import org.h2.message.Message;
import org.h2.value.Value;
/**
* A client side (remote) parameter.
*/
public class ParameterRemote implements ParameterInterface {
private Value value;
private int index;
public ParameterRemote(int index) {
this.index = index;
}
public void setValue(Value value) {
this.value = value;
}
......@@ -26,7 +29,7 @@ public class ParameterRemote implements ParameterInterface {
public Value getParamValue() {
return value;
}
public void checkSet() throws SQLException {
if (value == null) {
throw Message.getSQLException(ErrorCode.PARAMETER_NOT_SET_1, "#" + (index + 1));
......
......@@ -14,10 +14,13 @@ import org.h2.table.TableFilter;
import org.h2.value.Value;
import org.h2.value.ValueInt;
/**
* Represents the ROWNUM function.
*/
public class Rownum extends Expression {
private Prepared prepared;
public Rownum(Prepared prepared) {
this.prepared = prepared;
}
......@@ -47,7 +50,7 @@ public class Rownum extends Expression {
public long getPrecision() {
return ValueInt.PRECISION;
}
public int getDisplaySize() {
return ValueInt.DISPLAY_SIZE;
}
......@@ -80,7 +83,7 @@ public class Rownum extends Expression {
throw Message.getInternalError("type="+visitor.type);
}
}
public int getCost() {
return 0;
}
......
......@@ -15,6 +15,9 @@ import org.h2.value.Value;
import org.h2.value.ValueInt;
import org.h2.value.ValueLong;
/**
* Wraps a sequence when used in a statement.
*/
public class SequenceValue extends Expression {
private Sequence sequence;
......@@ -56,10 +59,10 @@ public class SequenceValue extends Expression {
public long getPrecision() {
return ValueInt.PRECISION;
}
public int getDisplaySize() {
return ValueInt.DISPLAY_SIZE;
}
}
public String getSQL() {
return "(NEXT VALUE FOR " + sequence.getSQL() +")";
......@@ -89,9 +92,9 @@ public class SequenceValue extends Expression {
throw Message.getInternalError("type="+visitor.type);
}
}
public int getCost() {
return 1;
}
}
......@@ -18,9 +18,9 @@ import org.h2.value.ValueArray;
import org.h2.value.ValueNull;
/**
* @author Thomas
* A query returning a single value.
* Subqueries are used inside other statements.
*/
public class Subquery extends Expression {
private Query query;
......@@ -79,10 +79,10 @@ public class Subquery extends Expression {
public long getPrecision() {
return getExpression().getPrecision();
}
public int getDisplaySize() {
return getExpression().getDisplaySize();
}
}
public String getSQL() {
return "(" + query.getPlanSQL() + ")";
......
......@@ -15,6 +15,9 @@ import org.h2.value.Value;
import org.h2.value.ValueBoolean;
import org.h2.value.ValueNull;
/**
* An expression representing a constant value.
*/
public class ValueExpression extends Expression {
private Value value;
......@@ -62,10 +65,10 @@ public class ValueExpression extends Expression {
public boolean isConstant() {
return true;
}
public boolean isValueSet() {
return true;
}
}
public void setEvaluatable(TableFilter tableFilter, boolean b) {
}
......@@ -77,7 +80,7 @@ public class ValueExpression extends Expression {
public long getPrecision() {
return value.getPrecision();
}
public int getDisplaySize() {
return value.getDisplaySize();
}
......
......@@ -13,11 +13,11 @@ import org.h2.table.ColumnResolver;
import org.h2.table.TableFilter;
import org.h2.value.Value;
/**
* @author Thomas
* A wildcard expression as in SELECT * FROM TEST.
* This object is only used temporarily during the parsing phase, and later
* replaced by column expressions.
*/
public class Wildcard extends Expression {
private String schema;
private String table;
......@@ -80,13 +80,13 @@ public class Wildcard extends Expression {
}
public void updateAggregate(Session session) {
throw Message.getInternalError();
throw Message.getInternalError();
}
public boolean isEverything(ExpressionVisitor visitor) {
throw Message.getInternalError();
}
public int getCost() {
throw Message.getInternalError();
}
......
......@@ -14,6 +14,9 @@ import java.util.HashSet;
import org.h2.util.ObjectUtils;
/**
* The global settings of a full text search.
*/
public class FullTextSettings {
private static HashMap settings = new HashMap();
......
......@@ -4,6 +4,9 @@
*/
package org.h2.fulltext;
/**
* The settings of one full text search index.
*/
public class IndexInfo {
int id;
String schemaName;
......
......@@ -9,14 +9,13 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;
public abstract class FileSystem {
public static final String MEMORY_PREFIX = "memFS:";
public static final String MEMORY_PREFIX_LZF = "memLZF:";
public static final String DB_PREFIX = "jdbc:";
public static final String ZIP_PREFIX = "zip:";
public static FileSystem getInstance(String fileName) {
if (isInMemory(fileName)) {
return FileSystemMemory.getInstance();
......@@ -27,27 +26,27 @@ public abstract class FileSystem {
}
return FileSystemDisk.getInstance();
}
private static boolean isInMemory(String fileName) {
return fileName != null && (fileName.startsWith(MEMORY_PREFIX) || fileName.startsWith(MEMORY_PREFIX_LZF));
}
}
public abstract long length(String fileName);
public abstract void rename(String oldName, String newName) throws SQLException;
public abstract boolean createNewFile(String fileName) throws SQLException;
public abstract boolean exists(String fileName);
public abstract void delete(String fileName) throws SQLException;
public abstract boolean tryDelete(String fileName);
public abstract String createTempFile(String prefix, String suffix, boolean deleteOnExit, boolean inTempDir) throws IOException;
public abstract String[] listFiles(String path) throws SQLException;
public abstract void deleteRecursive(String fileName) throws SQLException;
public abstract boolean isReadOnly(String fileName);
......@@ -59,7 +58,7 @@ public abstract class FileSystem {
public abstract boolean isDirectory(String fileName);
public abstract boolean isAbsolute(String fileName);
public abstract String getAbsolutePath(String fileName);
public abstract long getLastModified(String fileName);
......@@ -67,20 +66,20 @@ public abstract class FileSystem {
public abstract boolean canWrite(String fileName);
public abstract void copy(String original, String copy) throws SQLException;
public void mkdirs(String directoryName) throws SQLException {
createDirs(directoryName + "/x");
}
public abstract void createDirs(String fileName) throws SQLException;
public abstract String getFileName(String name) throws SQLException;
public abstract boolean fileStartsWith(String fileName, String prefix);
public abstract OutputStream openFileOutputStream(String fileName, boolean append) throws SQLException;
public abstract FileObject openFileObject(String fileName, String mode) throws IOException;
public abstract InputStream openFileInputStream(String fileName) throws IOException;
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论