Beispiel #1
0
 public MTMRelationInfo(String tableName, PropertyInfo OwnMTMProp)
 {
     this.tableName  = tableName;
     this.OwnMTMProp = OwnMTMProp;
     this.OppType    = OwnMTMProp.PropertyType.GetGenericArguments()[0];
     this.OppMTMProp = this.OppType.GetProperty(CustomAttr.GetMTMTargetAndTable(OwnMTMProp).Item1);
 }
Beispiel #2
0
        /// <summary>
        /// (Read)Fills the 'one' side of OTM relations for each request
        /// </summary>
        public bool FillOTM()
        {
            bool result = false;

            while (OTMReqList.Count() > 0)
            {
                result = true;
                Tuple <object, PropertyInfo> request = OTMReqList[0];
                int      id         = (int)request.Item1.GetType().GetProperty("id").GetValue(request.Item1);
                String   otm_target = CustomAttr.GetOTMTarget(request.Item2);
                List <T> filtered   = new List <T>();
                foreach (T record in allRecords)
                {
                    BaseClass target = (BaseClass)record.GetType().GetProperty(otm_target).GetValue(record);
                    if (target == null)
                    {
                        continue;
                    }
                    if (target.id == id)
                    {
                        IList list = request.Item1.GetType().GetProperty(request.Item2.Name).GetValue(request.Item1) as IList;
                        list.Add(record);
                    }
                }
                OTMReqList.RemoveAt(0);
            }
            return(result);
        }
Beispiel #3
0
        /// <summary>
        /// Reads a single property from the file. Used in readsinglerecord method.
        /// </summary>
        /// <param name="br">BinaryReader that has been created before.</param>
        /// <param name="primitiveInfos">To check where the current info belongs</param>
        /// <param name="customInfos">To check where the current info belongs</param>
        /// <param name="info">İnfo to be read</param>
        /// <returns>Returns the read value in object form</returns>
        public static object ReadSingleProperty(BinaryReader br, Type ty, PropertyInfo info)
        {
            BaseClass trash = (BaseClass)Activator.CreateInstance(ty);

            Type t = info.PropertyType;

            if (trash.OTORelInfos().Contains(info))
            {
                return(br.ReadInt32());
            }
            else if (t == typeof(String))
            {
                CustomAttr.MaxLengthAttr lengthAttr
                    = (CustomAttr.MaxLengthAttr)
                      CustomAttr.GetAttribute(typeof(CustomAttr.MaxLengthAttr), info);

                return(RemoveFiller(new string(br.ReadChars(lengthAttr.MaxLength + delimiter.Length)), delimiter));
            }
            else if (t == typeof(bool))
            {
                return(br.ReadBoolean());
            }
            else if (t == typeof(int))
            {
                return(br.ReadInt32());
            }
            else if (t == typeof(DateTime))
            {
                return(DateTime.Parse(new string(br.ReadChars(10))));
            }
            return(null); //exception
        }
Beispiel #4
0
        public static int CalculateRowByteSize(List <PropertyInfo> infos, List <PropertyInfo> customInfos, List <PropertyInfo> primitiveInfos)
        {
            int total = 0;

            foreach (PropertyInfo info in infos)
            {
                if (customInfos.Contains(info))
                {
                    total += sizeof(int); //foreign key
                }
                else if (info.PropertyType == typeof(Boolean))
                {
                    total += 1; //marshal 4 döndürüyor, yanlışsın marshal.
                    //dosyaya yazarken booleanlar 1 yer kaplıyor.
                }
                else if (info.PropertyType == typeof(String))
                {
                    total += CustomAttr.GetLength(info) + delimiter.Length;
                }
                else if (info.PropertyType.IsGenericType)
                {
                    continue;
                }
                else
                {
                    total += System.Runtime.InteropServices.Marshal.SizeOf(info.PropertyType);
                }
            }
            return(total);
        }
