Search in sources :

Example 81 with Expression

use of de.neemann.digital.hdl.model2.expression.Expression in project h2database by h2database.

the class Parser method parseSelectSimpleSelectPart.

private void parseSelectSimpleSelectPart(Select command) {
    Select temp = currentSelect;
    // make sure aggregate functions will not work in TOP and LIMIT
    currentSelect = null;
    if (readIf("TOP")) {
        // can't read more complex expressions here because
        // SELECT TOP 1 +? A FROM TEST could mean
        // SELECT TOP (1+?) A FROM TEST or
        // SELECT TOP 1 (+?) AS A FROM TEST
        Expression limit = readTerm().optimize(session);
        command.setLimit(limit);
    } else if (readIf("LIMIT")) {
        Expression offset = readTerm().optimize(session);
        command.setOffset(offset);
        Expression limit = readTerm().optimize(session);
        command.setLimit(limit);
    }
    currentSelect = temp;
    if (readIf("DISTINCT")) {
        command.setDistinct(true);
    } else {
        readIf("ALL");
    }
    ArrayList<Expression> expressions = New.arrayList();
    do {
        if (readIf("*")) {
            expressions.add(new Wildcard(null, null));
        } else {
            Expression expr = readExpression();
            if (readIf("AS") || currentTokenType == IDENTIFIER) {
                String alias = readAliasIdentifier();
                boolean aliasColumnName = database.getSettings().aliasColumnName;
                aliasColumnName |= database.getMode().aliasColumnName;
                expr = new Alias(expr, alias, aliasColumnName);
            }
            expressions.add(expr);
        }
    } while (readIf(","));
    command.setExpressions(expressions);
}
Also used : Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) Wildcard(org.h2.expression.Wildcard) CreateFunctionAlias(org.h2.command.ddl.CreateFunctionAlias) FunctionAlias(org.h2.engine.FunctionAlias) DropFunctionAlias(org.h2.command.ddl.DropFunctionAlias) Alias(org.h2.expression.Alias) ConditionInSelect(org.h2.expression.ConditionInSelect) Select(org.h2.command.dml.Select) ValueString(org.h2.value.ValueString)

Example 82 with Expression

use of de.neemann.digital.hdl.model2.expression.Expression in project h2database by h2database.

the class Parser method readTableFilter.

