NOT FULL AST: partition options, foreign key, ORDER BY not supported
상속: IDdlStatement
예제 #1
0
 public virtual void Visit(DdlAlterTableStatement node)
 {
     VisitInternal(node.Table);
 }
예제 #2
0
        /// <summary>token of table name has been consumed</summary>
        /// <exception cref="System.SqlSyntaxErrorException" />
        private DdlAlterTableStatement AlterTable(DdlAlterTableStatement stmt)
        {
            var options = new DdlTableOptions();
            stmt.TableOptions = options;
            Identifier id = null;
            Identifier id2 = null;
            Identifier id3 = null;
            DdlColumnDefinition colDef = null;
            IndexDefinition indexDef = null;
            IExpression expr = null;
            for (var i = 0; lexer.Token() != MySqlToken.Eof; ++i)
            {
                if (i > 0)
                {
                    Match(MySqlToken.PuncComma);
                }
                if (TableOptions(options))
                {
                    continue;
                }
                switch (lexer.Token())
                {
                    case MySqlToken.KwConvert:
                    {
                        // | CONVERT TO CHARACTER SET charset_name [COLLATE
                        // collation_name]
                        lexer.NextToken();
                        Match(MySqlToken.KwTo);
                        Match(MySqlToken.KwCharacter);
                        Match(MySqlToken.KwSet);
                        id = Identifier();
                        id2 = null;
                        if (lexer.Token() == MySqlToken.KwCollate)
                        {
                            lexer.NextToken();
                            id2 = Identifier();
                        }
                        stmt.ConvertCharset = new Pair<Identifier, Identifier>(id, id2);
                        goto main_switch_break;
                    }

                    case MySqlToken.KwRename:
                    {
                        // | RENAME [TO] new_tbl_name
                        if (lexer.NextToken() == MySqlToken.KwTo)
                        {
                            lexer.NextToken();
                        }
                        id = Identifier();
                        stmt.RenameTo = id;
                        goto main_switch_break;
                    }

                    case MySqlToken.KwDrop:
                    {
                        switch (lexer.NextToken())
                        {
                            case MySqlToken.KwIndex:
                            case MySqlToken.KwKey:
                            {
                                // | DROP {INDEX|KEY} index_name
                                lexer.NextToken();
                                id = Identifier();
                                stmt.AddAlterSpecification(new DdlAlterTableStatement.DropIndex(id));
                                goto drop_switch_break;
                            }

                            case MySqlToken.KwPrimary:
                            {
                                // | DROP PRIMARY KEY
                                lexer.NextToken();
                                Match(MySqlToken.KwKey);
                                stmt.AddAlterSpecification(new DdlAlterTableStatement.DropPrimaryKey());
                                goto drop_switch_break;
                            }

                            case MySqlToken.Identifier:
                            {
                                // | DROP [COLUMN] col_name
                                id = Identifier();
                                stmt.AddAlterSpecification(new DdlAlterTableStatement.DropColumn(id));
                                goto drop_switch_break;
                            }

                            case MySqlToken.KwColumn:
                            {
                                // | DROP [COLUMN] col_name
                                lexer.NextToken();
                                id = Identifier();
                                stmt.AddAlterSpecification(new DdlAlterTableStatement.DropColumn(id));
                                goto drop_switch_break;
                            }

                            default:
                            {
                                throw new SqlSyntaxErrorException("ALTER TABLE error for DROP");
                            }
                        }
                        drop_switch_break:
                        ;
                        goto main_switch_break;
                    }

                    case MySqlToken.KwChange:
                    {
                        // | CHANGE [COLUMN] old_col_name new_col_name column_definition
                        // [FIRST|AFTER col_name]
                        if (lexer.NextToken() == MySqlToken.KwColumn)
                        {
                            lexer.NextToken();
                        }
                        id = Identifier();
                        id2 = Identifier();
                        colDef = ColumnDefinition();
                        if (lexer.Token() == MySqlToken.Identifier)
                        {
                            if ("FIRST".Equals(lexer.GetStringValueUppercase()))
                            {
                                lexer.NextToken();
                                stmt.AddAlterSpecification(new DdlAlterTableStatement.ChangeColumn(id, id2, colDef
                                    , null));
                            }
                            else
                            {
                                if ("AFTER".Equals(lexer.GetStringValueUppercase()))
                                {
                                    lexer.NextToken();
                                    id3 = Identifier();
                                    stmt.AddAlterSpecification(new DdlAlterTableStatement.ChangeColumn(id, id2, colDef
                                        , id3));
                                }
                                else
                                {
                                    stmt.AddAlterSpecification(new DdlAlterTableStatement.ChangeColumn(id, id2, colDef
                                        ));
                                }
                            }
                        }
                        else
                        {
                            stmt.AddAlterSpecification(new DdlAlterTableStatement.ChangeColumn(id, id2, colDef
                                ));
                        }
                        goto main_switch_break;
                    }

                    case MySqlToken.KwAlter:
                    {
                        // | ALTER [COLUMN] col_name {SET DEFAULT literal | DROP
                        // DEFAULT}
                        if (lexer.NextToken() == MySqlToken.KwColumn)
                        {
                            lexer.NextToken();
                        }
                        id = Identifier();
                        switch (lexer.Token())
                        {
                            case MySqlToken.KwSet:
                            {
                                lexer.NextToken();
                                Match(MySqlToken.KwDefault);
                                expr = exprParser.Expression();
                                stmt.AddAlterSpecification(new DdlAlterTableStatement.AlterColumnDefaultVal(id, expr
                                    ));
                                break;
                            }

                            case MySqlToken.KwDrop:
                            {
                                lexer.NextToken();
                                Match(MySqlToken.KwDefault);
                                stmt.AddAlterSpecification(new DdlAlterTableStatement.AlterColumnDefaultVal(id));
                                break;
                            }

                            default:
                            {
                                throw new SqlSyntaxErrorException("ALTER TABLE error for ALTER");
                            }
                        }
                        goto main_switch_break;
                    }

                    case MySqlToken.KwAdd:
                    {
                        switch (lexer.NextToken())
                        {
                            case MySqlToken.Identifier:
                            {
                                // | ADD [COLUMN] col_name column_definition [FIRST | AFTER
                                // col_name ]
                                id = Identifier();
                                colDef = ColumnDefinition();
                                if (lexer.Token() == MySqlToken.Identifier)
                                {
                                    if ("FIRST".Equals(lexer.GetStringValueUppercase()))
                                    {
                                        lexer.NextToken();
                                        stmt.AddAlterSpecification(new DdlAlterTableStatement.AddColumn(id, colDef, null));
                                    }
                                    else
                                    {
                                        if ("AFTER".Equals(lexer.GetStringValueUppercase()))
                                        {
                                            lexer.NextToken();
                                            id2 = Identifier();
                                            stmt.AddAlterSpecification(new DdlAlterTableStatement.AddColumn(id, colDef,
                                                id2));
                                        }
                                        else
                                        {
                                            stmt.AddAlterSpecification(new DdlAlterTableStatement.AddColumn(id, colDef));
                                        }
                                    }
                                }
                                else
                                {
                                    stmt.AddAlterSpecification(new DdlAlterTableStatement.AddColumn(id, colDef));
                                }
                                goto add_switch_break;
                            }

                            case MySqlToken.PuncLeftParen:
                            {
                                // | ADD [COLUMN] (col_name column_definition,...)
                                lexer.NextToken();
                                for (var j = 0; lexer.Token() != MySqlToken.PuncRightParen; ++j)
                                {
                                    var addColumns = new DdlAlterTableStatement.AddColumns();
                                    stmt.AddAlterSpecification(addColumns);
                                    if (j > 0)
                                    {
                                        Match(MySqlToken.PuncComma);
                                    }
                                    id = Identifier();
                                    colDef = ColumnDefinition();
                                    addColumns.AddColumn(id, colDef);
                                }
                                Match(MySqlToken.PuncRightParen);
                                goto add_switch_break;
                            }

                            case MySqlToken.KwColumn:
                            {
                                if (lexer.NextToken() == MySqlToken.PuncLeftParen)
                                {
                                    // | ADD [COLUMN] (col_name column_definition,...)
                                    lexer.NextToken();
                                    for (var j_1 = 0; lexer.Token() != MySqlToken.PuncRightParen; ++j_1)
                                    {
                                        var addColumns = new DdlAlterTableStatement.AddColumns();
                                        stmt.AddAlterSpecification(addColumns);
                                        if (j_1 > 0)
                                        {
                                            Match(MySqlToken.PuncComma);
                                        }
                                        id = Identifier();
                                        colDef = ColumnDefinition();
                                        addColumns.AddColumn(id, colDef);
                                    }
                                    Match(MySqlToken.PuncRightParen);
                                }
                                else
                                {
                                    // | ADD [COLUMN] col_name column_definition [FIRST |
                                    // AFTER col_name ]
                                    id = Identifier();
                                    colDef = ColumnDefinition();
                                    if (lexer.Token() == MySqlToken.Identifier)
                                    {
                                        if ("FIRST".Equals(lexer.GetStringValueUppercase()))
                                        {
                                            lexer.NextToken();
                                            stmt.AddAlterSpecification(new DdlAlterTableStatement.AddColumn(id, colDef,
                                                null));
                                        }
                                        else
                                        {
                                            if ("AFTER".Equals(lexer.GetStringValueUppercase()))
                                            {
                                                lexer.NextToken();
                                                id2 = Identifier();
                                                stmt.AddAlterSpecification(new DdlAlterTableStatement.AddColumn(id,
                                                    colDef, id2));
                                            }
                                            else
                                            {
                                                stmt.AddAlterSpecification(new DdlAlterTableStatement.AddColumn(id,
                                                    colDef));
                                            }
                                        }
                                    }
                                    else
                                    {
                                        stmt.AddAlterSpecification(new DdlAlterTableStatement.AddColumn(id, colDef));
                                    }
                                }
                                goto add_switch_break;
                            }

                            case MySqlToken.KwIndex:
                            case MySqlToken.KwKey:
                            {
                                // | ADD {INDEX|KEY} [index_name] [index_type]
                                // (index_col_name,...) [index_option] ...
                                id = null;
                                if (lexer.NextToken() == MySqlToken.Identifier)
                                {
                                    id = Identifier();
                                }
                                indexDef = IndexDefinition();
                                stmt.AddAlterSpecification(new DdlAlterTableStatement.AddIndex(id, indexDef));
                                goto add_switch_break;
                            }

                            case MySqlToken.KwPrimary:
                            {
                                // | ADD PRIMARY KEY [index_type] (index_col_name,...)
                                // [index_option] ...
                                lexer.NextToken();
                                Match(MySqlToken.KwKey);
                                indexDef = IndexDefinition();
                                stmt.AddAlterSpecification(new DdlAlterTableStatement.AddPrimaryKey(indexDef));
                                goto add_switch_break;
                            }

                            case MySqlToken.KwUnique:
                            {
                                switch (lexer.NextToken())
                                {
                                    case MySqlToken.KwIndex:
                                    case MySqlToken.KwKey:
                                    {
                                        // | ADD UNIQUE [INDEX|KEY] [index_name] [index_type]
                                        // (index_col_name,...) [index_option] ...
                                        lexer.NextToken();
                                        break;
                                    }
                                }
                                id = null;
                                if (lexer.Token() == MySqlToken.Identifier)
                                {
                                    id = Identifier();
                                }
                                indexDef = IndexDefinition();
                                stmt.AddAlterSpecification(new DdlAlterTableStatement.AddUniqueKey(id, indexDef));
                                goto add_switch_break;
                            }

                            case MySqlToken.KwFulltext:
                            {
                                switch (lexer.NextToken())
                                {
                                    case MySqlToken.KwIndex:
                                    case MySqlToken.KwKey:
                                    {
                                        // | ADD FULLTEXT [INDEX|KEY] [index_name]
                                        // (index_col_name,...) [index_option] ...
                                        lexer.NextToken();
                                        break;
                                    }
                                }
                                id = null;
                                if (lexer.Token() == MySqlToken.Identifier)
                                {
                                    id = Identifier();
                                }
                                indexDef = IndexDefinition();
                                stmt.AddAlterSpecification(new DdlAlterTableStatement.AddFullTextIndex(id, indexDef
                                    ));
                                goto add_switch_break;
                            }

                            case MySqlToken.KwSpatial:
                            {
                                switch (lexer.NextToken())
                                {
                                    case MySqlToken.KwIndex:
                                    case MySqlToken.KwKey:
                                    {
                                        // | ADD SPATIAL [INDEX|KEY] [index_name]
                                        // (index_col_name,...) [index_option] ...
                                        lexer.NextToken();
                                        break;
                                    }
                                }
                                id = null;
                                if (lexer.Token() == MySqlToken.Identifier)
                                {
                                    id = Identifier();
                                }
                                indexDef = IndexDefinition();
                                stmt.AddAlterSpecification(new DdlAlterTableStatement.AddSpatialIndex(id, indexDef
                                    ));
                                goto add_switch_break;
                            }

                            default:
                            {
                                throw new SqlSyntaxErrorException("ALTER TABLE error for ADD");
                            }
                        }
                        add_switch_break:
                        ;
                        goto main_switch_break;
                    }

                    case MySqlToken.Identifier:
                    {
                        var si = specialIdentifiers.GetValue(lexer.GetStringValueUppercase());
                        if (si != SpecialIdentifier.None)
                        {
                            switch (si)
                            {
                                case SpecialIdentifier.Import:
                                {
                                    // | IMPORT TABLESPACE
                                    lexer.NextToken();
                                    MatchIdentifier("TABLESPACE");
                                    stmt.IsImportTableSpace = true;
                                    goto main_switch_break;
                                }

                                case SpecialIdentifier.Discard:
                                {
                                    // | DISCARD TABLESPACE
                                    lexer.NextToken();
                                    MatchIdentifier("TABLESPACE");
                                    stmt.DiscardTableSpace = true;
                                    goto main_switch_break;
                                }

                                case SpecialIdentifier.Enable:
                                {
                                    // | ENABLE KEYS
                                    lexer.NextToken();
                                    Match(MySqlToken.KwKeys);
                                    stmt.EnableKeys = true;
                                    goto main_switch_break;
                                }

                                case SpecialIdentifier.Disable:
                                {
                                    // | DISABLE KEYS
                                    lexer.NextToken();
                                    Match(MySqlToken.KwKeys);
                                    stmt.DisableKeys = true;
                                    goto main_switch_break;
                                }

                                case SpecialIdentifier.Modify:
                                {
                                    // | MODIFY [COLUMN] col_name column_definition [FIRST |
                                    // AFTER col_name]
                                    if (lexer.NextToken() == MySqlToken.KwColumn)
                                    {
                                        lexer.NextToken();
                                    }
                                    id = Identifier();
                                    colDef = ColumnDefinition();
                                    if (lexer.Token() == MySqlToken.Identifier)
                                    {
                                        if ("FIRST".Equals(lexer.GetStringValueUppercase()))
                                        {
                                            lexer.NextToken();
                                            stmt.AddAlterSpecification(new DdlAlterTableStatement.ModifyColumn(id,
                                                colDef, null
                                                ));
                                        }
                                        else
                                        {
                                            if ("AFTER".Equals(lexer.GetStringValueUppercase()))
                                            {
                                                lexer.NextToken();
                                                id2 = Identifier();
                                                stmt.AddAlterSpecification(new DdlAlterTableStatement.ModifyColumn(id,
                                                    colDef, id2
                                                    ));
                                            }
                                            else
                                            {
                                                stmt.AddAlterSpecification(new DdlAlterTableStatement.ModifyColumn(id,
                                                    colDef));
                                            }
                                        }
                                    }
                                    else
                                    {
                                        stmt.AddAlterSpecification(new DdlAlterTableStatement.ModifyColumn(id, colDef));
                                    }
                                    goto main_switch_break;
                                }
                            }
                        }
                        goto default;
                    }

                    default:
                    {
                        throw new SqlSyntaxErrorException("unknown ALTER specification");
                    }
                }
                main_switch_break:
                ;
            }
            return stmt;
        }
