public string GetHtmlModelString <T>() { DataConfigureAttribute attribute = DataConfigureAttribute.GetAttribute <T>(); StringBuilder columnBuilder = new StringBuilder(); if (attribute != null) { attribute.InitDisplayName(); foreach (var item in attribute.GetViewPortDescriptors(true)) { columnBuilder.Append(GetHtmlModelString(item, item.DataType.Name)); } } else { Type tType = typeof(T); PropertyInfo[] propertys = tType.GetProperties(); foreach (var item in propertys) { string typeName = item.PropertyType.IsGenericType ? item.PropertyType.GetGenericArguments()[0].Name : item.PropertyType.Name; columnBuilder.AppendFormat("{0}:{{ Name: '{0}',DisplayName:'{0}',Width:150,DataType:'{1}',DateFormat:'',JsDateFormat:'',ValueDateFormat:'',Hidden:0 }},", item.Name, typeName); } } return(string.Format("{{{0}}}", columnBuilder.ToString().Trim(','))); }
public string GetHtmlModelString <T>() { DataConfigureAttribute attribute = DataConfigureAttribute.GetAttribute <T>(); StringBuilder ColumnBuilder = new StringBuilder(); if (attribute != null) { foreach (var item in attribute.GetHtmlTags(true)) { ColumnBuilder.Append(GetHtmlModelString(item, item.DataType.Name)); } } else { Type tType = typeof(T); System.Reflection.PropertyInfo[] propertys = tType.GetProperties(); foreach (var item in propertys) { string typeName = item.PropertyType.Name; if (item.PropertyType.Name == "Nullable`1") { typeName = item.PropertyType.GetGenericArguments()[0].Name; } ColumnBuilder.AppendFormat("{0}:{{ Name: '{0}',DisplayName:'{0}',Width:150,DataType:'{1}',Format:'',,Hidden:0 }},", item.Name, typeName); } } return(string.Format("{{{0}}}", ColumnBuilder.ToString().Trim(','))); }
public virtual bool Update <T>(T item, params object[] primaryKeys) where T : class { DataFilter filter = new DataFilter(); DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute <T>(); List <PrimaryKey> primaryKey = GetPrimaryKeys(custAttribute); if (primaryKeys.Length != primaryKey.Count && primaryKeys.Length > 0) { throw new Exception("输入的参数与设置的主键个数不对应!"); } if (primaryKeys.Length > 0) { for (int i = 0; i < primaryKey.Count; i++) { filter.Where(primaryKey[i].ColumnName, OperatorType.Equal, primaryKeys[i]); } } else { Type entityType = typeof(T); foreach (var primary in primaryKey) { PropertyInfo proper = entityType.GetProperty(primary.PropertyName); if (proper != null && proper.CanRead) { filter.Where(primary.ColumnName, OperatorType.Equal, proper.GetValue(item, null)); } } } return(Update(item, filter)); }
public virtual int Delete <T>(DataFilter filter) where T : class { DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute <T>(); filter = custAttribute.MetaData.DataAccess(filter);//数据权限 return(Delete(GetTableName <T>(custAttribute), filter.ToString(), filter.GetParameterValues())); }
public virtual bool Update <T>(T item, DataFilter filter) where T : class { DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute <T>(); string tableName = GetTableName <T>(custAttribute); PropertyInfo[] propertys = typeof(T).GetProperties(); StringBuilder builder = new StringBuilder(); List <KeyValuePair <string, object> > keyValue = new List <KeyValuePair <string, object> >(); foreach (var property in propertys) { if (filter.UpdateProperties != null && !filter.UpdateProperties.Contains(property.Name)) { continue; } string name = property.Name; object value = property.GetValue(item, null); if (custAttribute != null) { if (custAttribute.MetaData.PropertyDataConfig.ContainsKey(name)) { PropertyDataInfo data = custAttribute.MetaData.PropertyDataConfig[name]; if (data.IsRelation || data.Ignore || !data.CanUpdate) { continue; } if (!string.IsNullOrEmpty(data.ColumnName)) { name = data.ColumnName; } } } if (value != null) { builder.AppendFormat(builder.Length == 0 ? "[{0}]=@{0}" : ",[{0}]=@{0}", name); KeyValuePair <string, object> kv = new KeyValuePair <string, object>(name, value); keyValue.Add(kv); } else { builder.AppendFormat(builder.Length == 0 ? "[{0}]=NULL" : ",[{0}]=NULL", name); } } if (builder.Length == 0) { return(false); } string condition = filter.ToString(); if (!string.IsNullOrEmpty(condition)) { builder.Append(" WHERE "); builder.Append(condition); } keyValue.AddRange(filter.GetParameterValues()); return(Update(tableName, builder.ToString(), keyValue)); }
public override IEnumerable <T> Get <T>(DataFilter filter, Pagination pagin) { DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute <T>(); string tableName = GetTableName <T>(custAttribute); List <KeyValuePair <string, string> > comMatch; string selectCol = GetSelectColumn <T>(custAttribute, out comMatch); string condition = filter.ToString(); var primaryKey = GetPrimaryKeys(custAttribute); foreach (var item in primaryKey) { filter.OrderBy(item, OrderType.Ascending); } string orderby = filter.GetOrderString(); StringBuilder builder = new StringBuilder(); builder.Append("WITH T AS( "); builder.AppendFormat("SELECT TOP {0} ", pagin.PageSize * (pagin.PageIndex + 1)); builder.Append(selectCol); builder.AppendFormat(",ROW_NUMBER() OVER ({0}) AS RowIndex ", orderby); builder.AppendFormat(" FROM [{0}] ", tableName); builder.Append(custAttribute == null ? "T0" : custAttribute.MetaData.Alias); StringBuilder builderRela = new StringBuilder(); if (custAttribute != null) { foreach (var item in custAttribute.MetaData.DataRelations) { builder.Append(item); builderRela.Append(item); } } builder.Append(string.IsNullOrEmpty(condition) ? "" : " WHERE " + condition); builder.AppendFormat(") SELECT * FROM T WHERE RowIndex>{0} AND RowIndex<={1}", pagin.PageIndex * pagin.PageSize, pagin.PageSize * (pagin.PageIndex + 1)); DataTable table = GetTable(builder.ToString(), filter.GetParameterValues()); if (table == null) { return(new List <T>()); } List <T> list = new List <T>(); Dictionary <string, PropertyInfo> properties = GetProperties <T>(custAttribute); for (int i = 0; i < table.Rows.Count; i++) { list.Add(Reflection.ClassAction.GetModel <T>(table, i, comMatch, properties)); } DataTable recordCound = GetTable(string.Format("SELECT COUNT(1) FROM [{0}] {3} {2} {1}", tableName, string.IsNullOrEmpty(condition) ? "" : "WHERE " + condition, builderRela, custAttribute == null ? "T0" : custAttribute.MetaData.Alias), filter.GetParameterValues()); pagin.RecordCount = Convert.ToInt64(recordCound.Rows[0][0]); return(list); }
public DataFilter Where<T>(Expression<Func<T, object>> expression, OperatorType operatorType, object value) { string property = Common.GetLinqExpressionText(expression); DataConfigureAttribute attribute = DataConfigureAttribute.GetAttribute<T>(); if (attribute != null && attribute.MetaData.PropertyDataConfig.ContainsKey(property)) { string propertyMap = attribute.MetaData.PropertyDataConfig[property].ColumnName; if (!string.IsNullOrEmpty(propertyMap)) property = propertyMap; } return Where(new Condition(property, operatorType, value)); }
public string GetJsonDataForGrid <T>(IEnumerable <T> data, long pageSize, long pageIndex, long recordCount) { string baseStr = "{{ Columns: {0}, Rows: [{1}] ,PageIndex:{2},PageSize:{3},RecordCount:{4}}}"; StringBuilder rowBuilder = new StringBuilder(); DataConfigureAttribute attribute = DataConfigureAttribute.GetAttribute <T>(); foreach (var item in data) { rowBuilder.Append(GetModelString(item, attribute)); } return(string.Format(baseStr, GetHtmlModelString <T>(), rowBuilder.ToString().Trim(','), pageIndex, pageSize, recordCount)); }
public virtual bool Update <T>(T item, params object[] primaryKeys) where T : class { DataFilter filter = new DataFilter(); var primaryKey = new List <string> { "ID" }; DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute <T>(); if (custAttribute != null) { primaryKey = custAttribute.MetaData.Primarykey ?? primaryKey; } if (primaryKeys.Length != primaryKey.Count && primaryKeys.Length > 0) { throw new Exception("输入的参数与设置的主键个数不对应!"); } if (primaryKeys.Length > 0) { for (int i = 0; i < primaryKey.Count; i++) { filter.Where(primaryKey[i], OperatorType.Equal, primaryKeys[i]); } } else { Type entityType = typeof(T); foreach (var primary in primaryKey) { PropertyInfo proper = null; if (custAttribute != null) { foreach (string key in custAttribute.MetaData.PropertyDataConfig.Keys) { if (custAttribute.MetaData.PropertyDataConfig[key].ColumnName == primary) {//属性名 proper = entityType.GetProperty(key); break; } } } if (proper == null) { proper = entityType.GetProperty(primary); } if (proper != null && proper.CanRead) { filter.Where(primary, OperatorType.Equal, proper.GetValue(item, null)); } } } return(Update <T>(item, filter)); }
public virtual void Add <T>(T item) where T : class { DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute <T>(); string tableName = GetTableName <T>(custAttribute); Dictionary <string, object> allValues = Reflection.ClassAction.GetPropertieValues <T>(item); Dictionary <string, object> insertValues = new Dictionary <string, object>(); if (custAttribute != null) { foreach (var valueitem in allValues) { if (custAttribute.MetaData.PropertyDataConfig.ContainsKey(valueitem.Key)) { PropertyDataInfo config = custAttribute.MetaData.PropertyDataConfig[valueitem.Key]; if (!config.Ignore && config.CanInsert) { insertValues.Add(config.ColumnName, valueitem.Value); } } else { insertValues.Add(valueitem.Key, valueitem.Value); } } } else { insertValues = allValues; } object resu = this.Insert(tableName, insertValues); if (custAttribute != null && resu != null && !resu.Equals(0)) { if (custAttribute.MetaData.Primarykey != null && custAttribute.MetaData.Primarykey.Count == 1) { foreach (var dataConfig in custAttribute.MetaData.PropertyDataConfig) { if (dataConfig.Value.IsPrimaryKey && dataConfig.Value.IsIncreasePrimaryKey) { PropertyInfo pro = typeof(T).GetProperty(dataConfig.Value.PropertyName); if (pro != null && pro.CanWrite) { pro.SetValue(item, Easy.Reflection.ClassAction.ValueConvert(pro, resu), null); } break; } } } } }
protected Dictionary <string, PropertyInfo> GetProperties <T>(DataConfigureAttribute custAttribute) { var properties = new Dictionary <string, PropertyInfo>(); if (custAttribute != null) { properties = custAttribute.MetaData.Properties; } else { typeof(T).GetProperties().Each(m => properties.Add(m.Name, m)); } return(properties); }
public virtual void CreateTable <T>() where T : class { DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute <T>(); string tableName = GetTableName <T>(custAttribute); var buildColumn = new StringBuilder(); GetCulumnSchema <T>(custAttribute).Each(m => buildColumn.Append(m)); if (custAttribute != null && custAttribute.MetaData.Primarykey.Any()) { var primaryKeyBuilder = new StringBuilder(); custAttribute.MetaData.Primarykey.Each(m => primaryKeyBuilder.AppendFormat("[{0}],", m)); buildColumn.Append(string.Format("PRIMARY KEY({0})", primaryKeyBuilder.ToString().Trim(','))); } CustomerSql(string.Format("Create Table [{0}] ({1}) ", tableName, buildColumn.ToString().Trim(','))).ExecuteNonQuery(); }
private List <KeyValuePair <string, string> > GetMap <T>() { DataConfigureAttribute attribute = DataConfigureAttribute.GetAttribute <T>(); var map = new List <KeyValuePair <string, string> >(); if (attribute != null && attribute.MetaData != null && attribute.MetaData.PropertyDataConfig != null) { attribute.MetaData.PropertyDataConfig.Each(m => map.Add(new KeyValuePair <string, string>(m.Value.ColumnName, m.Key))); } else { var proertyInfoArray = typeof(T).GetProperties(); proertyInfoArray.Where(m => m.CanWrite).Each(m => map.Add(new KeyValuePair <string, string>(m.Name, m.Name))); } return(map); }
protected string GetSelectColumn <T>(DataConfigureAttribute custAttribute, out List <KeyValuePair <string, string> > comMatch) where T : class { PropertyInfo[] propertys = typeof(T).GetProperties(); var selectCom = new StringBuilder(); comMatch = new List <KeyValuePair <string, string> >(); foreach (var item in propertys) { if (custAttribute != null) { if (custAttribute.MetaData.PropertyDataConfig.ContainsKey(item.Name)) { PropertyDataInfo config = custAttribute.MetaData.PropertyDataConfig[item.Name]; if (!config.Ignore) { if (!string.IsNullOrEmpty(config.ColumnName)) { string alias = config.IsRelation ? config.TableAlias : custAttribute.MetaData.Alias; selectCom.AppendFormat("[{0}].[{1}],", alias, config.ColumnName); var keyVale = new KeyValuePair <string, string>(config.ColumnName, item.Name); comMatch.Add(keyVale); } else { string alias = config.IsRelation ? config.TableAlias : custAttribute.MetaData.Alias; selectCom.AppendFormat("[{0}].[{1}],", alias, item.Name); var keyVale = new KeyValuePair <string, string>(item.Name, item.Name); comMatch.Add(keyVale); } } } else { selectCom.AppendFormat("[{0}].[{1}],", custAttribute.MetaData.Alias, item.Name); var keyVale = new KeyValuePair <string, string>(item.Name, item.Name); comMatch.Add(keyVale); } } else { selectCom.AppendFormat("[{0}],", item.Name); var keyVale = new KeyValuePair <string, string>(item.Name, item.Name); comMatch.Add(keyVale); } } return(selectCom.ToString().Trim(',')); }
public virtual IEnumerable <T> Get <T>(DataFilter filter) where T : class { DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute <T>(); if (custAttribute != null) { filter = custAttribute.MetaData.DataAccess(filter);//数据权限 } string tableName = GetTableName <T>(custAttribute); List <KeyValuePair <string, string> > comMatch; string selectCol = GetSelectColumn <T>(custAttribute, out comMatch); string condition = filter.ToString(); string orderby = filter.GetOrderString(); var selectCom = new StringBuilder(); selectCom.Append("SELECT "); selectCom.Append(selectCol); selectCom.AppendFormat(" FROM [{0}] ", tableName); selectCom.Append(custAttribute == null ? "T0" : custAttribute.MetaData.Alias); if (custAttribute != null) { foreach (var item in custAttribute.MetaData.DataRelations) { selectCom.Append(item); } } if (!string.IsNullOrEmpty(condition)) { selectCom.AppendFormat(" WHERE {0} ", condition); } selectCom.AppendFormat(orderby); DataTable table = this.GetTable(selectCom.ToString(), filter.GetParameterValues()); if (table == null) { return(new List <T>()); } var list = new List <T>(); Dictionary <string, PropertyInfo> properties = GetProperties <T>(custAttribute); for (int i = 0; i < table.Rows.Count; i++) { list.Add(Reflection.ClassAction.GetModel <T>(table, i, comMatch, properties)); } return(list); }
private static KeyValuePair <string, object> MemberConvert(MemberExpression expression, object obj) { Func <MemberExpression, object> valueGetter = exp => { var objectMember = Expression.Convert(exp, typeof(object)); var getterLambda = Expression.Lambda <Func <object> >(objectMember); return(getterLambda.Compile()()); }; switch (expression.Member.MemberType) { case MemberTypes.Field: { return(new KeyValuePair <string, object>(Guid.NewGuid().ToString("N"), valueGetter(expression))); } case MemberTypes.Property: { if (expression.Expression.NodeType == ExpressionType.MemberAccess) { return(new KeyValuePair <string, object>(Guid.NewGuid().ToString("N"), valueGetter(expression))); } if (obj != null) { var objType = obj.GetType(); if (expression.Member.ReflectedType != null && expression.Member.ReflectedType.IsAssignableFrom(objType)) { var property = objType.GetProperty(expression.Member.Name); if (property != null) { var value = property.GetValue(obj, null); if (value != null) { return(new KeyValuePair <string, object>(Guid.NewGuid().ToString("N"), value)); } } } } DataConfigureAttribute attribute = DataConfigureAttribute.GetAttribute(expression.Member.ReflectedType); string column = attribute != null?attribute.GetPropertyMapper(expression.Member.Name) : expression.Member.Name; return(new KeyValuePair <string, object>(column, null)); } } return(new KeyValuePair <string, object>()); }
public virtual int Delete <T>(params object[] primaryKeys) where T : class { DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute <T>(); var primaryKey = GetPrimaryKeys(custAttribute); if (primaryKeys.Length != primaryKey.Count) { throw new Exception("输入的参数与设置的主键个数不对应!"); } var filter = new DataFilter(); for (int i = 0; i < primaryKey.Count; i++) { filter.Where(primaryKey[i], OperatorType.Equal, primaryKeys[i]); } return(Delete <T>(filter)); }
protected string GetTableName <T>(DataConfigureAttribute custAttribute) where T : class { string tableName = null; if (custAttribute != null) { if (!string.IsNullOrEmpty(custAttribute.MetaData.Table)) { tableName = custAttribute.MetaData.Table; } } if (string.IsNullOrEmpty(tableName)) { tableName = typeof(T).Name; } return(tableName); }
protected List <string> GetPrimaryKeys(DataConfigureAttribute custAttribute) { List <string> primaryKey; if (custAttribute != null) { primaryKey = custAttribute.MetaData.Primarykey ?? new List <string> { "ID" }; } else { primaryKey = new List <string> { "ID" }; } return(primaryKey); }
private IEnumerable <string> GetCulumnSchema <T>(DataConfigureAttribute custAttribute) where T : class { PropertyInfo[] propertys = typeof(T).GetProperties(); var result = new List <string>(); foreach (var item in propertys) { string isNull = "null"; var code = Type.GetTypeCode(item.PropertyType.Name == "Nullable`1" ? item.PropertyType.GetGenericArguments()[0] : item.PropertyType); if (custAttribute != null) { if (custAttribute.MetaData.PropertyDataConfig.ContainsKey(item.Name)) { PropertyDataInfo config = custAttribute.MetaData.PropertyDataConfig[item.Name]; if (config.IsPrimaryKey) { isNull = "not null"; } if (config.Ignore || config.IsRelation) { continue; } if (!string.IsNullOrEmpty(config.ColumnName)) { result.Add(string.Format("[{0}] {1} {2},", config.ColumnName, config.IsIncreasePrimaryKey ? "INT IDENTITY" : GetDBTypeStr(Common.ConvertToDbType(code), config.StringLength), isNull)); } else { result.Add(string.Format("[{0}] {1} {2},", config.PropertyName, GetDBTypeStr(Common.ConvertToDbType(code), config.StringLength), isNull)); } } else { result.Add(string.Format("[{0}] {1} {2},", item.Name, GetDBTypeStr(Common.ConvertToDbType(code)), isNull)); } } else { result.Add(string.Format("[{0}] {1} {2},", item.Name, GetDBTypeStr(Common.ConvertToDbType(code)), isNull)); } } return(result); }
public virtual JsonResult Delete(string ids) { try { string[] id = ids.Split(','); List <object> listIds = new List <object>(); string primary = DataConfigureAttribute.GetAttribute <TEntity>().MetaData.Primarykey[0].ColumnName; bool isString = typeof(TEntity).GetProperty(primary).PropertyType.Name.ToLower().Equals("string"); foreach (string item in id) { long test = 0; if (!isString && long.TryParse(item, out test)) { listIds.Add(test); } else { listIds.Add(item); } } int result = Service.Delete(new DataFilter().Where(primary, OperatorType.In, listIds)); if (result > 0) { return(Json(new AjaxResult { Status = AjaxStatus.Normal, Message = ids })); } else { return(Json(new AjaxResult { Status = AjaxStatus.Warn, Message = "未删除任何数据!" })); } } catch (Exception ex) { return(Json(new AjaxResult { Status = AjaxStatus.Error, Message = ex.Message })); } }
public List <PrimaryKey> GetPrimaryKeys(DataConfigureAttribute custAttribute) { List <PrimaryKey> primaryKey; if (custAttribute != null) { primaryKey = custAttribute.MetaData.Primarykey ?? new List <PrimaryKey> { new PrimaryKey { PropertyName = "ID", ColumnName = "ID" } }; } else { primaryKey = new List <PrimaryKey> { new PrimaryKey { PropertyName = "ID", ColumnName = "ID" } }; } return(primaryKey); }
public ViewModelDecode(T Entity) { Type entityType; if (Entity != null) { _entity = Entity; _withValue = true; entityType = _entity.GetType(); _attribute = DataConfigureAttribute.GetAttribute(entityType); } else { _entity = ServiceLocator.Current.GetInstance <T>(); entityType = typeof(T); _attribute = DataConfigureAttribute.GetAttribute <T>(); } if (_attribute == null) { throw new Exception(entityType.FullName + "未使用特性,请在其上使用[EasyFrameWork.Attribute.DataConfigureAttribute]特性!"); } }
public EasyModelMetaData(ModelMetadataProvider provider, Type containerType, Func <object> modelAccessor, Type modelType, string propertyName) : base(provider, containerType, modelAccessor, modelType, propertyName) { if (containerType != null) { DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute(containerType); if (custAttribute != null) { custAttribute.InitDisplayName(); if (custAttribute.MetaData.ViewPortDescriptors.ContainsKey(propertyName)) { ViewPortDescriptor = custAttribute.MetaData.ViewPortDescriptors[propertyName]; DisplayFormatString = ViewPortDescriptor.ValueFormat; if (!string.IsNullOrEmpty(ViewPortDescriptor.DisplayName)) { DisplayName = ViewPortDescriptor.DisplayName; } else { DisplayName = ViewPortDescriptor.Name; } EditFormatString = ViewPortDescriptor.ValueFormat; IsReadOnly = ViewPortDescriptor.IsReadOnly; IsRequired = ViewPortDescriptor.IsRequired; Order = ViewPortDescriptor.OrderIndex; ShowForDisplay = ViewPortDescriptor.IsShowForDisplay; ShowForEdit = ViewPortDescriptor.IsShowForEdit; TemplateHint = ViewPortDescriptor.TemplateName; HideSurroundingHtml = ViewPortDescriptor.IsHidden; } if (custAttribute.MetaData.PropertyDataConfig.ContainsKey(propertyName)) { PropertyData = custAttribute.MetaData.PropertyDataConfig[propertyName]; } } } }
public virtual T Get <T>(params object[] primaryKeys) where T : class { DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute <T>(); var primaryKey = GetPrimaryKeys(custAttribute); if (primaryKeys.Length != primaryKey.Count) { throw new Exception("输入的参数与设置的主键个数不对应!"); } DataFilter filter = new DataFilter(); for (int i = 0; i < primaryKey.Count; i++) { filter.Where(primaryKey[i].ColumnName, OperatorType.Equal, primaryKeys[i]); } var list = Get <T>(filter); if (list.Count() == 1) { return(list.ElementAt(0)); } return(default(T)); }
public EasyModelMetaData(ModelMetadataProvider provider, Type containerType, Func <object> modelAccessor, Type modelType, string propertyName) : base(provider, containerType, modelAccessor, modelType, propertyName) { if (containerType != null) { DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute(containerType); if (custAttribute != null) { if (custAttribute.MetaData.HtmlTags.ContainsKey(propertyName)) { this.HtmlTag = custAttribute.MetaData.HtmlTags[propertyName]; this.DisplayFormatString = this.HtmlTag.ValueFormat; if (!string.IsNullOrEmpty(this.HtmlTag.DisplayName)) { this.DisplayName = this.HtmlTag.DisplayName; } else { this.DisplayName = this.HtmlTag.Name; } this.EditFormatString = this.HtmlTag.ValueFormat; this.IsReadOnly = this.HtmlTag.IsReadOnly; this.IsRequired = this.HtmlTag.IsRequired; this.Order = this.HtmlTag.OrderIndex; this.ShowForDisplay = this.HtmlTag.IsShowForDisplay; this.ShowForEdit = this.HtmlTag.IsShowForEdit; this.TemplateHint = this.HtmlTag.TemplateName; } if (custAttribute.MetaData.PropertyDataConfig.ContainsKey(propertyName)) { this.PropertyData = custAttribute.MetaData.PropertyDataConfig[propertyName]; } } } }
public virtual long Count <T>(DataFilter filter) where T : class { DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute <T>(); if (custAttribute != null) { filter = custAttribute.MetaData.DataAccess(filter);//数据权限 } string tableName = GetTableName <T>(custAttribute); string condition = filter.ToString(); string orderby = filter.GetOrderString(); var selectCom = new StringBuilder(); selectCom.Append("SELECT COUNT(1) "); selectCom.AppendFormat(" FROM [{0}] ", tableName); selectCom.Append(custAttribute == null ? "T0" : custAttribute.MetaData.Alias); if (custAttribute != null) { foreach (var item in custAttribute.MetaData.DataRelations) { selectCom.Append(item); } } if (!string.IsNullOrEmpty(condition)) { selectCom.AppendFormat(" WHERE {0} ", condition); } selectCom.AppendFormat(orderby); DataTable table = this.GetTable(selectCom.ToString(), filter.GetParameterValues()); if (table == null) { return(-1); } return(Convert.ToInt64(table.Rows[0][0])); }
private string GetModelString(object source, DataConfigureAttribute attribute) { Type tType = source.GetType(); System.Reflection.PropertyInfo[] propertys = tType.GetProperties(); StringBuilder columnBuilder = new StringBuilder(); columnBuilder.Append("{"); foreach (var item in propertys) { object value = item.GetValue(source, null); if ((attribute.MetaData.ViewPortDescriptors[item.Name].TagType == HTMLEnumerate.HTMLTagTypes.DropDownList || attribute.MetaData.ViewPortDescriptors[item.Name].TagType == HTMLEnumerate.HTMLTagTypes.MutiSelect) && value != null) { var tag = (DropDownListDescriptor)attribute.MetaData.ViewPortDescriptors[item.Name]; IDictionary <string, string> options = tag.OptionItems; if (tag.SourceType == SourceType.ViewData) { if (_dropDownDataFunc != null) { options = _dropDownDataFunc(tag); } } if (options != null) { if (typeof(ICollection).IsAssignableFrom(item.PropertyType)) { ICollection vals = (ICollection)value; StringBuilder builderResult = new StringBuilder(); foreach (object val in vals) { if (options.ContainsKey(val.ToString())) { builderResult.AppendFormat("{0},", options[val.ToString()]); } } value = builderResult.ToString().Trim(','); } else if (options.ContainsKey(value.ToString())) { value = options[value.ToString()]; } } } else if ((attribute.MetaData.ViewPortDescriptors[item.Name].TagType == HTMLEnumerate.HTMLTagTypes.Input || attribute.MetaData.ViewPortDescriptors[item.Name].TagType == HTMLEnumerate.HTMLTagTypes.MutiLineTextBox || attribute.MetaData.ViewPortDescriptors[item.Name].TagType == HTMLEnumerate.HTMLTagTypes.Hidden) && value != null) { if (attribute.MetaData.ViewPortDescriptors[item.Name].TagType == HTMLEnumerate.HTMLTagTypes.Input && (item.PropertyType.Name == "DateTime" || (item.PropertyType.IsGenericType && item.PropertyType.GetGenericArguments()[0].Name == "DateTime"))) { string dateFormat = ((TextBoxDescriptor)attribute.MetaData.ViewPortDescriptors[item.Name]).DateFormat; DateTime dateTime = Convert.ToDateTime(value); value = dateTime.ToString(dateFormat); } else { string val = value.ToString().NoHTML().HtmlEncode(); if (val.Length > 50) { val = val.Substring(0, 50) + "..."; } value = val; } if (attribute.MetaData.ViewPortDescriptors[item.Name].TagType == HTMLEnumerate.HTMLTagTypes.Input) { string valueFormat = ((TextBoxDescriptor)attribute.MetaData.ViewPortDescriptors[item.Name]).ValueFormat; if (valueFormat.IsNotNullAndWhiteSpace()) { value = string.Format("{{0:{0}}}", valueFormat).FormatWith(value); } } } else if (attribute.MetaData.ViewPortDescriptors[item.Name].TagType == HTMLEnumerate.HTMLTagTypes.PassWord) { value = "******"; } if (columnBuilder.Length > 1) { columnBuilder.Append(","); } columnBuilder.AppendFormat("{0}:\"{1}\"", item.Name, (value ?? "")); } columnBuilder.Append("},"); return(columnBuilder.ToString()); }
public DataFilter GetDataFilter <T>() { DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute <T>(); DataFilter filter = new DataFilter(); List <Condition> conditions = new List <Condition>(); List <ConditionGroup> groups = new List <ConditionGroup>(); _propertyInfos = typeof(T).GetProperties(); foreach (var item in _form.AllKeys) { string property = GetConditionProperty(item); if (property.IsNullOrEmpty()) { continue; } #region 单个件 int conditionIndex = GetConditionIndex(item); if (conditionIndex >= 0) { if (conditionIndex == conditions.Count) { conditions.Add(new Condition()); } SetCondition(conditions[conditionIndex], property, _form[item]); } #endregion #region 组合条件 int groupIndex = GetConditionGroupIndex(item); if (groupIndex >= 0) { if (groupIndex == groups.Count) { groups.Add(new ConditionGroup()); } int groupConditionIndex = GetGroupConditionIndex(item); if (groupConditionIndex >= 0) { if (groupConditionIndex == groups[groupIndex].Conditions.Count) { groups[groupIndex].Conditions.Add(new Condition()); } SetCondition(groups[groupIndex].Conditions[groupConditionIndex], property, _form[item]); } else { SetGroup(groups[groupIndex], property, _form[item]); } } #endregion } foreach (var con in conditions) { if (custAttribute != null) { con.Property = custAttribute.GetPropertyMapper(con.Property); } filter.Where(con); } foreach (var gro in groups) { if (custAttribute != null) { foreach (var con in gro.Conditions) { con.Property = custAttribute.GetPropertyMapper(con.Property); } } filter.Where(gro); } for (int i = 0; ; i++) { string orderp = _form[string.Format("OrderBy[{0}][OrderCol]", i)]; string orderType = _form[string.Format("OrderBy[{0}][OrderType]", i)]; if (string.IsNullOrEmpty(orderp) || string.IsNullOrEmpty(orderType)) { break; } Order order = new Order { Property = orderp }; if (custAttribute != null) { order.Property = custAttribute.GetPropertyMapper(order.Property); } order.OrderType = (OrderType)int.Parse(orderType); filter.OrderBy(order); } return(filter); }
public virtual IEnumerable <T> Get <T>(DataFilter filter, Pagination pagin) where T : class { DataConfigureAttribute custAttribute = DataConfigureAttribute.GetAttribute <T>(); string alias = "T0"; if (custAttribute != null) { filter = custAttribute.MetaData.DataAccess(filter);//数据权限 alias = custAttribute.MetaData.Alias; } string tableName = GetTableName <T>(custAttribute); List <KeyValuePair <string, string> > comMatch; string selectCol = GetSelectColumn <T>(custAttribute, out comMatch); string condition = filter.ToString(); var primaryKey = GetPrimaryKeys(custAttribute); foreach (var item in primaryKey) { filter.OrderBy(string.Format("[{0}].[{1}]", alias, item), OrderType.Ascending); } string orderby = filter.GetOrderString(); string orderByContrary = filter.GetContraryOrderString(); var builderRela = new StringBuilder(); if (custAttribute != null) { foreach (var item in custAttribute.MetaData.DataRelations) { builderRela.Append(item); } } const string formatCount = "SELECT COUNT(*) FROM [{0}] {3} {2} {1}"; DataTable recordCound = this.GetTable(string.Format(formatCount, tableName, string.IsNullOrEmpty(condition) ? "" : "WHERE " + condition, builderRela, alias), filter.GetParameterValues()); pagin.RecordCount = Convert.ToInt64(recordCound.Rows[0][0]); if (pagin.AllPage == pagin.PageIndex && pagin.RecordCount > 0) { pagin.PageIndex--; } int pageSize = pagin.PageSize; if ((pagin.PageIndex + 1) * pagin.PageSize > pagin.RecordCount && pagin.RecordCount != 0) { pageSize = (int)(pagin.RecordCount - pagin.PageIndex * pagin.PageSize); if (pageSize < 0) { pageSize = pagin.PageSize; } } var builder = new StringBuilder(); const string formatTable = "SELECT * FROM (SELECT TOP {1} * FROM (SELECT TOP {2} {0} FROM {3} {6} {5} {4}) TEMP0 {7}) TEMP1 {8}"; builder.AppendFormat(formatTable, selectCol, pageSize, pagin.PageSize * (pagin.PageIndex + 1), string.Format("[{0}] {1}", tableName, custAttribute == null ? "T0" : custAttribute.MetaData.Alias), orderby, string.IsNullOrEmpty(condition) ? "" : "WHERE " + condition, builderRela, orderByContrary.Replace("[{0}].".FormatWith(alias), ""), orderby.Replace("[{0}].".FormatWith(alias), "")); DataTable table = this.GetTable(builder.ToString(), filter.GetParameterValues()); if (table == null) { return(new List <T>()); } ; var list = new List <T>(); Dictionary <string, PropertyInfo> properties = GetProperties <T>(custAttribute); for (int i = 0; i < table.Rows.Count; i++) { list.Add(Reflection.ClassAction.GetModel <T>(table, i, comMatch, properties)); } return(list); }