private TableFilter readTableFilter() {
    Table table;
    String alias = null;
    if (readIf("(")) {
        if (isSelect()) {
            Query query = parseSelectUnion();
            read(")");
            query.setParameterList(new ArrayList<>(parameters));
            query.init();
            Session s;
            if (createView != null) {
                s = database.getSystemSession();
            } else {
                s = session;
            }
            alias = session.getNextSystemIdentifier(sqlCommand);
            table = TableView.createTempView(s, session.getUser(), alias, query, currentSelect);
        } else {
            TableFilter top;
            top = readTableFilter();
            top = readJoin(top);
            read(")");
            alias = readFromAlias(null);
            if (alias != null) {
                top.setAlias(alias);
                ArrayList<String> derivedColumnNames = readDerivedColumnNames();
                if (derivedColumnNames != null) {
                    top.setDerivedColumns(derivedColumnNames);
                }
            }
            return top;
        }
    } else if (readIf("VALUES")) {
        table = parseValuesTable(0).getTable();
    } else {
        String tableName = readIdentifierWithSchema(null);
        Schema schema = getSchema();
        boolean foundLeftBracket = readIf("(");
        if (foundLeftBracket && readIf("INDEX")) {
            // Sybase compatibility with
            // "select * from test (index table1_index)"
            readIdentifierWithSchema(null);
            read(")");
            foundLeftBracket = false;
        }
        if (foundLeftBracket) {
            Schema mainSchema = database.getSchema(Constants.SCHEMA_MAIN);
            if (equalsToken(tableName, RangeTable.NAME) || equalsToken(tableName, RangeTable.ALIAS)) {
                Expression min = readExpression();
                read(",");
                Expression max = readExpression();
                if (readIf(",")) {
                    Expression step = readExpression();
                    read(")");
                    table = new RangeTable(mainSchema, min, max, step, false);
                } else {
                    read(")");
                    table = new RangeTable(mainSchema, min, max, false);
                }
            } else {
                Expression expr = readFunction(schema, tableName);
                if (!(expr instanceof FunctionCall)) {
                    throw getSyntaxError();
                }
                FunctionCall call = (FunctionCall) expr;
                if (!call.isDeterministic()) {
                    recompileAlways = true;
                }
                table = new FunctionTable(mainSchema, session, expr, call);
            }
        } else if (equalsToken("DUAL", tableName)) {
            table = getDualTable(false);
        } else if (database.getMode().sysDummy1 && equalsToken("SYSDUMMY1", tableName)) {
            table = getDualTable(false);
        } else {
            table = readTableOrView(tableName);
        }
    }
    ArrayList<String> derivedColumnNames = null;
    IndexHints indexHints = null;
    // for backward compatibility, handle case where USE is a table alias
    if (readIf("USE")) {
        if (readIf("INDEX")) {
            indexHints = parseIndexHints(table);
        } else {
            alias = "USE";
            derivedColumnNames = readDerivedColumnNames();
        }
    } else {
        alias = readFromAlias(alias);
        if (alias != null) {
            derivedColumnNames = readDerivedColumnNames();
            // if alias present, a second chance to parse index hints
            if (readIf("USE")) {
                read("INDEX");
                indexHints = parseIndexHints(table);
            }
        }
    }
    // inherit alias for CTE as views from table name
    if (table.isView() && table.isTableExpression() && alias == null) {
        alias = table.getName();
    }
    TableFilter filter = new TableFilter(session, table, alias, rightsChecked, currentSelect, orderInFrom++, indexHints);
    if (derivedColumnNames != null) {
        filter.setDerivedColumns(derivedColumnNames);
    }
    return filter;
}
Also used : RangeTable(org.h2.table.RangeTable) TruncateTable(org.h2.command.ddl.TruncateTable) CreateTable(org.h2.command.ddl.CreateTable) FunctionTable(org.h2.table.FunctionTable) CreateLinkedTable(org.h2.command.ddl.CreateLinkedTable) Table(org.h2.table.Table) DropTable(org.h2.command.ddl.DropTable) Query(org.h2.command.dml.Query) DropSchema(org.h2.command.ddl.DropSchema) CreateSchema(org.h2.command.ddl.CreateSchema) Schema(org.h2.schema.Schema) ValueString(org.h2.value.ValueString) IndexHints(org.h2.table.IndexHints) RangeTable(org.h2.table.RangeTable) TableFilter(org.h2.table.TableFilter) Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) FunctionTable(org.h2.table.FunctionTable) FunctionCall(org.h2.expression.FunctionCall) Session(org.h2.engine.Session)

Example 83 with Expression

use of de.neemann.digital.hdl.model2.expression.Expression in project h2database by h2database.

the class CreateTable method generateColumnsFromQuery.

private void generateColumnsFromQuery() {
    int columnCount = asQuery.getColumnCount();
    ArrayList<Expression> expressions = asQuery.getExpressions();
    ColumnNamer columnNamer = new ColumnNamer(session);
    for (int i = 0; i < columnCount; i++) {
        Expression expr = expressions.get(i);
        int type = expr.getType();
        String name = columnNamer.getColumnName(expr, i, expr.getAlias());
        long precision = expr.getPrecision();
        int displaySize = expr.getDisplaySize();
        DataType dt = DataType.getDataType(type);
        if (precision > 0 && (dt.defaultPrecision == 0 || (dt.defaultPrecision > precision && dt.defaultPrecision < Byte.MAX_VALUE))) {
            // dont' set precision to MAX_VALUE if this is the default
            precision = dt.defaultPrecision;
        }
        int scale = expr.getScale();
        if (scale > 0 && (dt.defaultScale == 0 || (dt.defaultScale > scale && dt.defaultScale < precision))) {
            scale = dt.defaultScale;
        }
        if (scale > precision) {
            precision = scale;
        }
        String[] enumerators = null;
        if (dt.type == Value.ENUM) {
            /**
             * Only columns of tables may be enumerated.
             */
            if (!(expr instanceof ExpressionColumn)) {
                throw DbException.get(ErrorCode.GENERAL_ERROR_1, "Unable to resolve enumerators of expression");
            }
            enumerators = ((ExpressionColumn) expr).getColumn().getEnumerators();
        }
        Column col = new Column(name, type, precision, scale, displaySize, enumerators);
        addColumn(col);
    }
}
Also used : ColumnNamer(org.h2.util.ColumnNamer) Expression(org.h2.expression.Expression) ExpressionColumn(org.h2.expression.ExpressionColumn) Column(org.h2.table.Column) DataType(org.h2.value.DataType) ExpressionColumn(org.h2.expression.ExpressionColumn)

