public static LambdaExpression Make(IncludeNode graphNode, IModelInfoProvider infoProvider)
        {
            // b => b.Posts
            var unaryExpr  = (UnaryExpression)graphNode.Expression;
            var lambdaExpr = (LambdaExpression)unaryExpr.Operand;

            return(Make(lambdaExpr, infoProvider));
        }
Exemple #2
0
        private static void TranslateGraphNode(
            IncludeNode graphNode, IModelInfoProvider infoProvider, IDbObjectFactory dbFactory,
            UniqueNameGenerator nameGenerator, AbstractMethodTranslator[] addons)
        {
            //TODO: check if there is a chain in the relation and throw exceptions

            var fromSelect = graphNode.FromNode?.Select;

            var state = new TranslationState();

            if (fromSelect != null)
            {
                state.ResultStack.Push(graphNode.FromNode.Select);
            }

            var translator = new ExpressionTranslator(infoProvider, dbFactory, state, addons);

            // translated current included node
            translator.Visit(graphNode.Expression);

            var dbObject = translator.GetElement();
            var dbRef    = dbObject as DbReference;

            var includedSelect = dbRef != null?dbFactory.BuildSelect(dbRef) : (IDbSelect)dbObject;

            includedSelect = SqlSelectOptimizer.Optimize(includedSelect);

            // if from node is not null, then we need to add the forgien key into the selection
            // of the temp table, and make the new select join to the translated select
            if (graphNode.FromNode != null)
            {
                UpdateFromNodeTempTable(graphNode.FromNode, dbObject, includedSelect, dbFactory, nameGenerator);
            }

            // if the graph node has child node, we need to create temp table for current node
            // so that the child nodes can join to the temp table containing forgien keys
            if (graphNode.ToNodes.Any())
            {
                UpdateIncludeSelectAndProcessToNodes(graphNode, includedSelect, infoProvider, dbFactory, nameGenerator, addons);
            }
            else
            {
                graphNode.Select = includedSelect;
            }

            var returnType = graphNode.Expression.GetReturnBaseType();

            graphNode.Select = DbColumnToEntityPropertyMapper.Map(graphNode.Select, returnType, infoProvider, dbFactory, nameGenerator);

            // update fill function
            if (graphNode.FromNode != null)
            {
                graphNode.FillFunc = FillFunctionMaker.Make(graphNode, infoProvider);
            }

            graphNode.Graph.ScriptToNodes[graphNode.Select] = graphNode;
        }
Exemple #3
0
 public void Initialize(IncludeNode includeNode)
 {
     SetContent(includeNode);
     var args = includeNode.Arguments;
     for (var i = 0; i < args.Count; i++)
     {
         SetVariable(new VariableNode(_args[i].Name, args[i]));
     }
 }
