public static String ToStringHelper(ISerializableObject iso, int width)
        {
            FieldInfo[]   fis = iso.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
            StringBuilder b   = new StringBuilder();

            b.Append(System.Environment.NewLine);
            b.Append("-----------------------------------").Append(System.Environment.NewLine);
            b.Append(iso.GetType().Name).Append(System.Environment.NewLine);
            b.Append("-----------------------------------").Append(System.Environment.NewLine);

            foreach (FieldInfo fi in fis)
            {
                Object tmp = fi.GetValue(iso);
                if (tmp == null)
                {
                    tmp = "null";
                }
                if (AttributeWorker.IsRelationField(fi))
                {
                    continue;
                }
                b.Append(fi.Name).Append(" = ").Append(tmp).Append(System.Environment.NewLine);
            }

            return(b.ToString());
        }
Exemple #2
0
        public String ToSqlStringForward(String target)
        {
            AttributeWorker w = AttributeWorker.GetInstance(target);

            StringBuilder tmp = new StringBuilder();

            bool start = true;

            foreach (String key in _virtualKeyValues.Keys)
            {
                if (!start)
                {
                    tmp.Append(" AND ");
                }
                start = false;
                Object val = _virtualKeyValues[key];

                if (val == null)
                {
                    throw new InvalidOperationException("The key is not initialized");
                }

                String    translatedKey = _keyTranslation[key];
                FieldInfo joinField     = _virtualJoinKeyFields[translatedKey];
                String    column        = w.GetColumnMapping(joinField);
                tmp.Append(column).Append("=").Append(SqlUtil.SqlConvert(val));
            }

            return(tmp.ToString());
        }
 public static AttributeWorker GetInstance(String target)
 {
     if (_workers.ContainsKey(target))
     {
         return(_workers[target]);
     }
     else
     {
         _workers[target] = new AttributeWorker(target);
         return(_workers[target]);
     }
 }
Exemple #4
0
        private QueryGenerator ToSqlRestrictionForward(String target, Serializer.Serializer ser)
        {
            AttributeWorker w = AttributeWorker.GetInstance(target);

            QueryGenerator gen = new QueryGenerator();

            gen.AddQueryComponent(new SqlStringComponent(" WHERE "));

            bool start = true;

            foreach (String key in _virtualDefKeyFields.Keys)
            {
                if (!start)
                {
                    gen.AddQueryComponent(new SqlStringComponent(" AND "));
                }
                else
                {
                    gen.AddQueryComponent(new SqlStringComponent("("));
                }
                start = false;

                String    translatedKey = _keyTranslation[key];
                FieldInfo joinField     = _virtualJoinKeyFields[translatedKey];
                String    joinMapping   = w.GetColumnMapping(joinField);

                FieldInfo defField   = _virtualDefKeyFields[key];
                String    defMapping = w.GetColumnMapping(defField);

                StringBuilder tmp = new StringBuilder();

                tmp.Append(defMapping).Append(" IN (SELECT ").Append(joinMapping).Append(" FROM ");
                tmp.Append(w.GetTableMapping(TargetType, ser.Praefix));
                gen.AddQueryComponent(new SqlStringComponent(tmp));
                gen.AddQueryComponent(new PlaceHolderComponent());
                gen.AddQueryComponent(new SqlStringComponent(")"));
            }

            gen.AddQueryComponent(new SqlStringComponent(")"));
            return(gen);

            //ret.Append(")");
            //return ret.ToString();
        }
Exemple #5
0
        public String ToSqlStringBackward(String target)
        {
            AttributeWorker w = AttributeWorker.GetInstance(target);

            StringBuilder tmp = new StringBuilder();

            bool start = true;

            foreach (String key in _virtualKeyValues.Keys)
            {
                if (!start)
                {
                    tmp.Append(" AND ");
                }
                start = false;
                Object    val      = _virtualKeyValues[key];
                FieldInfo defField = _virtualDefKeyFields[key];
                String    column   = w.GetColumnMapping(defField);
                tmp.Append(column).Append("=").Append(SqlUtil.SqlConvert(val));
            }

            return(tmp.ToString());
        }
