Search in sources :

Example 41 with OrderedHashSet

use of org.hsqldb_voltpatches.lib.OrderedHashSet in project voltdb by VoltDB.

the class ParserDML method compileInsertStatement.

/**
     * Retrieves an INSERT Statement from this parse context.
     */
StatementDMQL compileInsertStatement(RangeVariable[] outerRanges) {
    read();
    readThis(Tokens.INTO);
    boolean[] columnCheckList;
    int[] columnMap;
    int colCount;
    Table table = readTableName();
    boolean overridingUser = false;
    boolean overridingSystem = false;
    int enforcedDefaultIndex = table.getIdentityColumnIndex();
    boolean assignsToIdentity = false;
    columnCheckList = null;
    columnMap = table.getColumnMap();
    colCount = table.getColumnCount();
    int position = getPosition();
    if (!table.isInsertable()) {
        throw Error.error(ErrorCode.X_42545);
    }
    Table baseTable = table.getBaseTable();
    switch(token.tokenType) {
        case Tokens.DEFAULT:
            {
                read();
                readThis(Tokens.VALUES);
                Expression insertExpression = new Expression(OpTypes.ROW, new Expression[] {});
                insertExpression = new Expression(OpTypes.TABLE, new Expression[] { insertExpression });
                columnCheckList = table.getNewColumnCheckList();
                for (int i = 0; i < table.colDefaults.length; i++) {
                    if (table.colDefaults[i] == null && table.identityColumn != i) {
                        throw Error.error(ErrorCode.X_42544);
                    }
                }
                StatementDMQL cs = new StatementInsert(session, table, columnMap, insertExpression, columnCheckList, compileContext);
                return cs;
            }
        case Tokens.OPENBRACKET:
            {
                int brackets = readOpenBrackets();
                if (brackets == 1) {
                    boolean isQuery = false;
                    switch(token.tokenType) {
                        case Tokens.WITH:
                        case Tokens.SELECT:
                        case Tokens.TABLE:
                            {
                                rewind(position);
                                isQuery = true;
                                break;
                            }
                        default:
                    }
                    if (isQuery) {
                        break;
                    }
                    OrderedHashSet columnNames = new OrderedHashSet();
                    readSimpleColumnNames(columnNames, table);
                    readThis(Tokens.CLOSEBRACKET);
                    colCount = columnNames.size();
                    columnMap = table.getColumnIndexes(columnNames);
                    if (token.tokenType != Tokens.VALUES && token.tokenType != Tokens.OVERRIDING) {
                        break;
                    }
                // $FALL-THROUGH$
                } else {
                    rewind(position);
                    break;
                }
            }
        // $FALL-THROUGH$
        case Tokens.OVERRIDING:
            {
                if (token.tokenType == Tokens.OVERRIDING) {
                    read();
                    if (token.tokenType == Tokens.USER) {
                        read();
                        overridingUser = true;
                    } else if (token.tokenType == Tokens.SYSTEM) {
                        read();
                        overridingSystem = true;
                    } else {
                        unexpectedToken();
                    }
                }
                if (token.tokenType != Tokens.VALUES) {
                    break;
                }
            }
        // $FALL-THROUGH$
        case Tokens.VALUES:
            {
                read();
                columnCheckList = table.getColumnCheckList(columnMap);
                Expression insertExpressions = XreadContextuallyTypedTable(colCount);
                HsqlList unresolved = insertExpressions.resolveColumnReferences(outerRanges, null);
                ExpressionColumn.checkColumnsResolved(unresolved);
                insertExpressions.resolveTypes(session, null);
                setParameterTypes(insertExpressions, table, columnMap);
                if (table != baseTable) {
                    int[] baseColumnMap = table.getBaseTableColumnMap();
                    int[] newColumnMap = new int[columnMap.length];
                    ArrayUtil.projectRow(baseColumnMap, columnMap, newColumnMap);
                    columnMap = newColumnMap;
                }
                Expression[] rowList = insertExpressions.nodes;
                for (int j = 0; j < rowList.length; j++) {
                    Expression[] rowArgs = rowList[j].nodes;
                    for (int i = 0; i < rowArgs.length; i++) {
                        Expression e = rowArgs[i];
                        if (enforcedDefaultIndex == columnMap[i]) {
                            assignsToIdentity = true;
                            if (e.getType() != OpTypes.DEFAULT) {
                                if (table.identitySequence.isAlways()) {
                                    if (!overridingUser && !overridingSystem) {
                                        throw Error.error(ErrorCode.X_42543);
                                    }
                                } else {
                                /*
                                    if (overridingUser) {
                                        throw Trace.error(
                                            Trace.SQL_DEFAULT_CLAUSE_REQUITED);
                                    }
*/
                                }
                            }
                        }
                        if (e.isParam()) {
                            e.setAttributesAsColumn(table.getColumn(columnMap[i]), true);
                        } else if (e.getType() == OpTypes.DEFAULT) {
                            if (table.colDefaults[i] == null && table.identityColumn != columnMap[i]) {
                                throw Error.error(ErrorCode.X_42544);
                            }
                        }
                    }
                }
                if (!assignsToIdentity && (overridingUser || overridingSystem)) {
                    unexpectedTokenRequire(Tokens.T_OVERRIDING);
                }
                StatementDMQL cs = new StatementInsert(session, table, columnMap, insertExpressions, columnCheckList, compileContext);
                return cs;
            }
        case Tokens.WITH:
        case Tokens.SELECT:
        case Tokens.TABLE:
            {
                break;
            }
        default:
            {
                throw unexpectedToken();
            }
    }
    columnCheckList = table.getColumnCheckList(columnMap);
    QueryExpression queryExpression = XreadQueryExpression();
    queryExpression.setAsTopLevel();
    queryExpression.resolve(session, outerRanges);
    if (colCount != queryExpression.getColumnCount()) {
        throw Error.error(ErrorCode.X_42546);
    }
    if (table != baseTable) {
        int[] baseColumnMap = table.getBaseTableColumnMap();
        int[] newColumnMap = new int[columnMap.length];
        ArrayUtil.projectRow(baseColumnMap, columnMap, newColumnMap);
        columnMap = newColumnMap;
    }
    if (enforcedDefaultIndex != -1 && ArrayUtil.find(columnMap, enforcedDefaultIndex) > -1) {
        if (table.identitySequence.isAlways()) {
            if (!overridingUser && !overridingSystem) {
                throw Error.error(ErrorCode.X_42543);
            }
        } else {
        /*
                if (overridingUser) {
                    throw Trace.error(
                        Trace.SQL_DEFAULT_CLAUSE_REQUITED);
                }
*/
        }
    } else if (overridingUser || overridingSystem) {
        unexpectedTokenRequire(Tokens.T_OVERRIDING);
    }
    StatementDMQL cs = new StatementInsert(session, table, columnMap, columnCheckList, queryExpression, compileContext);
    return cs;
}
Also used : OrderedHashSet(org.hsqldb_voltpatches.lib.OrderedHashSet) HsqlList(org.hsqldb_voltpatches.lib.HsqlList)

