Inheritance: MySqlDmlInsertReplaceParser
Example #1
0
        public virtual void TestInsert()
        {
            var sql    = "insErt HIGH_PRIORITY intO test.t1 seT t1.id1=?, id2 := '123'";
            var lexer  = new MySqlLexer(sql);
            var parser = new MySqlDmlInsertParser(lexer, new MySqlExprParser(lexer));
            var insert = parser.Insert();

            parser.Match(MySqlToken.Eof);
            var output = Output2MySql(insert, sql);

            Assert.IsNotNull(insert);
            Assert.AreEqual("INSERT HIGH_PRIORITY INTO test.t1 (t1.id1, id2) VALUES (?, '123')", output);
            sql    = "insErt  IGNORE test.t1 seT t1.id1:=? oN dupLicatE key UPDATE ex.col1=?, col2=12";
            lexer  = new MySqlLexer(sql);
            parser = new MySqlDmlInsertParser(lexer, new MySqlExprParser(lexer));
            insert = parser.Insert();
            parser.Match(MySqlToken.Eof);
            output = Output2MySql(insert, sql);
            Assert.AreEqual(
                "INSERT IGNORE INTO test.t1 (t1.id1) VALUES (?) ON DUPLICATE KEY UPDATE ex.col1 = ?, col2 = 12", output);
            sql    = "insErt t1 value (123,?) oN dupLicatE key UPDATE ex.col1=?";
            lexer  = new MySqlLexer(sql);
            parser = new MySqlDmlInsertParser(lexer, new MySqlExprParser(lexer));
            insert = parser.Insert();
            parser.Match(MySqlToken.Eof);
            output = Output2MySql(insert, sql);
            Assert.AreEqual("INSERT INTO t1 VALUES (123, ?) ON DUPLICATE KEY UPDATE ex.col1 = ?", output);
            sql    = "insErt LOW_PRIORITY t1 valueS (12e-2,1,2), (?),(default)";
            lexer  = new MySqlLexer(sql);
            parser = new MySqlDmlInsertParser(lexer, new MySqlExprParser(lexer));
            insert = parser.Insert();
            parser.Match(MySqlToken.Eof);
            output = Output2MySql(insert, sql);
            Assert.AreEqual("INSERT LOW_PRIORITY INTO t1 VALUES (0.12, 1, 2), (?), (DEFAULT)", output);
            sql    = "insErt LOW_PRIORITY t1 select id from t1";
            lexer  = new MySqlLexer(sql);
            parser = new MySqlDmlInsertParser(lexer, new MySqlExprParser(lexer));
            insert = parser.Insert();
            parser.Match(MySqlToken.Eof);
            output = Output2MySql(insert, sql);
            Assert.AreEqual("INSERT LOW_PRIORITY INTO t1 SELECT id FROM t1",
                            output);
            sql    = "insErt delayed t1 select id from t1";
            lexer  = new MySqlLexer(sql);
            parser = new MySqlDmlInsertParser(lexer, new MySqlExprParser(lexer));
            insert = parser.Insert();
            parser.Match(MySqlToken.Eof);
            output = Output2MySql(insert, sql);
            Assert.AreEqual("INSERT DELAYED INTO t1 SELECT id FROM t1", output);
            sql    = "insErt LOW_PRIORITY t1 (select id from t1) oN dupLicatE key UPDATE ex.col1=?, col2=12";
            lexer  = new MySqlLexer(sql);
            parser = new MySqlDmlInsertParser(lexer, new MySqlExprParser(lexer));
            insert = parser.Insert();
            parser.Match(MySqlToken.Eof);
            output = Output2MySql(insert, sql);
            Assert.AreEqual(
                "INSERT LOW_PRIORITY INTO t1 SELECT id FROM t1 ON DUPLICATE KEY UPDATE ex.col1 = ?, col2 = 12", output);
            sql    = "insErt LOW_PRIORITY t1 (t1.col1) valueS (123),('12''34')";
            lexer  = new MySqlLexer(sql);
            parser = new MySqlDmlInsertParser(lexer, new MySqlExprParser(lexer));
            insert = parser.Insert();
            parser.Match(MySqlToken.Eof);
            output = Output2MySql(insert, sql);
            Assert.AreEqual("INSERT LOW_PRIORITY INTO t1 (t1.col1) VALUES (123), ('12\\'34')", output);
            sql    = "insErt LOW_PRIORITY t1 (col1, t1.col2) VALUE (123,'123\\'4') oN dupLicatE key UPDATE ex.col1=?";
            lexer  = new MySqlLexer(sql);
            parser = new MySqlDmlInsertParser(lexer, new MySqlExprParser(lexer));
            insert = parser.Insert();
            parser.Match(MySqlToken.Eof);
            output = Output2MySql(insert, sql);
            Assert.AreEqual(
                "INSERT LOW_PRIORITY INTO t1 (col1, t1.col2) VALUES (123, '123\\'4') ON DUPLICATE KEY UPDATE ex.col1 = ?",
                output);
            sql    = "insErt LOW_PRIORITY t1 (col1, t1.col2) select id from t3 oN dupLicatE key UPDATE ex.col1=?";
            lexer  = new MySqlLexer(sql);
            parser = new MySqlDmlInsertParser(lexer, new MySqlExprParser(lexer));
            insert = parser.Insert();
            parser.Match(MySqlToken.Eof);
            output = Output2MySql(insert, sql);
            Assert.AreEqual(
                "INSERT LOW_PRIORITY INTO t1 (col1, t1.col2) SELECT id FROM t3 ON DUPLICATE KEY UPDATE ex.col1 = ?",
                output);
            sql    = "insErt LOW_PRIORITY IGNORE intO t1 (col1) ( select id from t3) ";
            lexer  = new MySqlLexer(sql);
            parser = new MySqlDmlInsertParser(lexer, new MySqlExprParser(lexer));
            insert = parser.Insert();
            parser.Match(MySqlToken.Eof);
            output = Output2MySql(insert, sql);
            Assert.AreEqual("INSERT LOW_PRIORITY IGNORE INTO t1 (col1) SELECT id FROM t3", output);
        }
 /// <exception cref="System.Exception" />
 public override void Route(SchemaConfig schema, int loop, string sql)
 {
     for (var i = 0; i < loop; ++i)
     {
         var lexer = new MySqlLexer(sql);
         var insert = new MySqlDmlInsertParser(lexer, new MySqlExprParser(lexer)).Insert();
     }
 }
 public virtual void TestInsert()
 {
     var sql = "insErt HIGH_PRIORITY intO test.t1 seT t1.id1=?, id2 := '123'";
     var lexer = new MySqlLexer(sql);
     var parser = new MySqlDmlInsertParser(lexer, new MySqlExprParser(lexer));
     var insert = parser.Insert();
     parser.Match(MySqlToken.Eof);
     var output = Output2MySql(insert, sql);
     Assert.IsNotNull(insert);
     Assert.AreEqual("INSERT HIGH_PRIORITY INTO test.t1 (t1.id1, id2) VALUES (?, '123')", output);
     sql = "insErt  IGNORE test.t1 seT t1.id1:=? oN dupLicatE key UPDATE ex.col1=?, col2=12";
     lexer = new MySqlLexer(sql);
     parser = new MySqlDmlInsertParser(lexer, new MySqlExprParser(lexer));
     insert = parser.Insert();
     parser.Match(MySqlToken.Eof);
     output = Output2MySql(insert, sql);
     Assert.AreEqual(
         "INSERT IGNORE INTO test.t1 (t1.id1) VALUES (?) ON DUPLICATE KEY UPDATE ex.col1 = ?, col2 = 12", output);
     sql = "insErt t1 value (123,?) oN dupLicatE key UPDATE ex.col1=?";
     lexer = new MySqlLexer(sql);
     parser = new MySqlDmlInsertParser(lexer, new MySqlExprParser(lexer));
     insert = parser.Insert();
     parser.Match(MySqlToken.Eof);
     output = Output2MySql(insert, sql);
     Assert.AreEqual("INSERT INTO t1 VALUES (123, ?) ON DUPLICATE KEY UPDATE ex.col1 = ?", output);
     sql = "insErt LOW_PRIORITY t1 valueS (12e-2,1,2), (?),(default)";
     lexer = new MySqlLexer(sql);
     parser = new MySqlDmlInsertParser(lexer, new MySqlExprParser(lexer));
     insert = parser.Insert();
     parser.Match(MySqlToken.Eof);
     output = Output2MySql(insert, sql);
     Assert.AreEqual("INSERT LOW_PRIORITY INTO t1 VALUES (0.12, 1, 2), (?), (DEFAULT)", output);
     sql = "insErt LOW_PRIORITY t1 select id from t1";
     lexer = new MySqlLexer(sql);
     parser = new MySqlDmlInsertParser(lexer, new MySqlExprParser(lexer));
     insert = parser.Insert();
     parser.Match(MySqlToken.Eof);
     output = Output2MySql(insert, sql);
     Assert.AreEqual("INSERT LOW_PRIORITY INTO t1 SELECT id FROM t1",
         output);
     sql = "insErt delayed t1 select id from t1";
     lexer = new MySqlLexer(sql);
     parser = new MySqlDmlInsertParser(lexer, new MySqlExprParser(lexer));
     insert = parser.Insert();
     parser.Match(MySqlToken.Eof);
     output = Output2MySql(insert, sql);
     Assert.AreEqual("INSERT DELAYED INTO t1 SELECT id FROM t1", output);
     sql = "insErt LOW_PRIORITY t1 (select id from t1) oN dupLicatE key UPDATE ex.col1=?, col2=12";
     lexer = new MySqlLexer(sql);
     parser = new MySqlDmlInsertParser(lexer, new MySqlExprParser(lexer));
     insert = parser.Insert();
     parser.Match(MySqlToken.Eof);
     output = Output2MySql(insert, sql);
     Assert.AreEqual(
         "INSERT LOW_PRIORITY INTO t1 SELECT id FROM t1 ON DUPLICATE KEY UPDATE ex.col1 = ?, col2 = 12", output);
     sql = "insErt LOW_PRIORITY t1 (t1.col1) valueS (123),('12''34')";
     lexer = new MySqlLexer(sql);
     parser = new MySqlDmlInsertParser(lexer, new MySqlExprParser(lexer));
     insert = parser.Insert();
     parser.Match(MySqlToken.Eof);
     output = Output2MySql(insert, sql);
     Assert.AreEqual("INSERT LOW_PRIORITY INTO t1 (t1.col1) VALUES (123), ('12\\'34')", output);
     sql = "insErt LOW_PRIORITY t1 (col1, t1.col2) VALUE (123,'123\\'4') oN dupLicatE key UPDATE ex.col1=?";
     lexer = new MySqlLexer(sql);
     parser = new MySqlDmlInsertParser(lexer, new MySqlExprParser(lexer));
     insert = parser.Insert();
     parser.Match(MySqlToken.Eof);
     output = Output2MySql(insert, sql);
     Assert.AreEqual(
         "INSERT LOW_PRIORITY INTO t1 (col1, t1.col2) VALUES (123, '123\\'4') ON DUPLICATE KEY UPDATE ex.col1 = ?",
         output);
     sql = "insErt LOW_PRIORITY t1 (col1, t1.col2) select id from t3 oN dupLicatE key UPDATE ex.col1=?";
     lexer = new MySqlLexer(sql);
     parser = new MySqlDmlInsertParser(lexer, new MySqlExprParser(lexer));
     insert = parser.Insert();
     parser.Match(MySqlToken.Eof);
     output = Output2MySql(insert, sql);
     Assert.AreEqual(
         "INSERT LOW_PRIORITY INTO t1 (col1, t1.col2) SELECT id FROM t3 ON DUPLICATE KEY UPDATE ex.col1 = ?",
         output);
     sql = "insErt LOW_PRIORITY IGNORE intO t1 (col1) ( select id from t3) ";
     lexer = new MySqlLexer(sql);
     parser = new MySqlDmlInsertParser(lexer, new MySqlExprParser(lexer));
     insert = parser.Insert();
     parser.Match(MySqlToken.Eof);
     output = Output2MySql(insert, sql);
     Assert.AreEqual("INSERT LOW_PRIORITY IGNORE INTO t1 (col1) SELECT id FROM t3", output);
 }
        /// <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);
            }
        }