Example 84 with Expression

use of de.neemann.digital.hdl.model2.expression.Expression in project h2database by h2database.

the class IndexCondition method getCurrentValueList.

/**
 * Get the current value list of the expression. The value list is of the
 * same type as the column, distinct, and sorted.
 *
 * @param session the session
 * @return the value list
 */
public Value[] getCurrentValueList(Session session) {
    HashSet<Value> valueSet = new HashSet<>();
    for (Expression e : expressionList) {
        Value v = e.getValue(session);
        v = column.convert(v);
        valueSet.add(v);
    }
    Value[] array = valueSet.toArray(new Value[valueSet.size()]);
    final CompareMode mode = session.getDatabase().getCompareMode();
    Arrays.sort(array, new Comparator<Value>() {

        @Override
        public int compare(Value o1, Value o2) {
            return o1.compareTo(o2, mode);
        }
    });
    return array;
}
Also used : Expression(org.h2.expression.Expression) Value(org.h2.value.Value) CompareMode(org.h2.value.CompareMode) HashSet(java.util.HashSet)

Example 85 with Expression

use of de.neemann.digital.hdl.model2.expression.Expression in project h2database by h2database.

the class TestColumnNamer method test.

@Override
public void test() {
    ColumnNamer columnNamer = new ColumnNamer(null);
    columnNamer.getConfiguration().configure("MAX_IDENTIFIER_LENGTH = 30");
    columnNamer.getConfiguration().configure("REGULAR_EXPRESSION_MATCH_ALLOWED = '[A-Za-z0-9_]+'");
    columnNamer.getConfiguration().configure("REGULAR_EXPRESSION_MATCH_DISALLOWED = '[^A-Za-z0-9_]+'");
    columnNamer.getConfiguration().configure("DEFAULT_COLUMN_NAME_PATTERN = 'colName$$'");
    columnNamer.getConfiguration().configure("GENERATE_UNIQUE_COLUMN_NAMES = 1");
    int index = 0;
    for (String id : ids) {
        Expression columnExp = ValueExpression.getDefault();
        String newColumnName = columnNamer.getColumnName(columnExp, index + 1, id);
        assertNotNull(newColumnName);
        assertTrue(newColumnName.length() <= 30);
        assertTrue(newColumnName.length() >= 1);
        assertEquals(newColumnName, expectedColumnName[index]);
        index++;
    }
}
Also used : ColumnNamer(org.h2.util.ColumnNamer) ValueExpression(org.h2.expression.ValueExpression) Expression(org.h2.expression.Expression)

Aggregations

Expression (org.h2.expression.Expression)84 ValueExpression (org.h2.expression.ValueExpression)53 Column (org.h2.table.Column)45 ExpressionColumn (org.h2.expression.ExpressionColumn)38 IndexColumn (org.h2.table.IndexColumn)24 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)21 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)18 ValueString (org.h2.value.ValueString)18 AlterTableRenameColumn (org.h2.command.ddl.AlterTableRenameColumn)17 TableFilter (org.h2.table.TableFilter)15 AlterTableDropConstraint (org.h2.command.ddl.AlterTableDropConstraint)14 AlterTableRenameConstraint (org.h2.command.ddl.AlterTableRenameConstraint)14 Expression (expression.Expression)13 Table (org.h2.table.Table)13 Value (org.h2.value.Value)12 ArrayList (java.util.ArrayList)11 Node (expression.Node)10 RangeTable (org.h2.table.RangeTable)10 Number (expression.Number)9 CreateTable (org.h2.command.ddl.CreateTable)9