public static string GetRoutineName(string sql)
        {
            StringBuilder sb;

            MySQL51Parser.program_return pr = ParseSql(sql, false, out sb);
            if (sb.Length != 0)
            {
                throw new ApplicationException(string.Format("Syntactic error in stored routine: {0}", sb));
            }
            else
            {
                CommonTree t = (CommonTree)pr.Tree;
                if (t.IsNil)
                {
                    t = (CommonTree)t.GetChild(0);
                }
                string name;
                if (string.Equals(t.GetChild(1).Text, "definer", StringComparison.OrdinalIgnoreCase))
                {
                    name = t.GetChild(3).Text;
                }
                else
                {
                    name = t.GetChild(1).Text;
                }
                return(name.Replace("`", ""));
            }
        }
        private void GetCompleteStatement(
            ITextSnapshot snapshot, SnapshotPoint snapPos, out StringBuilder sbErrors, out ITree treeStmt)
        {
            string sql = snapshot.GetText();

            treeStmt  = null;
            sbErrors  = new StringBuilder();
            _position = snapPos.Position;
            _tokens   = RemoveToken(sql, snapPos);
            if (_tokens.Count == 1 && _tokens.Get(0).Type == MySQL51Lexer.EOF)
            {
                return;
            }
            MySQL51Parser.program_return r =
                LanguageServiceUtil.ParseSql(sql, false, out sbErrors, _tokens);
            if (r == null)
            {
                return;
            }
            ITree t = r.Tree as ITree;

            treeStmt = t;
            // locate current statement's AST
            if (t.IsNil)
            {
                ITree tmp = FindStmt(t);
                if (tmp != null)
                {
                    treeStmt = tmp;
                }
            }
        }
        /// <summary>
        /// Returns true is the first SQL statement in the string returns a result set
        /// (after parsing comments and other noise).
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="con"></param>
        /// <returns></returns>
        public static bool DoesStmtReturnResults(string sql, MySqlConnection con)
        {
            StringBuilder sb;

            MySQL51Parser.program_return t = ParseSql(sql, false, out sb, con.ServerVersion);
            ITree tree = t.Tree as ITree;

            if (tree != null && tree.IsNil)
            {
                tree = tree.GetChild(0);
            }

            return(tree != null && _keywords4ResultSets.ContainsKey(tree.Text));
        }
        private static MySQL51Parser.program_return DoParse(
            CommonTokenStream tokens, bool expectErrors, out StringBuilder sb, Version version)
        {
            MySQLParser parser = new MySQLParser(tokens);

            parser.MySqlVersion = version;
            sb = new StringBuilder();
            TextWriter tw = new StringWriter(sb);

            parser.TraceDestination = tw;
            MySQL51Parser.program_return r = null;
            int tokCount = tokens.Count;

            try
            {
                r = parser.program();
            }
            catch (RewriteEmptyStreamException e)
            {
                if (!expectErrors)
                {
                    sb.AppendLine();
                    sb.Append(e.Message);
                }
            }

            /*
             * The parser inserts a new <EOF> to the token stream, this is probably an ANTLR bug, the code here takes care of it.
             * */
            if ((tokens.Count - 1) == tokCount)
            {
                TokenStreamRemovable tsr = (TokenStreamRemovable)tokens;
                tsr.Remove(tokens.Get(tokens.Count - 1));
            }
            tokens.Reset();
            return(r);
        }
 public static string GetRoutineName(string sql)
 {
   MySQL51Parser.program_return r = new MySQL51Parser.program_return();
   StringBuilder sb;
   bool expectErrors = false;
   CommonTokenStream cts;
   
   MemoryStream ms = new MemoryStream(ASCIIEncoding.ASCII.GetBytes(sql));
   CaseInsensitiveInputStream input = new CaseInsensitiveInputStream(ms);
   MySQLLexer lexer = new MySQLLexer(input);
   CommonTokenStream tokens = new CommonTokenStream(lexer);
   MySQLParser parser = new MySQLParser(tokens);
   sb = new StringBuilder();
   TextWriter tw = new StringWriter(sb);
   parser.TraceDestination = tw;
   try
   {
     r = parser.program();
   }
   catch (RewriteEmptyStreamException e)
   {
     sb.AppendLine();
     sb.Append(e.Message);
   }
   cts = tokens;
   if (!expectErrors && sb.Length != 0)
   {
     throw new DebugSyntaxException(sb.ToString());
   }
   CommonTree t = (CommonTree)r.Tree;
   if (t.IsNil)
     t = (CommonTree)t.GetChild(0);
   return GetRoutineName(t);
 }
 public MySQL51Parser.program_return ParseSql(string sql, bool expectErrors, out StringBuilder sb, out CommonTokenStream cts)
 {
   Version ver = ParserUtils.GetVersion( _connection.ServerVersion );
   // The grammar supports upper case only
   MemoryStream ms = new MemoryStream(ASCIIEncoding.ASCII.GetBytes(sql));
   CaseInsensitiveInputStream input = new CaseInsensitiveInputStream(ms);
   MySQLLexer lexer = new MySQLLexer(input);
   lexer.MySqlVersion = ver;
   CommonTokenStream tokens = new CommonTokenStream(lexer);
   MySQLParser parser = new MySQLParser(tokens);
   parser.MySqlVersion = ver;
   sb = new StringBuilder();
   TextWriter tw = new StringWriter(sb);
   parser.TraceDestination = tw;
   MySQL51Parser.program_return r = new MySQL51Parser.program_return();
   r.Tree = null;
   try
   {
     r = parser.program();
   }
   catch (RewriteEmptyStreamException e)
   {
     sb.AppendLine();
     sb.Append(e.Message);
   }
   cts = tokens;
   if (!expectErrors && sb.Length != 0)
   {
     throw new DebugSyntaxException(sb.ToString());
   }
   return r;
 }