Inheritance: MySqlParser
Ejemplo n.º 1
0
 public virtual void TestDdlStmt()
 {
     var sql = "alTer ignore table tb_name";
     var lexer = new MySqlLexer(sql);
     var parser = new MySqlDdlParser(lexer, new MySqlExprParser(lexer));
     var dst = parser.DdlStmt();
     sql = "alTeR table tb_name";
     lexer = new MySqlLexer(sql);
     parser = new MySqlDdlParser(lexer, new MySqlExprParser(lexer));
     dst = parser.DdlStmt();
     sql = "crEate temporary tabLe if not exists tb_name";
     lexer = new MySqlLexer(sql);
     parser = new MySqlDdlParser(lexer, new MySqlExprParser(lexer));
     dst = parser.DdlStmt();
     sql = "crEate tabLe if not exists tb_name";
     lexer = new MySqlLexer(sql);
     parser = new MySqlDdlParser(lexer, new MySqlExprParser(lexer));
     dst = parser.DdlStmt();
     sql = "crEate temporary tabLe tb_name";
     lexer = new MySqlLexer(sql);
     parser = new MySqlDdlParser(lexer, new MySqlExprParser(lexer));
     dst = parser.DdlStmt();
     sql = "crEate unique index index_name on tb(col(id)) desc";
     lexer = new MySqlLexer(sql);
     parser = new MySqlDdlParser(lexer, new MySqlExprParser(lexer));
     dst = parser.DdlStmt();
     sql = "crEate fulltext index index_name on tb(col(id))";
     lexer = new MySqlLexer(sql);
     parser = new MySqlDdlParser(lexer, new MySqlExprParser(lexer));
     dst = parser.DdlStmt();
     sql = "crEate spatial index index_name on tb(col(id))";
     lexer = new MySqlLexer(sql);
     parser = new MySqlDdlParser(lexer, new MySqlExprParser(lexer));
     dst = parser.DdlStmt();
     sql = "crEate index index_name using hash on tb(col(id))";
     lexer = new MySqlLexer(sql);
     parser = new MySqlDdlParser(lexer, new MySqlExprParser(lexer));
     dst = parser.DdlStmt();
     sql = "drop index index_name on tb1";
     lexer = new MySqlLexer(sql);
     parser = new MySqlDdlParser(lexer, new MySqlExprParser(lexer));
     dst = parser.DdlStmt();
     var output = Output2MySql(dst, sql);
     Assert.AreEqual("DROP INDEX index_name ON tb1", output);
     sql = "drop temporary tabLe if exists tb1,tb2,tb3 restrict";
     lexer = new MySqlLexer(sql);
     parser = new MySqlDdlParser(lexer, new MySqlExprParser(lexer));
     dst = parser.DdlStmt();
     output = Output2MySql(dst, sql);
     Assert.AreEqual("DROP TEMPORARY TABLE IF EXISTS tb1, tb2, tb3 RESTRICT", output);
     sql = "drop temporary tabLe if exists tb1,tb2,tb3 cascade";
     lexer = new MySqlLexer(sql);
     parser = new MySqlDdlParser(lexer, new MySqlExprParser(lexer));
     dst = parser.DdlStmt();
     output = Output2MySql(dst, sql);
     Assert.AreEqual("DROP TEMPORARY TABLE IF EXISTS tb1, tb2, tb3 CASCADE", output);
     sql = "drop temporary tabLe if exists tb1 cascade";
     lexer = new MySqlLexer(sql);
     parser = new MySqlDdlParser(lexer, new MySqlExprParser(lexer));
     dst = parser.DdlStmt();
     output = Output2MySql(dst, sql);
     Assert.AreEqual("DROP TEMPORARY TABLE IF EXISTS tb1 CASCADE", output);
     sql = "drop tabLe if exists tb1 cascade";
     lexer = new MySqlLexer(sql);
     parser = new MySqlDdlParser(lexer, new MySqlExprParser(lexer));
     dst = parser.DdlStmt();
     output = Output2MySql(dst, sql);
     Assert.AreEqual("DROP TABLE IF EXISTS tb1 CASCADE", output);
     sql = "drop temporary tabLe tb1 cascade";
     lexer = new MySqlLexer(sql);
     parser = new MySqlDdlParser(lexer, new MySqlExprParser(lexer));
     dst = parser.DdlStmt();
     output = Output2MySql(dst, sql);
     Assert.AreEqual("DROP TEMPORARY TABLE tb1 CASCADE", output);
     sql = "rename table tb1 to ntb1,tb2 to ntb2";
     lexer = new MySqlLexer(sql);
     parser = new MySqlDdlParser(lexer, new MySqlExprParser(lexer));
     dst = parser.DdlStmt();
     output = Output2MySql(dst, sql);
     Assert.AreEqual("RENAME TABLE tb1 TO ntb1, tb2 TO ntb2", output);
     sql = "rename table tb1 to ntb1";
     lexer = new MySqlLexer(sql);
     parser = new MySqlDdlParser(lexer, new MySqlExprParser(lexer));
     dst = parser.DdlStmt();
     output = Output2MySql(dst, sql);
     Assert.AreEqual("RENAME TABLE tb1 TO ntb1", output);
 }