Beispiel #5
0
        /// <summary>
        /// Writes a single property to the file. Used in Add method, iterating through all properties.
        /// </summary>
        /// <param name="bw">BinaryWriter that has been created before</param>
        /// <param name="info">İnfo to be written</param>
        /// <param name="o">The object the info belongs to.</param>
        public static void WriteSingleProperty(BinaryWriter bw, PropertyInfo info, object o)
        {
            object value = o;
            Type   t     = info.PropertyType;

            if (t == typeof(String))
            {
                bw.Write(FillString((String)value, delimiter, CustomAttr.GetLength(info)).ToCharArray());
            }
            else if (t == typeof(bool))
            {
                bw.Write((bool)value);
            }
            else if (t == typeof(int))
            {
                bw.Write((int)value);
            }
            else if (t == typeof(DateTime))
            {
                char[] charArray = ((DateTime)value).ToString().ToCharArray();
                bw.Write(charArray);
            }
            else if (t == typeof(long))
            {
                bw.Write((long)value);
            }
            else if (t == typeof(byte[]))
            {
                bw.Write((byte[])value);
            }
            //else if custom info
        }
Beispiel #6
0
 /// <summary>
 /// Adds given record to update-queue, to be updated in SaveChanges operation. Also checks for inner properties' relationships.
 /// </summary>
 /// <param name="record">Record to be analyzed and updated.</param>
 public void Update(T record)
 {
     if (CustomAttr.Validator(record)) //Checks the attributes
     {
         CheckInside(record, "Update");
         ProcessMTM(record);
         this.Updates.Add(record);
     }
 }
Beispiel #7
0
        public BaseClass()
        {
            String name = this.GetType().Name;

            if (!InheritedTypes.ContainsKey(name))
            {
                InheritedTypes.Add(name, this.GetType());

                AllPrimitiveInfos.Add(name, new List <PropertyInfo>());
                AllOTORelInfos.Add(name, new List <PropertyInfo>());
                AllOTM_One.Add(name, new List <PropertyInfo>());
                AllOrderedInfos.Add(name, new List <PropertyInfo>());
                AllMTMInfoList.Add(name, new List <MTMRelationInfo>());

                List <PropertyInfo> infos = this.GetType().GetProperties().ToList();
                foreach (PropertyInfo info in infos)
                {
                    if (info.PropertyType.IsSubclassOf(typeof(BaseClass)))
                    {
                        AllOTORelInfos[name].Add(info);
                        AllOrderedInfos[name].Add(info);
                    }
                    else if (info.PropertyType.IsGenericType)
                    {
                        if (CustomAttr.GetOTMTarget(info) != null)
                        {
                            AllOTM_One[name].Add(info);
                        }
                        else
                        {
                            Tuple <string, string> Target_Table = CustomAttr.GetMTMTargetAndTable(info);
                            if (Target_Table != null)
                            {
                                Type         targetType = info.PropertyType.GetGenericArguments()[0];
                                PropertyInfo targetInfo = info.PropertyType.GetGenericArguments()[0].GetProperty(Target_Table.Item1);

                                if (!AllMTMRelations.Any(e => e.Key == Target_Table.Item2)) //tablo adına göre kontrol ediyor
                                {
                                    AllMTMRelations.Add(
                                        Target_Table.Item2, new Tuple <Type, Type>(this.GetType(), targetType)
                                        );
                                }
                                AllMTMInfoList[name].Add(new MTMRelationInfo(Target_Table.Item2, info));
                            }
                        }
                    }
                    else
                    {
                        AllPrimitiveInfos[name].Add(info);
                        AllOrderedInfos[name].Add(info);
                    }
                }
                RowSizes.Add(name, FileOps.CalculateRowByteSize(OrderedInfos(), OTORelInfos(), PrimitiveInfos()));
            }
        }
Beispiel #8
0
 /// <summary>
 /// Adds given record to add-queue, to be added in SaveChanges operation. Also checks for inner properties' relationships.
 /// </summary>
 /// <param name="record">Record to add.</param>
 public void Add(T record)
 {
     if (CustomAttr.Validator(record)) //Checks the attributes
     {
         CheckInside(record, "Add");   //içteki OTO ve OTM ilişkileri işler.
         ProcessMTM(record);           //içteki MTM ilişkileri işler.
     }
     if (allRecords.Contains(record))
     {
         return;
     }
     recordsToAddDirectly.Add(record);
 }