Exemple #4
0
        private static void UpdateIncludeSelectAndProcessToNodes(
            IncludeNode graphNode, IDbSelect includedSelect, IModelInfoProvider infoProvider,
            IDbObjectFactory dbFactory, UniqueNameGenerator nameGenerator, AbstractMethodTranslator[] addons)
        {
            // create temp table
            var entityRef   = includedSelect.GetReturnEntityRef();
            var returnTable = (IDbTable)entityRef.Referee;

            var newIncludedSelect = dbFactory.BuildSelect(returnTable);

            newIncludedSelect.From.Alias = nameGenerator.GenerateAlias(newIncludedSelect, returnTable.TableName);

            var tempTableName = TranslationConstants.TempTablePreix + nameGenerator.GenerateAlias(null, returnTable.TableName, true);
            var tempTable     = dbFactory.BuildTempTable(tempTableName, includedSelect);

            var tempSelect = dbFactory.BuildSelect(tempTable);

            tempSelect.From.Alias = nameGenerator.GenerateAlias(tempSelect, tempTable.TableName);

            var joinToTemp = MakeJoin(newIncludedSelect, tempSelect, dbFactory, nameGenerator);
            var joinTo     = joinToTemp.To;

            newIncludedSelect.Joins.Add(joinToTemp);

            foreach (var pk in returnTable.PrimaryKeys)
            {
                var fromPkCol = dbFactory.BuildColumn(entityRef, pk.Name, pk.ValType);
                includedSelect.Selection.Add(fromPkCol);

                var toPkCol = dbFactory.BuildColumn(tempSelect.From, pk.Name, pk.ValType);
                tempSelect.Selection.Add(toPkCol);
                tempSelect.GroupBys.Add(toPkCol);

                toPkCol = dbFactory.BuildColumn(joinTo, pk.Name, pk.ValType);
                var binary = dbFactory.BuildBinary(fromPkCol, DbOperator.Equal, toPkCol);
                joinToTemp.Condition = joinToTemp.Condition.UpdateBinary(binary, dbFactory);
            }

            var orderCol = dbFactory.BuildColumn(tempSelect.From, tempTable.RowNumberColumnName, typeof(int));

            tempSelect.Selection.Add(orderCol);

            orderCol = dbFactory.BuildColumn(joinTo, tempTable.RowNumberColumnName, typeof(int));
            newIncludedSelect.OrderBys.Add(orderCol);

            graphNode.Select    = newIncludedSelect;
            graphNode.TempTable = tempTable;

            foreach (var toNode in graphNode.ToNodes)
            {
                TranslateGraphNode(toNode, infoProvider, dbFactory, nameGenerator, addons);
            }
        }
 private IEnumerable <Parser <Node <TContext, TRequest, TResponse> > > GetParsers()
 {
     return(new[] {
         IfSetNode <TContext, TRequest, TResponse> .GetParser(Options, ViewOptions),
         EndIfSetNode <TContext, TRequest, TResponse> .GetParser(Options, ViewOptions),
         ForEachNode <TContext, TRequest, TResponse> .GetParser(Options, ViewOptions),
         EndForEachNode <TContext, TRequest, TResponse> .GetParser(Options, ViewOptions),
         IncludeNode <TContext, TRequest, TResponse> .GetParser(Options, ViewOptions),
         ModelNode <TContext, TRequest, TResponse> .GetParser(Options, ViewOptions),
         RootNode <TContext, TRequest, TResponse> .GetParser(Options, ViewOptions),
         ParentNode <TContext, TRequest, TResponse> .GetParser(Options, ViewOptions),
         ChildNode <TContext, TRequest, TResponse> .GetParser(Options, ViewOptions)
     }
            .Concat(ViewOptions.CustomExpressionParsers.Select(_ => _.Select(e => new CustomExpressionNode <TContext, TRequest, TResponse>(Options, ViewOptions, e))))
            .Concat(new[] { ContentNode <TContext, TRequest, TResponse> .GetParser(Options, ViewOptions) }));
 }
        public IEnumerable <string> Transform(IncludeNode item)
        {
            var evaluatedTemplateList = Environment.Evaluation.EvaluateDynamic(item.Templates.ExpressionParserNode, ExpressionParserTransformer);
            var templateList          = evaluatedTemplateList switch
            {
                string str => Enumerable.Repeat(str, 1),
                IEnumerable <string> strList => strList,
                _ => throw new NotImplementedException(),
            };
            var template = templateList.Select(templateName =>
            {
                var result = Environment.TryGetDynamicTemplate(templateName, out var template);
                return(new
                {
                    Result = result,
                    Template = template
                });
            }
                                               ).FirstOrDefault(res => res.Result)?.Template;

            if (template == null)
            {
                if (item.IgnoreMissing)
                {
                    yield break;
                }
                throw new TemplateNotFoundException();
            }

            if (item.WithContext != false)
            {
                foreach (var output in template.TemplateNode.Transform(this))
                {
                    yield return(output);
                }
            }
            else
            {
                yield return(template.Render());
            }
        }
 public void Transform(IncludeNode item)
 {
     throw new NotImplementedException();
 }
Exemple #8
0
 public Expression Transform(IncludeNode item)
 {
     throw new NotImplementedException();
 }
