public void Visit(FromStatement statement)
        {
            CodeMemberMethod method = new CodeMemberMethod();
            method.Name = "From_" + Guid.NewGuid().ToString("N");
            method.Attributes = MemberAttributes.Private;
            GenerateCallStatement(method.Statements, statement.Line.Line);
            _mainType.Type.Members.Add(method);

            _joinMembers.Clear();
            CodeTypeReference anonType;
            var fetchMethod = CreateFetch(statement, out anonType);
            method.Statements.Add(new CodeVariableDeclarationStatement(new CodeTypeReference("IEnumerable", anonType), "join",
                    new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(null, fetchMethod.Name))));

            if(statement.Join != null)
            {
                var args = VisitChild(statement.Join, new CodeDomArg() {Scope = new ScopeData<Type> { Type = typeof(int), CodeDomReference = anonType} });

                method.Statements.Add(new CodeMethodReturnStatement(args.CodeExpression));
                method.ReturnType = args.Scope.CodeDomReference;
                _codeStack.Peek().Scope = args.Scope;
            }
            else
            {
                var tableType = new CodeTypeReference("CodeTable", anonType);
                method.ReturnType = tableType;
                _codeStack.Peek().Scope = new ScopeData<Type> { Type = typeof(int), CodeDomReference = tableType };

                method.Statements.Add(new CodeVariableDeclarationStatement(tableType, "newTable",
                    new CodeObjectCreateExpression(tableType)));

                method.Statements.Add(new CodeMethodInvokeExpression(
                    new CodeVariableReferenceExpression("newTable"), "SetRows",
                    new CodeMethodInvokeExpression(new CodeVariableReferenceExpression("join"), "ToList")));

                method.Statements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("newTable")));
            }

            var methodcall = new CodeMethodInvokeExpression(
              new CodeMethodReferenceExpression(null, method.Name));

            _codeStack.Peek().CodeExpression = methodcall;
        }
Esempio n. 2
0
 public void Visit(FromStatement statement, CommonTree tree)
 {
     Parent(tree).Children.Add(statement);
     SetLine(statement, tree);
     VisitChildren(tree);
 }
        public void Visit(UpdateStatement statement)
        {
            using (Scope.PushSelect())
            {
                CodeMemberMethod method = new CodeMemberMethod();
                method.Name = "Update_" + Guid.NewGuid().ToString("N");
                method.Attributes = MemberAttributes.Private;
                GenerateCallStatement(method.Statements, statement.Line.Line);

                _mainType.Type.Members.Add(method);

                FromStatement fromStatement = null;
                if (statement.From == null) //alias must be a table ref
                {
                    var tableReference = new TableVariableReference() { Id = statement.Alias.Id, Line = statement.Alias.Line };
                    fromStatement = new FromStatement() { Line = tableReference.Line };
                    fromStatement.Children.Add(tableReference);

                }
                else
                    fromStatement = statement.From;

                var fromDomArg = VisitChild(fromStatement);
                var rowType = fromDomArg.Scope.CodeDomReference.TypeArguments[0];

                var type = Scope.Current.FindTypeWithAlias(statement.Alias.Id);
                if (type != null && type == typeof(FileTable<>)) //filetables cannot be updated only inserted into
                    Errors.Add(new FileTableImmutable(new Semantic.LineInfo(statement.Line.Line, statement.Line.CharacterPosition)));

                method.Statements.Add(new CodeVariableDeclarationStatement(fromDomArg.Scope.CodeDomReference,
                   "fromTable",
                   fromDomArg.CodeExpression));

                if (statement.Where != null)
                {
                    var domWhereArgs = VisitChild(statement.Where, new CodeDomArg() { Scope = fromDomArg.Scope });
                    method.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("fromTable"), domWhereArgs.CodeExpression));
                }

                //outside iterator
                method.Statements.Add(new CodeVariableDeclarationStatement(new CodeTypeReference("IEnumerator", rowType),
                    "x",
                new CodeMethodInvokeExpression(
                        new CodeMethodReferenceExpression(new CodeTypeReferenceExpression("fromTable"), "GetEnumerator",
                        null))));

                var loop = new CodeIterationStatement();
                loop.InitStatement = new CodeSnippetStatement();
                loop.IncrementStatement = new CodeSnippetStatement();
                loop.TestExpression = new CodeMethodInvokeExpression(
                    new CodeMethodReferenceExpression(new CodeTypeReferenceExpression("x"), "MoveNext",
                    null));

                loop.Statements.Add(new CodeVariableDeclarationStatement(rowType,
                    "row",
                    new CodePropertyReferenceExpression(new CodeTypeReferenceExpression("x"), "Current")));

                foreach (var a in statement.SetArgs.AssignStatements)
                {
                    var assignmentArg = VisitChild(a);
                    loop.Statements.AddRange(assignmentArg.ParentStatements);
                }

                method.Statements.Add(loop);

                var methodcall = new CodeMethodInvokeExpression(
                        new CodeMethodReferenceExpression(null, method.Name));
                _codeStack.Peek().CodeExpression = methodcall;
                _codeStack.Peek().ParentStatements.Add(methodcall);
            }
        }