public override bool ShouldCacheRelation(RelationInfo relation, ClassInfo classInfo)
 {
     if (classInfo.Cardinality == ClassCardinality.Small || classInfo.Cardinality == ClassCardinality.Medium)
         return true;
     else
         return false;
 }
 private ClassInfo GetRootClass(ClassInfo ci)
 {
     if (ci.InheritsFromClass != null)
         return GetRootClass(ci.InheritsFromClass);
     else
         return ci;
 }
        public CodeDomClassStubGenerator(ClassInfo ci, SoodaProject options)
        {
            this.classInfo = ci;
            this.options = options;
            string keyGen = "none";

            if (!ci.ReadOnly && ci.GetPrimaryKeyFields().Length == 1)
            {
                switch (ci.GetFirstPrimaryKeyField().DataType)
                {
                    case FieldDataType.Integer:
                        keyGen = "integer";
                        break;

                    case FieldDataType.Guid:
                        keyGen = "guid";
                        break;

                    case FieldDataType.Long:
                        keyGen = "long";
                        break;
                }
            }

            if (ci.KeyGenName != null)
                keyGen = ci.KeyGenName;

            this.KeyGen = keyGen;
        }
 public override bool ShouldCacheCollection(ClassInfo classInfo, SoodaWhereClause whereClause, SoodaOrderBy orderBy, int startIdx, int pageCount)
 {
     if (classInfo.Cardinality == ClassCardinality.Small || classInfo.Cardinality == ClassCardinality.Medium)
         return true;
     else
         return false;
 }
예제 #5
0
        public static SoqlBooleanExpression ClassRestriction(SoqlPathExpression path, SchemaInfo schema, ClassInfo classInfo)
        {
            // returns no additional filter clause for parent (master-parent) class
            if (classInfo.InheritsFromClass == null)
                return null;

            SoqlExpressionCollection literals = new SoqlExpressionCollection();

            foreach (ClassInfo subclass in classInfo.GetSubclassesForSchema(schema))
            {
                if (subclass.SubclassSelectorValue != null)
                {
                    literals.Add(new SoqlLiteralExpression(subclass.SubclassSelectorValue));
                }
            }
            if (classInfo.SubclassSelectorValue != null)
            {
                literals.Add(new SoqlLiteralExpression(classInfo.SubclassSelectorValue));
            }

            // returns false when class is abstract (no SubClassSelectorValue) and there is no subclasses
            if (literals.Count == 0)
                return new SoqlBooleanLiteralExpression(false);

            SoqlBooleanExpression restriction = new SoqlBooleanInExpression(
                new SoqlPathExpression(path, classInfo.SubclassSelectorField.Name),
                literals
            );

            return restriction;
        }