Exemple #6
0
        public void RegisterType(Type type)
        {
            FieldInfo[] fis = AttributeWorker.RetrieveAllFields(type);

            foreach (FieldInfo fi in fis)
            {
                RelationalAttribute r = AttributeWorker.GetRelationAttribute(fi);

                if (r != null)
                {
                    VirtualKey template;

                    if (r is OneToManyAttribute)
                    {
                        template = RegisterOneToManyVKey(type, fi);
                    }

                    else if (r is ManyToOneAttribute)
                    {
                        template = RegisterManyToOneVKey(type, fi);
                    }

                    else if (r is OneToOneDefAttribute)
                    {
                        if (((OneToOneDefAttribute)r).Reflexive)
                        {
                            template = RegisteReflexiveOneToOneDefVKey(ref type, fi);
                        }
                        else
                        {
                            throw new NotImplementedException();
                        }
                    }
                }
            }
        }
Exemple #7
0
        private VirtualKey RegisterManyToOneVKey(Type type, FieldInfo fi)
        {
            VirtualKey        template;
            MappedByAttribute a = AttributeWorker.GetMappedByAttribute(fi);

            if (a != null)
            {
                Type fType = fi.FieldType;

                FieldInfo tmp = _worker.RetrieveField(fType, a.MappedBy, true);

                if (tmp == null)
                {
                    throw new Serializer.SerializerException();
                }

                RelationalAttribute rTmp = AttributeWorker.GetRelationAttribute(tmp);
                if (rTmp == null || !(rTmp is OneToManyAttribute))
                {
                    throw new Serializer.SerializerException();
                }

                if (AttributeWorker.GetJoinColumnsAttributes(fi).Length > 0)
                {
                    throw new Serializer.SerializerException();
                }

                template             = CreateTemplateEntry(type, fType);
                template.TargetField = fi;
            }
            else
            {
                Type navigatedType = fi.FieldType;

                template = CreateTemplateEntry(type, navigatedType);


                JoinColumsAttribute[] jcs = AttributeWorker.GetJoinColumnsAttributes(fi);
                if (jcs.Length <= 0)
                {
                    throw new Serializer.SerializerException();
                }

                foreach (JoinColumsAttribute jc in jcs)
                {
                    Console.WriteLine(type + ":" + _worker.RetrieveField(type, jc.JoinColumn, false));
                    Console.WriteLine(navigatedType + ":" + _worker.RetrieveField(navigatedType, jc.DefColumn, false));

                    FieldInfo tmp = _worker.RetrieveField(type, jc.JoinColumn, false);
                    if (tmp == null)
                    {
                        throw new Serializer.SerializerException();
                    }

                    template.AddJoinKeyField(jc.JoinColumn, tmp);
                    template.TargetType = type;
                    List <String> tmpList;
                    try
                    {
                        tmpList = _foreignKeys[type];
                    }
                    catch (KeyNotFoundException)
                    {
                        tmpList            = new List <String>();
                        _foreignKeys[type] = tmpList;
                    }
                    tmpList.Add(jc.JoinColumn);


                    tmp = _worker.RetrieveField(navigatedType, jc.DefColumn, false);
                    if (tmp == null)
                    {
                        throw new Serializer.SerializerException();
                    }

                    template.AddDefKeyField(jc.DefColumn, tmp);
                    template.SourceType = navigatedType;

                    template.AddKeyTranslation(jc.DefColumn, jc.JoinColumn);
                    template.TargetField = fi;
                }
            }

            return(template);
        }
