Esempio n. 1
0
        private static void RunReport(StringBuilder sb, long contextType, IExpression expr)
        {
            // Build structured query
            ResourceEntity re = new ResourceEntity();

            re.EntityTypeId = new EntityRef(contextType);
            StructuredQuery sq = new StructuredQuery {
                RootEntity = re
            };

            // Convert script to query
            QueryBuilderSettings qsettings = new QueryBuilderSettings();

            qsettings.StructuredQuery = sq;
            qsettings.ContextEntity   = re;
            ScalarExpression resExpr = Factory.ExpressionCompiler.CreateQueryEngineExpression(expr, qsettings);

            sb.AppendLine("OK");

            // Render XML
            sb.AppendLine("\nXML:");
            sb.AppendLine(StructuredQueryHelper.ToXml(sq));

            // Render SQL
            sb.AppendLine("\nSQL:");
            sq.SelectColumns.Add(new SelectColumn {
                Expression = resExpr
            });
            sb.AppendLine("declare @tenant as bigint = (select min(Id) from _vTenant) -- test harness");
            string sql = EDC.ReadiNow.Metadata.Query.Structured.Builder.QueryBuilder.GetSql(sq);

            sb.AppendLine(sql);
        }
        public StructuredQuery CreateQuery(string script, EntityRef rootType)
        {
            StructuredQuery sq = new StructuredQuery();

            sq.RootEntity = new ResourceEntity
            {
                EntityTypeId = rootType
            };

            BuilderSettings settings = new BuilderSettings();

            settings.RootContextType = ExprTypeHelper.EntityOfType(rootType);
            settings.TestMode        = true;
            IExpression expr = Factory.ExpressionCompiler.Compile(script, settings);
            string      xml  = expr.ToXml( );

            QueryBuilderSettings qbSettings = new QueryBuilderSettings();

            qbSettings.StructuredQuery = sq;
            qbSettings.ContextEntity   = (ResourceEntity)sq.RootEntity;

            ScalarExpression queryExpr = Factory.ExpressionCompiler.CreateQueryEngineExpression(expr, qbSettings);

            sq.SelectColumns.Add(new SelectColumn {
                Expression = queryExpr
            });
            return(sq);
        }
Esempio n. 3
0
        /// <summary>
        /// Creates a structured query expression.
        /// </summary>
        /// <remarks>
        /// This method does not add the new expression to the target structured query, however it will add new tree nodes to the structured query as they are requred.
        /// </remarks>
        public ScalarExpression CreateQueryEngineExpression(IExpression expression, QueryBuilderSettings settings)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }

            Expression expressionToCreate = expression as Expression;

            if (expressionToCreate == null)
            {
                throw new ArgumentException("Expected instance of type Expression.", "expression");
            }

            using (EntryPointContext.AppendEntryPoint("ExprQuery"))
                using (new SecurityBypassContext( ))
                    using (Profiler.Measure("ExpressionEngine.CreateQueryEngineExpression"))
                    {
                        if (settings == null)
                        {
                            settings = new QueryBuilderSettings();
                        }

                        var queryContext = new QueryBuilderContext
                        {
                            Settings = settings
                        };

                        // Add nodes to the query tree, if necessary.
                        expressionToCreate.ListRoot.BuildQueryNode(queryContext, true);

                        // Build up the expression itself
                        ScalarExpression result = expressionToCreate.Root.BuildQuery(queryContext);

                        // Special handing for bools
                        //if (expressionToCreate.ResultType.Type == DataType.Bool)
                        //{
                        //    if (settings.ConvertBoolsToString)
                        //        result = CastFromBool.CastToString(result);
                        //}

                        // Set result type
                        // TODO: return entity type
                        // TODO: return for any expression type
                        var calcExpression = result as CalculationExpression;
                        if (calcExpression != null)
                        {
                            calcExpression.DisplayType = ExprTypeHelper.ToDatabaseType(expressionToCreate.ResultType);
                        }

                        return(result);
                    }
        }