예제 #6
0
        public void Resolve()
        {
            if (Includes == null)
            {
                Includes = new List <IncludeInfo>();
            }

            classNameHash    = new Hashtable(StringComparer.OrdinalIgnoreCase);
            relationNameHash = new Hashtable(StringComparer.OrdinalIgnoreCase);
            Rehash();

            _backRefCollections = new Dictionary <string, StringCollection>();

            foreach (ClassInfo ci in Classes)
            {
                ci.ResolveInheritance(this);
            }
            Resolve(Classes);

            if (Relations != null)
            {
                foreach (RelationInfo ri in Relations)
                {
                    ri.Resolve(this);
                }
            }

            foreach (DataSourceInfo dsi in DataSources)
            {
                dsi.Resolve();
            }


            LocalClasses = new List <ClassInfo>();
            foreach (ClassInfo ci in Classes)
            {
                if (ci.Schema == this)
                {
                    LocalClasses.Add(ci);
                }
            }
            LocalRelations = new List <RelationInfo>();
            foreach (RelationInfo ri in Relations)
            {
                if (ri.Schema == this)
                {
                    LocalRelations.Add(ri);
                }
            }

            _subclasses = new Dictionary <string, List <ClassInfo> >();

            foreach (ClassInfo ci in Classes)
            {
                _subclasses[ci.Name] = new List <ClassInfo>();
            }

            foreach (ClassInfo ci0 in Classes)
            {
                for (ClassInfo ci = ci0.InheritsFromClass; ci != null; ci = ci.InheritsFromClass)
                {
                    _subclasses[ci.Name].Add(ci0);
                }
            }
        }
        public void GenerateFields(CodeTypeDeclaration ctd, ClassInfo ci)
        {
            if (GetFieldRefCacheCount(ci) > 0)
            {
                CodeMemberField field = new CodeMemberField(new CodeTypeReference(new CodeTypeReference("SoodaObject"), 1), "_refcache");
                field.Attributes = MemberAttributes.Private;
                field.InitExpression = new CodeArrayCreateExpression(
                    new CodeTypeReference(typeof(SoodaObject)), new CodePrimitiveExpression(GetFieldRefCacheCount(ci)));
                ctd.Members.Add(field);
            }

            if (classInfo.Collections1toN != null)
            {
                foreach (CollectionOnetoManyInfo coli in classInfo.Collections1toN)
                {
                    ctd.Members.Add(GetCollectionCache(coli));
                }
                foreach (CollectionOnetoManyInfo coli in classInfo.Collections1toN)
                {
                    CodeMemberField field = new CodeMemberField("Sooda.SoodaWhereClause", "_collectionWhere_" + coli.Name);
                    field.Attributes = MemberAttributes.Static | MemberAttributes.Private;
                    if (!string.IsNullOrEmpty(coli.Where))
                    {
                        field.InitExpression = new CodeObjectCreateExpression(
                            "Sooda.SoodaWhereClause",
                            new CodePrimitiveExpression(coli.Where));
                    }
                    else
                    {
                        field.InitExpression = new CodePrimitiveExpression(null);
                    }
                    ctd.Members.Add(field);
                }
            }

            if (classInfo.CollectionsNtoN != null)
            {
                foreach (CollectionManyToManyInfo coli in classInfo.CollectionsNtoN)
                {
                    ctd.Members.Add(GetCollectionCache(coli));
                }
            }
        }
 static CodeExpression RefCacheExpression(ClassInfo ci, FieldInfo fi)
 {
     return new CodeArrayIndexerExpression(RefCacheArray(),
         new CodePrimitiveExpression(GetFieldRefCacheIndex(ci, fi)));
 }
        static int GetFieldRefCacheIndex(ClassInfo ci, FieldInfo fi0)
        {
            int p = 0;

            foreach (FieldInfo fi in ci.LocalFields)
            {
                if (fi == fi0)
                    return p;
                if (fi.ReferencedClass != null)
                    p++;
            }

            return -1;
        }
예제 #10
0
 private void GenerateLoaderClass(CodeNamespace nspace, ClassInfo ci)
 {
     if (!string.IsNullOrEmpty(ci.Schema.AssemblyName))
         return;
     CodeTypeDeclaration ctd = GetLoaderClass(ci);
     nspace.Types.Add(ctd);
 }
예제 #11
0
 public bool GetExpirationTimeout(RelationInfo relation, ClassInfo resultClass, int itemCount, out TimeSpan expirationTimeout, out bool slidingExpiration)
 {
     expirationTimeout = _expirationTimeout;
     slidingExpiration = _slidingExpiration;
     return true;
 }
예제 #12
0
 public bool GetExpirationTimeout(ClassInfo classInfo, SoodaWhereClause whereClause, SoodaOrderBy orderBy, int startIdx, int pageCount, int itemCount, out TimeSpan expirationTimeout, out bool slidingExpiration)
 {
     expirationTimeout = _expirationTimeout;
     slidingExpiration = _slidingExpiration;
     return true;
 }
 public SoodaObjectOneToManyCollection(SoodaTransaction tran, Type childType, SoodaObject parentObject, string childRefField, Sooda.Schema.ClassInfo classInfo, SoodaWhereClause additionalWhereClause, bool cached)
     : base(tran, classInfo)
 {
     this.childType             = childType;
     this.parentObject          = parentObject;
     this.childRefField         = childRefField;
     this.additionalWhereClause = additionalWhereClause;
     this.cached = cached;
 }
예제 #14
0
 static void Resolve(ClassInfo ci)
 {
     Resolve(ci.Schema, new HashSet <ClassInfo> {
         ci
     });
 }