Exemple #8
0
        private VirtualKey RegisterOneToManyVKey(Type type, FieldInfo fi)
        {
            VirtualKey template;

            if (!fi.FieldType.IsGenericType)
            {
                throw new Serializer.SerializerException();
            }

            if (fi.FieldType.GetGenericArguments().Length != 1)
            {
                throw new Serializer.SerializerException();
            }

            if (AttributeWorker.GetMappedByAttribute(fi) != null)
            {
                throw new Serializer.SerializerException();
            }

            Type navigatedType = fi.FieldType.GetGenericArguments()[0];

            template = CreateTemplateEntry(type, navigatedType);

            JoinColumsAttribute[] jcs = AttributeWorker.GetJoinColumnsAttributes(fi);
            if (jcs.Length <= 0)
            {
                throw new Serializer.SerializerException();
            }

            foreach (JoinColumsAttribute jc in jcs)
            {
                Console.WriteLine(type + ":" + _worker.RetrieveField(type, jc.DefColumn, false));
                Console.WriteLine(navigatedType + ":" + _worker.RetrieveField(navigatedType, jc.JoinColumn, false));

                FieldInfo tmp = _worker.RetrieveField(type, jc.DefColumn, false);
                if (tmp == null)
                {
                    throw new Serializer.SerializerException();
                }
                template.AddDefKeyField(jc.DefColumn, tmp);
                template.SourceType = type;

                tmp = _worker.RetrieveField(navigatedType, jc.JoinColumn, false);
                if (tmp == null)
                {
                    throw new Serializer.SerializerException("Field " + jc.JoinColumn + " in " + navigatedType.FullName + " not found!");
                }

                template.AddJoinKeyField(jc.JoinColumn, tmp);
                template.TargetType = navigatedType;
                List <String> tmpList;
                try
                {
                    tmpList = _foreignKeys[navigatedType];
                }
                catch (KeyNotFoundException)
                {
                    tmpList = new List <String>();
                    _foreignKeys[navigatedType] = tmpList;
                }
                tmpList.Add(jc.JoinColumn);

                template.AddKeyTranslation(jc.DefColumn, jc.JoinColumn);

                template.SourceField = fi;
            }
            return(template);
        }
Exemple #9
0
        private VirtualKey RegisteReflexiveOneToOneDefVKey(ref Type type, FieldInfo fi)
        {
            VirtualKey template;
            Type       navigatedType = fi.FieldType;


            template = CreateTemplateEntry(type, navigatedType);

            JoinColumsAttribute[] jcs = AttributeWorker.GetJoinColumnsAttributes(fi);
            if (jcs.Length <= 0)
            {
                throw new Serializer.SerializerException();
            }

            bool dependencyResolved = false;

            foreach (JoinColumsAttribute jc in jcs)
            {
                if (_worker.RetrieveField(type, jc.DefColumn, false) != null &&
                    _worker.RetrieveField(navigatedType, jc.JoinColumn, false) != null)
                {
                    dependencyResolved = true;
                    continue;
                }
                else if (_worker.RetrieveField(navigatedType, jc.DefColumn, false) != null &&
                         _worker.RetrieveField(type, jc.JoinColumn, false) != null)
                {
                    Type tmp = navigatedType;
                    navigatedType      = type;
                    type               = tmp;
                    dependencyResolved = true;
                    continue;
                }
            }

            if (!dependencyResolved)
            {
                throw new Serializer.SerializerException();
            }

            foreach (JoinColumsAttribute jc in jcs)
            {
                Console.WriteLine(type + ":" + _worker.RetrieveField(type, jc.DefColumn, false));
                Console.WriteLine(navigatedType + ":" + _worker.RetrieveField(navigatedType, jc.JoinColumn, false));

                FieldInfo tmp = _worker.RetrieveField(type, jc.DefColumn, false);
                if (tmp == null)
                {
                    throw new Serializer.SerializerException();
                }
                template.AddDefKeyField(jc.DefColumn, tmp);
                template.SourceType = type;


                tmp = _worker.RetrieveField(navigatedType, jc.JoinColumn, false);
                if (tmp == null)
                {
                    throw new Serializer.SerializerException();
                }
                template.AddJoinKeyField(jc.JoinColumn, tmp);
                template.TargetType = navigatedType;

                List <String> tmpList;
                try
                {
                    tmpList = _foreignKeys[navigatedType];
                }
                catch (KeyNotFoundException)
                {
                    tmpList = new List <String>();
                    _foreignKeys[navigatedType] = tmpList;
                }
                tmpList.Add(jc.JoinColumn);

                template.AddKeyTranslation(jc.DefColumn, jc.JoinColumn);
                template.SourceField = fi;
                template.TargetField = fi;
            }
            return(template);
        }
Exemple #10
0
 public VirtualKeyFactory(AttributeWorker worker)
 {
     _worker      = worker;
     _templates   = new Dictionary <Type, IDictionary <Type, VirtualKey> >();
     _foreignKeys = new Dictionary <Type, List <String> >();
 }