/// <summary> /// 获取要执行SQL时的列,添加和修改数据时 /// </summary> /// <param name="des"></param> /// <param name="add">是否是添加</param> /// <returns></returns> internal static IList <ParamColumnModel> GetExecColumns(ModelDes des, bool add = true) { var columns = new List <ParamColumnModel>(); if (des != null && des.Properties != null) { foreach (var item in des.Properties) { if ((!add) && item.CusAttribute is PrimaryKeyAttribute) { continue; } else if ((item.CusAttribute is PrimaryKeyAttribute) && ((item.CusAttribute as PrimaryKeyAttribute).CheckAutoId)) { continue; } else if ((item.CusAttribute is ColumnAttribute) && ((item.CusAttribute as ColumnAttribute).AutoIncrement)) { continue; } columns.Add(new ParamColumnModel() { ColumnName = item.Column, FieldName = item.Field }); } } return(columns); }
/// <summary> /// 添加缓存 /// </summary> /// <param name="key"></param> /// <param name="des"></param> private static void Add(string key, ModelDes des) { lock (objLock) { if ((!_ModelDesCache.ContainsKey(key)) && des != null) { _ModelDesCache.Add(key, des); } } }
/// <summary> /// 获取对象的主键标识列和属性 /// </summary> /// <param name="des"></param> /// <returns></returns> internal static PropertyDes GetPrimary(ModelDes des) { var p = des.Properties.Where(m => m.CusAttribute is PrimaryKeyAttribute).FirstOrDefault(); if (p == null) { throw new Exception("没有任何列标记为主键特性"); } return(p as PropertyDes); }
/// <summary> /// 更新缓存 /// </summary> /// <typeparam name="T"></typeparam> private static ModelDes UpdateModelDesCache <T>() { var type = typeof(T); var cacheValue = ExistModelDesCache(type.FullName); if (cacheValue == null) { var model = new ModelDes(); #region 表描述 model.ClassType = type; model.ClassName = type.Name; var tbAttrObj = type.GetCustomAttributes(typeof(TableAttribute), true).FirstOrDefault(); if (tbAttrObj != null) { var tbAttr = tbAttrObj as TableAttribute; if (!string.IsNullOrEmpty(tbAttr.Name)) { model.TableName = tbAttr.Name; } else { model.TableName = model.ClassName; } } else { model.TableName = model.ClassName; } #endregion #region 属性描述 foreach (var propertyInfo in type.GetProperties()) { var pty = new PropertyDes(); pty.Field = propertyInfo.Name; var arri = propertyInfo.GetCustomAttributes(typeof(BaseAttribute), true).FirstOrDefault(); if (arri is IgnoreAttribute) { continue; } else if (arri is PrimaryKeyAttribute) { pty.CusAttribute = arri as PrimaryKeyAttribute; } else if (arri is ColumnAttribute) { pty.CusAttribute = arri as ColumnAttribute; } model.Properties.Add(pty); } #endregion Add(type.FullName, model); cacheValue = model; } return(cacheValue); }
/// <summary> /// 通过表达式树获取属性名 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="expr"></param> /// <returns></returns> internal static PropertyDes GetPropertyByExpress <T>(ModelDes des, Expression <Func <T, object> > expr) where T : class { var pname = ""; if (expr.Body is UnaryExpression) { var uy = expr.Body as UnaryExpression; pname = (uy.Operand as MemberExpression).Member.Name; } else { pname = (expr.Body as MemberExpression).Member.Name; } var p = des.Properties.Where(m => m.Column == pname).FirstOrDefault(); if (p == null) { throw new Exception(string.Format("{0}不是映射列,不能进行SQL处理", pname)); } return(p); }