/// <summary>
        /// Finds the demanded DiagnosticsQuery and returns it including Query-Arguments
        /// </summary>
        /// <param name="queryName">the name of the requested DiagnosticsQuery</param>
        /// <returns>a DiagnosticsQueryDefinition-Object containing all parameters and permissions required to execute it</returns>
        public DiagnosticsQueryDefinition GetQuery(string queryName)
        {
            var dbQuery = dbContext.DiagnosticsQueries.FirstOrDefault(n => n.DiagnosticsQueryName == queryName);

            if (dbQuery != null)
            {
                var retVal = new DiagnosticsQueryDefinition
                {
                    AutoReturn           = dbQuery.AutoReturn,
                    DbContext            = dbQuery.DbContext,
                    DiagnosticsQueryName = dbQuery.DiagnosticsQueryName,
                    Permission           = dbQuery.Permission.PermissionName,
                    QueryText            = dbQuery.QueryText
                };
                foreach (var diagnosticsQueryParameter in dbQuery.Parameters)
                {
                    retVal.Parameters.Add(new DiagnosticsQueryParameterDefinition
                    {
                        DefaultValue  = diagnosticsQueryParameter.DefaultValue,
                        Format        = diagnosticsQueryParameter.Format,
                        Optional      = diagnosticsQueryParameter.Optional,
                        ParameterName = diagnosticsQueryParameter.ParameterName,
                        ParameterType = diagnosticsQueryParameter.ParameterType
                    });
                }

                return(retVal);
            }

            return(null);
        }
示例#2
0
        public static IEnumerable RunDiagnosticsQuery(this DbContext context, DiagnosticsQueryDefinition query, IDictionary <string, object> arguments)
        {
            var args      = new Dictionary <string, object>(arguments);
            var queryText = CreateDiagQuery(context, query, args);

            return(RunQuery(context, queryText, RosDiagConfig, args));
        }
示例#3
0
        public static IDictionary <string, object> BuildArguments(DiagnosticsQueryDefinition query, IDictionary <string, string> arguments)
        {
            var retVal = new Dictionary <string, object>();

            BuildArguments(query, arguments, (name, value, type, nullable) => retVal.Add(name, value), (name, type, nullable) => retVal.Add(name, DBNull.Value));
            return(retVal);
        }
