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;
    }
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 ; } } else { rewind ( position ) ; break ; } } 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 ; } } 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 ( 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 { } } else if ( overridingUser || overridingSystem ) { unexpectedTokenRequire ( Tokens . T_OVERRIDING ) ; } StatementDMQL cs = new StatementInsert ( session , table , columnMap , columnCheckList , queryExpression , compileContext ) ; return cs ; }
Retrieves an INSERT Statement from this parse context.