예제 #1
0
        private void GetMtmRelationValues(ref EntityBase entity, DataRow row,
                                          RelationAttribute relAttr, PropertyDescription property)
        {
            DataRelation rel = ForeignKeys[property];

            //foreach ( DataRelation r in UnderlyngDataSet.Tables[Mapper.TableName].ChildRelations )
            //{
            //    if ( r.ChildTable == UnderlyngDataSet.Tables[relAttr.MamyToManyRelationTable] )
            //        rel = r;
            //}
            //if ( rel == null )
            //    throw new Exception("The child relation was not found");

            (property.GetValue(entity) as IList).Clear();
            foreach (DataRow childrow in entity.SourceRow.GetChildRows(rel))
            {
                object childid = childrow[relAttr.MtmRelationTableChildColumn];
                if (childid == null || childid == DBNull.Value)
                {
                    continue;
                }
                EntityBase e = _ps.GetEntityById(relAttr.RelatedType, childid);
                (property.GetValue(entity) as IList).Add(e);
            }
        }
예제 #2
0
        private void GetOtmRealationValues(ref EntityBase entity, DataRow row,
                                           RelationAttribute relAttr, PropertyDescription property)
        {
            ObjectDescription od = ClassFactory.GetObjectDescription(ReflectedType, _ps);

            Cryptany.Core.DPO.Mapper m   = new Mapper(relAttr.RelatedType, _ps);
            DataRelation             rel = ForeignKeys[property];

            //foreach ( DataRelation r in UnderlyngDataSet.Tables[Mapper.TableName].ChildRelations )
            //{
            //    if ( r.ChildTable == UnderlyngDataSet.Tables[m.TableName] )
            //        rel = r;
            //}
            //if ( rel == null )
            //    throw new Exception("The child relation was not found");

            (property.GetValue(entity) as IList).Clear();

            string relatedTypeIdField = ClassFactory.GetObjectDescription(relAttr.RelatedType, _ps).IdField.Name;

            foreach (DataRow childrow in entity.SourceRow.GetChildRows(rel))
            {
                object childid = childrow[m[relatedTypeIdField]];
                if (childid == null || childid == DBNull.Value)
                {
                    continue;
                }
                EntityBase e = _ps.GetEntityById(relAttr.RelatedType, childid);
                (property.GetValue(entity) as IList).Add(e);
            }
        }
예제 #3
0
 private void SetOtmRelationValues(ref EntityBase entity, DataRow row,
                                   RelationAttribute relAttr, PropertyDescription property)
 {
     if (entity.State == EntityState.Deleted)
     {
         if (relAttr.DeleteMode == DeleteMode.Single)
         {
         }
         else if (relAttr.DeleteMode == DeleteMode.Cascade)
         {
             foreach (EntityBase e in (IList)property.GetValue(entity))
             {
                 e.Delete();
                 _ps.SaveInner(e);
             }
         }
     }
     //else
     {
         IList    l    = ((IList)property.GetValue(entity));
         object[] objs = new object[l.Count];
         l.CopyTo(objs, 0);
         foreach (EntityBase e in objs)
         {
             ObjectDescription od = ClassFactory.GetObjectDescription(relAttr.RelatedType, _ps);
             od.Properties[relAttr.RelatedColumn].SetValue(e, entity);
             //if ( e.State == EntityState.Unchanged )
             //    e.State = EntityState.Changed;
             _ps.SaveInner(e);
             if (e.State == EntityState.NewDeleted || e.State == EntityState.Deleted)
             {
                 (property.GetValue(entity) as IList).Remove(e);
             }
         }
     }
 }
예제 #4
0
        private void SetOtoRelationValue(ref EntityBase entity, DataRow row,
                                         RelationAttribute relAttr, PropertyDescription property)
        {
            if (entity.State == EntityState.Deleted)
            {
                return;
            }
            Cryptany.Core.DPO.Mapper m = new Mapper(relAttr.RelatedType, _ps);
            EntityBase e = (EntityBase)property.GetValue(entity);

            if (e != null)
            {
                //if (e.State == EntityState.New)
                _ps.SaveInner(e);
                object id = ClassFactory.GetObjectDescription(m.EntityType, _ps).IdField.GetValue(e);
                row[Mapper[property.Name]] = id;
            }
            else
            {
                row[Mapper[property.Name]] = DBNull.Value;
            }
        }