예제 #15
0
        private void GenerateConditionalSets(CodeStatementCollection stats, int min, int max, ClassInfo ci)
        {
            if (!string.IsNullOrEmpty(ci.Schema.AssemblyName))
                return;
            if (min >= max)
            {
                stats.Add(new CodeCommentStatement("ordinal: " + min));
                return;
            }
            // stats.Add(new CodeCommentStatement("range: [" + min + ".." + max + "]"));

            int mid = (min + max) / 2;

            CodeConditionStatement ifStatement = new CodeConditionStatement();
            ifStatement.Condition = new CodeBinaryOperatorExpression(
                            new CodeArgumentReferenceExpression("fieldOrdinal"),
                            CodeBinaryOperatorType.LessThanOrEqual,
                            new CodePrimitiveExpression(mid));

            stats.Add(ifStatement);
            GenerateConditionalSets(ifStatement.TrueStatements, min, mid, ci);
            GenerateConditionalSets(ifStatement.FalseStatements, mid + 1, max, ci);
        }
예제 #16
0
        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);
        }
예제 #17
0
        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);
            }
        }
예제 #18
0
        private void DumpTable(SchemaInfo schemaInfo, string owner, string table)
        {
            Console.WriteLine("Dumping table {0}.{1}", owner, table);

            ClassInfo ci = new ClassInfo();
            ci.Name = MakePascalCase(table);

            TableInfo ti = new TableInfo();
            ci.LocalTables = new List<TableInfo>();
            ti.DBTableName = table;
            ci.LocalTables.Add(ti);

            foreach (DataRow r in dataSet.Tables["Columns"].Select("TABLE_NAME='" + table + "' and TABLE_OWNER='" + owner + "'", "ORDINAL_POSITION"))
            {
                try
                {
                    string columnName = r["COLUMN_NAME"].ToString();

                    FieldInfo fi = new FieldInfo();
                    fi.Name = MakePascalCase(columnName);
                    fi.DBColumnName = columnName;
                    GetSoodaFieldAttributes(fi, r, false);
                    ti.Fields.Add(fi);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("WARNING: {0}", ex.Message);
                }
            }

            bool hasPrimaryKey = false;

            foreach (DataRow r in dataSet.Tables["PrimaryKeys"].Select("TABLE_NAME='" + table + "' and TABLE_OWNER='" + owner + "'"))
            {
                string column = Convert.ToString(r["COLUMN_NAME"]);
                foreach (FieldInfo fi in ti.Fields)
                {
                    if (0 == String.Compare(fi.DBColumnName, column, true))
                    {
                        fi.IsPrimaryKey = true;
                        hasPrimaryKey = true;
                    }
                }
            }
            if (!hasPrimaryKey)
            {
                Console.WriteLine("WARNING: Created artificial primary key from the first column of the " + ti.DBTableName + " table. This may be incorrect.");
                ti.Fields[0].IsPrimaryKey = true;
            }
            schemaInfo.Classes.Add(ci);
        }
예제 #19
0
 internal void ResolveInheritance(SchemaInfo schema)
 {
     if (InheritFrom != null)
     {
         InheritsFromClass = schema.FindClassByName(InheritFrom);
     }
     else
     {
         InheritsFromClass = null;
     }
 }
예제 #20
0
 public static string GetCollectionKey(ClassInfo classInfo, SoodaWhereClause wc)
 {
     return GetCollectionKey(classInfo.Name, wc);
 }
예제 #21
0
 public abstract bool ShouldCacheCollection(ClassInfo classInfo, SoodaWhereClause whereClause, SoodaOrderBy orderBy, int startIdx, int pageCount);
예제 #22
0
        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;
        }