Ejemplo n.º 2
0
        /// <exception cref="System.SqlSyntaxErrorException" />
        public static ISqlStatement Parse(string sql, MySqlLexer lexer, string charset)
        {
            try
            {
                ISqlStatement stmt = null;
                var isEof = true;
                var exprParser = new MySqlExprParser(lexer, charset);
                switch (lexer.Token())
                {
                    case MySqlToken.KwDesc:
                    case MySqlToken.KwDescribe:
                    {
                        stmt = new MySqlDalParser(lexer, exprParser).Desc();
                        goto stmtSwitch_break;
                    }

                    case MySqlToken.KwSelect:
                    case MySqlToken.PuncLeftParen:
                    {
                        stmt = new MySqlDmlSelectParser(lexer, exprParser).SelectUnion();
                        goto stmtSwitch_break;
                    }

                    case MySqlToken.KwDelete:
                    {
                        stmt = new MySqlDmlDeleteParser(lexer, exprParser).Delete();
                        goto stmtSwitch_break;
                    }

                    case MySqlToken.KwInsert:
                    {
                        stmt = new MySqlDmlInsertParser(lexer, exprParser).Insert();
                        goto stmtSwitch_break;
                    }

                    case MySqlToken.KwReplace:
                    {
                        stmt = new MySqlDmlReplaceParser(lexer, exprParser).Replace();
                        goto stmtSwitch_break;
                    }

                    case MySqlToken.KwUpdate:
                    {
                        stmt = new MySqlDmlUpdateParser(lexer, exprParser).Update();
                        goto stmtSwitch_break;
                    }

                    case MySqlToken.KwCall:
                    {
                        stmt = new MySqlDmlCallParser(lexer, exprParser).Call();
                        goto stmtSwitch_break;
                    }

                    case MySqlToken.KwSet:
                    {
                        stmt = new MySqlDalParser(lexer, exprParser).Set();
                        goto stmtSwitch_break;
                    }

                    case MySqlToken.KwShow:
                    {
                        stmt = new MySqlDalParser(lexer, exprParser).Show();
                        goto stmtSwitch_break;
                    }

                    case MySqlToken.KwAlter:
                    case MySqlToken.KwCreate:
                    case MySqlToken.KwDrop:
                    case MySqlToken.KwRename:
                    {
                        stmt = new MySqlDdlParser(lexer, exprParser).DdlStmt();
                        isEof = IsEOFedDdl(stmt);
                        goto stmtSwitch_break;
                    }

                    case MySqlToken.KwRelease:
                    {
                        stmt = new MySqlMtsParser(lexer).Release();
                        goto stmtSwitch_break;
                    }

                    case MySqlToken.Identifier:
                    {
                        var si = SpecialIdentifier.None;
                        if ((si = SpecialIdentifiers[lexer.GetStringValueUppercase()]) != SpecialIdentifier.None)
                        {
                            switch (si)
                            {
                                case SpecialIdentifier.Truncate:
                                {
                                    stmt = new MySqlDdlParser(lexer, exprParser).Truncate();
                                    goto stmtSwitch_break;
                                }

                                case SpecialIdentifier.Savepoint:
                                {
                                    stmt = new MySqlMtsParser(lexer).Savepoint();
                                    goto stmtSwitch_break;
                                }

                                case SpecialIdentifier.Rollback:
                                {
                                    stmt = new MySqlMtsParser(lexer).Rollback();
                                    goto stmtSwitch_break;
                                }
                            }
                        }
                        goto default;
                    }

                    default:
                    {
                        throw new SqlSyntaxErrorException("sql is not a supported statement");
                    }
                }
                stmtSwitch_break:
                ;
                if (isEof)
                {
                    while (lexer.Token() == MySqlToken.PuncSemicolon)
                    {
                        lexer.NextToken();
                    }
                    if (lexer.Token() != MySqlToken.Eof)
                    {
                        throw new SqlSyntaxErrorException("Sql syntax error!");
                    }
                }
                return stmt;
            }
            catch (Exception e)
            {
                throw new SqlSyntaxErrorException(BuildErrorMsg(e, lexer, sql), e);
            }
        }
Ejemplo n.º 3
0
 public virtual void TestTruncate()
 {
     var sql = "Truncate table tb1";
     var lexer = new MySqlLexer(sql);
     var parser = new MySqlDdlParser(lexer, new MySqlExprParser(lexer));
     IDdlStatement trun = parser.Truncate();
     parser.Match(MySqlToken.Eof);
     var output = Output2MySql(trun, sql);
     Assert.AreEqual("TRUNCATE TABLE tb1", output);
     sql = "Truncate tb1";
     lexer = new MySqlLexer(sql);
     parser = new MySqlDdlParser(lexer, new MySqlExprParser(lexer));
     trun = parser.Truncate();
     parser.Match(MySqlToken.Eof);
     output = Output2MySql(trun, sql);
     Assert.AreEqual("TRUNCATE TABLE tb1", output);
 }