~tjoneslo/akiban-sql-parser/fix-bug-1167451

« back to all changes in this revision

Viewing changes to src/main/javacc/SQLGrammar.jj

merge oontvoo: support adding multiple columns in ALTER TABLE

https://code.launchpad.net/~oontvoo/akiban-sql-parser/alter_adding_multiple_cols/+merge/134983

Show diffs side-by-side

added added

removed removed

Lines of Context:
1736
1736
        }
1737
1737
    }
1738
1738
 
 
1739
    /**
 
1740
     * check if the next token  is an alter-table action
 
1741
     * (ie., ADD, DROP, ALTER, UPDATE, RENAME
 
1742
     */
 
1743
    boolean notAlterActionFollows()
 
1744
    {
 
1745
        switch(getToken(1).kind)
 
1746
        {
 
1747
            case ADD:
 
1748
            case DROP:
 
1749
            case ALTER:
 
1750
            case UPDATE:
 
1751
            case RENAME:
 
1752
                return false;
 
1753
            default:
 
1754
                return true;
 
1755
         }
 
1756
    }
 
1757
 
1739
1758
    void setParserContext(SQLParserContext parserContext) {
1740
1759
        this.parserContext = parserContext;
1741
1760
        this.nodeFactory = parserContext.getNodeFactory();
1757
1776
        StatementList(result);
1758
1777
        return result;
1759
1778
    }
1760
 
 
1761
1779
}
1762
1780
 
1763
1781
PARSER_END(SQLGrammar)
3749
3767
void
3750
3768
tableElement(TableElementList tableElementList) throws StandardException :
3751
3769
{
3752
 
    TableElementNode tableElement;
3753
3770
}
3754
3771
{
3755
 
    tableElement = columnDefinition(tableElementList)
3756
 
    {
3757
 
        tableElementList.addTableElement(tableElement);
3758
 
    }
3759
 
|
3760
 
    tableElement = tableConstraintDefinition()
3761
 
    {
3762
 
        tableElementList.addTableElement(tableElement);
 
3772
    columnDefinition(tableElementList) | tableConstraintDefinition(tableElementList)
 
3773
    {
 
3774
        return ;
3763
3775
    }
3764
3776
}
3765
3777
 
3766
 
TableElementNode
 
3778
boolean
3767
3779
columnDefinition(TableElementList tableElementList) throws StandardException :
3768
3780
{
3769
3781
    DataTypeDescriptor[] typeDescriptor = new DataTypeDescriptor[1];
3784
3796
        if (autoIncrementInfo[QueryTreeNode.AUTOINCREMENT_IS_AUTOINCREMENT_INDEX] == 0) {
3785
3797
            autoIncrementInfo = null;
3786
3798
        }
3787
 
 
3788
 
        return (TableElementNode)nodeFactory.getNode(NodeTypes.COLUMN_DEFINITION_NODE,
3789
 
                                                     columnName,
3790
 
                                                     defaultNode,
3791
 
                                                     typeDescriptor[0],
3792
 
                                                     autoIncrementInfo,
3793
 
                                                     parserContext);
 
3799
        
 
3800
        tableElementList.addTableElement((TableElementNode) nodeFactory.getNode(
 
3801
                                                            NodeTypes.COLUMN_DEFINITION_NODE,
 
3802
                                                            columnName,
 
3803
                                                            defaultNode,
 
3804
                                                            typeDescriptor[0],
 
3805
                                                            autoIncrementInfo,
 
3806
                                                            parserContext));
 
3807
        return autoIncrementInfo != null;
3794
3808
    }
3795
3809
}
3796
3810
 
3797
 
 
3798
3811
ValueNode
3799
3812
defaultAndConstraints(DataTypeDescriptor[] typeDescriptor,
3800
3813
                      TableElementList tableElementList,
12432
12445
    }
12433
12446
}
12434
12447
 
12435
 
TableElementNode
12436
 
tableConstraintDefinition() throws StandardException :
 
12448
void
 
12449
tableConstraintDefinition(TableElementList tableElementList) throws StandardException :
12437
12450
{
12438
12451
    Properties properties = null;
12439
12452
    ConstraintDefinitionNode tcdn;
12447
12460
        if (properties != null) {
12448
12461
            tcdn.setProperties(properties);
12449
12462
        }
12450
 
        return tcdn;
 
12463
 
 
12464
        tableElementList.addTableElement((TableElementNode)tcdn);
12451
12465
    }
12452
12466
}
12453
12467
 
13056
13070
                                                  parserContext);
13057
13071
    }
13058
13072
|
13059
 
    lockGranularity = alterTableAction(tableElementList, changeType, behavior)
 
13073
    alterTableAction(tableElementList, changeType, behavior)
13060
13074
    {
13061
13075
        return (StatementNode)nodeFactory.getNode(NodeTypes.ALTER_TABLE_NODE,
13062
13076
                                                  tableName,
13063
13077
                                                  tableElementList,
13064
 
                                                  new Character(lockGranularity),
13065
 
                                                  changeType,
13066
 
                                                  behavior,
13067
 
                                                  parserContext);
 
13078
                                                  new Character(lockGranularity), // Not used, but we kind of nned it
 
13079
                                                  changeType,                     // to make the method take 5 args
 
13080
                                                  behavior,                       // The one that takes 4 args does
 
13081
                                                  parserContext);                 // something else!
13068
13082
    }
13069
13083
}
13070
13084
 
13108
13122
}
13109
13123
 
13110
13124
 
13111
 
char
 
