Ejemplo n.º 1
0
        /// <summary>
        /// 返回生成模板所需entity对象
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <returns></returns>
        public Entity GenTemplateEntity(string tableName)
        {
            NameTransService            nameTransService = new NameTransService();
            Dictionary <string, string> commentMap       = SqlInfo.CommentMap(tableName);
            string    tableNodes = SqlInfo.TableNodes(tableName);
            string    keyCol     = SqlInfo.KeyCol(tableName);
            DataTable dataTable  = SqlInfo.Columns(tableName);

            if (dataTable.Rows.Count == 0)
            {
                throw new Exception("找不到该数据表!");
            }
            string entityName = NameTrans.TableNameToEntityName(tableName);
            Entity entity     = new Entity
            {
                TableName   = tableName,
                EntityName  = entityName,
                EntityNames = nameTransService.SingleToComplex(entityName),
                EntityNotes = tableNodes,
                KeyCol      = keyCol,
                KeyName     = NameTrans.ColNameToPropName(keyCol)
            };

            foreach (DataRow cl in dataTable.Rows)
            {
                string colName     = (string)cl.ItemArray[3];
                string dbType      = (string)cl.ItemArray[SqlInfo.ColTypeInfoIndex()];
                string srcPropName = NameTrans.ColNameToPropName(colName);
                entity.PropList.Add(new Prop
                {
                    IsKey            = colName == keyCol,
                    ColName          = colName,
                    SrcPropName      = srcPropName,
                    PropName         = srcPropName,
                    CapUpperPropName = nameTransService.HumpToBigHump(srcPropName),
                    PropType         = DbTypeTrans.SqlType2EntityType(dbType),
                    PropNotes        = commentMap.ContainsKey(colName)?commentMap[colName]:"",
                    ParamsType       = "equal"
                });
                string paramsType = DbTypeTrans.SqlType2ParamsType(dbType);
                if (paramsType == "range")
                {
                    entity.PropList.Add(new Prop
                    {
                        IsKey            = colName == keyCol,
                        ColName          = colName,
                        SrcPropName      = srcPropName,
                        CapUpperPropName = nameTransService.HumpToBigHump(srcPropName + "Start"),
                        PropName         = srcPropName + "Start",
                        PropType         = DbTypeTrans.SqlType2EntityType(dbType),
                        PropNotes        = commentMap.ContainsKey(colName) ? commentMap[colName] : "",
                        ParamsType       = "rangeStart"
                    });
                    entity.PropList.Add(new Prop
                    {
                        IsKey            = colName == keyCol,
                        ColName          = colName,
                        SrcPropName      = srcPropName,
                        CapUpperPropName = nameTransService.HumpToBigHump(srcPropName + "End"),
                        PropName         = srcPropName + "End",
                        PropType         = DbTypeTrans.SqlType2EntityType(dbType),
                        PropNotes        = commentMap.ContainsKey(colName) ? commentMap[colName] : "",
                        ParamsType       = "rangeEnd"
                    });
                }
                else if (paramsType == "like")
                {
                    entity.PropList.Add(new Prop
                    {
                        IsKey            = colName == keyCol,
                        ColName          = colName,
                        SrcPropName      = srcPropName,
                        CapUpperPropName = nameTransService.HumpToBigHump(srcPropName + "Like"),
                        PropName         = srcPropName + "Like",
                        PropType         = DbTypeTrans.SqlType2EntityType(dbType),
                        PropNotes        = commentMap.ContainsKey(colName) ? commentMap[colName] : "",
                        ParamsType       = "like"
                    });
                }
                if (DbTypeTrans.SqlTypeIsChangeType(dbType))
                {
                    entity.PropList.Add(new Prop
                    {
                        ColName          = colName,
                        SrcPropName      = srcPropName,
                        CapUpperPropName = nameTransService.HumpToBigHump(srcPropName + "Change"),
                        PropName         = srcPropName + "Change",
                        PropType         = DbTypeTrans.SqlType2EntityType(dbType),
                        PropNotes        = commentMap.ContainsKey(colName) ? commentMap[colName] : "",
                        ParamsType       = "change"
                    });
                }
            }
            return(entity);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 生成实体
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public Entity GenTemplateEntity(object obj)
        {
            Type             type             = obj.GetType();
            NameTransService nameTransService = new NameTransService();
            Entity           entity           = new Entity
            {
                EntityName  = type.Name,
                EntityNames = nameTransService.SingleToComplex(type.Name)
            };

            foreach (PropertyInfo propertyInfo in type.GetProperties())
            {
                KeyAttr keyAttr = propertyInfo.GetCustomAttribute <KeyAttr>();
                if (keyAttr != null)
                {
                    entity.KeyName = propertyInfo.Name;
                }
                if (propertyInfo.PropertyType.Name == "String")
                {
                    entity.PropList.Add(new Prop
                    {
                        PropName         = propertyInfo.Name,
                        CapUpperPropName = nameTransService.HumpToBigHump(propertyInfo.Name),
                        IsKey            = keyAttr != null,
                        ParamsType       = "equal",
                        PropType         = nameTransService.BigHumpToHump(propertyInfo.PropertyType.Name),
                        SrcPropName      = propertyInfo.Name,
                    });
                    entity.PropList.Add(new Prop
                    {
                        PropName         = propertyInfo.Name + "Like",
                        CapUpperPropName = nameTransService.HumpToBigHump(propertyInfo.Name) + "Like",
                        IsKey            = keyAttr != null,
                        PropType         = nameTransService.BigHumpToHump(propertyInfo.PropertyType.Name),
                        ParamsType       = "like",
                        SrcPropName      = propertyInfo.Name
                    });
                }
                else if (propertyInfo.PropertyType.Name.StartsWith("Nullable"))
                {
                    Type   temp     = propertyInfo.GetValue(obj).GetType();
                    string typeName = nameTransService.BigHumpToHump(temp.Name);
                    if (typeName == "int64" || typeName == "int32" || typeName == "int16")
                    {
                        if (typeName == "int64")
                        {
                            typeName = "long";
                        }
                        else if (typeName == "int32")
                        {
                            typeName = "int";
                        }
                        else if (typeName == "int16")
                        {
                            typeName = "byte";
                        }
                        entity.PropList.Add(new Prop
                        {
                            PropName         = propertyInfo.Name,
                            CapUpperPropName = nameTransService.HumpToBigHump(propertyInfo.Name),
                            IsKey            = keyAttr != null,
                            PropType         = typeName + "?",
                            ParamsType       = "equal",
                            SrcPropName      = propertyInfo.Name
                        });
                        entity.PropList.Add(new Prop
                        {
                            PropName         = propertyInfo.Name + "Start",
                            CapUpperPropName = nameTransService.HumpToBigHump(propertyInfo.Name) + "Start",
                            IsKey            = keyAttr != null,
                            PropType         = typeName + "?",
                            ParamsType       = "rangStart",
                            SrcPropName      = propertyInfo.Name
                        });
                        entity.PropList.Add(new Prop
                        {
                            PropName         = propertyInfo.Name + "End",
                            CapUpperPropName = nameTransService.HumpToBigHump(propertyInfo.Name) + "End",
                            IsKey            = keyAttr != null,
                            PropType         = typeName + "?",
                            ParamsType       = "rangEnd",
                            SrcPropName      = propertyInfo.Name
                        });
                    }
                }
                else if (propertyInfo.PropertyType.Name == "DateTime")
                {
                    entity.PropList.Add(new Prop
                    {
                        PropName         = propertyInfo.Name,
                        CapUpperPropName = nameTransService.HumpToBigHump(propertyInfo.Name),
                        IsKey            = keyAttr != null,
                        ParamsType       = "equal",
                        PropType         = propertyInfo.PropertyType.Name + "?",
                        SrcPropName      = propertyInfo.Name
                    });
                    entity.PropList.Add(new Prop
                    {
                        PropName         = propertyInfo.Name + "Start",
                        CapUpperPropName = nameTransService.HumpToBigHump(propertyInfo.Name) + "Start",
                        IsKey            = keyAttr != null,
                        PropType         = propertyInfo.PropertyType.Name + "?",
                        ParamsType       = "rangStart",
                        SrcPropName      = propertyInfo.Name
                    });
                    entity.PropList.Add(new Prop
                    {
                        PropName         = propertyInfo.Name + "End",
                        CapUpperPropName = nameTransService.HumpToBigHump(propertyInfo.Name) + "End",
                        IsKey            = keyAttr != null,
                        PropType         = propertyInfo.PropertyType.Name + "?",
                        ParamsType       = "rangEnd",
                        SrcPropName      = propertyInfo.Name
                    });
                }
            }
            return(entity);
        }