Exemplo n.º 1
0
        /// <summary>
        /// 获取model属性标记源代码
        /// </summary>
        /// <param name="column">列信息</param>
        /// <returns>model属性标记源代码</returns>
        public virtual string GetAttributeCode(ColumnInfoModel column)
        {
            if (column == null)
            {
                throw new ArgumentNullException("column");
            }
            StringBuilder attsString = new StringBuilder();
            string        attrFormat = this.GetPropertyAttribute();

            if (column.IsKey)
            {
                attsString.AppendLine(string.Format(attrFormat, "Key"));
            }

            if (!column.IsKey && !column.IsNullable)
            {
                attsString.AppendLine(string.Format(attrFormat, "Required"));
            }

            string s            = null;
            string propertyType = this.GetPropertyType(column);

            if (string.Equals("string", propertyType, StringComparison.OrdinalIgnoreCase) ||
                string.Equals("byte[]", propertyType, StringComparison.OrdinalIgnoreCase))
            {
                s = string.Format("MaxLength({0})", column.MaxLength);
                attsString.AppendLine(string.Format(attrFormat, s));
            }
            else if (string.Equals("decimal", propertyType, StringComparison.OrdinalIgnoreCase))
            {
                s = string.Format("Decimal({0}, {1})", column.MaxLength, column.MinLength);
                attsString.AppendLine(string.Format(attrFormat, s));
            }

            s = string.Format("Column(\"{0}\")", column.Name); //string.Format("Column(\"{0}\",Order={1})", column.Name, column.Order);
            attsString.AppendLine(string.Format(attrFormat, s));


            if (!column.IsKey && !string.IsNullOrEmpty(column.IndexName))
            {
                s = string.Format("Index(\"{0}\", IsUnique = {1})", column.IndexName, column.IsUnique ? "true" : "false");
                attsString.AppendLine(string.Format(attrFormat, s));
            }

            if (string.Equals("int", propertyType, StringComparison.OrdinalIgnoreCase) ||
                string.Equals("long", propertyType, StringComparison.OrdinalIgnoreCase))
            {
                if (column.IsAutoIncrement)
                {
                    attsString.AppendLine(string.Format(attrFormat, "DatabaseGenerated(DatabaseGeneratedOption.Identity)"));
                }
                else if (!column.IsAutoIncrement && column.IsKey)
                {
                    attsString.AppendLine(string.Format(attrFormat, "DatabaseGenerated(DatabaseGeneratedOption.None)"));
                }
            }

            return(attsString.ToString());
        }
Exemplo n.º 2
0
        /// <summary>
        /// 获取model属性源代码
        /// </summary>
        /// <param name="column">列信息</param>
        /// <returns>model属性源代码</returns>
        public virtual string GetPropertyCode(ColumnInfoModel column)
        {
            if (column == null)
            {
                throw new ArgumentNullException("column");
            }
            string type         = this.GetPropertyType(column);
            string propertyName = this.GetPropertyName(column.Name);

            return(string.Format(this.GetPropertyFormat(), type ?? "", propertyName ?? ""));
        }
Exemplo n.º 3
0
        /// <summary>
        /// 获取model属性源代码
        /// </summary>
        /// <param name="column">列信息</param>
        /// <returns>model属性源代码</returns>
        public virtual string GetPropertyCode(ColumnInfoModel column)
        {
            if (column == null)
            {
                throw new ArgumentNullException("column");
            }
            string type         = this.GetPropertyType(column);
            string propertyName = this.GetPropertyName(column.Name);
            string attrs        = this.GetAttributeCode(column);

            if (!string.IsNullOrEmpty(attrs))
            {
                attrs = "\r\n" + attrs;
            }

            return(string.Format(this.GetPropertyFormat(), type ?? "", propertyName ?? "", column.Comment ?? "", attrs));
        }
Exemplo n.º 4
0
        //public abstract bool DeleteTable(string table);

        /// <summary>
        /// 添加列
        /// </summary>
        /// <param name="table">表名</param>
        /// <param name="column">列信息</param>
        /// <returns>是否成功</returns>
        public abstract bool AddColumn(string table, ColumnInfoModel column);