예제 #5
0
        public static string CreateMtmInsert(PropertyDescription pd, PersistentStorage ps, IList <EntityBase> entities)
        {
            RelationAttribute attr = pd.GetAttribute <RelationAttribute>();

            if (attr == null || attr.RelationType != RelationType.ManyToMany)
            {
                throw new Exception("A many-to-many relationship expected");
            }
            string script = "INSERT INTO " + (string.IsNullOrEmpty(attr.SchemaName) ? "" : attr.SchemaName + ".") +
                            attr.MamyToManyRelationTable + "(" + attr.MtmRelationTableChildColumn + "," + attr.MtmRelationTableParentColumn + ")\r\n";
            string data = "";

            foreach (EntityBase entity in entities)
            {
                foreach (EntityBase e in pd.GetValue <System.Collections.IList>(entity))
                {
                    if (string.IsNullOrEmpty(data))
                    {
                        data = "SELECT " + ValueToString(e.ID) + ", " + ValueToString(entity.ID) + "\r\n";
                    }
                    else
                    {
                        data += "UNION ALL\r\nSELECT " + ValueToString(e.ID) + ", " + ValueToString(entity.ID) + "\r\n";
                    }
                }
            }
            if (string.IsNullOrEmpty(data))
            {
                return("");
            }
            else
            {
                script += data;
            }
            return(script);
        }
예제 #6
0
        private void SetMtmRelationValues(ref EntityBase entity, DataRow row,
                                          RelationAttribute relAttr, PropertyDescription property)
        {
            DataView dv         = new DataView(UnderlyngDataSet.Tables[relAttr.MamyToManyRelationTable]);
            string   rowfilter  = CreateFilterString(relAttr.MtmRelationTableParentColumn, entity.ID.ToString());
            string   rowfilter2 = string.Format(" and {0} = ", relAttr.MtmRelationTableChildColumn);

            List <object> ids = new List <object>();

            foreach (EntityBase e in (IList)property.GetValue(entity))
            {
                if (e.State == EntityState.New)
                {
                    _ps.SaveInner(e);

                    //DataRow r = dv.Table.NewRow();
                    ////r.ini
                    //r[relAttr.MtmRelationTableParentColumn] = entity.ID;
                    //r[relAttr.MtmRelationTableChildColumn] = e.ID;

                    ////this is absolutely incorrect !!!!!!!!!
                    //try
                    //{
                    //    r["id"] = Guid.NewGuid();
                    //}
                    //catch
                    //{
                    //}

                    //dv.Table.Rows.Add(r);
                }
                if (e.State == EntityState.Deleted)
                {
                    //dv.RowFilter = rowfilter + rowfilter2 + e.ID.ToString();
                    ////e.State = EntityState.Deleted;
                    ////_ps.Save(e);
                    //row.Delete();
                    ////row.AcceptChanges();
                    //continue;

                    //the appropriate actions are performed later
                }
                if (e.State == EntityState.Changed)
                {
                    _ps.SaveInner(e);
                }

                rowfilter2 = string.Format(" and {0}", CreateFilterString(relAttr.MtmRelationTableChildColumn, e.ID));
                //.RowFilter = rowfilter + rowfilter2;
                if (dv.Table.Select(rowfilter + rowfilter2).Length == 0)
                {
                    DataRow r = dv.Table.NewRow();
                    // TEMP !!!!
                    if (r.Table.PrimaryKey[0].DataType == typeof(Guid))
                    {
                        r[r.Table.PrimaryKey[0]] = Guid.NewGuid();
                    }
                    //
                    r[relAttr.MtmRelationTableParentColumn] = entity.ID;
                    r[relAttr.MtmRelationTableChildColumn]  = e.ID;
                    dv.Table.Rows.Add(r);
                }
                ids.Add(e.ID);
            }

            DataView dMtm = _underlyngDataSet.Tables[relAttr.MamyToManyRelationTable].DefaultView;

            dMtm.RowFilter = "";
            string filter = CreateFilterString(relAttr.MtmRelationTableParentColumn, entity.ID);

            foreach (DataRow dr in dMtm.Table.Select(filter))
            {
                if (!ids.Contains(dr[relAttr.MtmRelationTableChildColumn]))
                {
                    dr.Delete();
                }
            }
        }