Example 42 with OrderedHashSet

use of org.hsqldb_voltpatches.lib.OrderedHashSet in project voltdb by VoltDB.

the class ParserDML method compileUpdateStatement.

/**
     * Creates an UPDATE-type Statement from this parse context.
     */
StatementDMQL compileUpdateStatement(RangeVariable[] outerRanges) {
    read();
    Expression[] updateExpressions;
    int[] columnMap;
    boolean[] columnCheckList;
    OrderedHashSet colNames = new OrderedHashSet();
    HsqlArrayList exprList = new HsqlArrayList();
    RangeVariable[] rangeVariables = { readSimpleRangeVariable(StatementTypes.UPDATE_WHERE) };
    Table table = rangeVariables[0].rangeTable;
    Table baseTable = table.getBaseTable();
    readThis(Tokens.SET);
    readSetClauseList(rangeVariables, colNames, exprList);
    columnMap = table.getColumnIndexes(colNames);
    columnCheckList = table.getColumnCheckList(columnMap);
    updateExpressions = new Expression[exprList.size()];
    exprList.toArray(updateExpressions);
    Expression condition = null;
    if (token.tokenType == Tokens.WHERE) {
        read();
        condition = XreadBooleanValueExpression();
        HsqlList unresolved = condition.resolveColumnReferences(outerRanges, null);
        unresolved = Expression.resolveColumnSet(rangeVariables, unresolved, null);
        ExpressionColumn.checkColumnsResolved(unresolved);
        condition.resolveTypes(session, null);
        if (condition.isParam()) {
            condition.dataType = Type.SQL_BOOLEAN;
        } else if (condition.getDataType() != Type.SQL_BOOLEAN) {
            throw Error.error(ErrorCode.X_42568);
        }
    }
    resolveUpdateExpressions(table, rangeVariables, columnMap, updateExpressions, outerRanges);
    if (baseTable != null && table != baseTable) {
        QuerySpecification select = ((TableDerived) table).getQueryExpression().getMainSelect();
        if (condition != null) {
            condition = condition.replaceColumnReferences(rangeVariables[0], select.exprColumns);
        }
        rangeVariables[0] = new RangeVariable(select.rangeVariables[0]);
        condition = ExpressionLogical.andExpressions(select.queryCondition, condition);
    }
    if (condition != null) {
        RangeVariableResolver resolver = new RangeVariableResolver(rangeVariables, condition, compileContext);
        resolver.processConditions();
        rangeVariables = resolver.rangeVariables;
    }
    if (baseTable != null && table != baseTable) {
        int[] baseColumnMap = table.getBaseTableColumnMap();
        int[] newColumnMap = new int[columnMap.length];
        ArrayUtil.projectRow(baseColumnMap, columnMap, newColumnMap);
        columnMap = newColumnMap;
    }
    StatementDMQL cs = new StatementDML(session, table, rangeVariables, columnMap, updateExpressions, columnCheckList, compileContext);
    return cs;
}
Also used : HsqlArrayList(org.hsqldb_voltpatches.lib.HsqlArrayList) HsqlList(org.hsqldb_voltpatches.lib.HsqlList) OrderedHashSet(org.hsqldb_voltpatches.lib.OrderedHashSet)