示例#4
0
        private static string CreateDiagQuery(DbContext context, DiagnosticsQueryDefinition query, IDictionary <string, object> arguments)
        {
            ConfigureLinqForContext(context, RosDiagConfig, out var contextType);
            StringBuilder fullQuery = new StringBuilder($@"{contextType.Name} db = Global.Db;
");

            DiagnoseQueryHelper.VerifyArguments(query, arguments, fullQuery);
            fullQuery.AppendLine($"{(query.AutoReturn ? "return " : "")}{query.QueryText}{(query.AutoReturn ? ";" : "")}");
            return(fullQuery.ToString());
        }
示例#5
0
        private static void BuildArguments(DiagnosticsQueryDefinition query, IDictionary <string, string> arguments, Action <string, object, string, bool> paramWithValue, Action <string, string, bool> paramWithoutValue)
        {
            ProcessArguments(query, s =>
            {
                object retVal = null;
                if (arguments.ContainsKey(s))
                {
                    retVal = arguments[s];
                }

                return(retVal);
            }, paramWithValue, paramWithoutValue);
        }
示例#6
0
        public static void BuildArguments(DiagnosticsQueryDefinition query, IDictionary <string, string> arguments, IDictionary <string, object> queryArguments, StringBuilder fullQuery)
        {
            var writeParam = new Action <string, string, bool>((name, type, nullable) =>
            {
                fullQuery.AppendLine($"{type}{(nullable ? "?" : "")} {name} = Global.{name};");
            });

            BuildArguments(query, arguments, (name, value, type, nullable) =>
            {
                if (!queryArguments.ContainsKey(name))
                {
                    queryArguments.Add(name, value);
                }

                writeParam(name, type, nullable);
            }, writeParam);
        }
        /// <summary>
        /// Gets the EntityContext for a DiagnosticsQuery and the DiagnosticsQuery object that represents the requested query
        /// </summary>
        /// <param name="services">the services for the current request</param>
        /// <param name="queryName">the name of the requested query</param>
        /// <param name="queryObject">the query-object that was found in the Diagnostics-DB</param>
        /// <returns>the Db-Context that is used to execute the requested query</returns>
        public static IWrappedDataSource ContextForDiagnosticsQuery(this IServiceProvider services, string queryName,
                                                                    string area, out DiagnosticsQueryDefinition queryObject)
        {
            var store = services.GetService <IDiagnosticsStore>();

            queryObject = store.GetQuery(queryName);
            if (queryObject != null)
            {
                if (services.VerifyUserPermissions(new[] { queryObject.Permission }))
                {
                    var options = services.GetService <IOptions <DiagnosticsSourceOptions> >().Value;
                    Func <IServiceProvider, string, string, object> factory = null;
                    var connection = queryObject.DbContext;
                    if (options.Factories.ContainsKey(connection))
                    {
                        factory = options.Factories[connection];
                    }
                    else if (options.Factories.ContainsKey("*"))
                    {
                        factory = options.Factories["*"];
                    }

                    if (factory != null)
                    {
                        var retVal = factory(services, connection, area);
                        if (retVal is DbContext dbc)
                        {
                            return(new WrappedDbContext(dbc, services));
                        }
                        else if (retVal is DynamicDataAdapter dynda)
                        {
                            return(new WrappedDynamicDataAdapter(dynda));
                        }
                        else
                        {
                            throw new InvalidOperationException("Unexpected returned value");
                        }
                    }
                }
            }

            return(null);
        }
示例#8
0
        private static void ProcessArguments(DiagnosticsQueryDefinition query, Func <string, object> argumentValue, Action <string, object, string, bool> paramWithValue, Action <string, string, bool> paramWithoutValue)
        {
            foreach (var arg in query.Parameters)
            {
                var rawValue = argumentValue(arg.ParameterName);
                if (rawValue == null && !arg.Optional)
                {
                    throw new ArgumentException($"Missing parameter {arg.ParameterName}!", arg.ParameterName);
                }

                bool nullable = arg.Optional && string.IsNullOrEmpty(arg.DefaultValue);
                rawValue ??= arg.DefaultValue;
                string typeDef;
                if (rawValue is string s && !string.IsNullOrEmpty(s))
                {
                    var paramValue = ParseArgument(s, arg.ParameterType, arg.Format, out typeDef);
                    paramWithValue(arg.ParameterName, paramValue, typeDef, nullable);
                    //queryArguments.Add(arg.ParameterName, ParseArgument(arg.DefaultValue, arg.ParameterType, arg.Format, out typeDef));
                }
        public IEnumerable RunDiagnosticsQuery(DiagnosticsQueryDefinition query, IDictionary <string, object> arguments)
        {
            var arg = DiagnoseQueryHelper.VerifyArguments(query, arguments);

            return(src.SqlQuery(query.QueryText, arg));
        }
        public IEnumerable RunDiagnosticsQuery(DiagnosticsQueryDefinition query, IDictionary <string, string> queryArguments)
        {
            var arguments = DiagnoseQueryHelper.BuildArguments(query, queryArguments);

            return(src.SqlQuery(query.QueryText, arguments));
        }
示例#11
0
 public IEnumerable RunDiagnosticsQuery(DiagnosticsQueryDefinition query, IDictionary <string, object> arguments)
 {
     return(decoratedContext.RunDiagnosticsQuery(query, arguments));
 }
示例#12
0
 public IEnumerable RunDiagnosticsQuery(DiagnosticsQueryDefinition qr, IDictionary <string, string> queryArguments)
 {
     return(decoratedContext.RunDiagnosticsQuery(qr, queryArguments));
 }