예제 #23
0
        internal void Merge(ClassInfo merge)
        {
            Hashtable mergeNames = new Hashtable();
            foreach (TableInfo mti in this.LocalTables)
                mergeNames.Add(mti.DBTableName, mti);
            foreach (TableInfo ti in merge.LocalTables)
            {
                if (mergeNames.ContainsKey(ti.DBTableName))
                    ((TableInfo)mergeNames[ti.DBTableName]).Merge(ti);
                else
                    LocalTables.Add(ti);
            }
            mergeNames.Clear();

            if (this.Collections1toN != null)
            {
                foreach (CollectionOnetoManyInfo ci in Collections1toN)
                    mergeNames.Add(ci.Name, ci);
            }
            if (this.Collections1toN == null)
                this.Collections1toN = merge.Collections1toN;
            else
            {
                if (merge.Collections1toN != null)
                {
                    foreach (CollectionOnetoManyInfo mci in merge.Collections1toN)
                        if (mergeNames.ContainsKey(mci.Name))
                            throw new SoodaSchemaException(String.Format("Duplicate collection 1:N '{0}' found!", mci.Name));
                    this.Collections1toN = (CollectionOnetoManyInfo[])MergeArray(this.Collections1toN, merge.Collections1toN);
                }
            }
            mergeNames.Clear();

            if (this.CollectionsNtoN != null)
            {
                foreach (CollectionManyToManyInfo ci in CollectionsNtoN)
                    mergeNames.Add(ci.Name, ci);
            }
            if (this.CollectionsNtoN == null)
                this.CollectionsNtoN = merge.CollectionsNtoN;
            else
            {
                if (merge.CollectionsNtoN != null)
                {
                    foreach (CollectionManyToManyInfo mci in merge.CollectionsNtoN)
                        if (mergeNames.ContainsKey(mci.Name))
                            throw new SoodaSchemaException(String.Format("Duplicate collection N:N '{0}' found!", mci.Name));
                    this.CollectionsNtoN = (CollectionManyToManyInfo[])MergeArray(this.CollectionsNtoN, merge.CollectionsNtoN);
                }
            }
            mergeNames.Clear();

            if (this.Constants != null)
            {
                foreach (ConstantInfo ci in Constants)
                    mergeNames.Add(ci.Name, ci);
            }
            if (this.Constants == null)
                this.Constants = merge.Constants;
            else
            {
                if (merge.Constants != null)
                {
                    foreach (ConstantInfo mci in merge.Constants)
                        if (mergeNames.ContainsKey(mci.Name))
                            throw new SoodaSchemaException(String.Format("Duplicate constant name '{0}' found!", mci.Name));
                    this.Constants = (ConstantInfo[])MergeArray(this.Constants, merge.Constants);
                }
            }
        }
예제 #24
0
 public abstract bool ShouldCacheRelation(RelationInfo relation, ClassInfo resultClass);
 private CodeTypeReferenceExpression LoaderClass(ClassInfo ci)
 {
     if (options.LoaderClass)
         return new CodeTypeReferenceExpression(ci.Name + "Loader");
     else
         return new CodeTypeReferenceExpression(ci.Name + "_Stub");
 }
예제 #26
0
 public void AddClass(ClassInfo ci)
 {
     Classes.Add(ci);
     Rehash();
 }
        static int GetFieldRefCacheCount(ClassInfo ci)
        {
            int p = 0;

            foreach (FieldInfo fi in ci.LocalFields)
            {
                if (fi.ReferencedClass != null)
                    p++;
            }

            return p;
        }