Beispiel #9
0
 /// <summary>
 /// Adds the record to addaschild-queue to be added in SaveChanges operation. Record will be given an id, and the parent will be updated with the id as foreign key in data tables.
 /// </summary>
 /// <param name="owner">Owner of the record.</param>
 /// <param name="record">Record.</param>
 public void AddAsChild(BaseClass owner, BaseClass record)
 {
     //add'in içindeki işlemler. bunun da içinde çocuk nesne olabilir.
     if (CustomAttr.Validator(record)) //Checks the attributes
     {
         CheckInside((T)record, "AddAsChild");
         ProcessMTM((T)record);
     }
     if (allRecords.Contains((T)record))
     {
         return;
     }
     recordsToAddAsOTO.Add(new Tuple <object, object>(owner, record));
 }
Beispiel #10
0
        public static void MakeReferenceNull(Stream mainFile, PropertyInfo info, Type T, Dictionary <int, List <int> > toChange)
        {
            //ToChange:
            //key = silinecek id
            //list int = key'e eskiden referans eden kayıtlar

            BaseClass trash = (BaseClass)Activator.CreateInstance(T);

            int total = 0;

            foreach (PropertyInfo orderedInfo in trash.OrderedInfos()) //sütun kaçıncı pozisyonda bul
            {
                if (orderedInfo.Name.Equals(info.Name))                //yazacağımız sütunu bulduk.
                {
                    break;
                }
                else
                {
                    if (trash.OTORelInfos().Contains(orderedInfo))
                    {
                        total += sizeof(int); //foreign key
                    }
                    else if (orderedInfo.PropertyType == typeof(Boolean))
                    {
                        total += 1; //marshal 4 döndürüyor, yanlışsın marshal.
                                    //dosyaya yazarken booleanlar 1 yer kaplıyor.
                    }
                    else if (orderedInfo.PropertyType == typeof(String))
                    {
                        total += CustomAttr.GetLength(orderedInfo) + delimiter.Length;
                    }
                    else
                    {
                        total += System.Runtime.InteropServices.Marshal.SizeOf(orderedInfo.PropertyType);
                    }
                }
            }
            BinaryWriter bw = new BinaryWriter(mainFile);

            foreach (KeyValuePair <int, List <int> > kp in toChange)
            {
                foreach (int id in kp.Value)
                {
                    bw.BaseStream.Position = (id - 1) * trash.RowSize() + total;
                    bw.Write(-1);
                }
            }
        }
Beispiel #11
0
 /// <summary>
 /// Checks inside the given record for OTO-OTM-MTM relations and processes them by sending to the corresponding dbset methods.
 /// </summary>
 /// <param name="record"></param>
 /// <param name="mode"></param>
 public void CheckInside(BaseClass record, String mode)
 {
     foreach (PropertyInfo info in record.GetType().GetProperties())
     {
         if (ctx.dbsetTypes.Any(t => t.Name == info.PropertyType.Name))
         {
             BaseClass childObject = (BaseClass)info.GetValue(record);
             if (childObject != null)
             {
                 if (childObject.id == 0)
                 {
                     object   dbset      = ctx.GetDBSetByType(childObject.GetType());
                     object[] parameters = { record, childObject };
                     dbset.GetType().GetMethod("AddAsChild").Invoke(dbset, parameters);
                 }
             }
         }
     }
     //one to many
     foreach (PropertyInfo OTM in record.OTM_One())
     {
         IList  list  = OTM.GetValue(record) as IList;
         object dbset = ctx.GetDBSetByType(OTM.PropertyType.GetGenericArguments()[0]);
         foreach (BaseClass eleman in list)
         {
             if (eleman.id != 0)
             {
                 PropertyInfo targetProp = eleman.GetType().GetProperty(CustomAttr.GetOTMTarget(OTM));
                 //bizde liste var, karşıda ise tek nesneye referans
                 if (targetProp.GetValue(eleman) != record)
                 {
                     targetProp.SetValue(eleman, record);
                     object[] parameters_update = { eleman };
                     object   updates           = dbset.GetType().GetField("Updates").GetValue(dbset); //içindeki tekrar eklenmesin diye Update fonksiyonunu pas geçtim.
                     updates.GetType().GetMethod("Add").Invoke(updates, parameters_update);
                 }
                 continue;
             }
             object[] parameters = new object[3];
             parameters[0] = record;
             parameters[1] = eleman;
             parameters[2] = eleman.GetType().GetProperty(CustomAttr.GetOTMTarget(OTM));
             dbset.GetType().GetMethod("AddAsOTM").Invoke(dbset, parameters);
         }
     }
 }