Exemple #9
0
        private static void UpdateFromNodeTempTable(
            IncludeNode fromNode, IDbObject dbObject, IDbSelect includedSelect,
            IDbObjectFactory dbFactory, UniqueNameGenerator nameGenerator)
        {
            var fromSelect = fromNode.Select;

            var dbJoin = fromSelect.Joins.Single(
                j => dbObject is DbReference ? ReferenceEquals(j.To, dbObject) : ReferenceEquals(j.To.Referee, dbObject));

            // remove the join to included relation from fromSelect
            fromSelect.Joins.Remove(dbJoin);

            if (dbObject is IDbSelect)
            {
                var refCol = includedSelect.Selection.Single(c => c is IDbRefColumn);
                includedSelect.Selection.Remove(refCol);
            }

            var keys     = dbJoin.Condition.GetDbObjects <IDbColumn>().ToArray();
            var fromKeys = keys.Where(c => !ReferenceEquals(c.Ref, dbJoin.To)).ToArray();
            var toKeys   = keys.Where(c => ReferenceEquals(c.Ref, dbJoin.To)).ToArray();

            var tempTable    = fromNode.TempTable;
            var sourceSelect = tempTable.SourceSelect;

            var fromRef   = sourceSelect.GetReturnEntityRef();
            var returnRef = includedSelect.GetReturnEntityRef();

            var tempSelect = dbFactory.BuildSelect(tempTable);

            tempSelect.From.Alias = nameGenerator.GenerateAlias(tempSelect, tempTable.TableName);

            var joinToTemp = MakeJoin(includedSelect, tempSelect, dbFactory, nameGenerator);

            includedSelect.Joins.Add(joinToTemp);

            for (var i = 0; i < fromKeys.Length; i++)
            {
                var fromKey = fromKeys[i];
                var toKey   = toKeys[i];

                var fromPkCol = dbFactory.BuildColumn(fromRef, fromKey.Name, fromKey.ValType);
                sourceSelect.Selection.Add(fromPkCol);

                var tempPkCol = dbFactory.BuildColumn(tempSelect.From, fromKey.Name, fromKey.ValType);
                tempSelect.Selection.Add(tempPkCol);
                tempSelect.GroupBys.Add(tempPkCol);

                if (dbObject is IDbSelect)
                {
                    var key = toKey;
                    toKey = (IDbColumn)includedSelect.Selection.Single(c => c.GetAliasOrName() == key.GetNameOrAlias());
                    includedSelect.Selection.Remove(toKey);
                    includedSelect.GroupBys.Remove(toKey);
                }

                fromPkCol = dbFactory.BuildColumn(joinToTemp.To, fromKey.Name, fromKey.ValType);
                var toPkCol = dbFactory.BuildColumn(returnRef, toKey.Name, toKey.ValType);
                var binary  = dbFactory.BuildBinary(toPkCol, DbOperator.Equal, fromPkCol);
                joinToTemp.Condition = joinToTemp.Condition.UpdateBinary(binary, dbFactory);
            }
        }
 public void Transform(IncludeNode item)
 {
     TransformASTNode(item);
 }
Exemple #11
0
 public IEnumerable <bool> Transform(IncludeNode item)
 {
     throw new NotImplementedException();
 }
 public void Transform(IncludeNode item, bool inner = false)
 {
     SetTrim();
 }
Exemple #13
0
        private Node ParseParagraph()
        {
            Token <TokenType> t    = PeekNextToken();
            ParaNode          node = new ParaNode();

            while (!t.End)
            {
                switch (t.TokenType)
                {
                case TokenType.If:
                    node.ParamNodes.Add(ParseIfBlock());
                    break;

                case TokenType.For:
                    node.ParamNodes.Add(ParseForBlock());
                    break;

                case TokenType.Let:
                    LetNode let = new LetNode(GetNextToken());
                    node.ParamNodes.Add(let);
                    break;

                case TokenType.Remark:
                    RemarkNode rem = new RemarkNode(GetNextToken());
                    node.ParamNodes.Add(rem);
                    break;

                case TokenType.Expression:
                case TokenType.Text:
                    SimpleNode sNode = new SimpleNode(GetNextToken());
                    node.ParamNodes.Add(sNode);
                    break;

                case TokenType.Include:
                    IncludeNode include = new IncludeNode(GetNextToken());
                    node.ParamNodes.Add(include);
                    break;

                case TokenType.Else:    //Note: if next token is else, then the trueNode of if is finished.
                case TokenType.End:
                    if (node.ParamNodes.Count == 1)
                    {
                        return(node.ParamNodes[0]);
                    }
                    else
                    {
                        return(node);
                    }

                case TokenType.Break:
                    GetNextToken();
                    node.ParamNodes.Add(new BreakNode());
                    break;

                case TokenType.Continue:
                    GetNextToken();
                    node.ParamNodes.Add(new ContinueNode());
                    break;
                }

                t = PeekNextToken();
            }

            if (node.ParamNodes.Count == 1)
            {
                return(node.ParamNodes[0]);
            }
            else
            {
                return(node);
            }
        }
 public IEnumerable <ASTNode> Transform(IncludeNode item)
 {
     yield return(item);
 }