Beispiel #1
0
        public static CodeTypeDeclaration ParseClass(string s, CDILContext context)
        {
            // Console.WriteLine(s, par);
            CDILParser parser = new CDILParser(context.Format(s), context);

            return(parser.ParseClass());
        }
Beispiel #2
0
        static string Preprocess(string txt, CDILContext context)
        {
            StringReader sr = new StringReader(txt);
            StringWriter sw = new StringWriter();

            string       line;
            bool         skip      = false;
            Stack <bool> skipStack = new Stack <bool>();

            while ((line = sr.ReadLine()) != null)
            {
                if (line.StartsWith("#ifnot "))
                {
                    string conditionalName = line.Substring(7);
                    object o = context[conditionalName];
                    if (o == null)
                    {
                        throw new ArgumentException("No such conditional: " + conditionalName);
                    }
                    skipStack.Push(skip);
                    skip |= Convert.ToBoolean(o);
                    continue;
                }
                if (line.StartsWith("#if "))
                {
                    string conditionalName = line.Substring(4);
                    object o = context[conditionalName];
                    if (o == null)
                    {
                        throw new ArgumentException("No such conditional: " + conditionalName);
                    }
                    skipStack.Push(skip);
                    skip |= !Convert.ToBoolean(o);
                    continue;
                }
                if (line.StartsWith("#endif"))
                {
                    skip = skipStack.Pop();
                    continue;
                }
                if (line.StartsWith("#else"))
                {
                    if (!skipStack.Peek())
                    {
                        skip = !skip;
                    }
                    continue;
                }
                if (!skip)
                {
                    sw.WriteLine(line);
                }
            }

            // Console.WriteLine("Preprocessed as {0}", sw);

            return(sw.ToString());
        }
Beispiel #3
0
        static string Preprocess(string txt, CDILContext context)
        {
            StringReader sr = new StringReader(txt);
            StringWriter sw = new StringWriter();

            string line;
            bool skip = false;
            Stack<bool> skipStack = new Stack<bool>();

            while ((line = sr.ReadLine()) != null)
            {
                if (line.StartsWith("#ifnot "))
                {
                    string conditionalName = line.Substring(7);
                    object o = context[conditionalName];
                    if (o == null)
                        throw new ArgumentException("No such conditional: " + conditionalName);
                    skipStack.Push(skip);
                    skip |= Convert.ToBoolean(o);
                    continue;
                }
                if (line.StartsWith("#if "))
                {
                    string conditionalName = line.Substring(4);
                    object o = context[conditionalName];
                    if (o == null)
                        throw new ArgumentException("No such conditional: " + conditionalName);
                    skipStack.Push(skip);
                    skip |= !Convert.ToBoolean(o);
                    continue;
                }
                if (line.StartsWith("#endif"))
                {
                    skip = skipStack.Pop();
                    continue;
                }
                if (line.StartsWith("#else"))
                {
                    if (!skipStack.Peek())
                        skip = !skip;
                    continue;
                }
                if (!skip)
                    sw.WriteLine(line);
            }

            // Console.WriteLine("Preprocessed as {0}", sw);

            return sw.ToString();
        }
Beispiel #4
0
 public static CodeStatement ParseStatement(string s, CDILContext context)
 {
     CDILParser parser = new CDILParser(context.Format(s), context);
     return parser.ParseStatement();
 }
Beispiel #5
0
 public CDILParser(string txt, CDILContext context)
     : base(Preprocess(txt, context))
 {
 }
Beispiel #6
0
        public static CodeTypeMemberCollection ParseMembers(string s, CDILContext context)
        {
            CDILParser parser = new CDILParser(context.Format(s), context);

            return(parser.ParseMembers());
        }