Example 43 with OrderedHashSet

use of org.hsqldb_voltpatches.lib.OrderedHashSet in project voltdb by VoltDB.

the class ParserDQL method XreadUnionCorrespondingClause.

void XreadUnionCorrespondingClause(QueryExpression queryExpression) {
    if (token.tokenType == Tokens.CORRESPONDING) {
        read();
        queryExpression.setUnionCorresponding();
        if (token.tokenType == Tokens.BY) {
            read();
            OrderedHashSet names = readColumnNames(false);
            queryExpression.setUnionCorrespondingColumns(names);
        }
    }
}
Also used : OrderedHashSet(org.hsqldb_voltpatches.lib.OrderedHashSet)

Example 44 with OrderedHashSet

use of org.hsqldb_voltpatches.lib.OrderedHashSet in project voltdb by VoltDB.

the class ParserDQL method compileCursorSpecification.

/**
     * Retrieves a SELECT or other query expression Statement from this parse context.
     */
StatementDMQL compileCursorSpecification() {
    QueryExpression queryExpression = XreadQueryExpression();
    queryExpression.setAsTopLevel();
    queryExpression.resolve(session);
    if (token.tokenType == Tokens.FOR) {
        read();
        if (token.tokenType == Tokens.READ) {
            read();
            readThis(Tokens.ONLY);
        } else {
            readThis(Tokens.UPDATE);
            if (token.tokenType == Tokens.OF) {
                readThis(Tokens.OF);
                OrderedHashSet colNames = readColumnNameList(null, false);
            }
        }
    }
    StatementDMQL cs = new StatementQuery(session, queryExpression, compileContext);
    return cs;
}
Also used : OrderedHashSet(org.hsqldb_voltpatches.lib.OrderedHashSet)

Example 45 with OrderedHashSet

use of org.hsqldb_voltpatches.lib.OrderedHashSet in project voltdb by VoltDB.

the class ParserDQL method readColumnNames.

OrderedHashSet readColumnNames(BitMap quotedFlags, boolean readAscDesc) {
    readThis(Tokens.OPENBRACKET);
    OrderedHashSet set = readColumnNameList(quotedFlags, readAscDesc);
    readThis(Tokens.CLOSEBRACKET);
    return set;
}
Also used : OrderedHashSet(org.hsqldb_voltpatches.lib.OrderedHashSet)

Aggregations

OrderedHashSet (org.hsqldb_voltpatches.lib.OrderedHashSet)86 HsqlName (org.hsqldb_voltpatches.HsqlNameManager.HsqlName)47 Iterator (org.hsqldb_voltpatches.lib.Iterator)26 WrapperIterator (org.hsqldb_voltpatches.lib.WrapperIterator)24 Constraint (org.hsqldb_voltpatches.Constraint)19 SchemaObject (org.hsqldb_voltpatches.SchemaObject)19 PersistentStore (org.hsqldb_voltpatches.persist.PersistentStore)19 Table (org.hsqldb_voltpatches.Table)18 HsqlException (org.hsqldb_voltpatches.HsqlException)16 TextTable (org.hsqldb_voltpatches.TextTable)16 HsqlArrayList (org.hsqldb_voltpatches.lib.HsqlArrayList)11 HsqlList (org.hsqldb_voltpatches.lib.HsqlList)8 Grantee (org.hsqldb_voltpatches.rights.Grantee)6 Routine (org.hsqldb_voltpatches.Routine)5 RoutineSchema (org.hsqldb_voltpatches.RoutineSchema)5 Right (org.hsqldb_voltpatches.rights.Right)5 TriggerDef (org.hsqldb_voltpatches.TriggerDef)4 OrderedIntHashSet (org.hsqldb_voltpatches.lib.OrderedIntHashSet)3 Type (org.hsqldb_voltpatches.types.Type)3 HashMappedList (org.hsqldb_voltpatches.lib.HashMappedList)2