13125
void
13112
13126
alterTableAction(TableElementList tableElementList, int[] changeType, int[] behavior) 
13113
13127
        throws StandardException :
13114
13128
{
13115
 
    char lockGranularity = '\0';
13116
 
    TableElementNode    tableElement;
 
13129
    TableElementNode tableElement;
 
13130
    boolean hasAutoIncrement = false;
13117
13131
    DataTypeDescriptor  typeDescriptor;
13118
13132
    Token tok = null;
13119
13133
    String name;
13124
13138
    <ADD>
13125
13139
    (
13126
13140
        LOOKAHEAD ( {getToken(1).kind == UNIQUE && getToken(2).kind == INDEX || getToken(1).kind == INDEX} )
13127
 
        tableElement = addIndex()
13128
 
    |
13129
 
        tableElement = addColumnDefinition(tableElementList)
13130
 
    |
13131
 
        tableElement = tableConstraintDefinition()
 
13141
        addIndex(tableElementList)
 
13142
    |
 
13143
        hasAutoIncrement = addColumnDefinition(tableElementList)
 
13144
    |
 
13145
        tableConstraintDefinition(tableElementList)
13132
13146
    )
13133
13147
    {
13134
 
        if (tableElement instanceof ColumnDefinitionNode) {
 
13148
        if (hasAutoIncrement)
13135
13149
            //bug 5724 - auto increment columns not allowed in ALTER TABLE statement
13136
 
            ColumnDefinitionNode cdn = (ColumnDefinitionNode)tableElement;
13137
 
            if (cdn.isAutoincrementColumn())
13138
 
                throw new StandardException("Auto increment column not allowed in ALTER TABLE");
13139
 
        }
 
13150
            throw new StandardException("Auto increment column not allowed in ALTER TABLE");
 
13151
 
13140
13152
        changeType[0] = DDLStatementNode.ADD_TYPE;
13141
 
        tableElementList.addTableElement(tableElement);
13142
 
        return lockGranularity;
13143
13153
    }
13144
13154
|
13145
13155
    <ALTER> [ <COLUMN> ] name = identifier() 
13147
13157
    {
13148
13158
        changeType[0] = DDLStatementNode.MODIFY_TYPE;
13149
13159
        tableElementList.addTableElement(tableElement);
13150
 
        return lockGranularity;
13151
13160
    }
13152
13161
|
13153
13162
    <DROP>
13162
13171
    {
13163
13172
        changeType[0] = DDLStatementNode.DROP_TYPE;
13164
13173
        tableElementList.addTableElement(tableElement);
13165
 
        return lockGranularity;
13166
13174
    }
13167
13175
|
13168
13176
    <RENAME> [<TO> | <AS>] newName = qualifiedName()
13172
13180
                                                             newName,
13173
13181
                                                             parserContext);
13174
13182
        tableElementList.addTableElement(tableElement);
13175
 
        return lockGranularity;
13176
 
    }
13177
 
|
13178
 
    lockGranularity = DB2lockGranularityClause()
13179
 
    {
13180
 
        changeType[0] = DDLStatementNode.LOCKING_TYPE;
13181
 
        return lockGranularity;
13182
13183
    }
13183
13184
}
13184
13185
 
13236
13237
    }
13237
13238
}
13238
13239
 
13239
 
TableElementNode
13240
 
addIndex() throws StandardException :
 
13240
void
 
13241
addIndex(TableElementList tableElementList) throws StandardException :
13241
13242
{
13242
13243
    boolean groupFormat = hasFeature(SQLParserFeature.GROUPING);
13243
13244
 
13269
13270
    [ <AS> location = getLocation() ]
13270
13271
    {
13271
13272
        
13272
 
        return (TableElementNode)nodeFactory.getNode(NodeTypes.AT_ADD_INDEX_NODE,
13273
 
                                                  cond,
13274
 
                                                  unique,
13275
 
                                                  indexName,
13276
 
                                                  indexColumnList,
13277
 
                                                  joinType,
13278
 
                                                  properties,
13279
 
                                                  location,
13280
 
                                                  parserContext);
 
13273
        tableElementList.addTableElement((TableElementNode)nodeFactory.getNode
 
13274
                                                                (NodeTypes.AT_ADD_INDEX_NODE,
 
13275
                                                                 cond,
 
13276
                                                                 unique,
 
13277
                                                                 indexName,
 
13278
                                                                 indexColumnList,
 
13279
                                                                 joinType,
 
13280
                                                                 properties,
 
13281
                                                                 location,
 
13282
                                                                 parserContext));
13281
13283
    }
13282
13284
}
13283
13285
 
13303
13305
    }
13304
13306
}
13305
13307
 
13306
 
TableElementNode
 
13308
 
 
13309
boolean
13307
13310
addColumnDefinition(TableElementList tableElementList) throws StandardException :
13308
13311
{
13309
 
    TableElementNode tableElement;
 
13312
    boolean autoIncre;
 
13313
    boolean hasAutoIncre;
13310
13314
}
13311
13315
{
13312
 
    [ <COLUMN> ] tableElement = columnDefinition(tableElementList)
 
13316
    [ <COLUMN> ]
 
13317
    hasAutoIncre = columnDefinition(tableElementList)
 
13318
    ( <COMMA>
 
13319
      LOOKAHEAD ( { notAlterActionFollows() } )
 
13320
      (autoIncre = columnDefinition(tableElementList) { hasAutoIncre |= autoIncre;})
 
13321
    )*
13313
13322
    {
13314
 
        return tableElement;
 
13323
        return hasAutoIncre;
13315
13324
    }
13316
13325
}
13317
13326