예제 #3
0
 public virtual void Visit(DdlAlterTableStatement.AlterSpecification node)
 {
 }
예제 #4
0
        /// <summary>nothing has been pre-consumed</summary>
        /// <exception cref="System.SqlSyntaxErrorException" />
        public virtual IDdlStatement DdlStmt()
        {
            Identifier idTemp1;
            Identifier idTemp2;
            SpecialIdentifier siTemp;
            switch (lexer.Token())
            {
                case MySqlToken.KwAlter:
                {
                    var ignore = false;
                    if (lexer.NextToken() == MySqlToken.KwIgnore)
                    {
                        ignore = true;
                        lexer.NextToken();
                    }
                    switch (lexer.Token())
                    {
                        case MySqlToken.KwTable:
                        {
                            lexer.NextToken();
                            idTemp1 = Identifier();
                            var alterTableStatement = new DdlAlterTableStatement(ignore, idTemp1);
                            return AlterTable(alterTableStatement);
                        }

                        default:
                        {
                            throw Err("Only ALTER TABLE is supported");
                        }
                    }
                    //goto case MySqlToken.KwCreate;
                }

                case MySqlToken.KwCreate:
                {
                    switch (lexer.NextToken())
                    {
                        case MySqlToken.KwUnique:
                        case MySqlToken.KwFulltext:
                        case MySqlToken.KwSpatial:
                        {
                            lexer.NextToken();
                            goto case MySqlToken.KwIndex;
                        }

                        case MySqlToken.KwIndex:
                        {
                            lexer.NextToken();
                            idTemp1 = Identifier();
                            for (; lexer.Token() != MySqlToken.KwOn; lexer.NextToken())
                            {
                            }
                            lexer.NextToken();
                            idTemp2 = Identifier();
                            return new DdlCreateIndexStatement(idTemp1, idTemp2);
                        }

                        case MySqlToken.KwTable:
                        {
                            lexer.NextToken();
                            return CreateTable(false);
                        }

                        case MySqlToken.Identifier:
                        {
                            siTemp = specialIdentifiers.GetValue(lexer.GetStringValueUppercase());
                            if (siTemp != SpecialIdentifier.None)
                            {
                                switch (siTemp)
                                {
                                    case SpecialIdentifier.Temporary:
                                    {
                                        lexer.NextToken();
                                        Match(MySqlToken.KwTable);
                                        return CreateTable(true);
                                    }

                                    case SpecialIdentifier.Policy:
                                    {
                                        lexer.NextToken();
                                        var policyName = Identifier();
                                        Match(MySqlToken.PuncLeftParen);
                                        var policy = new ExtDdlCreatePolicy(policyName);
                                        for (var j = 0; lexer.Token() != MySqlToken.PuncRightParen; ++j)
                                        {
                                            if (j > 0)
                                            {
                                                Match(MySqlToken.PuncComma);
                                            }
                                            var id = (int)lexer.GetIntegerValue();
                                            Match(MySqlToken.LiteralNumPureDigit);
                                            var val = exprParser.Expression();
                                            policy.AddProportion(id, val);
                                        }
                                        Match(MySqlToken.PuncRightParen);
                                        return policy;
                                    }
                                }
                            }
                            goto default;
                        }

                        default:
                        {
                            throw Err("unsupported Ddl for CREATE");
                        }
                    }
                    //goto case MySqlToken.KwDrop;
                }

                case MySqlToken.KwDrop:
                {
                    switch (lexer.NextToken())
                    {
                        case MySqlToken.KwIndex:
                        {
                            lexer.NextToken();
                            idTemp1 = Identifier();
                            Match(MySqlToken.KwOn);
                            idTemp2 = Identifier();
                            return new DdlDropIndexStatement(idTemp1, idTemp2);
                        }

                        case MySqlToken.KwTable:
                        {
                            lexer.NextToken();
                            return DropTable(false);
                        }

                        case MySqlToken.Identifier:
                        {
                            siTemp = specialIdentifiers.GetValue(lexer.GetStringValueUppercase());
                            if (siTemp != SpecialIdentifier.None)
                            {
                                switch (siTemp)
                                {
                                    case SpecialIdentifier.Temporary:
                                    {
                                        lexer.NextToken();
                                        Match(MySqlToken.KwTable);
                                        return DropTable(true);
                                    }

                                    case SpecialIdentifier.Policy:
                                    {
                                        lexer.NextToken();
                                        var policyName = Identifier();
                                        return new ExtDdlDropPolicy(policyName);
                                    }
                                }
                            }
                            goto default;
                        }

                        default:
                        {
                            throw Err("unsupported Ddl for DROP");
                        }
                    }
                    //goto case MySqlToken.KwRename;
                }

                case MySqlToken.KwRename:
                {
                    lexer.NextToken();
                    Match(MySqlToken.KwTable);
                    idTemp1 = Identifier();
                    Match(MySqlToken.KwTo);
                    idTemp2 = Identifier();
                    IList<Pair<Identifier, Identifier>> list;
                    if (lexer.Token() != MySqlToken.PuncComma)
                    {
                        list = new List<Pair<Identifier, Identifier>>(1);
                        list.Add(new Pair<Identifier, Identifier>(idTemp1, idTemp2));
                        return new DdlRenameTableStatement(list);
                    }
                    list = new List<Pair<Identifier, Identifier>>();
                    list.Add(new Pair<Identifier, Identifier>(idTemp1, idTemp2));
                    for (; lexer.Token() == MySqlToken.PuncComma;)
                    {
                        lexer.NextToken();
                        idTemp1 = Identifier();
                        Match(MySqlToken.KwTo);
                        idTemp2 = Identifier();
                        list.Add(new Pair<Identifier, Identifier>(idTemp1, idTemp2));
                    }
                    return new DdlRenameTableStatement(list);
                }

                case MySqlToken.Identifier:
                {
                    var si = specialIdentifiers.GetValue(lexer.GetStringValueUppercase());
                    if (si != SpecialIdentifier.None)
                    {
                        switch (si)
                        {
                            case SpecialIdentifier.Truncate:
                            {
                                return Truncate();
                            }
                        }
                    }
                    goto default;
                }

                default:
                {
                    throw Err("unsupported Ddl");
                }
            }
        }