Esempio n. 1
0
        private void addNonMergeColumn(Type type, IDataReader dr, ClassDesc clsDesc, ref object model, int idx)
        {
            Debug.LogError("addNonMergeColumn method called...............");

            var propertyDesc = clsDesc.GetPropertyInfo(dr.GetName(idx));

            if (propertyDesc != null)
            {
                if (dr[idx] is DBNull)
                {
                    Debug.Log(string.Format("DBNull found in column : {0}.If it's a test record, ignore", dr.GetName(idx)));
                }
                else if (propertyDesc.attribute.CustomType == null)
                {
                    // 设置非自定义值
                    // bool值特殊处理
                    if (propertyDesc.propertyInfo.PropertyType == typeof(bool))
                    {
                        propertyDesc.propertyInfo.SetValue(model, Convert.ToInt32(dr[idx]) == 1, null);
                    }
                    else
                    {
                        if (propertyDesc.propertyInfo.PropertyType == typeof(string))
                        {
                            string value = dr[idx] as string;
                            propertyDesc.propertyInfo.SetValue(model, value, null);
                        }
                        else
                        {
                            propertyDesc.propertyInfo.SetValue(model, dr[idx], null);
                        }
                    }
                }
                else
                {
                    // 对于自定义类型值, 使用CustomDBClass转换
                    if (dr[idx].GetType() != typeof(string))
                    {
                        Debug.LogError(string.Format("Column value of custom type must be string : {0}", dr.GetName(idx)));
                    }
                    else
                    {
                        var value = ConfigDataBase.Instance.CustomDbClass.ParseType(propertyDesc.attribute.CustomType, dr[idx] as string);
                        propertyDesc.propertyInfo.SetValue(model, value, null);
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 从数据流获取数据类, 支持外键
        /// </summary>
        private object CreateOneItem(Type type, IDataReader dr, ClassDesc clsDesc, IDbAccessorFactory dbAcsFty, object key)
        {
            // 创建具体类型
            var    model    = Activator.CreateInstance(type);
            object localKey = null;

            // 填充表数据
            for (int i = 0; i < dr.FieldCount; i++)
            {
                var propertyDesc = clsDesc.GetPropertyInfo(dr.GetName(i));
                if (propertyDesc != null)
                {
                    if (clsDesc.KeyAttribute != null && propertyDesc.attribute.ColumnName.Equals(clsDesc.KeyAttribute.ColumnName))
                    {
                        localKey = dr[i];
                    }

                    if (dr[i] is DBNull)
                    {
                        Debug.LogWarning(string.Format("DBNull found in column : {0}.If it's test record, ignore", dr.GetName(i)));
                    }
                    else if (propertyDesc.attribute.CustomType == null)
                    {
                        if (propertyDesc.attribute.IsFloatCol)
                        {
                            int   ori = Convert.ToInt32(dr[i]);
                            float dst = (float)ori / 10000;
                            propertyDesc.propertyInfo.SetValue(model, dst, null);
                        }
                        else
                        {
                            // 设置非自定义值
                            // bool值特殊处理
                            if (propertyDesc.propertyInfo.PropertyType == typeof(bool))
                            {
                                propertyDesc.propertyInfo.SetValue(model, Convert.ToInt32(dr[i]) == 1, null);
                            }
                            else if (propertyDesc.propertyInfo.PropertyType == typeof(int))
                            {
                                propertyDesc.propertyInfo.SetValue(model, Convert.ToInt32(dr[i]), null);
                            }
                            else
                            {
                                if (propertyDesc.propertyInfo.PropertyType == typeof(string))
                                {
                                    string value = dr[i] as string;
                                    propertyDesc.propertyInfo.SetValue(model, value, null);
                                }
                                else
                                {
                                    propertyDesc.propertyInfo.SetValue(model, dr[i], null);
                                }
                            }
                        }
                    }
                    else
                    {
                        // 对于自定义类型值, 使用CustomDBClass转换
                        if (dr[i].GetType() != typeof(string))
                        {
                            Debug.LogError(string.Format("Column value of custom type must be string : {0}", dr.GetName(i)));
                        }
                        else
                        {
                            var value = ConfigDataBase.Instance.CustomDbClass.ParseType(propertyDesc.attribute.CustomType, dr[i] as string);
                            propertyDesc.propertyInfo.SetValue(model, value, null);
                        }
                    }
                }
                else
                {
                    var splitPropertyInfo = clsDesc.GetSplitPropertyInfo(dr.GetName(i));
                    var splitRangeInfo    = clsDesc.GetSplitRangePropertyInfo(dr.GetName(i));
                    if (splitPropertyInfo != null)
                    {
                        if (dr[i] is DBNull || dr.GetValue(i) == null)
                        {
                            Debug.LogWarning(string.Format("DBNull found in column : {0}. If it's test record, ignore", dr.GetName(i)));
                        }
                        else
                        {
                            AddSplitColumn(splitPropertyInfo.splitAttribute.Type, splitPropertyInfo.propertyInfo, dr.GetValue(i).ToString(), splitPropertyInfo.splitAttribute.IsCustomType, model);
                        }
                    }
                    else if (splitRangeInfo != null)
                    {
                        if (dr[i] is DBNull || dr.GetValue(i) == null)
                        {
                            Debug.LogWarning(string.Format("DBNull found in column : {0}.If it's test record, ignore", dr.GetName(i)));
                        }
                        else
                        {
                            AddSplitRangeColumn(splitRangeInfo.propertyInfo, dr.GetValue(i).ToString(), model);
                        }
                    }
                    else
                    {
                        int    dstPrefixLen = 0;
                        string className    = string.Empty;
                        if (string.IsNullOrEmpty(clsDesc.ToClassName))
                        {
                            className    = clsDesc.ClassName + "List";
                            dstPrefixLen = clsDesc.TableName.Length;// 如此,则合并表不能有editor等类似的前缀
                        }
                        else // 有to_name注释
                        {   // 需要根据className 去 获得mergeColumnDesc
                            className    = clsDesc.ToClassName + "List";
                            dstPrefixLen = clsDesc.ToNameStr.Length;
                        }
                        var mergeColumnDesc = clsDesc.GetMergePropertyInfo(className);
                        if (mergeColumnDesc != null)
                        {
                            PropertyInfo[]    piArr     = mergeColumnDesc.mergeAttribute.Type.GetProperties();
                            DbColumnAttribute secondAtb = piArr[1].GetCustomAttributes(typeof(DbColumnAttribute), false)[0] as DbColumnAttribute;
                            string            dstName   = secondAtb.ColumnName;

                            int    mergeColCount = piArr.Length;
                            string colName       = dr.GetName(i);
                            int    index         = colName.IndexOf('_');
                            if (index != -1 && dr.FieldCount >= i + mergeColCount - 1)
                            {
                                string prefix = colName.Substring(0, index);
                                dstName = prefix + dstName.Substring(dstPrefixLen);
                                if (colName.Equals(dstName, StringComparison.CurrentCultureIgnoreCase))
                                {
                                    List <string> valueList = new List <string>();
                                    valueList.Add(prefix.ToUpper());
                                    valueList.Add(i.ToString());

                                    for (int j = 1; j < mergeColCount - 1; j++)
                                    {
                                        colName = dr.GetName(i + j);
                                        DbColumnAttribute atb = piArr[j + 1].GetCustomAttributes(typeof(DbColumnAttribute), false)[0] as DbColumnAttribute;
                                        dstName = atb.ColumnName;
                                        dstName = prefix + dstName.Substring(dstPrefixLen);
                                        if (!colName.Equals(dstName, StringComparison.CurrentCultureIgnoreCase))
                                        {
                                            throw new InvalidOperationException("DBClassLoader found MergeTable column format error");
                                        }
                                        else
                                        {
                                            valueList.Add((j + i).ToString());
                                        }
                                    }

                                    AddMergeColumn(mergeColumnDesc.mergeAttribute.Type, mergeColumnDesc.propertyInfo, model, valueList, dr);
                                    i += mergeColCount - 2;
                                }
                                else
                                {
                                    addNonMergeColumn(mergeColumnDesc.mergeAttribute.Type, dr, clsDesc, ref model, i);
                                }
                            }
                            else
                            {
                                addNonMergeColumn(mergeColumnDesc.mergeAttribute.Type, dr, clsDesc, ref model, i);
                            }
                        }
                    }
                }
            }

            // 填充子表数据
            for (int i = 0; i < clsDesc.SubTablePropertyDescList.Count; i++)
            {
                var subTablePropertyDesc = clsDesc.SubTablePropertyDescList[i];
                // 外键属性都应该是List<>类型
                var list = subTablePropertyDesc.propertyInfo.GetValue(model, null) as IList;
                // 根据具List<>对应的泛型类型获取数据
                var subItemList = GetSubItemList(subTablePropertyDesc.attribute.ClassType, dbAcsFty, localKey);
                // 将于model匹配的数据添加到对应的外键属性
                AddSubItems2ReferenceList(subItemList, GetClassDesc(subTablePropertyDesc.attribute.ClassType), list, model);
            }

            return(model);
        }