Example #1
0
        //recursively sets values 
		private void RecurSetValues(sTable map,Connection conn)
		{
            Type ty = conn.Pool.Mapping[map.Name];
            List<string> extFields = new List<string>(map.ForeignTableProperties);
			foreach (string prop in map.Properties)
			{
                if (!map.ArrayProperties.Contains(prop))
                {
                    PropertyInfo pi = ty.GetProperty(prop, Utility._BINDING_FLAGS);
                    if (pi != null)
                    {
                        if (extFields.Contains(prop) && !Utility.IsEnum(pi.PropertyType))
                        {
                            Table t = (Table)pi.PropertyType.GetConstructor(Type.EmptyTypes).Invoke(new object[0]);
                            t._loadStatus = LoadStatus.Partial;
                            t = (Table)LazyProxy.Instance(t);
                            bool setValue = false;
                            t = SetExternalValues(map, prop, conn, out setValue, t);
                            if (!t.AllPrimaryKeysNull && setValue)
                            {
                                t.InitPrimaryKeys();
                                this.SetField(prop, t);
                            }
                        }
                        else
                        {
                            sTableField fld = map[prop][0];
                            if (conn.ContainsField(fld.Name))
                            {
                                if (conn.IsDBNull(conn.GetOrdinal(fld.Name)))
                                {
                                    try
                                    {
                                        this.SetField(prop, null);
                                    }
                                    catch (Exception e) { }
                                }
                                else
                                {
                                    if (fld.Type == FieldType.ENUM)
                                        this.SetField(prop, conn.Pool.GetEnumValue(pi.PropertyType, (int)conn[fld.Name]));
                                    else
                                        this.SetField(prop, conn[fld.Name]);
                                }
                            }
                        }
                    }
                }
			}
			if (conn.Pool.Mapping.IsMappableType(ty.BaseType))
			{
				RecurSetValues(conn.Pool.Mapping[ty.BaseType],conn);
			}
            this.InitPrimaryKeys();
		}
Example #2
0
 //called to set values onto a table that is externally mapped to this current table
 //this is used through lazy loading proxies by only setting the primary key fields.
 private Table SetExternalValues(sTable map,string propertyName, Connection conn, out bool setValue,Table table)
 {
     setValue = false;
     sTable eMap = conn.Pool.Mapping[table.GetType()];
     sTableField[] flds = map[propertyName];
     List<string> fProps = new List<string>(eMap.ForeignTableProperties);
     Type ty = table.GetType().BaseType;
     while (conn.Pool.Mapping.IsMappableType(ty))
     {
         foreach (string str in conn.Pool.Mapping[ty].ForeignTableProperties)
         {
             if (!fProps.Contains(str))
                 fProps.Add(str);
         }
         ty = ty.BaseType;
     }
     foreach (string prop in eMap.PrimaryKeyProperties)
     {
         if (fProps.Contains(prop) && !eMap.IsEnumProperty(prop))
         {
             PropertyInfo pi = table.GetType().GetProperty(prop, Utility._BINDING_FLAGS);
             if (pi == null)
                 pi = table.GetType().GetProperty(prop, Utility._BINDING_FLAGS_WITH_INHERITANCE);
             Table t = (Table)pi.PropertyType.GetConstructor(Type.EmptyTypes).Invoke(new object[0]);
             t._loadStatus = LoadStatus.Partial;
             t = (Table)LazyProxy.Instance(t);
             foreach (sTableField f in eMap[prop])
             {
                 foreach (sTableField fld in flds)
                 {
                     if (fld.ExternalField == f.Name)
                     {
                         if (conn.ContainsField(fld.Name) && !conn.IsDBNull(conn.GetOrdinal(fld.Name)))
                         {
                             RecurSetPropertyValue(f.ExternalField, conn, fld.Name, t);
                         }
                         break;
                     }
                 }
             }
             if (!t.AllPrimaryKeysNull)
             {
                 t.InitPrimaryKeys();
                 table.SetField(prop, t);
                 setValue = true;
             }
         }
         else
         {
             foreach (sTableField f in eMap[prop])
             {
                 foreach (sTableField fld in flds)
                 {
                     if (fld.ExternalField == f.Name)
                     {
                         if (conn.ContainsField(fld.Name)&&!conn.IsDBNull(conn.GetOrdinal(fld.Name))){
                             if (f.Type == FieldType.ENUM)
                                 table.SetField(prop, conn.Pool.GetEnumValue(table.GetType().GetProperty(f.ClassProperty,Utility._BINDING_FLAGS_WITH_INHERITANCE).PropertyType, (int)conn[fld.Name]));
                             else
                                 table.SetField(prop, conn[fld.Name]);
                             setValue = true;
                         }
                         break;
                     }
                 }
             }
         }
     }
     return table;
 }