Beispiel #7
0
        public static CodeStatement ParseStatement(string s, CDILContext context)
        {
            CDILParser parser = new CDILParser(context.Format(s), context);

            return(parser.ParseStatement());
        }
        public void GenerateClassFactory(CodeNamespace nspace, ClassInfo ci)
        {
            if (!string.IsNullOrEmpty(ci.Schema.AssemblyName))
                return;
            FieldInfo fi = ci.GetFirstPrimaryKeyField();
            Sooda.ObjectMapper.SoodaFieldHandler fieldHandler = fi.GetNullableFieldHandler();
            string pkClrTypeName = fieldHandler.GetFieldType().FullName;
            string pkFieldHandlerTypeName = fieldHandler.GetType().FullName;

            CDILContext context = new CDILContext();
            context["ClassName"] = ci.Name;
            context["OutNamespace"] = Project.OutputNamespace;
            if (ci.GetPrimaryKeyFields().Length == 1)
            {
                context["GetRefArgumentType"] = pkClrTypeName;
                context["MultiColumnPrimaryKey"] = false;
            }
            else
            {
                context["GetRefArgumentType"] = "SoodaTuple";
                context["MultiColumnPrimaryKey"] = true;
            }
            context["PrimaryKeyHandlerType"] = pkFieldHandlerTypeName;
            context["IsAbstract"] = ci.IsAbstractClass();
            if (Project.LoaderClass)
                context["LoaderClass"] = /*Project.OutputNamespace.Replace(".", "") + "." + */ci.Name + "Loader";
            else
                context["LoaderClass"] = /*Project.OutputNamespace.Replace(".", "") + "Stubs." + */ci.Name + "_Stub";

            CodeTypeDeclaration factoryClass = CDILParser.ParseClass(CDILTemplate.Get("Factory.cdil"), context);

            factoryClass.CustomAttributes.Add(new CodeAttributeDeclaration("SoodaObjectFactoryAttribute",
                new CodeAttributeArgument(new CodePrimitiveExpression(ci.Name)),
                new CodeAttributeArgument(new CodeTypeOfExpression(ci.Name))
                ));

            nspace.Types.Add(factoryClass);
        }
