コード例 #1
0
ファイル: ObjectMapper.cs プロジェクト: judypol/JordyLibary
        private void OnInit(Type type)
        {
            TableAttribute[]        ta   = Utils.GetTypeAttributes <TableAttribute>(type, false);
            SelectChangeAttribute[] SCAS = Utils.GetTypeAttributes <SelectChangeAttribute>(type, true);
            if (SCAS.Length > 0)
            {
                SelectChange = SCAS[0];
            }
            IDAttribute[]           id;
            ColumnAttribute[]       col;
            AggregationAttribute[]  aggr;
            PropertyCastAttribute[] pca;
            PropertyMapper          pm;

            ValueAttribute[]    val;
            RelationAttribute[] ra;
            if (ta.Length == 0)
            {
                throw new PeanutException(string.Format(DataMsg.OBJECT_NOT_MAPTOTABLE, type.FullName));
            }
            mTable = string.IsNullOrEmpty(ta[0].Name) ? ObjectType.Name : ta[0].Name;
            if (ta[0].DISTINCT)
            {
                DISTINCT = " DISTINCT ";
            }
            else
            {
                DISTINCT = " ";
            }
            Connection = new ConnectionAttribute(ta[0].DefaultConnection);
            foreach (PropertyInfo pi in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                id   = Utils.GetPropertyAttributes <IDAttribute>(pi, false);
                col  = Utils.GetPropertyAttributes <ColumnAttribute>(pi, false);
                aggr = Utils.GetPropertyAttributes <AggregationAttribute>(pi, false);
                pca  = Utils.GetPropertyAttributes <PropertyCastAttribute>(pi, false);
                val  = Utils.GetPropertyAttributes <ValueAttribute>(pi, false);
                ra   = Utils.GetPropertyAttributes <RelationAttribute>(pi, false);
                if (pca.Length == 0)
                {
                    pca = Utils.GetTypeAttributes <PropertyCastAttribute>(pi.PropertyType, false);
                }

                if (id.Length > 0)
                {
                    if (ID != null)
                    {
                        throw new PeanutException(string.Format(DataMsg.REPEATE_DESC_ID, type.FullName));
                    }
                    ID            = new PropertyMapper();
                    ID.OM         = this;
                    ID.Validaters = Utils.GetPropertyAttributes <ValidaterAttribute>(pi, false);
                    ID.ColumnName = string.IsNullOrEmpty(id[0].Name) ? pi.Name : id[0].Name;
                    ID.Handler    = new PropertyHandler(pi);
                    if (pca.Length > 0)
                    {
                        ID.Cast = pca[0];
                    }
                    if (val.Length > 0)
                    {
                        ID.Value = val[0];
                    }
                    mKeyByProperties.Add(ID.ColumnName, ID);

                    foreach (RelationAttribute raitem in ra)
                    {
                        raitem.Owner         = this;
                        raitem.OwnerProperty = ID;
                        Relations.Add(raitem.Partner, raitem);
                    }
                }
                if (col.Length > 0)
                {
                    pm            = new PropertyMapper();
                    pm.ColumnName = string.IsNullOrEmpty(col[0].Name) ? pi.Name : col[0].Name;
                    pm.Handler    = new PropertyHandler(pi);
                    pm.OM         = this;
                    pm.Validaters = Utils.GetPropertyAttributes <ValidaterAttribute>(pi, false);
                    if (aggr.Length > 0)
                    {
                        pm.Aggregation = aggr[0];
                    }
                    if (pca.Length > 0)
                    {
                        pm.Cast = pca[0];
                    }
                    if (val.Length > 0)
                    {
                        pm.Value = val[0];
                    }
                    if (Properties.Contains(pm))
                    {
                        throw new PeanutException(string.Format(DataMsg.REPEATE_DESC_FIELD, type.FullName, pm.ColumnName));
                    }
                    Properties.Add(pm);
                    if (pm.Cast == null)
                    {
                        pm.Cast = DBContext.GetCast(pi.PropertyType);
                    }
                    mKeyByProperties.Add(pm.ColumnName, pm);

                    foreach (RelationAttribute raitem in ra)
                    {
                        raitem.Owner         = this;
                        raitem.OwnerProperty = pm;
                        Relations.Add(raitem.Partner, raitem);
                    }
                }
            }
            CreateSql();
        }
コード例 #2
0
ファイル: ObjectMapper.cs プロジェクト: judypol/JordyLibary
 public ObjectMapper(Type objtype)
 {
     Connection = new ConnectionAttribute(DBContext.CurrentConnectionName);
     ObjectType = objtype;
     OnInit(objtype);
 }