예제 #28
0
 internal List<ClassInfo> GetSubclasses(ClassInfo ci)
 {
     List<ClassInfo> subclasses;
     _subclasses.TryGetValue(ci.Name, out subclasses);
     return subclasses;
 }
        public void GenerateProperties(CodeTypeDeclaration ctd, ClassInfo ci)
        {
            CodeMemberProperty prop;

            foreach (FieldInfo fi in classInfo.LocalFields)
            {
                if (fi.References != null)
                    continue;

                if (fi.IsNullable)
                {
                    if (options.NullableRepresentation == PrimitiveRepresentation.RawWithIsNull)
                    {
                        ctd.Members.Add(_IsNull(fi));
                        if (!ci.ReadOnly)
                        {
                            ctd.Members.Add(_SetNull(fi));
                        }
                    }
                }
                else
                {
                    if (options.NotNullRepresentation == PrimitiveRepresentation.RawWithIsNull)
                    {
                        if (!ci.ReadOnly)
                        {
                            // if it's read-only, not-null means not-null and there's no
                            // exception
                            ctd.Members.Add(_IsNull(fi));
                        }
                    }
                }
            }

            int primaryKeyComponentNumber = 0;

            foreach (FieldInfo fi in classInfo.LocalFields)
            {
                PrimitiveRepresentation actualNullableRepresentation = options.NullableRepresentation;
                PrimitiveRepresentation actualNotNullRepresentation = options.NotNullRepresentation;

                if (fi.GetNullableFieldHandler().GetSqlType() == null)
                {
                    if (actualNotNullRepresentation == PrimitiveRepresentation.SqlType)
                        actualNotNullRepresentation = PrimitiveRepresentation.Raw;

                    if (actualNullableRepresentation == PrimitiveRepresentation.SqlType)
                        actualNullableRepresentation = PrimitiveRepresentation.Raw;
                }

                CodeTypeReference returnType;

                //if (fi.Name == ci.PrimaryKeyFieldName)
                //{
                //  returnType = GetReturnType(PrimitiveRepresentation.Raw, fi.DataType);
                //}
                //else
                if (fi.References != null)
                {
                    returnType = new CodeTypeReference(fi.References);
                }
                else if (fi.IsNullable)
                {
                    returnType = GetReturnType(actualNullableRepresentation, fi);
                }
                else
                {
                    returnType = GetReturnType(actualNotNullRepresentation, fi);
                }

                prop = new CodeMemberProperty();
                prop.Name = fi.Name;
                prop.Attributes = MemberAttributes.Final | MemberAttributes.Public;
                prop.Type = returnType;
                //prop.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(null, "_FieldNames")));
                if (fi.Description != null)
                {
                    prop.Comments.Add(new CodeCommentStatement("<summary>", true));
                    prop.Comments.Add(new CodeCommentStatement(fi.Description, true));
                    prop.Comments.Add(new CodeCommentStatement("</summary>", true));
                }
                ctd.Members.Add(prop);

                if (fi.Size != -1)
                {
                    CodeAttributeDeclaration cad = new CodeAttributeDeclaration("Sooda.SoodaFieldSizeAttribute");
                    cad.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(fi.Size)));
                    prop.CustomAttributes.Add(cad);
                }

                if (fi.IsPrimaryKey)
                {
                    CodeExpression getPrimaryKeyValue = new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "GetPrimaryKeyValue");

                    if (classInfo.GetPrimaryKeyFields().Length > 1)
                    {
                        getPrimaryKeyValue = new CodeMethodInvokeExpression(
                            new CodeTypeReferenceExpression(typeof(SoodaTuple)), "GetValue", getPrimaryKeyValue, new CodePrimitiveExpression(primaryKeyComponentNumber));
                    }

                    if (fi.References != null)
                    {
                        prop.GetStatements.Add(
                            new CodeMethodReturnStatement(
                            new CodeMethodInvokeExpression(
                                LoaderClass(fi.ReferencedClass),
                                "GetRef",
                                GetTransaction(),
                                new CodeCastExpression(
                                    GetReturnType(actualNotNullRepresentation, fi),
                                    getPrimaryKeyValue
                            ))));
                    }
                    else
                    {
                        prop.GetStatements.Add(
                            new CodeMethodReturnStatement(
                            new CodeCastExpression(
                            prop.Type,
                            getPrimaryKeyValue
                            )));
                    }

                    if (!classInfo.ReadOnly && !fi.ReadOnly)
                    {
                        if (classInfo.GetPrimaryKeyFields().Length == 1)
                        {
                            prop.SetStatements.Add(
                                new CodeExpressionStatement(
                                new CodeMethodInvokeExpression(
                                new CodeThisReferenceExpression(), "SetPrimaryKeyValue",
                                new CodePropertySetValueReferenceExpression())));
                        }
                        else
                        {
                            CodeExpression plainValue = new CodePropertySetValueReferenceExpression();
                            if (fi.References != null)
                                plainValue = new CodeMethodInvokeExpression(plainValue, "GetPrimaryKeyValue");
                            prop.SetStatements.Add(
                                new CodeExpressionStatement(
                                new CodeMethodInvokeExpression(
                                new CodeThisReferenceExpression(), "SetPrimaryKeySubValue",
                                plainValue,
                                new CodePrimitiveExpression(primaryKeyComponentNumber),
                                new CodePrimitiveExpression(classInfo.GetPrimaryKeyFields().Length))));
                        }
                    }
                    primaryKeyComponentNumber++;
                    continue;
                }

                if (options.NullPropagation && (fi.References != null || fi.IsNullable) && actualNullableRepresentation != PrimitiveRepresentation.Raw)
                {
                    CodeExpression retVal = new CodePrimitiveExpression(null);

                    if (fi.References == null && actualNullableRepresentation == PrimitiveRepresentation.SqlType)
                    {
                        retVal = new CodePropertyReferenceExpression(
                            new CodeTypeReferenceExpression(fi.GetNullableFieldHandler().GetSqlType()), "Null");
                    }

                    prop.GetStatements.Add(
                        new CodeConditionStatement(
                        new CodeBinaryOperatorExpression(
                        new CodeThisReferenceExpression(),
                        CodeBinaryOperatorType.IdentityEquality,
                        new CodePrimitiveExpression(null)),
                        new CodeStatement[]
                            {
                                new CodeMethodReturnStatement(retVal)
                            },
                        new CodeStatement[]
                            {
                            }));
                }

                if (fi.References != null)
                {
                    // reference field getter
                    //
                    CodeExpression pk = new CodeVariableReferenceExpression("pk");
                    Type pkType;
                    CodeExpression isFieldNotNull;
                    CodeExpression getRef;
                    if (fi.ParentClass.GetDataSource().EnableDynamicFields)
                    {
                        pkType = typeof(object);
                        isFieldNotNull = new CodeBinaryOperatorExpression(
                            pk,
                            CodeBinaryOperatorType.IdentityInequality,
                            new CodePrimitiveExpression(null));
                        getRef = new CodeMethodInvokeExpression(
                            new CodeTypeReferenceExpression(typeof(SoodaObject)),
                            "GetRefHelper",
                            GetTransaction(),
                            Factory(fi.References),
                            pk);
                    }
                    else
                    {
                        pkType = fi.GetNullableFieldHandler().GetSqlType();
                        isFieldNotNull = new CodeBinaryOperatorExpression(
                            new CodePropertyReferenceExpression(pk, "IsNull"),
                            CodeBinaryOperatorType.ValueEquality,
                            new CodePrimitiveExpression(false));
                        getRef = new CodeMethodInvokeExpression(
                            LoaderClass(fi.ReferencedClass),
                            "GetRef",
                            GetTransaction(),
                            new CodePropertyReferenceExpression(pk, "Value"));
                    }
                    prop.GetStatements.Add(
                            new CodeConditionStatement(
                                new CodeBinaryOperatorExpression(
                                    RefCacheExpression(ci, fi),
                                    CodeBinaryOperatorType.IdentityEquality,
                                    new CodePrimitiveExpression(null)),
                                new CodeStatement[]
                                {
                                new CodeVariableDeclarationStatement(pkType, "pk", GetFieldValueForRead(fi)),
                                new CodeConditionStatement(
                                    isFieldNotNull,
                                    new CodeStatement[]
                                    {
                                    new CodeAssignStatement(
                                        RefCacheExpression(ci, fi),
                                        getRef)
                                    })
                                }
                    ));


                    prop.GetStatements.Add(
                            new CodeMethodReturnStatement(
                                new CodeCastExpression(returnType,
                                    RefCacheExpression(ci, fi))));

                    // reference field setter
                    if (!classInfo.ReadOnly && !fi.ReadOnly)
                    {
                        prop.SetStatements.Add(
                                new CodeExpressionStatement(

                                    new CodeMethodInvokeExpression(
                                        new CodeTypeReferenceExpression(typeof(Sooda.ObjectMapper.SoodaObjectImpl)), "SetRefFieldValue",

                                        // parameters
                                        new CodeThisReferenceExpression(),
                                        new CodePrimitiveExpression(fi.Table.OrdinalInClass),
                                        new CodePrimitiveExpression(fi.Name),
                                        new CodePrimitiveExpression(fi.ClassUnifiedOrdinal),
                                        new CodePropertySetValueReferenceExpression(),
                                        RefCacheArray(),
                                        new CodePrimitiveExpression(GetFieldRefCacheIndex(ci, fi)),
                                        Factory(returnType.BaseType)
                                        )));
                    }
                }
                else
                {
                    // plain field getter

                    CodeExpression fieldValue = GetFieldValueForRead(fi);
                    if (fi.ParentClass.GetDataSource().EnableDynamicFields)
                    {
                        switch (fi.IsNullable ? actualNullableRepresentation : actualNotNullRepresentation)
                        {
                            case PrimitiveRepresentation.Boxed:
                                break;

                            case PrimitiveRepresentation.SqlType:
                            case PrimitiveRepresentation.RawWithIsNull:
                            case PrimitiveRepresentation.Raw:
                                fieldValue = new CodeCastExpression(new CodeTypeReference(fi.GetNullableFieldHandler().GetFieldType()), fieldValue);
                                break;

                            case PrimitiveRepresentation.Nullable:
                                fieldValue = new CodeCastExpression(new CodeTypeReference(fi.GetNullableFieldHandler().GetNullableType()), fieldValue);
                                break;

                            default:
                                throw new NotImplementedException("Unknown PrimitiveRepresentation");
                        }
                    }
                    prop.GetStatements.Add(new CodeMethodReturnStatement(fieldValue));

                    if (!classInfo.ReadOnly && !fi.ReadOnly)
                    {
                        // plain field setter

                        CodeExpression beforeDelegate = new CodePrimitiveExpression(null);
                        CodeExpression afterDelegate = new CodePrimitiveExpression(null);

                        if (classInfo.Triggers)
                        {
                            beforeDelegate = new CodeDelegateCreateExpression(new CodeTypeReference(typeof(SoodaFieldUpdateDelegate)),
                                    new CodeThisReferenceExpression(), "BeforeFieldUpdate_" + fi.Name);
                            afterDelegate = new CodeDelegateCreateExpression(new CodeTypeReference(typeof(SoodaFieldUpdateDelegate)),
                                    new CodeThisReferenceExpression(), "AfterFieldUpdate_" + fi.Name);
                        }

                        prop.SetStatements.Add(
                                new CodeExpressionStatement(
                                    new CodeMethodInvokeExpression(
                                        new CodeTypeReferenceExpression(typeof(Sooda.ObjectMapper.SoodaObjectImpl)), "SetPlainFieldValue",

                                        // parameters
                                        new CodeThisReferenceExpression(),
                                        new CodePrimitiveExpression(fi.Table.OrdinalInClass),
                                        new CodePrimitiveExpression(fi.Name),
                                        new CodePrimitiveExpression(fi.ClassUnifiedOrdinal),
                                        Box(new CodePropertySetValueReferenceExpression()),
                                        beforeDelegate,
                                        afterDelegate
                                        )));
                    }
                }
            }


            if (classInfo.Collections1toN != null)
            {
                foreach (CollectionOnetoManyInfo coli in classInfo.Collections1toN)
                {
                    prop = new CodeMemberProperty();
                    prop.Name = coli.Name;
                    prop.Attributes = MemberAttributes.Final | MemberAttributes.Public;
                    prop.Type = GetCollectionPropertyType(coli.ClassName);

                    prop.GetStatements.Add(
                        new CodeConditionStatement(
                        new CodeBinaryOperatorExpression(
                        new CodeFieldReferenceExpression(This, "_collectionCache_" + coli.Name),
                        CodeBinaryOperatorType.IdentityEquality,
                        new CodePrimitiveExpression(null)), new CodeStatement[]
                            {
                                new CodeAssignStatement(
                                new CodeFieldReferenceExpression(This, "_collectionCache_" + coli.Name),
                                new CodeObjectCreateExpression(GetCollectionWrapperType(coli.ClassName),
                                new CodeObjectCreateExpression(new CodeTypeReference(typeof(Sooda.ObjectMapper.SoodaObjectOneToManyCollection)),
                                new CodeExpression[] {
                                    new CodeMethodInvokeExpression(This, "GetTransaction"),
                                    new CodeTypeOfExpression(new CodeTypeReference(coli.ClassName)),
                                    new CodeThisReferenceExpression(),
                                    new CodePrimitiveExpression(coli.ForeignFieldName),
                                    new CodePropertyReferenceExpression(new CodeTypeReferenceExpression(coli.ClassName + "_Factory"), "TheClassInfo"),
                                    new CodeFieldReferenceExpression(null, "_collectionWhere_" + coli.Name),
                                    new CodePrimitiveExpression(coli.Cache)
                            }))),
                    }, new CodeStatement[] { }));

                    prop.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(This, "_collectionCache_" + coli.Name)));
                    ctd.Members.Add(prop);

