Exemple #1
0
        /// <summary>
        /// Heursitical crawl through a database command to find features suitable for making metric names.
        /// This uses linear search through the regexp patterns.
        /// </summary>
        /// <returns>A ParsedDatabaseStatement if some heuristic matches; otherwise null</returns>
        public static ParsedSqlStatement GetParsedDatabaseStatement(DatastoreVendor datastoreVendor, CommandType commandType, string commandText)
        {
            try
            {
                switch (commandType)
                {
                case CommandType.TableDirect:
                    return(new ParsedSqlStatement(datastoreVendor, commandText, "select"));

                case CommandType.StoredProcedure:
                    return(new ParsedSqlStatement(datastoreVendor, StringsHelper.FixDatabaseObjectName(commandText), "ExecuteProcedure"));
                }
                // Remove comments.
                var statement = CommentPattern.Replace(commandText, string.Empty).TrimStart();

                if (!IsSingleSqlStatement(statement))
                {
                    // Remove leading SET commands

                    // Trimming any trailing semicolons is necessary to avoid having the LeadingSetPattern
                    // match a SQL statement that ONLY contains SET commands, which would leave us with nothing
                    statement = statement.TrimEnd(SemiColon);
                    statement = LeadingSetPattern.Replace(statement, string.Empty).TrimStart();
                }

                return(_statementParser(datastoreVendor, commandType, commandText, statement));
            }
            catch
            {
                return(new ParsedSqlStatement(datastoreVendor, null, null));
            }
        }
Exemple #2
0
            public virtual ParsedSqlStatement ParseStatement(DatastoreVendor vendor, CommandType commandType, string commandText, string statement)
            {
                if (!string.IsNullOrEmpty(_shortcut) && !statement.StartsWith(_shortcut, StringComparison.CurrentCultureIgnoreCase))
                {
                    return(null);
                }

                var matcher = _pattern.Match(statement);

                if (!matcher.Success)
                {
                    return(null);
                }

                var model = "unknown";

                foreach (Group g in matcher.Groups)
                {
                    var str = g.ToString();
                    if (!string.IsNullOrEmpty(str))
                    {
                        model = str;
                    }
                }

                if (string.Equals(model, "select", StringComparison.CurrentCultureIgnoreCase))
                {
                    model = "(subquery)";
                }
                else
                {
                    model = StringsHelper.FixDatabaseObjectName(model);
                    if (!IsValidModelName(model))
                    {
                        model = "ParseError";
                    }
                }
                return(CreateParsedDatabaseStatement(vendor, model));
            }
Exemple #3
0
        /// <summary>
        /// Heursitical crawl through a database command to find features suitable for making metric names.
        /// This uses linear search through the regexp patterns.
        /// </summary>
        /// <returns>A ParsedDatabaseStatement if some heuristic matches; otherwise null</returns>
        public static ParsedSqlStatement GetParsedDatabaseStatement(DatastoreVendor datastoreVendor, CommandType commandType, string commandText)
        {
            try
            {
                switch (commandType)
                {
                case CommandType.TableDirect:
                    return(new ParsedSqlStatement(datastoreVendor, commandText, "select"));

                case CommandType.StoredProcedure:
                    return(new ParsedSqlStatement(datastoreVendor, StringsHelper.FixDatabaseObjectName(commandText), "ExecuteProcedure"));
                }
                // Remove comments.
                var statement = CommentPattern.Replace(commandText, string.Empty).TrimStart();

                return(_statementParser(datastoreVendor, commandType, commandText, statement));
            }
            catch
            {
                return(new ParsedSqlStatement(datastoreVendor, null, null));
            }
        }
 public static void TestUnquoteTick()
 {
     Assert.AreEqual("dude", StringsHelper.FixDatabaseObjectName("`dude`"));
 }
 public static void TestUnquoteSingle()
 {
     Assert.AreEqual("dude", StringsHelper.FixDatabaseObjectName("'dude'"));
 }
 public static void TestUnquoteDouble()
 {
     Assert.AreEqual("dude", StringsHelper.FixDatabaseObjectName("\"dude\""));
 }
 public static void TestFixDatabaseObjectNameWithBrackets()
 {
     Assert.AreEqual("dude", StringsHelper.FixDatabaseObjectName("[dude]"));
 }