Beispiel #9
0
 public CDILParser(string txt, CDILContext context)
     : base(Preprocess(txt, context))
 {
 }
        private void GenerateTypedInternalQueryWrappers(CodeNamespace ns, ClassInfo ci)
        {
            if (!string.IsNullOrEmpty(ci.Schema.AssemblyName))
                return;
            CDILContext context = new CDILContext();
            context["ClassName"] = ci.Name;
            context["PrimaryKeyType"] = ci.GetFirstPrimaryKeyField().GetNullableFieldHandler().GetFieldType().FullName;
            context["CSharp"] = _codeProvider is Microsoft.CSharp.CSharpCodeProvider;

            CodeTypeDeclaration ctd = CDILParser.ParseClass(CDILTemplate.Get("TypedCollectionWrapper.cdil"), context);
            ns.Types.Add(ctd);

            context = new CDILContext();
            context["ClassName"] = ci.Name;
            context["PrimaryKeyType"] = ci.GetFirstPrimaryKeyField().GetNullableFieldHandler().GetFieldType().FullName;
            context["CSharp"] = _codeProvider is Microsoft.CSharp.CSharpCodeProvider;
            context["ParameterAttributes"] = _codeGenerator.Supports(GeneratorSupport.ParameterAttributes);

            ctd = CDILParser.ParseClass(CDILTemplate.Get("TypedWrapper.cdil"), context);
            ns.Types.Add(ctd);

            foreach (CollectionBaseInfo coll in ci.UnifiedCollections)
            {
                CodeMemberProperty prop = new CodeMemberProperty();

                prop.Name = coll.Name;
                prop.Attributes = MemberAttributes.Public;
                prop.Type = new CodeTypeReference(coll.GetItemClass().Name + "CollectionExpression");
                prop.GetStatements.Add(
                    new CodeMethodReturnStatement(
                    new CodeObjectCreateExpression(prop.Type, new CodeThisReferenceExpression(), new CodePrimitiveExpression(coll.Name))
                    ));

                ctd.Members.Add(prop);
            }

            foreach (FieldInfo fi in ci.UnifiedFields)
            {
                CodeMemberProperty prop = new CodeMemberProperty();

                prop.Name = fi.Name;
                prop.Attributes = MemberAttributes.Public;
                string fullWrapperTypeName;
                string optionalNullable = fi.IsNullable ? "Nullable" : "";

                if (fi.ReferencedClass == null)
                {
                    fullWrapperTypeName = fi.GetFieldHandler().GetTypedWrapperClass();
                    if (fullWrapperTypeName == null)
                        continue;

                    prop.GetStatements.Add(new CodeMethodReturnStatement(
                        new CodeObjectCreateExpression(fullWrapperTypeName,
                        new CodeObjectCreateExpression("Sooda.QL.SoqlPathExpression", new CodeThisReferenceExpression(), new CodePrimitiveExpression(fi.Name)))));
                }
                else
                {
                    fullWrapperTypeName = fi.ReferencedClass.Name + optionalNullable + "WrapperExpression";
                    prop.GetStatements.Add(new CodeMethodReturnStatement(
                        new CodeObjectCreateExpression(fullWrapperTypeName,
                        new CodeThisReferenceExpression(), new CodePrimitiveExpression(fi.Name))));
                }

                prop.Type = new CodeTypeReference(fullWrapperTypeName);
                ctd.Members.Add(prop);
            }

            CodeTypeDeclaration nullablectd = CDILParser.ParseClass(CDILTemplate.Get("NullableTypedWrapper.cdil"), context);
            ns.Types.Add(nullablectd);
        }
        private void GenerateTypedPublicQueryWrappers(CodeNamespace ns, ClassInfo ci)
        {
            if (!string.IsNullOrEmpty(ci.Schema.AssemblyName))
                return;
            CDILContext context = new CDILContext();
            context["ClassName"] = ci.Name;

            CodeTypeDeclaration ctd = CDILParser.ParseClass(CDILTemplate.Get("ClassField.cdil"), context);
            ns.Types.Add(ctd);

            foreach (CollectionBaseInfo coll in ci.UnifiedCollections)
            {
                CodeMemberProperty prop = new CodeMemberProperty();

                prop.Name = coll.Name;
                prop.Attributes = MemberAttributes.Public | MemberAttributes.Static;
                prop.Type = new CodeTypeReference(coll.GetItemClass().Name + "CollectionExpression");
                prop.GetStatements.Add(
                    new CodeMethodReturnStatement(
                    new CodeObjectCreateExpression(prop.Type, new CodePrimitiveExpression(null), new CodePrimitiveExpression(coll.Name))
                    ));

                ctd.Members.Add(prop);
            }

            foreach (FieldInfo fi in ci.UnifiedFields)
            {
                CodeMemberProperty prop = new CodeMemberProperty();

                prop.Name = fi.Name;
                prop.Attributes = MemberAttributes.Public | MemberAttributes.Static;
                string fullWrapperTypeName;
                string optionalNullable = "";
                if (fi.IsNullable)
                    optionalNullable = "Nullable";

                if (fi.ReferencedClass == null)
                {
                    fullWrapperTypeName = fi.GetFieldHandler().GetTypedWrapperClass();
                    if (fullWrapperTypeName == null)
                        continue;

                    prop.GetStatements.Add(new CodeMethodReturnStatement(
                        new CodeObjectCreateExpression(fullWrapperTypeName,
                        new CodeObjectCreateExpression("Sooda.QL.SoqlPathExpression", new CodePrimitiveExpression(fi.Name)))));
                }
                else
                {
                    fullWrapperTypeName = fi.ReferencedClass.Name + optionalNullable + "WrapperExpression";
                    prop.GetStatements.Add(new CodeMethodReturnStatement(
                        new CodeObjectCreateExpression(fullWrapperTypeName,
                        new CodePrimitiveExpression(null), new CodePrimitiveExpression(fi.Name))));
                }

                prop.Type = new CodeTypeReference(fullWrapperTypeName);
                ctd.Members.Add(prop);
            }
        }
        public CodeTypeDeclaration GetLoaderClass(ClassInfo ci)
        {
            CDILContext context = new CDILContext();
            context["ClassName"] = ci.Name;
            context["HasBaseClass"] = ci.InheritsFromClass != null;

            string formalParameters = "";
            string actualParameters = "";

            foreach (FieldInfo fi in ci.GetPrimaryKeyFields())
            {
                if (formalParameters != "")
                {
                    formalParameters += ", ";
                    actualParameters += ", ";
                }
                string pkClrTypeName = fi.GetNullableFieldHandler().GetFieldType().FullName;
                formalParameters += pkClrTypeName + " " + MakeCamelCase(fi.Name);
                actualParameters += "arg(" + MakeCamelCase(fi.Name) + ")";
            }

            context["PrimaryKeyFormalParameters"] = formalParameters;
            context["PrimaryKeyActualParameters"] = actualParameters;
            if (ci.GetPrimaryKeyFields().Length == 1)
            {
                context["PrimaryKeyActualParametersTuple"] = actualParameters;
                context["PrimaryKeyIsTuple"] = false;
            }
            else
            {
                context["PrimaryKeyIsTuple"] = true;
                context["PrimaryKeyActualParametersTuple"] = "new SoodaTuple(" + actualParameters + ")";
            }

            context["ClassUnifiedFieldCount"] = ci.UnifiedFields.Count;
            context["PrimaryKeyFieldHandler"] = ci.GetFirstPrimaryKeyField().GetNullableFieldHandler().GetType().FullName;
            context["OptionalNewAttribute"] = ci.InheritsFromClass != null ? ",New" : "";
            if (_codeProvider is Microsoft.VisualBasic.VBCodeProvider)
            {
                context["OptionalNewAttribute"] = "";
            }
            if (Project.LoaderClass)
            {
                context["LoaderClass"] = /*Project.OutputNamespace.Replace(".", "") + "." + */ci.Name + "Loader";
                context["OptionalNewAttribute"] = "";
            }
            else
                context["LoaderClass"] = /*Project.OutputNamespace.Replace(".", "") + "Stubs." + */ci.Name + "_Stub";
            context["WithSoql"] = Project.WithSoql;
#if DOTNET35
            context["Linq"] = true;
#else
            context["Linq"] = false;
#endif
            CodeTypeDeclaration ctd = CDILParser.ParseClass(CDILTemplate.Get("Loader.cdil"), context);
            foreach (FieldInfo fi in ci.LocalFields)
            {
                GenerateFindMethod(ctd, fi, false);
                GenerateFindMethod(ctd, fi, true);
            }
            return ctd;
        }
        public void GenerateListWrapper(CodeNamespace nspace, ClassInfo ci)
        {
            if (!string.IsNullOrEmpty(ci.Schema.AssemblyName))
                return;
            //Output.Verbose("      * list wrapper {0}.{1}.{2}", ci.Schema.AssemblyName, ci.Schema.Namespace, ci.Name);
            CDILContext context = new CDILContext();
            context["ClassName"] = ci.Name;
            context["OptionalNewAttribute"] = (_codeProvider is Microsoft.VisualBasic.VBCodeProvider) ? "" : ",New";

            CodeTypeDeclaration listWrapperClass = CDILParser.ParseClass(CDILTemplate.Get("ListWrapper.cdil"), context);
            nspace.Types.Add(listWrapperClass);
        }
        public void GenerateDatabaseSchema(CodeNamespace nspace, SchemaInfo schema)
        {
            CDILContext context = new CDILContext();
            context["OutNamespace"] = Project.OutputNamespace;

            CodeTypeDeclaration databaseSchemaClass = CDILParser.ParseClass(CDILTemplate.Get("DatabaseSchema.cdil"), context);

            CodeArrayCreateExpression cace = new CodeArrayCreateExpression("ISoodaObjectFactory");
            OutputFactories(cace, Project.OutputNamespace, schema);

            CodeTypeConstructor ctc = new CodeTypeConstructor();
            ctc.Statements.Add(
                    new CodeAssignStatement(
                        new CodeFieldReferenceExpression(null, "_theSchema"),
                        new CodeMethodInvokeExpression(null,"LoadSchema")));

            databaseSchemaClass.Members.Add(ctc);

            CodeConstructor ctor = new CodeConstructor();
            ctor.Attributes = MemberAttributes.Public;
            ctor.Statements.Add(
                    new CodeAssignStatement(
                        new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "_factories"), cace));

            databaseSchemaClass.Members.Add(ctor);

            nspace.Types.Add(databaseSchemaClass);
        }
