示例#1
0
        public DataType VisitFrom(FromStatement i)
        {
            if (i.DottedName == null)
            {
                return(DataType.Cont);
            }

            DataType dtModule = analyzer.LoadModule(i.DottedName.segs, scope);

            if (dtModule == null)
            {
                analyzer.putProblem(i, "Cannot load module");
            }
            else if (i.isImportStar())
            {
                importStar(i, dtModule);
            }
            else
            {
                foreach (var a in i.AliasedNames)
                {
                    Identifier     first = a.orig.segs[0];
                    ISet <Binding> bs    = dtModule.Table.Lookup(first.Name);
                    if (bs != null)
                    {
                        if (a.alias != null)
                        {
                            scope.Update(a.alias.Name, bs);
                            analyzer.putRef(a.alias, bs);
                        }
                        else
                        {
                            scope.Update(first.Name, bs);
                            analyzer.putRef(first, bs);
                        }
                    }
                    else
                    {
                        List <Identifier> ext = new List <Identifier>(i.DottedName.segs)
                        {
                            first
                        };
                        DataType mod2 = analyzer.LoadModule(ext, scope);
                        if (mod2 != null)
                        {
                            if (a.alias != null)
                            {
                                scope.Insert(analyzer, a.alias.Name, a.alias, mod2, BindingKind.VARIABLE);
                            }
                            else
                            {
                                scope.Insert(analyzer, first.Name, first, mod2, BindingKind.VARIABLE);
                            }
                        }
                    }
                }
            }
            return(DataType.Cont);
        }
示例#2
0
 public void VisitFrom(FromStatement f)
 {
     foreach (var alias in f.AliasedNames)
     {
         if (f.DottedName != null)
         {
             var total = f.DottedName.segs.Concat(alias.orig.segs)
                         .Select(s => gen.EscapeKeywordName(s.Name));
             gen.Using(alias.alias.Name, string.Join(".", total));
         }
     }
 }
        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;
        }
示例#4
0
        public void Visit(FromStatement statement)
        {
            string tableName = statement.Name;

            if (tableName == null)
            {
                return;
            }

            string tableVariable;

            if (statement.Alias != null)
            {
                tableVariable = statement.Alias;
            }
            else
            {
                tableVariable = statement.Name;
            }

            if (_scope.IsTableDefineSameLevel(tableName)) //if variable exists at this level we have conflict.
            {
                throw new InvalidOperationException("");
            }

            if (!_scope.IsTableDefinedAnyLevel(tableName)) //we don't know this table at any level
            {
                Errors.Add(new TableNotDefined(tableName, statement.Bounds.Start));
                return;
            }

            var table = _scope.TableLookupAnyLevel(tableName);

            table = table.Create();
            _scope.AddTable(tableVariable, table);

            VisitWhereIgnoreErrors(statement.ParentQueryStatement.Where);

            var result = table.Execute(statement.ParentQueryStatement.Limit);

            foreach (var e in table.Errors)
            {
                Errors.Add(e);
            }

            _scope.RemoveTable(tableVariable);
            _scope.AddTable(tableVariable, result);

            VisitChild(statement.InnerJoin);
        }
示例#5
0
        public override Element VisitFromStatementExp(SqlParser.FromStatementExpContext context)
        {
            var from = new FromStatement(CreateParseInfo(context))
            {
                Name = context.t != null ? context.t.Text : null, Alias = context.a != null ? context.a.Text : null
            };

            if (context.j != null)
            {
                from.Children.Add(Visit(context.j));
            }

            return(from);
        }
示例#6
0
 public void Visit(FromStatement statement, CommonTree tree)
 {
     Parent(tree).Children.Add(statement);
     SetLine(statement, tree);
     VisitChildren(tree);
 }
示例#7
0
 public void VisitFrom(FromStatement f)
 {
     foreach (var alias in f.AliasedNames)
     {
         if (f.DottedName != null)
         {
             var total = f.DottedName.segs.Concat(alias.orig.segs)
                 .Select(s => gen.EscapeKeywordName(s.Name));
             gen.Using(alias.alias.Name, string.Join(".", total));
         }
     }
 }
示例#8
0
 public void VisitFrom(FromStatement f)
 {
     throw new NotImplementedException();
 }
示例#9
0
        private void importStar(FromStatement i, DataType mt)
        {
            if (mt == null || mt.file == null)
            {
                return;
            }

            Module node = analyzer.getAstForFile(mt.file);

            if (node == null)
            {
                return;
            }

            List <string> names   = new List <string>();
            DataType      allType = mt.Table.lookupType("__all__");

            if (allType != null && allType is ListType)
            {
                ListType lt = (ListType)allType;

                foreach (object o in lt.values)
                {
                    if (o is string)
                    {
                        names.Add((string)o);
                    }
                }
            }

            if (names.Count > 0)
            {
                int start = i.Start;
                foreach (string name in names)
                {
                    ISet <Binding> b = mt.Table.LookupLocal(name);
                    if (b != null)
                    {
                        scope.Update(name, b);
                    }
                    else
                    {
                        List <Identifier> m2       = new List <Identifier>(i.DottedName.segs);
                        Identifier        fakeName = new Identifier(name, i.Filename, start, start + name.Length);
                        m2.Add(fakeName);
                        DataType type = analyzer.LoadModule(m2, scope);
                        if (type != null)
                        {
                            start += name.Length;
                            scope.Insert(analyzer, name, fakeName, type, BindingKind.VARIABLE);
                        }
                    }
                }
            }
            else
            {
                // Fall back to importing all names not starting with "_".
                foreach (var e in mt.Table.entrySet().Where(en => !en.Key.StartsWith("_")))
                {
                    scope.Update(e.Key, e.Value);
                }
            }
        }
示例#10
0
 public SymbolTable VisitFrom(FromStatement f)
 {
     throw new NotImplementedException();
 }
示例#11
0
        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);
            }
        }
 public FromClauseBuilder(FromStatement from, IFromStatementsSorter sorter)
 {
     _from   = from;
     _sorter = sorter;
 }