Exemplo n.º 5
0
        /// <summary>
        /// 获取列信息
        /// </summary>
        /// <param name="property">model 属性</param>
        /// <param name="table">表名</param>
        /// <returns>列信息</returns>
        public virtual ColumnInfoModel GetColumnInfo(PropertyInfo property, string table)
        {
            if (property == null)
            {
                throw new ArgumentNullException("property");
            }
            if (string.IsNullOrEmpty(table))
            {
                throw new ArgumentNullException("table");
            }
            ColumnInfoModel m = new ColumnInfoModel();

            m.Name = property.Name;
            int maxlength = 0;
            int minlength = 0;

            GetColumnInLength(property, out maxlength, out minlength);
            Type propertyType = property.PropertyType;

            m.IsNullable = false;
            if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                m.IsNullable = true;
                propertyType = propertyType.GetGenericArguments()[0];
            }
            string dataType = this.GetColumnType(propertyType, maxlength, minlength);

            if (string.IsNullOrEmpty(dataType))
            {
                throw new Exception("未找到" + property.PropertyType.FullName + "对应的数据库类型!");
            }
            m.DataType  = dataType;
            m.MaxLength = maxlength;
            m.MinLength = minlength;
            var atts = property.GetCustomAttributes(typeof(ColumnAttribute), false);

            if (atts != null && atts.Length > 0)
            {
                var att = atts[0] as ColumnAttribute;
                if (!string.IsNullOrEmpty(att.Name))
                {
                    m.Name = att.Name;
                }
            }

            m.IsAutoIncrement = false;
            if (propertyType == typeof(int) || propertyType == typeof(uint) ||
                propertyType == typeof(long) || propertyType == typeof(ulong))
            {
                atts = property.GetCustomAttributes(typeof(DatabaseGeneratedAttribute), false);
                if (atts != null && atts.Length > 0)
                {
                    var att = atts[0] as DatabaseGeneratedAttribute;
                    if (att.DatabaseGeneratedOption == DatabaseGeneratedOption.Identity)
                    {
                        m.IsAutoIncrement = true;
                    }
                }
            }

            m.IsKey          = false;
            m.IsNonClustered = false;
            atts             = property.GetCustomAttributes(typeof(KeyAttribute), false);
            if (atts != null && atts.Length > 0)
            {
                m.IsKey          = true;
                atts             = property.GetCustomAttributes(typeof(NonClusteredAttribute), false);
                m.IsNonClustered = atts != null && atts.Length > 0;
            }
            else
            {
                if (!propertyType.IsValueType)
                {
                    m.IsNullable = true;
                    atts         = property.GetCustomAttributes(typeof(RequiredAttribute), false);
                    if (atts != null && atts.Length > 0)
                    {
                        m.IsNullable = false;
                    }
                }

                atts = property.GetCustomAttributes(typeof(IndexAttribute), false);
                if (atts != null && atts.Length > 0)
                {
                    m.Indexs = new List <IndexModel>(atts.Length);
                    foreach (var o in atts)
                    {
                        var att   = o as IndexAttribute;
                        var index = new IndexModel();
                        m.Indexs.Add(index);
                        index.ColumnName = m.Name;
                        index.IsUnique   = att.IsUnique;
                        if (!string.IsNullOrEmpty(att.Name))
                        {
                            index.Name = att.Name;
                        }
                        else
                        {
                            m.Name = string.Format("IX_{0}_{1}", table, m.Name);
                        }
                    }
                }
            }

            m.Comment = this.GetComment(property.DeclaringType.Assembly, "P:" + property.DeclaringType.FullName + "." + property.Name) ?? string.Empty;

            return(m);
        }
Exemplo n.º 6
0
 /// <summary>
 /// 获取model属性类型string
 /// </summary>
 /// <param name="column">列信息</param>
 /// <returns>model属性类型string</returns>
 public abstract string GetPropertyType(ColumnInfoModel column);
Exemplo n.º 7
0
        /// <summary>
        /// 获取列信息
        /// </summary>
        /// <param name="property">model 属性</param>
        /// <param name="table">表名</param>
        /// <returns>列信息</returns>
        public virtual ColumnInfoModel GetColumnInfo(PropertyInfo property, string table)
        {
            if (property == null)
            {
                throw new ArgumentNullException("property");
            }
            if (string.IsNullOrEmpty(table))
            {
                throw new ArgumentNullException("table");
            }
            ColumnInfoModel m         = null;
            int             maxlength = 0;
            int             minlength = 0;

            GetColumnInLength(property, out maxlength, out minlength);
            string dataType = this.GetColumnType(property.PropertyType, maxlength, minlength);

            if (string.IsNullOrEmpty(dataType))
            {
                throw new Exception("未找到" + property.PropertyType.FullName + "对应的数据库类型!");
            }
            m           = new ColumnInfoModel();
            m.Name      = property.Name;
            m.DataType  = dataType;
            m.MaxLength = maxlength;
            m.MinLength = minlength;
            var atts = property.GetCustomAttributes(typeof(ColumnAttribute), false);

            if (atts != null && atts.Length > 0)
            {
                var att = atts[0] as ColumnAttribute;
                if (!string.IsNullOrEmpty(att.Name))
                {
                    m.Name = att.Name;
                }
            }

            m.IsAutoIncrement = false;
            atts = property.GetCustomAttributes(typeof(DatabaseGeneratedAttribute), false);
            if (atts != null && atts.Length > 0)
            {
                var att = atts[0] as DatabaseGeneratedAttribute;
                if (att.DatabaseGeneratedOption == DatabaseGeneratedOption.Identity)
                {
                    m.IsAutoIncrement = true;
                }
            }

            m.IsKey      = false;
            m.IsUnique   = false;
            m.IsNullable = true;
            atts         = property.GetCustomAttributes(typeof(KeyAttribute), false);
            if (atts != null && atts.Length > 0)
            {
                m.IsKey      = true;
                m.IsUnique   = true;
                m.IsNullable = false;
            }
            else
            {
                atts = property.GetCustomAttributes(typeof(RequiredAttribute), false);
                if (atts != null && atts.Length > 0)
                {
                    m.IsNullable = false;
                }

                atts = property.GetCustomAttributes(typeof(IndexAttribute), false);
                if (atts != null && atts.Length > 0)
                {
                    var att = atts[0] as IndexAttribute;
                    m.IsUnique = att.IsUnique;
                    if (!string.IsNullOrEmpty(att.Name))
                    {
                        m.IndexName = att.Name;
                    }
                    else
                    {
                        m.IndexName = string.Format("IX_{0}_{1}", table, m.Name);
                    }
                }
            }

            return(m);
        }