Beispiel #15
0
 public static CodeTypeMemberCollection ParseMembers(string s, CDILContext context)
 {
     CDILParser parser = new CDILParser(context.Format(s), context);
     return parser.ParseMembers();
 }
Beispiel #16
0
 public static CodeTypeDeclaration ParseClass(string s, CDILContext context)
 {
     // Console.WriteLine(s, par);
     CDILParser parser = new CDILParser(context.Format(s), context);
     return parser.ParseClass();
 }
        public void GenerateClassStub(CodeNamespace nspace, ClassInfo ci, bool miniStub)
        {
            if (!string.IsNullOrEmpty(ci.Schema.AssemblyName))
                return;
            if (!miniStub)
                GenerateClassValues(nspace, ci, miniStub);

            CodeDomClassStubGenerator gen = new CodeDomClassStubGenerator(ci, Project);
            CDILContext context = new CDILContext();
            context["ClassName"] = ci.Name;
            context["HasBaseClass"] = ci.InheritsFromClass != null;
            context["MiniStub"] = miniStub;
            context["HasKeyGen"] = gen.KeyGen != "none";
            if (ci.ExtBaseClassName != null && !miniStub)
            {
                context["BaseClassName"] = ci.ExtBaseClassName;
            }
            else if (ci.InheritFrom != null && !miniStub)
            {
                context["BaseClassName"] = ci.InheritFrom;
            }
            else if (Project.BaseClassName != null && !miniStub)
            {
                context["BaseClassName"] = Project.BaseClassName;
            }
            else
            {
                context["BaseClassName"] = "SoodaObject";
            }
            context["ArrayFieldValues"] = ci.GetDataSource().EnableDynamicFields;

            CodeTypeDeclaration ctd = CDILParser.ParseClass(CDILTemplate.Get("Stub.cdil"), context);
            if (ci.Description != null)
            {
                ctd.Comments.Add(new CodeCommentStatement("<summary>", true));
                ctd.Comments.Add(new CodeCommentStatement(ci.Description, true));
                ctd.Comments.Add(new CodeCommentStatement("</summary>", true));
            }
            nspace.Types.Add(ctd);

            if (miniStub)
                return;

            CodeTypeDeclaration ctdLoader = GetLoaderClass(ci);

            if (!Project.LoaderClass)
            {
                foreach (CodeTypeMember m in ctdLoader.Members)
                {
                    ctd.Members.Add(m);
                }
            }

            // class constructor

            if (gen.KeyGen != "none")
            {
                ctd.Members.Add(gen.Field_keyGenerator());
            }

            gen.GenerateFields(ctd, ci);
            gen.GenerateProperties(ctd, ci);


            // literals
            if (ci.Constants != null && ci.GetPrimaryKeyFields().Length == 1)
            {
                foreach (ConstantInfo constInfo in ci.Constants)
                {
                    object value;
                    switch (ci.GetFirstPrimaryKeyField().DataType)
                    {
                        case FieldDataType.Integer:
                            value = int.Parse(constInfo.Key);
                            break;
                        case FieldDataType.String:
                        case FieldDataType.AnsiString:
                            value = constInfo.Key;
                            break;
                        default:
                            throw new NotSupportedException("Primary key type " + ci.GetFirstPrimaryKeyField().DataType + " is not supported");
                    }
                    ctd.Members.Add(gen.Prop_LiteralValue(constInfo.Name, value));
                }
            }

            foreach (FieldInfo fi in ci.LocalFields)
            {
                if (fi.IsPrimaryKey)
                    continue;

                if (ci.Triggers || fi.ForceTrigger)
                {
                    ctd.Members.Add(gen.Method_TriggerFieldUpdate(fi, "BeforeFieldUpdate"));
                    ctd.Members.Add(gen.Method_TriggerFieldUpdate(fi, "AfterFieldUpdate"));
                }
            }
        }