#if DOTNET35
                    CodeExpression whereExpression = new CodeMethodInvokeExpression(
                        new CodeTypeReferenceExpression(typeof(Sooda.QL.Soql)),
                        "FieldEquals",
                        new CodePrimitiveExpression(coli.ForeignFieldName),
                        This);
                    if (!string.IsNullOrEmpty(coli.Where))
                    {
                        whereExpression = new CodeObjectCreateExpression(
                            typeof(Sooda.QL.SoqlBooleanAndExpression),
                            whereExpression,
                            new CodePropertyReferenceExpression(new CodeFieldReferenceExpression(null, "_collectionWhere_" + coli.Name), "WhereExpression"));
                    }
                    prop = GetCollectionLinqQuery(coli, whereExpression);
                    ctd.Members.Add(prop);
#endif
                }
            }

            if (classInfo.CollectionsNtoN != null)
            {
                foreach (CollectionManyToManyInfo coli in classInfo.CollectionsNtoN)
                {
                    RelationInfo relationInfo = coli.GetRelationInfo();
                    // FieldInfo masterField = relationInfo.Table.Fields[1 - coli.MasterField];

                    string relationTargetClass = relationInfo.Table.Fields[coli.MasterField].References;

                    prop = new CodeMemberProperty();
                    prop.Name = coli.Name;
                    prop.Attributes = MemberAttributes.Final | MemberAttributes.Public;
                    prop.Type = GetCollectionPropertyType(relationTargetClass);

                    prop.GetStatements.Add(
                        new CodeConditionStatement(
                        new CodeBinaryOperatorExpression(
                        new CodeFieldReferenceExpression(This, "_collectionCache_" + coli.Name),
                        CodeBinaryOperatorType.IdentityEquality,
                        new CodePrimitiveExpression(null)), new CodeStatement[] {
                                                                                    new CodeAssignStatement(
                                                                                    new CodeFieldReferenceExpression(This, "_collectionCache_" + coli.Name),
                                                                                    new CodeObjectCreateExpression(GetCollectionWrapperType(relationTargetClass),
                                                                                    new CodeObjectCreateExpression(new CodeTypeReference(typeof(Sooda.ObjectMapper.SoodaObjectManyToManyCollection)),
                                                                                    new CodeExpression[] {
                                                                                                             new CodeMethodInvokeExpression(This, "GetTransaction"),
                                                                                                             new CodePrimitiveExpression(coli.MasterField),
                                                                                                             new CodeMethodInvokeExpression(This, "GetPrimaryKeyValue"),
                                                                                                             new CodeTypeOfExpression(relationInfo.Name + "_RelationTable"),
                                                                                                             new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(relationInfo.Name + "_RelationTable"), "theRelationInfo")

                                                                                                         }))
                                                                                    ),
                    }
                        , new CodeStatement[] { }));

                    prop.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(This, "_collectionCache_" + coli.Name)));

                    ctd.Members.Add(prop);