Esempio n. 4
0
        private static StructuredQuery TestBuildQuery(TestData testData, IExpression expression)
        {
            // Need to query against something (anything, whatever), in the case of test cases that don't specify a context
            EntityRef resourceType = testData.Context != null ?
                                     new EntityRef(testData.Context.ExprType.EntityType)
                : new EntityRef("core:type");

            // Create a query to graft results into
            StructuredQuery sq = new StructuredQuery();

            sq.RootEntity = new ResourceEntity
            {
                EntityTypeId = resourceType
            };

            // Select a single row (use the type resource as both the type and the instance)
            EntityRef resource = testData.Context != null && testData.Context.Resource != null ?
                                 new EntityRef(testData.Context.Resource.Id)
                : new EntityRef("core:type");

            sq.Conditions.Add(new QueryCondition
            {
                Expression = new IdExpression {
                    NodeId = sq.RootEntity.NodeId
                },
                Operator = ConditionType.Equal,
                Argument = new TypedValue {
                    Type = DatabaseType.IdentifierType, Value = resource.Id
                }
            });

            // Prep settings
            var settings = new QueryBuilderSettings();

            settings.StructuredQuery = sq;
            settings.ContextEntity   = (ResourceEntity)sq.RootEntity;

            // Build query
            ScalarExpression queryExpr = Factory.ExpressionCompiler.CreateQueryEngineExpression(expression, settings);

            sq.SelectColumns.Add(new SelectColumn {
                Expression = queryExpr
            });
            return(sq);
        }
Esempio n. 5
0
        /// <summary>
        /// Create a query that converts a calculation into a report that returns a list of IDs.
        /// </summary>
        /// <param name="filterCalculation">The calculation script.</param>
        /// <param name="entityType">The type of resource to load (and filter).</param>
        /// <param name="exactType">True if the report should return exact types only, false to include derived types.</param>
        public static StructuredQuery BuildFilterQuery(string filterCalculation, EntityRef entityType, bool exactType)
        {
            // Validate
            if (string.IsNullOrEmpty("filterCalculation"))
            {
                throw new ArgumentNullException("filterCalculation");
            }
            if (entityType == null)
            {
                throw new ArgumentNullException("entityType");
            }

            // Compile the calculation into an expression
            BuilderSettings builderSettings = new BuilderSettings {
                ExpectedResultType = ExprType.Bool,
                RootContextType    = ExprTypeHelper.EntityOfType(entityType),
                ScriptHost         = ScriptHostType.Report,
                ScriptHostIsApi    = true
            };
            IExpression filterCalcExpr = Factory.ExpressionCompiler.Compile(filterCalculation, builderSettings);

            // Create the structured query
            var rootEntity = new ResourceEntity {
                EntityTypeId = entityType,
                ExactType    = exactType
            };
            var query = new StructuredQuery()
            {
                RootEntity = rootEntity
            };

            // Generate a report condition for the filter
            QueryBuilderSettings queryBuilderSettings = new QueryBuilderSettings {
                ContextEntity        = rootEntity,
                ConvertBoolsToString = false,
                StructuredQuery      = query
            };
            ScalarExpression filterScalarExpr = Factory.ExpressionCompiler.CreateQueryEngineExpression(filterCalcExpr, queryBuilderSettings);

            rootEntity.Conditions = new List <ScalarExpression> {
                filterScalarExpr
            };

            return(query);
        }
        public ScalarExpression CreateScalarExpression(string script)
        {
            BuilderSettings settings = new BuilderSettings();

            settings.RootContextType = ExprType.None;
            //settings.StaticParameterResolver
            //settings.ExpectedResultType

            IExpression expr = Factory.ExpressionCompiler.Compile(script, settings);

            QueryBuilderSettings qbSettings = new QueryBuilderSettings();
            //qbSettings.ContextEntity =
            //qbSettings.StructuredQuery

            ScalarExpression queryExpr = Factory.ExpressionCompiler.CreateQueryEngineExpression(expr, qbSettings);

            return(queryExpr);
        }