Ejemplo n.º 1
0
 /// <summary>
 ///获取列类型和默认值
 /// </summary>
 /// <param name="info"></param>
 /// <returns></returns>
 public abstract string GetColumnType(CRL.Attribute.FieldInnerAttribute info, out string defaultValue);
Ejemplo n.º 2
0
 /// <summary>
 /// 创建索引
 /// </summary>
 /// <param name="filed"></param>
 /// <returns></returns>
 public abstract string GetColumnIndexScript(CRL.Attribute.FieldInnerAttribute filed);
Ejemplo n.º 3
0
 /// <summary>
 /// 增加列
 /// </summary>
 /// <param name="field"></param>
 /// <returns></returns>
 public abstract string GetCreateColumnScript(CRL.Attribute.FieldInnerAttribute field);
Ejemplo n.º 4
0
        static void InitProperties(Attribute.TableInnerAttribute table)
        {
            if (table.Fields.Count > 0)
            {
                return;
            }
            var type = table.Type;
            List <Attribute.FieldInnerAttribute> list = new List <CRL.Attribute.FieldInnerAttribute>();
            var fieldDic = new IgnoreCaseDictionary <Attribute.FieldInnerAttribute>();
            //string fieldPat = @"^([A-Z][a-z|\d]+)+$";
            int n = 0;

            Attribute.FieldInnerAttribute keyField = null;
            #region 读取
            var typeArry = table.Type.GetProperties().ToList();
            //移除重复的
            var dic = new Dictionary <string, PropertyInfo>();
            foreach (PropertyInfo info in typeArry)
            {
                if (!dic.ContainsKey(info.Name))
                {
                    dic.Add(info.Name, info);
                }
            }
            foreach (PropertyInfo info in dic.Values)
            {
                //if (!System.Text.RegularExpressions.Regex.IsMatch(info.Name, fieldPat))
                //{
                //    throw new CRLException(string.Format("属性名:{0} 不符合规则:{1}", info.Name, fieldPat));
                //}
                //排除没有SET方法的属性
                if (info.GetSetMethod() == null)
                {
                    continue;
                }
                Type propertyType = info.PropertyType;
                var  f            = new CRL.Attribute.FieldInnerAttribute();
                //排除集合类型
                if (propertyType.FullName.IndexOf("System.Collections") > -1)
                {
                    continue;
                }

                object[] attrs = info.GetCustomAttributes(typeof(Attribute.FieldAttribute), true);
                if (attrs != null && attrs.Length > 0)
                {
                    var atr = attrs[0] as Attribute.FieldAttribute;
                    atr.MemberName = info.Name;
                    f = atr.ToType <Attribute.FieldInnerAttribute>();
                }
                f.SetPropertyInfo(info);
                f.PropertyType = propertyType;
                f.MemberName   = info.Name;
                f.TableName    = table.TableName;
                f.ModelType    = table.Type;

                //排除不映射字段
                if (!f.MapingField)
                {
                    continue;
                }
                if (propertyType == typeof(System.String))
                {
                    if (f.Length == 0)
                    {
                        f.Length = 30;
                    }
                }
                if (f.IsPrimaryKey)//保存主键
                {
                    table.PrimaryKey = f;
                    f.FieldIndexType = Attribute.FieldIndexType.非聚集唯一;
                    keyField         = f;
                    n += 1;
                }

                if (!fieldDic.ContainsKey(f.MemberName))
                {
                    fieldDic.Add(f.MemberName, f);
                }

                list.Add(f);
            }
            if (n == 0)
            {
                //throw new CRLException(string.Format("对象{0}未设置任何主键", type.Name));
            }
            else if (n > 1)
            {
                throw new CRLException(string.Format("对象{0}设置的主键字段太多 {1}", type.Name, n));
            }
            #endregion
            //主键排前面
            if (keyField != null)
            {
                list.Remove(keyField);
                list.Insert(0, keyField);
            }
            table.Fields    = list;
            table.FieldsDic = fieldDic;
        }