public void Visit(MsSqlTable table)
        {
            var descriptor = new TableDescriptor(typeof(MsSqlTable<>));
            var sqlTable = new CodeTypeDeclaration(table.Variable) { TypeAttributes = TypeAttributes.NestedPrivate };
            sqlTable.BaseTypes.Add(new CodeTypeReference("IRow"));

            var sqlTableCodeDomType = new CodeTypeReference("MsSqlTable", new CodeTypeReference(table.Variable));
            Scope.Current.Type.Type.Members.Add(
                new CodeMemberField(sqlTableCodeDomType, table.Variable) { Attributes = MemberAttributes.Public | MemberAttributes.Final });

            Scope.Current.Type.Constructor.Statements.Add(new CodeAssignStatement(
               new CodeSnippetExpression(table.Variable),
               new CodeObjectCreateExpression(
                   new CodeTypeReference("MsSqlTable", new CodeTypeReference(table.Variable)))));

            var codeExpressions = new List<CodeExpression>();
            foreach (var arg in table.Args)
            {
                var domArg = VisitChild(arg);
                sqlTable.Members.AddRange(domArg.ParentMemberDefinitions);
                codeExpressions.Add(new CodePrimitiveExpression(arg.Variable));
                descriptor.Variables.Add(new VariableTypePair { Variable = arg.Variable, Primitive = TablePrimitive.FromString(arg.Type) });
            }

            _mainType.Type.Members.Add(sqlTable);

            if (Scope.Current.IsCurrentScopeRegistered(table.Variable))
                Errors.Add(new VariableAlreadyExists(new Semantic.LineInfo(table.Line.Line, table.Line.CharacterPosition), table.Variable));

            Scope.Current.RegisterTable(table.Variable, descriptor, sqlTableCodeDomType);

            //Init Code
            CodeMemberMethod method = new CodeMemberMethod();
            method.Name = "Init_" + sqlTable.Name;
            method.Attributes = MemberAttributes.Private;

            _mainType.Type.Members.Add(method);

            var assignment = new CodeAssignStatement(new CodeVariableReferenceExpression("_" + Scope.Current.ScopeIdentifier + "." + table.Variable + ".ConnectionString"), new CodePrimitiveExpression(table.ConnectionString));
            method.Statements.Add(assignment);

            assignment = new CodeAssignStatement(new CodeVariableReferenceExpression("_" + Scope.Current.ScopeIdentifier + "." + table.Variable + ".Table"), new CodePrimitiveExpression(table.Table));
            method.Statements.Add(assignment);

            assignment = new CodeAssignStatement(new CodeVariableReferenceExpression("_" + Scope.Current.ScopeIdentifier + "." + table.Variable + ".FieldNames"),
                new CodeArrayCreateExpression(new CodeTypeReference(typeof(string)), codeExpressions.ToArray()));
            method.Statements.Add(assignment);

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

            _codeStack.Peek().ParentStatements.Add(methodcall);
            _codeStack.Peek().CodeExpression = methodcall;
        }
Beispiel #2
0
        public void Visit(MsSqlTable table, CommonTree tree)
        {
            Parent(tree).Children.Add(table);
            SetLine(table, tree);

            table.Variable = tree.Children[0].Text;
            var args = tree.Children[1] as CommonTree;
            foreach (var arg in args.Children)
                table.Children.Add(new TableColumnArg() { Variable = arg.GetChild(0).Text, Type = arg.GetChild(1).Text });

            table.ConnectionString = ParseLiteral(tree.Children[2].GetChild(0).GetChild(0).Text);
            table.Table = ParseLiteral(tree.Children[2].GetChild(1).GetChild(0).Text);
        }