#if DOTNET35
                    CodeExpression whereExpression = new CodeMethodInvokeExpression(
                        new CodeTypeReferenceExpression(typeof(Sooda.QL.Soql)),
                        "CollectionFor",
                        new CodeMethodInvokeExpression(
                                new CodePropertyReferenceExpression(new CodeTypeReferenceExpression(classInfo.Name + "_Factory"), "TheClassInfo"),
                                "FindCollectionManyToMany",
                                new CodePrimitiveExpression(coli.Name)),
                        This);
                    prop = GetCollectionLinqQuery(coli, whereExpression);
                    ctd.Members.Add(prop);
#endif
                }
            }
        }
 public override bool ShouldCacheCollection(ClassInfo classInfo, SoodaWhereClause whereClause, SoodaOrderBy orderBy, int startIdx, int pageCount)
 {
     return false;
 }
 public override bool ShouldCacheRelation(RelationInfo relation, ClassInfo classInfo)
 {
     return false;
 }
예제 #32
0
        public TableInfo Clone(ClassInfo newParent)
        {
            TableInfo tableInfo = (TableInfo) this.MemberwiseClone();

            tableInfo.OwnerClass = newParent;
            return tableInfo;
        }
 static int CompareNames(ClassInfo x, ClassInfo y)
 {
     return x.Name.CompareTo(y.Name);
 }
예제 #34
0
 public void AddClass(ClassInfo ci)
 {
     Classes.Add(ci);
     Rehash();
 }